I am trying to parse go source files that contain interfaces and find the interfaces defined methods / signatures. I am using ast to parse the file. I am able to get some high level declarations, such as *ast.GenDecl, but I am not able to get to the next level of determining if this type is an interface and what its methods are.
This is for a scaffolding type problem I am trying to solve where a user defines the interface for a service and a tool will build out the skeleton of the service
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"reflect"
)
func main() {
fset := token.NewFileSet()
f, _ := parser.ParseFile(fset, "/tmp/tmp.go", `package service
type ServiceInterface interface {
Create(NewServiceRequest) (JsonResponse, error)
Delete(DelServiceRequest) (JsonResponse, error)
}`, 0)
for _, v := range f.Decls {
switch t := v.(type) {
case *ast.FuncDecl:
fmt.Println("func ", t.Name.Name)
case *ast.GenDecl:
switch x := t.Specs[0].(type) {
default:
fmt.Println(x, reflect.TypeOf(x))
}
default:
fmt.Printf("skipping %t
", t)
}
}
}
Results in but I cant seem to find anything about the internals of interface declaration at all.
&{<nil> ServiceInterface 0x8202d8260 <nil>} *ast.TypeSpec