/
model.go
416 lines (367 loc) · 8.28 KB
/
model.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model
import (
"errors"
"fmt"
"strings"
)
type Kind uint
const (
KindInvalid Kind = iota
KindBool
KindInt
KindInt8
KindInt16
KindInt32
KindInt64
KindUint
KindUint8
KindUint16
KindUint32
KindUint64
KindUintptr
KindFloat32
KindFloat64
KindComplex64
KindComplex128
KindArray
KindChan
KindFunc
KindInterface
KindMap
KindPtr
KindSlice
KindString
KindStruct
KindUnsafePointer
)
type Category int64
const (
CategoryConstant Category = 1
CategoryBinary Category = 8
CategoryMap Category = 9
CategoryList Category = 10
CategorySet Category = 11
CategoryEnum Category = 12
CategoryStruct Category = 13
CategoryUnion Category = 14
CategoryException Category = 15
CategoryTypedef Category = 16
CategoryService Category = 17
)
type Model struct {
FilePath string
Package string
Imports map[string]*Model //{{import}}:Model
// rendering data
PackageName string
// Imports map[string]string //{{alias}}:{{import}}
Typedefs []TypeDef
Constants []Constant
Variables []Variable
Functions []Function
Enums []Enum
Structs []Struct
Methods []Method
Oneofs []Oneof
}
func (m Model) IsEmpty() bool {
return len(m.Typedefs) == 0 && len(m.Constants) == 0 && len(m.Variables) == 0 &&
len(m.Functions) == 0 && len(m.Enums) == 0 && len(m.Structs) == 0 && len(m.Methods) == 0
}
type Models []*Model
func (a *Models) MergeMap(b map[string]*Model) {
for _, v := range b {
insert := true
for _, p := range *a {
if p == v {
insert = false
}
}
if insert {
*a = append(*a, v)
}
}
return
}
func (a *Models) MergeArray(b []*Model) {
for _, v := range b {
insert := true
for _, p := range *a {
if p == v {
insert = false
}
}
if insert {
*a = append(*a, v)
}
}
return
}
type RequiredNess int
const (
RequiredNess_Default RequiredNess = 0
RequiredNess_Required RequiredNess = 1
RequiredNess_Optional RequiredNess = 2
)
type Type struct {
Name string
Scope *Model
Kind Kind
Indirect bool
Category Category
Extra []*Type // [{key_type},{value_type}] for map, [{element_type}] for list or set
HasNew bool
}
func (rt *Type) ResolveDefaultValue() string {
if rt == nil {
return ""
}
switch rt.Kind {
case KindInt, KindInt8, KindInt16, KindInt32, KindInt64, KindUint, KindUint16, KindUint32, KindUint64,
KindFloat32, KindFloat64, KindComplex64, KindComplex128:
return "0"
case KindBool:
return "false"
case KindString:
return "\"\""
default:
return "nil"
}
}
func (rt *Type) ResolveNameForTypedef(scope *Model) (string, error) {
if rt == nil {
return "", errors.New("type is nil")
}
name := rt.Name
if rt.Scope == nil {
return rt.Name, nil
}
switch rt.Kind {
case KindArray, KindSlice:
if len(rt.Extra) != 1 {
return "", fmt.Errorf("the type: %s should have 1 extra type, but has %d", rt.Name, len(rt.Extra))
}
resolveName, err := rt.Extra[0].ResolveName(scope)
if err != nil {
return "", err
}
name = fmt.Sprintf("[]%s", resolveName)
case KindMap:
if len(rt.Extra) != 2 {
return "", fmt.Errorf("the type: %s should have 2 extra types, but has %d", rt.Name, len(rt.Extra))
}
resolveKey, err := rt.Extra[0].ResolveName(scope)
if err != nil {
return "", err
}
resolveValue, err := rt.Extra[1].ResolveName(scope)
if err != nil {
return "", err
}
name = fmt.Sprintf("map[%s]%s", resolveKey, resolveValue)
case KindChan:
if len(rt.Extra) != 1 {
return "", fmt.Errorf("the type: %s should have 1 extra type, but has %d", rt.Name, len(rt.Extra))
}
resolveName, err := rt.Extra[0].ResolveName(scope)
if err != nil {
return "", err
}
name = fmt.Sprintf("chan %s", resolveName)
}
if scope != nil && rt.Scope != &BaseModel && rt.Scope.Package != scope.Package {
name = rt.Scope.PackageName + "." + name
}
return name, nil
}
func (rt *Type) ResolveName(scope *Model) (string, error) {
if rt == nil {
return "", fmt.Errorf("type is nil")
}
name := rt.Name
if rt.Scope == nil {
if rt.Kind == KindStruct {
return "*" + rt.Name, nil
}
return rt.Name, nil
}
if rt.Category == CategoryTypedef {
if scope != nil && rt.Scope != &BaseModel && rt.Scope.Package != scope.Package {
name = rt.Scope.PackageName + "." + name
}
if rt.Kind == KindStruct {
name = "*" + name
}
return name, nil
}
switch rt.Kind {
case KindArray, KindSlice:
if len(rt.Extra) != 1 {
return "", fmt.Errorf("The type: %s should have 1 extra type, but has %d", rt.Name, len(rt.Extra))
}
resolveName, err := rt.Extra[0].ResolveName(scope)
if err != nil {
return "", err
}
name = fmt.Sprintf("[]%s", resolveName)
case KindMap:
if len(rt.Extra) != 2 {
return "", fmt.Errorf("The type: %s should have 2 extra type, but has %d", rt.Name, len(rt.Extra))
}
resolveKey, err := rt.Extra[0].ResolveName(scope)
if err != nil {
return "", err
}
resolveValue, err := rt.Extra[1].ResolveName(scope)
if err != nil {
return "", err
}
name = fmt.Sprintf("map[%s]%s", resolveKey, resolveValue)
case KindChan:
if len(rt.Extra) != 1 {
return "", fmt.Errorf("The type: %s should have 1 extra type, but has %d", rt.Name, len(rt.Extra))
}
resolveName, err := rt.Extra[0].ResolveName(scope)
if err != nil {
return "", err
}
name = fmt.Sprintf("chan %s", resolveName)
}
if scope != nil && rt.Scope != &BaseModel && rt.Scope.Package != scope.Package {
name = rt.Scope.PackageName + "." + name
}
if rt.Kind == KindStruct {
name = "*" + name
}
return name, nil
}
func (rt *Type) IsBinary() bool {
return rt.Category == CategoryBinary && (rt.Kind == KindSlice || rt.Kind == KindArray)
}
func (rt *Type) IsBaseType() bool {
return rt.Kind < KindComplex64
}
func (rt *Type) IsSettable() bool {
switch rt.Kind {
case KindArray, KindChan, KindFunc, KindInterface, KindMap, KindPtr, KindSlice, KindUnsafePointer:
return true
}
return false
}
type TypeDef struct {
Scope *Model
Alias string
Type *Type
}
type Constant struct {
Scope *Model
Name string
Type *Type
Value Literal
}
type Literal interface {
Expression() string
}
type Variable struct {
Scope *Model
Name string
Type *Type
Value Literal
}
type Function struct {
Scope *Model
Name string
Args []Variable
Rets []Variable
Code string
}
type Method struct {
Scope *Model
ReceiverName string
ReceiverType *Type
ByPtr bool
Function
}
type Enum struct {
Scope *Model
Name string
GoType string
Values []Constant
}
type Struct struct {
Scope *Model
Name string
Fields []Field
Category Category
LeadingComments string
}
type Field struct {
Scope *Struct
Name string
Type *Type
IsSetDefault bool
DefaultValue Literal
Required RequiredNess
Tags Tags
LeadingComments string
TrailingComments string
IsPointer bool
}
type Oneof struct {
MessageName string
OneofName string
InterfaceName string
Choices []Choice
}
type Choice struct {
MessageName string
ChoiceName string
Type *Type
}
type Tags []Tag
type Tag struct {
Key string
Value string
}
func (ts Tags) String() string {
ret := make([]string, 0, len(ts))
for _, t := range ts {
ret = append(ret, fmt.Sprintf("%v:%q", t.Key, t.Value))
}
return strings.Join(ret, " ")
}
func (ts *Tags) Remove(name string) {
ret := make([]Tag, 0, len(*ts))
for _, t := range *ts {
if t.Key != name {
ret = append(ret, t)
}
}
*ts = ret
}
func (ts Tags) Len() int { return len(ts) }
func (ts Tags) Less(i, j int) bool {
return ts[i].Key < ts[j].Key
}
func (ts Tags) Swap(i, j int) { ts[i], ts[j] = ts[j], ts[i] }
func (f Field) GenGoTags() string {
if len(f.Tags) == 0 {
return ""
}
return fmt.Sprintf("`%s`", f.Tags.String())
}