Created
September 20, 2020 05:02
-
-
Save csthompson/c4bc7a636c6316e98c517e78f5271d96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
//Get the statement from the standalone expression (x) and attempt to cast to a function call | |
funcCall, ok := n.(*ast.CallExpr) | |
if ok { | |
//Check if it is a selector expression | |
mthd, ok := funcCall.Fun.(*ast.SelectorExpr) | |
if ok { | |
//Retrieve the identifier (the left side of the . in a selector expression) | |
_, ok = mthd.X.(*ast.Ident) | |
if ok { | |
//If method matches and the identifier is "ec" (encoded nats connection), extract values | |
if mthd.Sel.Name == "Publish" && mthd.X.(*ast.Ident).Name == "ec" { | |
fmt.Printf("Topic: %s \n", funcCall.Args[0].(*ast.BasicLit).Value) | |
line := fset.Position(mthd.Pos()).Line | |
//Iterate over the comments in the file | |
for _, cGroup := range file.Comments { | |
//If the comment group matches the previous line, print the comment group as text | |
if fset.Position(cGroup.End()).Line == line-1 { | |
fmt.Println(cGroup.Text()) | |
} | |
} | |
} | |
} | |
} | |
} | |
return true | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment