Skip to content

Commit f14f17f

Browse files
ccojocarCosmin Cojocar
authored andcommitted
Add a helper function which extracts the string parameters values of a call expression
1 parent 2695567 commit f14f17f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

helpers.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,45 @@ func GetCallInfo(n ast.Node, ctx *Context) (string, string, error) {
165165
return "", "", fmt.Errorf("unable to determine call info")
166166
}
167167

168+
// GetCallStringArgsValues returns the values of strings arguments if they can be resolved
169+
func GetCallStringArgsValues(n ast.Node, ctx *Context) []string {
170+
values := []string{}
171+
switch node := n.(type) {
172+
case *ast.CallExpr:
173+
for _, arg := range node.Args {
174+
switch param := arg.(type) {
175+
case *ast.BasicLit:
176+
value, err := GetString(param)
177+
if err == nil {
178+
values = append(values, value)
179+
}
180+
case *ast.Ident:
181+
obj := param.Obj
182+
if obj != nil {
183+
switch decl := obj.Decl.(type) {
184+
case *ast.ValueSpec:
185+
for _, v := range decl.Values {
186+
value, err := GetString(v)
187+
if err == nil {
188+
values = append(values, value)
189+
}
190+
}
191+
case *ast.AssignStmt:
192+
for _, v := range decl.Rhs {
193+
value, err := GetString(v)
194+
if err == nil {
195+
values = append(values, value)
196+
}
197+
}
198+
}
199+
200+
}
201+
}
202+
}
203+
}
204+
return values
205+
}
206+
168207
// GetImportedName returns the name used for the package within the
169208
// code. It will resolve aliases and ignores initialization only imports.
170209
func GetImportedName(path string, ctx *Context) (string, bool) {

0 commit comments

Comments
 (0)