forked from recht/gowrap
/
types.go
314 lines (266 loc) · 7.23 KB
/
types.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package generator
import (
"fmt"
"go/ast"
"strings"
)
type typePrinter interface {
PrintType(ast.Node) (string, error)
}
// Method represents a method's signature
type Method struct {
Name string
Params ParamsSlice
Results ParamsSlice
ReturnsError bool
AcceptsContext bool
}
// Param represents fuction argument or result
type Param struct {
Name string
Type string
Variadic bool
}
// ParamsSlice slice of parameters
type ParamsSlice []Param
// String implements fmt.Stringer
func (ps ParamsSlice) String() string {
ss := []string{}
for _, p := range ps {
ss = append(ss, p.Name+" "+p.Type)
}
return strings.Join(ss, ", ")
}
// Pass returns comma separated params names to
// be passed to a function call with respect to
// variadic functions
func (ps ParamsSlice) Pass() string {
params := []string{}
for _, p := range ps {
params = append(params, p.Pass())
}
return strings.Join(params, ", ")
}
// Pass returns a name of the parameter
// If parameter is variadic it returns a name followed by a ...
func (p Param) Pass() string {
if p.Variadic {
return p.Name + "..."
}
return p.Name
}
// NewMethod returns pointer to Signature struct or error
func NewMethod(name string, f *ast.FuncType, printer typePrinter) (*Method, error) {
m := Method{Name: name}
usedNames := map[string]bool{}
//Always name the last return parameter as an "err" if it's of type "error"
if f.Results != nil {
ident, ok := f.Results.List[len(f.Results.List)-1].Type.(*ast.Ident)
m.ReturnsError = ok && ident.Name == "error"
usedNames["err"] = true
}
if len(f.Params.List) > 0 {
if se, ok := f.Params.List[0].Type.(*ast.SelectorExpr); ok {
m.AcceptsContext = ok && se.Sel.Name == "Context"
usedNames["ctx"] = true
}
}
var err error
m.Params, err = makeParams(f.Params, usedNames, printer)
if err != nil {
return nil, err
}
m.Results, err = makeParams(f.Results, usedNames, printer)
if err != nil {
return nil, err
}
if m.ReturnsError {
m.Results[len(m.Results)-1].Name = "err"
}
if m.AcceptsContext {
m.Params[0].Name = "ctx"
}
return &m, nil
}
// NewParam returns Param struct
func NewParam(name string, typ ast.Expr, usedNames map[string]bool, printer typePrinter) (*Param, error) {
if name == "" || usedNames[name] {
name = genName(typePrefix(typ), 1, usedNames)
}
usedNames[name] = true
typeStr, err := printer.PrintType(typ)
if err != nil {
return nil, err
}
_, variadic := typ.(*ast.Ellipsis)
return &Param{
Name: name,
Variadic: variadic,
Type: typeStr,
}, nil
}
func makeParams(params *ast.FieldList, usedNames map[string]bool, printer typePrinter) (ParamsSlice, error) {
if params == nil {
return nil, nil
}
result := []Param{}
for _, p := range params.List {
//for anonymous parameters we generate params and results names
//based on their type
if p.Names == nil {
param, err := NewParam("", p.Type, usedNames, printer)
if err != nil {
return nil, err
}
result = append(result, *param)
} else {
for _, ident := range p.Names {
param, err := NewParam(ident.Name, p.Type, usedNames, printer)
if err != nil {
return nil, err
}
result = append(result, *param)
}
}
}
return result, nil
}
func typePrefix(e ast.Expr) string {
switch t := e.(type) {
case *ast.SelectorExpr:
return typePrefix(t.Sel)
case *ast.StarExpr:
return typePrefix(t.X) + "p" //*string -> sp (string pointer)
case *ast.SliceExpr:
return typePrefix(t.X) + "s" //[]string -> ss (string slice)
case *ast.ArrayType:
return typePrefix(t.Elt) + "a" //[2]string -> sa (string array)
case *ast.MapType:
return "m"
case *ast.ChanType:
return "ch"
case *ast.StructType:
return "st"
case *ast.FuncType:
return "f"
case *ast.Ident:
return strings.ToLower(t.Name[0:1])
}
return "p"
}
func genName(prefix string, n int, usedNames map[string]bool) string {
name := fmt.Sprintf("%s%d", prefix, n)
if usedNames[name] {
return genName(prefix, n+1, usedNames)
}
return name
}
// Call returns a string with the method call
func (m Method) Call() string {
params := []string{}
for _, p := range m.Params {
params = append(params, p.Pass())
}
return m.Name + "(" + strings.Join(params, ", ") + ")"
}
// Pass returns a return statement followed by the method call
// If method does not have any results it returns a method call followed by return statement
func (m Method) Pass(prefix string) string {
if len(m.Results) > 0 {
return "return " + prefix + m.Call()
}
return prefix + m.Call() + "\nreturn"
}
// ParamsNames returns a list of method params names
func (m Method) ParamsNames() string {
ss := []string{}
for _, p := range m.Params {
ss = append(ss, p.Name)
}
return strings.Join(ss, ", ")
}
// ResultsNames returns a list of method results names
func (m Method) ResultsNames() string {
ss := []string{}
for _, r := range m.Results {
ss = append(ss, r.Name)
}
return strings.Join(ss, ", ")
}
// ParamsStruct returns a struct type with fields corresponding
// to the method params
func (m Method) ParamsStruct() string {
ss := []string{}
for _, p := range m.Params {
if p.Variadic {
ss = append(ss, p.Name+" "+strings.Replace(p.Type, "...", "[]", 1))
} else {
ss = append(ss, p.Name+" "+p.Type)
}
}
return "struct{\n" + strings.Join(ss, "\n ") + "}"
}
// ResultsStruct returns a struct type with fields corresponding
// to the method results
func (m Method) ResultsStruct() string {
ss := []string{}
for _, r := range m.Results {
ss = append(ss, r.Name+" "+r.Type)
}
return "struct{\n" + strings.Join(ss, "\n ") + "}"
}
// ParamsMap returns a string representation of the map[string]interface{}
// filled with method's params
func (m Method) ParamsMap() string {
ss := []string{}
for _, p := range m.Params {
ss = append(ss, `"`+p.Name+`": `+p.Name)
}
return "map[string]interface{}{\n" + strings.Join(ss, ",\n ") + "}"
}
// ResultsMap returns a string representation of the map[string]interface{}
// filled with method's results
func (m Method) ResultsMap() string {
ss := []string{}
for _, r := range m.Results {
ss = append(ss, `"`+r.Name+`": `+r.Name)
}
return "map[string]interface{}{\n" + strings.Join(ss, ",\n ") + "}"
}
// HasParams returns true if method has params
func (m Method) HasParams() bool {
return len(m.Params) > 0
}
// HasResults returns true if method has results
func (m Method) HasResults() bool {
return len(m.Results) > 0
}
// ReturnStruct returns return statement with the return params
// taken from the structName
func (m Method) ReturnStruct(structName string) string {
if len(m.Results) == 0 {
return "return"
}
ss := []string{}
for _, r := range m.Results {
ss = append(ss, structName+"."+r.Name)
}
return "return " + strings.Join(ss, ", ")
}
// Signature returns comma separated method's params followed by the comma separated
// method's results
func (m Method) Signature() string {
params := []string{}
for _, p := range m.Params {
params = append(params, p.Name+" "+p.Type)
}
results := []string{}
for _, r := range m.Results {
results = append(results, r.Name+" "+r.Type)
}
return "(" + strings.Join(params, ", ") + ") (" + strings.Join(results, ", ") + ")"
}
// Declaration returns a method name followed by it's signature
func (m Method) Declaration() string {
return m.Name + m.Signature()
}