-
Notifications
You must be signed in to change notification settings - Fork 1
/
struct.go
54 lines (47 loc) · 882 Bytes
/
struct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package parse
import (
"go/ast"
"go/token"
)
type structDecl struct {
doc string
fields map[string]field
}
type field struct {
methods []string
}
func searchStructDecl(decl *ast.GenDecl) (string, structDecl) {
s := structDecl{}
if decl.Tok != token.TYPE {
return "", s
}
spec := decl.Specs[0]
ts, ok := spec.(*ast.TypeSpec)
if !ok {
return "", s
}
st, ok := ts.Type.(*ast.StructType)
if !ok {
return "", s
}
fields := map[string]field{}
for _, f := range st.Fields.List {
fields[f.Names[0].Name] = field{
methods: getMethods(f),
}
}
s.fields = fields
if decl.Doc != nil {
s.doc = decl.Doc.List[0].Text
}
return ts.Name.Name, s
}
func getMethods(f *ast.Field) []string {
var m []string
if t, ok := f.Type.(*ast.InterfaceType); ok {
for _, methods := range t.Methods.List {
m = append(m, methods.Names[0].Name)
}
}
return m
}