/
parse.go
190 lines (178 loc) · 4.42 KB
/
parse.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
package ifacecodegen
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"strconv"
)
// ParseOptions are options used to parse
type ParseOptions struct {
Source io.Reader
}
// Parse returns parsed package
func Parse(opts ParseOptions) (*Package, error) {
if opts.Source == nil {
return nil, fmt.Errorf("Source should not be nil")
}
fs := token.NewFileSet()
file, err := parser.ParseFile(fs, "", opts.Source, parser.AllErrors)
if err != nil {
return nil, fmt.Errorf("failed parsing source %v", err)
}
pkg := &Package{
Name: file.Name.String(),
}
for _, decl := range file.Decls {
var gd, ok = decl.(*ast.GenDecl)
if !ok || gd.Tok != token.TYPE {
continue
}
for _, spec := range gd.Specs {
var ts *ast.TypeSpec
ts, ok = spec.(*ast.TypeSpec)
if !ok {
continue
}
// Filter for only type definitions that are also interfaces
var it *ast.InterfaceType
it, ok = ts.Type.(*ast.InterfaceType)
if !ok {
continue
}
iface := &Interface{
Name: ts.Name.String(),
}
for _, attribute := range it.Methods.List {
switch n := attribute.Type.(type) {
case *ast.FuncType:
m, e := parseFunc(pkg.Name, attribute.Names[0].String(), n)
if e != nil {
return nil, e
}
iface.Methods = append(iface.Methods, m)
}
}
pkg.Interfaces = append(pkg.Interfaces, iface)
}
}
return pkg, nil
}
func parseType(pkg string, arg ast.Expr) (Type, error) {
switch n := arg.(type) {
case *ast.ArrayType:
var len = -1
if n.Len != nil {
len, _ = strconv.Atoi(n.Len.(*ast.BasicLit).Value)
}
var typ, e = parseType(pkg, n.Elt)
if e != nil {
return nil, e
}
return &TypeArray{Len: len, Type: typ}, nil
case *ast.ChanType:
var t, e = parseType(pkg, n.Value)
if e != nil {
return nil, e
}
var chanType = &TypeChan{Type: t}
if n.Dir == ast.SEND {
chanType.WriteOnly = true
}
if n.Dir == ast.RECV {
chanType.ReadOnly = true
}
return chanType, nil
case *ast.Ellipsis:
var t, e = parseType(pkg, n.Elt)
if e != nil {
return nil, e
}
return &TypeVariadic{Type: t}, nil
case *ast.FuncType:
var method, e = parseFunc(pkg, "", n)
if e != nil {
return nil, e
}
var result = &TypeFunc{In: make([]Type, 0), Out: make([]Type, 0)}
for _, param := range method.In {
result.In = append(result.In, param.Type)
}
for _, param := range method.Out {
result.Out = append(result.Out, param.Type)
}
return result, nil
case *ast.Ident:
if n.IsExported() {
// assume type in this package
return &TypeExported{Package: pkg, Type: TypeBuiltin(n.Name)}, nil
}
return TypeBuiltin(n.Name), nil
case *ast.InterfaceType:
if n.Methods != nil && len(n.Methods.List) > 0 {
return nil, fmt.Errorf("can't handle non-empty unnamed interface types at %v", n.Pos())
}
return TypeBuiltin("interface{}"), nil
case *ast.MapType:
var key Type
var value Type
var e error
key, e = parseType(pkg, n.Key)
if e != nil {
return nil, e
}
value, e = parseType(pkg, n.Value)
if e != nil {
return nil, e
}
return &TypeMap{Key: key, Value: value}, nil
case *ast.SelectorExpr:
var pkgName = n.X.(*ast.Ident).String()
return &TypeExported{Package: pkgName, Type: TypeBuiltin(n.Sel.String())}, nil
case *ast.StarExpr:
var t, e = parseType(pkg, n.X)
if e != nil {
return nil, e
}
return &TypePointer{Type: t}, nil
case *ast.StructType:
if n.Fields != nil && len(n.Fields.List) > 0 {
return nil, fmt.Errorf("can't handle non-empty unnamed struct types at %v", n.Pos())
}
return TypeBuiltin("struct{}"), nil
}
return nil, fmt.Errorf("unknown type: %T", arg)
}
func parseFunc(pkg string, name string, f *ast.FuncType) (*Method, error) {
var method = &Method{Name: name, In: make([]*Parameter, 0), Out: make([]*Parameter, 0)}
if f.Params != nil {
for idx, arg := range f.Params.List {
var param = &Parameter{Name: fmt.Sprintf("_param%d", idx+1)}
if len(arg.Names) > 0 {
param.Name = arg.Names[0].String()
}
var t, e = parseType(pkg, arg.Type)
if e != nil {
return nil, e
}
param.Type = t
method.In = append(method.In, param)
}
}
if f.Results != nil {
for idx, arg := range f.Results.List {
var param = &Parameter{Name: fmt.Sprintf("_result%d", idx+1)}
if len(arg.Names) > 0 {
param.Name = arg.Names[0].String()
}
var t, e = parseType(pkg, arg.Type)
if e != nil {
return nil, e
}
param.Type = t
method.Out = append(method.Out, param)
}
}
return method, nil
}