I'm trying to parse function calls from a code using go/ast
package.
To do that, I first find all function calls like:
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.FuncDecl:
processFunction(x)
}
return true
})
And then, processFunction() looks like this:
func processFunction(e *ast.FuncDecl) {
// Save wrapper function name
f := e.Name.Name
for _, expression := range e.Body.List {
logrus.Printf("Current stmt: %#v", expression)
pkg := expression.(*ast.ExprStmt).X.(*ast.CallExpr).Fun.(*ast.SelectorExpr).X.(*ast.Ident).Name
fn := expression.(*ast.ExprStmt).X.(*ast.CallExpr).Fun.(*ast.SelectorExpr).Sel.Name
fcall := fmt.Sprintf("%s.%s", pkg, fn)
logrus.Printf("Yay. found my function call: ", fcall)
}
}
The problem with this code is that if that particular hierarchy is not found in AST, the program panics. I know that we can do interface conversions gracefully via
x, ok := x.(type)
But, if I do each conversion like this, my code will be huge. Trying to use that in this fails of course.
pkg, ok := expression.(*ast.ExprStmt).X.(*ast.CallExpr).Fun.(*ast.SelectorExpr).X.(*ast.Ident).Name
if !ok {
continue
}
Error:
./parser.go:41: assignment count mismatch: 2 = 1
Is there a concise way to do these series of conversions and also fail gracefully if this hierarchy is not found?
There is no way, that I know of, to chain type assertions. But you can simplify this specific example by extracting duplicate code into its own function.
func fnSelExpr(s ast.Stmt) (*ast.SelectorExpr, bool) {
if xs, ok := s.(*ast.ExprStmt); ok {
if cx, ok := xs.X.(*ast.CallExpr); ok {
return cx.Fun.(*ast.SelectorExpr)
}
}
return nil, false
}
Then you can simplify your processFunction
like this.
func processFunction(e *ast.FuncDecl) {
// Save wrapper function name
f := e.Name.Name
for _, expression := range e.Body.List {
logrus.Printf("Current stmt: %#v", expression)
sx, ok := fnSelExpr(expression)
if !ok {
continue
}
var pkg string
if id, ok := sx.X.(*ast.Ident); ok {
pkg = id.Name
}
fn := sx.Sel.Name
fcall := fmt.Sprintf("%s.%s", pkg, fn)
logrus.Printf("Yay. found my function call: ", fcall)
}
}