-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
263 lines (241 loc) · 5.7 KB
/
parser.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"path"
"reflect"
"strings"
)
var errBadReturn = errors.New("found return arg with no name: all args must be named")
type errUnexpectedType struct {
expected string
actual interface{}
}
func (e errUnexpectedType) Error() string {
return fmt.Sprintf("got wrong type expecting %s, got: %v", e.expected, reflect.TypeOf(e.actual))
}
// ParsedPkg holds information about a package that has been parsed,
// its name and the list of functions.
type ParsedPkg struct {
Name string
Functions []function
Imports []importSpec
}
type function struct {
Name string
Args []arg
Returns []arg
Doc string
}
type arg struct {
Name string
ArgType string
PackageSelector string
}
func (a *arg) String() string {
return a.Name + " " + a.ArgType
}
type importSpec struct {
Name string
Path string
}
func (s *importSpec) String() string {
var ss string
if len(s.Name) != 0 {
ss += s.Name
}
ss += s.Path
return ss
}
// Parse parses the given file for an interface definition with the given name.
func Parse(filePath string, objName string) (*ParsedPkg, error) {
fs := token.NewFileSet()
pkg, err := parser.ParseFile(fs, filePath, nil, parser.AllErrors)
if err != nil {
return nil, err
}
p := &ParsedPkg{}
p.Name = pkg.Name.Name
obj, exists := pkg.Scope.Objects[objName]
if !exists {
return nil, fmt.Errorf("could not find object %s in %s", objName, filePath)
}
if obj.Kind != ast.Typ {
return nil, fmt.Errorf("exected type, got %s", obj.Kind)
}
spec, ok := obj.Decl.(*ast.TypeSpec)
if !ok {
return nil, errUnexpectedType{"*ast.TypeSpec", obj.Decl}
}
iface, ok := spec.Type.(*ast.InterfaceType)
if !ok {
return nil, errUnexpectedType{"*ast.InterfaceType", spec.Type}
}
p.Functions, err = parseInterface(iface)
if err != nil {
return nil, err
}
// figure out what imports will be needed
imports := make(map[string]importSpec)
for _, f := range p.Functions {
args := append(f.Args, f.Returns...)
for _, arg := range args {
if len(arg.PackageSelector) == 0 {
continue
}
for _, i := range pkg.Imports {
if i.Name != nil {
if i.Name.Name != arg.PackageSelector {
continue
}
imports[i.Path.Value] = importSpec{Name: arg.PackageSelector, Path: i.Path.Value}
break
}
_, name := path.Split(i.Path.Value)
splitName := strings.Split(name, "-")
if len(splitName) > 1 {
name = splitName[len(splitName)-1]
}
// import paths have quotes already added in, so need to remove them for name comparison
name = strings.TrimPrefix(name, `"`)
name = strings.TrimSuffix(name, `"`)
if name == arg.PackageSelector {
imports[i.Path.Value] = importSpec{Path: i.Path.Value}
break
}
}
}
}
for _, spec := range imports {
p.Imports = append(p.Imports, spec)
}
return p, nil
}
func parseInterface(iface *ast.InterfaceType) ([]function, error) {
var functions []function
for _, field := range iface.Methods.List {
switch f := field.Type.(type) {
case *ast.FuncType:
method, err := parseFunc(field)
if err != nil {
return nil, err
}
if method == nil {
continue
}
functions = append(functions, *method)
case *ast.Ident:
spec, ok := f.Obj.Decl.(*ast.TypeSpec)
if !ok {
return nil, errUnexpectedType{"*ast.TypeSpec", f.Obj.Decl}
}
iface, ok := spec.Type.(*ast.InterfaceType)
if !ok {
return nil, errUnexpectedType{"*ast.TypeSpec", spec.Type}
}
funcs, err := parseInterface(iface)
if err != nil {
fmt.Println(err)
continue
}
functions = append(functions, funcs...)
default:
return nil, errUnexpectedType{"*astFuncType or *ast.Ident", f}
}
}
return functions, nil
}
func parseFunc(field *ast.Field) (*function, error) {
f := field.Type.(*ast.FuncType)
method := &function{Name: field.Names[0].Name}
if _, exists := skipFuncs[method.Name]; exists {
fmt.Println("skipping:", method.Name)
return nil, nil
}
if f.Params != nil {
args, err := parseArgs(f.Params.List)
if err != nil {
return nil, err
}
method.Args = args
}
if f.Results != nil {
returns, err := parseArgs(f.Results.List)
if err != nil {
return nil, fmt.Errorf("error parsing function returns for %q: %v", method.Name, err)
}
method.Returns = returns
}
return method, nil
}
func parseArgs(fields []*ast.Field) ([]arg, error) {
var args []arg
for _, f := range fields {
if len(f.Names) == 0 {
return nil, errBadReturn
}
for _, name := range f.Names {
p, err := parseExpr(f.Type)
if err != nil {
return nil, err
}
args = append(args, arg{name.Name, p.value, p.pkg})
}
}
return args, nil
}
type parsedExpr struct {
value string
pkg string
}
func parseExpr(e ast.Expr) (parsedExpr, error) {
var parsed parsedExpr
switch i := e.(type) {
case *ast.Ident:
parsed.value += i.Name
case *ast.StarExpr:
p, err := parseExpr(i.X)
if err != nil {
return parsed, err
}
parsed.value += "*"
parsed.value += p.value
parsed.pkg = p.pkg
case *ast.SelectorExpr:
p, err := parseExpr(i.X)
if err != nil {
return parsed, err
}
parsed.pkg = p.value
parsed.value += p.value + "."
parsed.value += i.Sel.Name
case *ast.MapType:
parsed.value += "map["
p, err := parseExpr(i.Key)
if err != nil {
return parsed, err
}
parsed.value += p.value
parsed.value += "]"
p, err = parseExpr(i.Value)
if err != nil {
return parsed, err
}
parsed.value += p.value
parsed.pkg = p.pkg
case *ast.ArrayType:
parsed.value += "[]"
p, err := parseExpr(i.Elt)
if err != nil {
return parsed, err
}
parsed.value += p.value
parsed.pkg = p.pkg
default:
return parsed, errUnexpectedType{"*ast.Ident or *ast.StarExpr", i}
}
return parsed, nil
}