Skip to content

Instantly share code, notes, and snippets.

@csthompson
Created September 12, 2020 15:29
Show Gist options
  • Save csthompson/397741ca4ed8498ab5f731114c0de39d to your computer and use it in GitHub Desktop.
Save csthompson/397741ca4ed8498ab5f731114c0de39d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
)
func main() {
//Create a new token set for the file
fset := token.NewFileSet()
//Parse the file and create an AST
file, err := parser.ParseFile(fset, "../nats_publisher/main.go", nil, parser.ParseComments)
if err != nil {
panic(err)
}
ast.Inspect(file, func(n ast.Node) bool {
// Find Return Statements
funcCall, ok := n.(*ast.CallExpr)
if ok {
mthd, ok := funcCall.Fun.(*ast.SelectorExpr)
if ok {
_, ok = mthd.X.(*ast.Ident)
if ok {
if mthd.Sel.Name == "Publish" && mthd.X.(*ast.Ident).Name == "ec" {
switch v := funcCall.Args[0].(type) {
case *ast.BasicLit:
fmt.Printf("Topic: %s \n", v.Value)
case *ast.Ident:
fmt.Printf("Topic is declared with: %s \n", v.Name)
default:
fmt.Println("Unrecognized type")
}
}
}
}
}
return true
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment