forked from petergtz/pegomock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
179 lines (162 loc) · 4.89 KB
/
loader.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
package loader
import (
"errors"
"fmt"
"go/ast"
"go/types"
"github.com/petergtz/pegomock/model"
"golang.org/x/tools/go/loader"
)
func GenerateModel(importPath string, interfaceName string) (*model.Package, error) {
var conf loader.Config
conf.Import(importPath)
program, e := conf.Load()
if e != nil {
panic(e)
}
info := program.Imported[importPath]
for def := range info.Defs {
if def.Name == interfaceName && def.Obj.Kind == ast.Typ {
interfacetype, ok := def.Obj.Decl.(*ast.TypeSpec).Type.(*ast.InterfaceType)
if ok {
g := &modelGenerator{info: info}
iface := &model.Interface{
Name: interfaceName,
Methods: g.modelMethodsFrom(interfacetype.Methods),
}
return &model.Package{
Name: info.Pkg.Name(),
Interfaces: []*model.Interface{iface},
}, nil
}
}
}
return nil, errors.New("Did not find interface name \"" + interfaceName + "\"")
}
type modelGenerator struct {
info *loader.PackageInfo
}
func (g *modelGenerator) modelMethodsFrom(astMethods *ast.FieldList) (modelMethods []*model.Method) {
for _, astMethod := range astMethods.List {
modelMethods = append(modelMethods, g.modelMethodFrom(astMethod))
}
return
}
func (g *modelGenerator) modelMethodFrom(astMethod *ast.Field) *model.Method {
in, out, variadic := g.signatureFrom(astMethod.Type.(*ast.FuncType))
return &model.Method{Name: astMethod.Names[0].Name, In: in, Variadic: variadic, Out: out}
}
func (g *modelGenerator) signatureFrom(astFuncType *ast.FuncType) (in, out []*model.Parameter, variadic *model.Parameter) {
in, variadic = g.inParamsFrom(astFuncType.Params)
out = g.outParamsFrom(astFuncType.Results)
return
}
func (g *modelGenerator) inParamsFrom(params *ast.FieldList) (in []*model.Parameter, variadic *model.Parameter) {
for _, param := range params.List {
for _, name := range param.Names {
if ellipsisType, isEllipsisType := param.Type.(*ast.Ellipsis); isEllipsisType {
variadic = g.newParam(name.Name, ellipsisType.Elt)
} else {
in = append(in, g.newParam(name.Name, param.Type))
}
}
if len(param.Names) == 0 {
if ellipsisType, isEllipsisType := param.Type.(*ast.Ellipsis); isEllipsisType {
variadic = g.newParam("", ellipsisType.Elt)
} else {
in = append(in, g.newParam("", param.Type))
}
}
}
return
}
func (g *modelGenerator) outParamsFrom(results *ast.FieldList) (out []*model.Parameter) {
if results != nil {
for _, param := range results.List {
for _, name := range param.Names {
out = append(out, g.newParam(name.Name, param.Type))
}
if len(param.Names) == 0 {
out = append(out, g.newParam("", param.Type))
}
}
}
return
}
func (g *modelGenerator) newParam(name string, typ ast.Expr) *model.Parameter {
return &model.Parameter{
Name: name,
Type: g.modelTypeFrom(g.info.TypeOf(typ)),
}
}
func (g *modelGenerator) modelTypeFrom(typesType types.Type) model.Type {
switch typedTyp := typesType.(type) {
case *types.Basic:
if !predeclared(typedTyp.Kind()) {
panic(fmt.Sprintf("Unexpected Basic Type %v", typedTyp.Name()))
}
return model.PredeclaredType(typedTyp.Name())
case *types.Pointer:
return &model.PointerType{
Type: g.modelTypeFrom(typedTyp.Elem()),
}
case *types.Array:
return &model.ArrayType{
Len: int(typedTyp.Len()),
Type: g.modelTypeFrom(typedTyp.Elem()),
}
case *types.Slice:
return &model.ArrayType{
Len: -1,
Type: g.modelTypeFrom(typedTyp.Elem()),
}
case *types.Map:
return &model.MapType{
Key: g.modelTypeFrom(typedTyp.Key()),
Value: g.modelTypeFrom(typedTyp.Elem()),
}
case *types.Chan:
return &model.ChanType{
Dir: model.ChanDir(typedTyp.Dir()),
Type: g.modelTypeFrom(typedTyp.Elem()),
}
case *types.Named:
if typedTyp.Obj().Pkg() == nil {
return model.PredeclaredType(typedTyp.Obj().Name())
}
return &model.NamedType{
Package: typedTyp.Obj().Pkg().Path(),
Type: typedTyp.Obj().Name(),
}
case *types.Interface:
return model.PredeclaredType(typedTyp.String())
case *types.Signature:
in, variadic := g.generateInParamsFrom(typedTyp.Params())
out := g.generateOutParamsFrom(typedTyp.Results())
return &model.FuncType{In: in, Out: out, Variadic: variadic}
default:
panic(fmt.Sprintf("Unknown types.Type: %v (%T)", typesType, typesType))
}
}
func (g *modelGenerator) generateInParamsFrom(params *types.Tuple) (in []*model.Parameter, variadic *model.Parameter) {
// TODO: variadic
for i := 0; i < params.Len(); i++ {
in = append(in, &model.Parameter{
Name: params.At(i).Name(),
Type: g.modelTypeFrom(params.At(i).Type()),
})
}
return
}
func (g *modelGenerator) generateOutParamsFrom(params *types.Tuple) (out []*model.Parameter) {
for i := 0; i < params.Len(); i++ {
out = append(out, &model.Parameter{
Name: params.At(i).Name(),
Type: g.modelTypeFrom(params.At(i).Type()),
})
}
return
}
func predeclared(basicKind types.BasicKind) bool {
return basicKind >= types.Bool && basicKind <= types.String
}