-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnippet_types.go
377 lines (295 loc) · 6.43 KB
/
snippet_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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package codegen
import (
"bytes"
"fmt"
"go/token"
"reflect"
)
type SnippetType interface {
Snippet
snippetType()
}
type ImportPathAliaser func(importPath string) string
var TypeOf = createTypeOf(LowerSnakeCase)
func createTypeOf(aliaser ImportPathAliaser) func(tpe reflect.Type) SnippetType {
return func(tpe reflect.Type) SnippetType {
if tpe.PkgPath() != "" {
return Type(aliaser(tpe.PkgPath()) + "." + tpe.Name())
}
typeof := createTypeOf(aliaser)
switch tpe.Kind() {
case reflect.Ptr:
return Star(typeof(tpe.Elem()))
case reflect.Chan:
return Chan(typeof(tpe.Elem()))
case reflect.Struct:
fields := make([]*SnippetField, 0)
for i := 0; i < tpe.NumField(); i++ {
f := tpe.Field(i)
if f.Anonymous {
fields = append(fields, Var(typeof(f.Type)).WithTag(string(f.Tag)))
} else {
fields = append(fields, Var(typeof(f.Type), f.Name).WithTag(string(f.Tag)))
}
}
return Struct(fields...)
case reflect.Array:
return Array(typeof(tpe.Elem()), tpe.Len())
case reflect.Slice:
return Slice(typeof(tpe.Elem()))
case reflect.Map:
return Map(typeof(tpe.Key()), typeof(tpe.Elem()))
default:
return BuiltInType(tpe.String())
}
}
}
func Ellipsis(tpe SnippetType) *EllipsisType {
return &EllipsisType{
Elem: tpe,
}
}
type EllipsisType struct {
SnippetType
Elem SnippetType
}
func (tpe *EllipsisType) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteString(token.ELLIPSIS.String())
buf.Write(tpe.Elem.Bytes())
return buf.Bytes()
}
func Chan(tpe SnippetType) *ChanType {
return &ChanType{
Elem: tpe,
}
}
type ChanType struct {
SnippetType
Elem SnippetType
}
func (tpe *ChanType) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteString(token.CHAN.String() + " ")
buf.Write(tpe.Elem.Bytes())
return buf.Bytes()
}
func Type(name string) *NamedType {
return &NamedType{
Name: Id(name),
}
}
type NamedType struct {
SnippetType
SnippetCanBeInterfaceMethod
SnippetCanAddr
Name *SnippetIdent
}
func (tpe *NamedType) Bytes() []byte {
return tpe.Name.Bytes()
}
func Func(params ...*SnippetField) *FuncType {
return &FuncType{
Params: params,
}
}
type FuncType struct {
SnippetType
SnippetCanBeInterfaceMethod
Name *SnippetIdent
Recv *SnippetField
Params []*SnippetField
Results []*SnippetField
Body []Snippet
noFuncToken bool
}
func (f FuncType) withoutFuncToken() *FuncType {
f.noFuncToken = true
return &f
}
func (f FuncType) Do(bodies ...Snippet) *FuncType {
f.Body = append([]Snippet{}, bodies...)
return &f
}
func (f FuncType) Named(name string) *FuncType {
f.Name = Id(name)
return &f
}
func (f FuncType) MethodOf(recv *SnippetField) *FuncType {
f.Recv = recv
return &f
}
func (f FuncType) Return(results ...*SnippetField) *FuncType {
f.Results = results
return &f
}
func (f *FuncType) Bytes() []byte {
buf := &bytes.Buffer{}
if !f.noFuncToken {
buf.WriteString(token.FUNC.String())
buf.WriteRune(' ')
}
if f.Recv != nil {
buf.WriteByte('(')
buf.Write(f.Recv.Bytes())
buf.WriteString(") ")
}
if f.Name != nil {
buf.Write(f.Name.Bytes())
}
buf.WriteByte('(')
for i := range f.Params {
if i > 0 {
buf.WriteString(", ")
}
buf.Write(f.Params[i].WithoutTag().Bytes())
}
buf.WriteByte(')')
hasResults := len(f.Results) > 0
if hasResults {
buf.WriteString(" (")
}
for i := range f.Results {
if i > 0 {
buf.WriteString(", ")
}
buf.Write(f.Results[i].WithoutTag().Bytes())
}
if hasResults {
buf.WriteByte(')')
}
if f.Body != nil {
buf.WriteRune(' ')
buf.Write(Body(f.Body).Bytes())
}
return buf.Bytes()
}
func Struct(fields ...*SnippetField) *StructType {
return &StructType{
Fields: fields,
}
}
type StructType struct {
SnippetType
Fields []*SnippetField
}
func (tpe *StructType) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteString(token.STRUCT.String() + " {")
for i := range tpe.Fields {
buf.WriteRune('\n')
buf.Write(tpe.Fields[i].Bytes())
}
buf.WriteRune('\n')
buf.WriteRune('}')
return buf.Bytes()
}
func Interface(methods ...SnippetCanBeInterfaceMethod) *InterfaceType {
return &InterfaceType{
Methods: methods,
}
}
type SnippetCanBeInterfaceMethod interface {
canBeInterfaceMethod()
}
type InterfaceType struct {
SnippetType
Methods []SnippetCanBeInterfaceMethod
}
func (tpe *InterfaceType) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteString(token.INTERFACE.String() + " {")
for i := range tpe.Methods {
if i == 0 {
buf.WriteRune('\n')
}
methodType := tpe.Methods[i]
switch methodType.(type) {
case *FuncType:
buf.Write(methodType.(*FuncType).withoutFuncToken().Bytes())
case *NamedType:
buf.Write(methodType.(*NamedType).Bytes())
}
buf.WriteRune('\n')
}
buf.WriteRune('}')
return buf.Bytes()
}
func Map(key SnippetType, value SnippetType) *MapType {
return &MapType{
Key: key,
Value: value,
}
}
type MapType struct {
SnippetType
Key SnippetType
Value SnippetType
}
func (tpe *MapType) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteString(token.MAP.String() + "[")
buf.Write(tpe.Key.Bytes())
buf.WriteRune(']')
buf.Write(tpe.Value.Bytes())
return buf.Bytes()
}
func Slice(tpe SnippetType) *SliceType {
return &SliceType{
Elem: tpe,
}
}
type SliceType struct {
SnippetType
Elem SnippetType
}
func (tpe *SliceType) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteString("[]")
buf.Write(tpe.Elem.Bytes())
return buf.Bytes()
}
func Array(tpe SnippetType, len int) *ArrayType {
return &ArrayType{
Elem: tpe,
Len: len,
}
}
type ArrayType struct {
SnippetType
Elem SnippetType
Len int
}
func (tpe *ArrayType) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteString(fmt.Sprintf("[%d]", tpe.Len))
buf.Write(tpe.Elem.Bytes())
return buf.Bytes()
}
type BuiltInType string
func (BuiltInType) snippetType() {}
func (tpe BuiltInType) Bytes() []byte {
return []byte(string(tpe))
}
const (
Bool BuiltInType = "bool"
Int BuiltInType = "int"
Int8 BuiltInType = "int8"
Int16 BuiltInType = "int16"
Int32 BuiltInType = "int32"
Int64 BuiltInType = "int64"
Uint BuiltInType = "uint"
Uint8 BuiltInType = "uint8"
Uint16 BuiltInType = "uint16"
Uint32 BuiltInType = "uint32"
Uint64 BuiltInType = "uint64"
Uintptr BuiltInType = "uintptr"
Float32 BuiltInType = "float32"
Float64 BuiltInType = "float64"
Complex64 BuiltInType = "complex64"
Complex128 BuiltInType = "complex128"
String BuiltInType = "string"
Byte BuiltInType = "byte"
Rune BuiltInType = "rune"
Error BuiltInType = "error"
)