-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.go
102 lines (82 loc) · 1.72 KB
/
func.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import "go/ast"
type GoFunc struct {
gf *GoFile
astFunc *ast.FuncDecl
receiver *GoField
param []*GoField
results []*GoField
errors []error
}
func NewGoFunc(gf *GoFile, funcDecl *ast.FuncDecl) *GoFunc {
return &GoFunc{
gf: gf,
astFunc: funcDecl,
}
}
func (g *GoFunc) verify() bool {
return true
}
func (g *GoFunc) Name() string {
return g.astFunc.Name.Name
}
func (g *GoFunc) ResolveTypeInfo() {
g.getReceiver()
g.getResultList()
g.getParamList()
}
func (g *GoFunc) getParamList() []*GoField {
if len(g.param) != 0 {
return g.param
}
if g.astFunc.Type.Params != nil {
g.param = g.getTypeInfo(g.astFunc.Type.Params)
}
return g.param
}
func (g *GoFunc) getResultList() []*GoField {
if len(g.results) != 0 {
return g.results
}
if g.astFunc.Type.Results != nil {
g.results = g.getTypeInfo(g.astFunc.Type.Results)
}
return g.results
}
func (g *GoFunc) getReceiver() *GoField {
if g.receiver != nil {
return g.receiver
}
if g.astFunc.Recv != nil {
r := g.getTypeInfo(g.astFunc.Recv)
g.receiver = r[0]
}
return g.receiver
}
func (g *GoFunc) getTypeInfo(fields *ast.FieldList) []*GoField {
var ret []*GoField
for _, field := range fields.List {
goType := NewGoType(field.Type)
goType.resolvePkgInfo(g.gf.Imports, g.gf.Pkg.Name)
if goType.err != nil {
g.errors = append(g.errors, goType.err)
break
}
ret = append(ret, goType)
}
return ret
}
func (g *GoFunc) getDepImports(pkgs map[string]string) {
for _, goType := range g.getParamList() {
if goType.pkgPath == "" {
continue
}
pkgs[goType.pkgPath] = goType.pkgName
}
for _, goType := range g.getResultList() {
if goType.pkgPath == "" {
continue
}
pkgs[goType.pkgPath] = goType.pkgName
}
}