forked from go-kit/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsevisitor.go
178 lines (162 loc) · 3.26 KB
/
parsevisitor.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"go/ast"
)
type (
parseVisitor struct {
src *sourceContext
}
typeSpecVisitor struct {
src *sourceContext
node *ast.TypeSpec
iface *iface
name *ast.Ident
}
interfaceTypeVisitor struct {
node *ast.TypeSpec
ts *typeSpecVisitor
methods []method
}
methodVisitor struct {
depth int
node *ast.TypeSpec
list *[]method
name *ast.Ident
params, results *[]arg
isMethod bool
}
argListVisitor struct {
list *[]arg
}
argVisitor struct {
node *ast.TypeSpec
parts []ast.Expr
list *[]arg
}
)
func (v *parseVisitor) Visit(n ast.Node) ast.Visitor {
switch rn := n.(type) {
default:
return v
case *ast.File:
v.src.pkg = rn.Name
return v
case *ast.ImportSpec:
v.src.imports = append(v.src.imports, rn)
return nil
case *ast.TypeSpec:
switch rn.Type.(type) {
default:
v.src.types = append(v.src.types, rn)
case *ast.InterfaceType:
// can't output interfaces
// because they'd conflict with our implementations
}
return &typeSpecVisitor{src: v.src, node: rn}
}
}
/*
package foo
type FooService interface {
Bar(ctx context.Context, i int, s string) (string, error)
}
*/
func (v *typeSpecVisitor) Visit(n ast.Node) ast.Visitor {
switch rn := n.(type) {
default:
return v
case *ast.Ident:
if v.name == nil {
v.name = rn
}
return v
case *ast.InterfaceType:
return &interfaceTypeVisitor{ts: v, methods: []method{}}
case nil:
if v.iface != nil {
v.iface.name = v.name
sn := *v.name
v.iface.stubname = &sn
v.iface.stubname.Name = v.name.String()
v.src.interfaces = append(v.src.interfaces, *v.iface)
}
return nil
}
}
func (v *interfaceTypeVisitor) Visit(n ast.Node) ast.Visitor {
switch n.(type) {
default:
return v
case *ast.Field:
return &methodVisitor{list: &v.methods}
case nil:
v.ts.iface = &iface{methods: v.methods}
return nil
}
}
func (v *methodVisitor) Visit(n ast.Node) ast.Visitor {
switch rn := n.(type) {
default:
v.depth++
return v
case *ast.Ident:
if rn.IsExported() {
v.name = rn
}
v.depth++
return v
case *ast.FuncType:
v.depth++
v.isMethod = true
return v
case *ast.FieldList:
if v.params == nil {
v.params = &[]arg{}
return &argListVisitor{list: v.params}
}
if v.results == nil {
v.results = &[]arg{}
}
return &argListVisitor{list: v.results}
case nil:
v.depth--
if v.depth == 0 && v.isMethod && v.name != nil {
*v.list = append(*v.list, method{name: v.name, params: *v.params, results: *v.results})
}
return nil
}
}
func (v *argListVisitor) Visit(n ast.Node) ast.Visitor {
switch n.(type) {
default:
return nil
case *ast.Field:
return &argVisitor{list: v.list}
}
}
func (v *argVisitor) Visit(n ast.Node) ast.Visitor {
switch t := n.(type) {
case *ast.CommentGroup, *ast.BasicLit:
return nil
case *ast.Ident: //Expr -> everything, but clarity
if t.Name != "_" {
v.parts = append(v.parts, t)
}
case ast.Expr:
v.parts = append(v.parts, t)
case nil:
names := v.parts[:len(v.parts)-1]
tp := v.parts[len(v.parts)-1]
if len(names) == 0 {
*v.list = append(*v.list, arg{typ: tp})
return nil
}
for _, n := range names {
*v.list = append(*v.list, arg{
name: n.(*ast.Ident),
typ: tp,
})
}
}
return nil
}