-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-golang.go
663 lines (568 loc) · 15.4 KB
/
generate-golang.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package main
import (
"embed"
"fmt"
"os"
"strconv"
"strings"
"text/template"
"ella.to/ella/internal/strcase"
)
//go:embed templates/golang/*.go.tmpl
var golangTemplateFiles embed.FS
func generateGo(pkg, output string, doc *Document) error {
// CONSTANTS
type GoConst struct {
Name string
Value string
}
// ENUMS
type GoEnumKeyValue struct {
Name string
Value string
}
type GoEnum struct {
Name string
Type string // int8, int16, int32, int64
Keys []GoEnumKeyValue
}
// MODELS
type GoModelField struct {
Name string
Type string
Tags string
}
type GoModel struct {
Name string
Fields []GoModelField
}
// SERVICES
type GoMethodArg struct {
Name string
Type string
}
type GoMethodReturn struct {
Name string
Type string
Stream bool
}
type GoMethodOption struct {
Name string
Value any
}
type GoMethod struct {
Name string
Type string // http, rpc
ServiceName string // add this so it would be easier to generate the service path
Args []GoMethodArg
Returns []GoMethodReturn
Options []GoMethodOption
HasArgs bool
HasReturns bool
HttpRawControl bool
HttpMethod string
IsBinary bool
IsStream bool
IsUpload bool
Timeout int64
TotalMaxSize int64
}
type GoService struct {
Name string
Methods []GoMethod
}
// ERRORS
type GoError struct {
Name string
Code int64
Status int
Message string
}
type Data struct {
PackageName string
Constants []GoConst
Enums []GoEnum
Models []GoModel
HttpServices []GoService
RpcServices []GoService
Errors []GoError
}
tmpl, err := template.
New("GenerateGo").
Funcs(defaultFuncsMap).
Funcs(template.FuncMap{
"ToMethodArgs": func(args []GoMethodArg) string {
var sb strings.Builder
sb.WriteString("ctx context.Context")
// for file upload we will suffle the order of the arguments
// and put the file upload argument at the end
{
// First find the index of the file upload argument
fileUploadArgIdx := -1
for i, arg := range args {
if arg.Type == "func() (string, io.Reader, error)" {
fileUploadArgIdx = i
break
}
}
// If we found the file upload argument, we will suffle the order
// and move it to the end
if fileUploadArgIdx != -1 {
element := args[fileUploadArgIdx]
// Remove the element from its current position
args = append(args[:fileUploadArgIdx], args[fileUploadArgIdx+1:]...)
// Append it to the end
args = append(args, element)
}
}
for _, arg := range args {
sb.WriteString(", ")
sb.WriteString(arg.Name)
sb.WriteString(" ")
sb.WriteString(arg.Type)
}
return sb.String()
},
"ToMethodReturns": func(returns []GoMethodReturn) string {
var sb strings.Builder
for i, ret := range returns {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(ret.Name)
sb.WriteString(" ")
sb.WriteString(ret.Type)
}
if len(returns) > 0 {
sb.WriteString(", ")
}
sb.WriteString("err error")
return sb.String()
},
"HasOption": func(options []GoMethodOption, name string) bool {
for _, opt := range options {
if opt.Name == name {
return true
}
}
return false
},
"ToServicePathName": func(service GoService) string {
return fmt.Sprintf("PathHttp%sPrefix", strcase.ToPascal(service.Name))
},
"ToServicePathValue": func(service GoService) string {
return fmt.Sprintf("/ella/http/%s/", strcase.ToPascal(service.Name))
},
"ToMethodPathName": func(method GoMethod) string {
return fmt.Sprintf("PathHttp%s%sMethod", strcase.ToPascal(method.ServiceName), strcase.ToPascal(method.Name))
},
"ToMethodPathValue": func(method GoMethod) string {
return fmt.Sprintf("/ella/http/%s/%s", strcase.ToPascal(method.ServiceName), strcase.ToPascal(method.Name))
},
"ToHttpServiceImplName": func(service GoService) string {
return fmt.Sprintf("http%sServer", strcase.ToPascal(service.Name))
},
"ToRpcServiceTopicName": func(service GoService) string {
return fmt.Sprintf("TopicRpc%s", strcase.ToPascal(service.Name))
},
"ToRpcServiceTopicValue": func(service GoService) string {
return fmt.Sprintf("ella.rpc.%s.*", strcase.ToSnake(service.Name))
},
"ToRpcServiceMethodTopicName": func(method GoMethod) string {
return fmt.Sprintf("TopicRpc%s%sMethod", strcase.ToPascal(method.ServiceName), strcase.ToPascal(method.Name))
},
"ToRpcServiceMethodTopicValue": func(method GoMethod) string {
return fmt.Sprintf("ella.rpc.%s.%s", strcase.ToSnake(method.ServiceName), strcase.ToSnake((method.Name)))
},
"ToArgsDefinition": func(tabs int, args []GoMethodArg) string {
var sb strings.Builder
i := 0
for _, arg := range args {
if arg.Type == "func() (string, io.Reader, error)" {
continue
}
if i > 0 {
sb.WriteString("\n")
}
sb.WriteString(strings.Repeat(" ", tabs))
sb.WriteString(strcase.ToPascal(arg.Name))
sb.WriteString(" ")
sb.WriteString(arg.Type)
sb.WriteString(" `json:\"")
sb.WriteString(strcase.ToCamel(arg.Name))
sb.WriteString("\"`")
i++
}
return sb.String()
},
"GetArgFileUploadName": func(args []GoMethodArg) string {
for _, arg := range args {
if arg.Type == "func() (string, io.Reader, error)" {
return arg.Name
}
}
return ""
},
"ArgsList": func(args []GoMethodArg) string {
var sb strings.Builder
i := 0
for _, arg := range args {
if arg.Type == "func() (string, io.Reader, error)" {
continue
}
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(arg.Name)
i++
}
return sb.String()
},
"ToArgsAccess": func(prefix string, args []GoMethodArg) string {
var sb strings.Builder
sb.WriteString("ctx")
i := 0
for _, arg := range args {
if arg.Type == "func() (string, io.Reader, error)" {
continue
}
sb.WriteString(", ")
sb.WriteString(prefix)
sb.WriteString(strcase.ToPascal(arg.Name))
i++
}
return sb.String()
},
"ToReturnsDefinition": func(tabs int, returns []GoMethodReturn) string {
// We don't need to return anything if the method is a stream
// because the return type is already known as (io.Reader, string, string, error)
// for binary or channel of the return type
for _, ret := range returns {
if ret.Stream || strings.Index(ret.Type, "<-chan") != -1 {
return ""
}
}
var sb strings.Builder
for i, ret := range returns {
if i > 0 {
sb.WriteString("\n")
}
sb.WriteString(strings.Repeat(" ", tabs))
sb.WriteString(strcase.ToPascal(ret.Name))
sb.WriteString(" ")
sb.WriteString(ret.Type)
sb.WriteString(" `json:\"")
sb.WriteString(strcase.ToCamel(ret.Name))
sb.WriteString("\"`")
i++
}
return sb.String()
},
"ToReturnsAccess": func(prefix string, returns []GoMethodReturn) string {
var sb strings.Builder
for i, ret := range returns {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(prefix)
sb.WriteString(strcase.ToPascal(ret.Name))
}
if len(returns) > 0 {
sb.WriteString(", ")
}
sb.WriteString("err")
return sb.String()
},
"StreamType": func(returns []GoMethodReturn) (typ string) {
ret := returns[0]
typ = strings.ReplaceAll(ret.Type, "<-chan ", "")
return
},
"IsPointerType": func(typ string) bool {
return strings.HasPrefix(typ, "*")
},
}).
ParseFS(golangTemplateFiles, "templates/golang/*.go.tmpl")
if err != nil {
return err
}
out, err := os.Create(output)
if err != nil {
return err
}
// Helper functions
isModelType := createIsModelTypeFunc(doc.Models)
getServicesByMethodType := func(typ MethodType) []GoService {
return mapperFunc(getGolangServicesByType(doc.Services, typ), func(service *Service) GoService {
return GoService{
Name: service.Name.Token.Value,
Methods: mapperFunc(service.Methods, func(method *Method) GoMethod {
goMethod := GoMethod{
Name: method.Name.Token.Value,
Type: method.Type.String(),
ServiceName: service.Name.Token.Value,
Args: mapperFunc(method.Args, func(arg *Arg) GoMethodArg {
return GoMethodArg{
Name: strcase.ToCamel(arg.Name.Token.Value),
Type: getGolangType(arg.Type, isModelType),
}
}),
Returns: mapperFunc(method.Returns, func(ret *Return) GoMethodReturn {
typ := getGolangType(ret.Type, isModelType)
if ret.Stream && typ == "[]byte" {
typ = "io.Reader"
}
return GoMethodReturn{
Name: strcase.ToCamel(ret.Name.Token.Value),
Type: typ,
Stream: ret.Stream,
}
}),
Options: mapperFunc(method.Options.List, func(opt *Option) GoMethodOption {
return GoMethodOption{
Name: opt.Name.Token.Value,
Value: opt.Value,
}
}),
}
goMethod.HasArgs = len(goMethod.Args) > 0
goMethod.HasReturns = len(goMethod.Returns) > 0
// the default value for http method is POST
if typ == MethodHTTP {
goMethod.HttpMethod = "POST"
goMethod.TotalMaxSize = 2 * 1024 * 1024
}
for _, opt := range goMethod.Options {
switch opt.Name {
case "HttpRawControl":
if typ != MethodHTTP {
break
}
goMethod.HttpRawControl = true
case "HttpMethod":
if typ != MethodHTTP {
break
}
if v, ok := opt.Value.(*ValueString); ok {
goMethod.HttpMethod = v.Value
} else {
goMethod.HttpMethod = "POST"
}
case "Timeout":
if typ != MethodHTTP {
break
}
if v, ok := opt.Value.(*ValueDuration); ok {
goMethod.Timeout = v.Value * int64(v.Scale)
}
case "TotalMaxSize":
if typ != MethodHTTP {
break
}
if v, ok := opt.Value.(*ValueByteSize); ok {
goMethod.TotalMaxSize = v.Value * int64(v.Scale)
}
}
}
for _, arg := range goMethod.Args {
if arg.Type == "func() (string, io.Reader, error)" {
if typ == MethodHTTP {
goMethod.IsUpload = true
}
}
}
for _, ret := range goMethod.Returns {
if ret.Stream {
if typ == MethodHTTP {
goMethod.IsStream = true
}
}
if ret.Type == "io.Reader" {
if typ == MethodHTTP {
goMethod.IsBinary = true
}
}
}
if goMethod.IsStream && !goMethod.IsBinary {
goMethod.Returns = []GoMethodReturn{
{
Name: goMethod.Returns[0].Name,
Type: "<-chan " + goMethod.Returns[0].Type,
},
}
}
// need to add filename:string and contentType:string to returns
// if the method is stream and binary
if goMethod.IsStream && goMethod.IsBinary {
goMethod.Returns = append(goMethod.Returns, GoMethodReturn{
Name: "filename",
Type: "string",
}, GoMethodReturn{
Name: "contentType",
Type: "string",
})
}
return goMethod
}),
}
})
}
data := Data{
PackageName: pkg,
Constants: mapperFunc(doc.Consts, func(c *Const) GoConst {
return GoConst{
Name: c.Identifier.Token.Value,
Value: getGolangValue(c.Value),
}
}),
Enums: mapperFunc(doc.Enums, func(enum *Enum) GoEnum {
return GoEnum{
Name: enum.Name.Token.Value,
Type: fmt.Sprintf("int%d", enum.Size),
Keys: mapperFunc(enum.Sets, func(set *EnumSet) GoEnumKeyValue {
return GoEnumKeyValue{
Name: set.Name.Token.Value,
Value: fmt.Sprintf("%d", set.Value.Value),
}
}),
}
}),
Models: mapperFunc(doc.Models, func(model *Model) GoModel {
return GoModel{
Name: model.Name.Token.Value,
Fields: mapperFunc(model.Fields, func(field *Field) GoModelField {
return GoModelField{
Name: field.Name.Token.Value,
Type: getGolangType(field.Type, isModelType),
Tags: getGolangModelFieldTag(field),
}
}),
}
}),
HttpServices: getServicesByMethodType(MethodHTTP),
RpcServices: getServicesByMethodType(MethodRPC),
Errors: mapperFunc(doc.Errors, func(err *CustomError) GoError {
return GoError{
Name: err.Name.Token.Value,
Code: err.Code,
Status: err.HttpStatus,
Message: err.Msg.Value,
}
}),
}
return tmpl.ExecuteTemplate(out, "main", data)
}
func getGolangServicesByType(services []*Service, typ MethodType) []*Service {
return mapperFunc(filterFunc(services, func(service *Service) bool {
for _, method := range service.Methods {
if method.Type == typ || method.Type == MethodRpcHttp {
return true
}
}
return false
}), func(service *Service) *Service {
return &Service{
Token: service.Token,
Name: service.Name,
Methods: filterFunc(service.Methods, func(method *Method) bool {
return method.Type == typ || method.Type == MethodRpcHttp
}),
Comments: service.Comments,
}
})
}
func getGolangValue(value Value) string {
var sb strings.Builder
switch v := value.(type) {
case *ValueString:
if v.Token.Type == TokConstStringSingleQuote {
return fmt.Sprintf(`"%s"`, strings.ReplaceAll(v.Token.Value, `"`, `\"`))
} else {
value.Format(&sb)
return sb.String()
}
case *ValueInt:
return strconv.FormatInt(v.Value, 10)
case *ValueByteSize:
return fmt.Sprintf(`%d`, v.Value*int64(v.Scale))
case *ValueDuration:
return fmt.Sprintf(`%d`, v.Value*int64(v.Scale))
default:
value.Format(&sb)
return sb.String()
}
}
func getGolangType(typ Type, isModelType func(value string) bool) string {
switch typ := typ.(type) {
case *CustomType:
var sb strings.Builder
typ.Format(&sb)
val := sb.String()
if isModelType(val) {
return "*" + val
}
return val
case *Any:
return "any"
case *Int:
return fmt.Sprintf("int%d", typ.Size)
case *Uint:
return fmt.Sprintf("uint%d", typ.Size)
case *Byte:
return "byte"
case *Float:
return fmt.Sprintf("float%d", typ.Size)
case *String:
return "string"
case *Bool:
return "bool"
case *Timestamp:
return "time.Time"
case *Map:
return fmt.Sprintf("map[%s]%s", getGolangType(typ.Key, isModelType), getGolangType(typ.Value, isModelType))
case *Array:
return fmt.Sprintf("[]%s", getGolangType(typ.Type, isModelType))
case *File:
return "func() (string, io.Reader, error)"
}
// This shouldn't happen as the validator should catch this any errors
panic(fmt.Sprintf("unknown type: %T", typ))
}
func getGolangModelFieldTag(field *Field) string {
var sb strings.Builder
mapper := make(map[string]Value)
for _, opt := range field.Options.List {
mapper[strings.ToLower(opt.Name.Token.Value)] = opt.Value
}
jsonTagValue := strcase.ToCamel(field.Name.Token.Value)
jsonValue, ok := mapper["json"]
if ok {
switch jsonValue := jsonValue.(type) {
case *ValueString:
jsonTagValue = jsonValue.Token.Value
case *ValueBool:
if !jsonValue.Value {
jsonTagValue = "-"
}
}
}
jsonOmitEmptyValue, isJsonOmitEmpty := mapper["jsonomitempty"]
if isJsonOmitEmpty && jsonTagValue != "-" {
switch value := jsonOmitEmptyValue.(type) {
case *ValueBool:
if value.Value {
jsonTagValue += ",omitempty"
}
}
}
if field.IsOptional {
if !isJsonOmitEmpty {
jsonTagValue += ",omitempty"
}
jsonTagValue += ",omitzero"
}
sb.WriteString(`json:"`)
sb.WriteString(jsonTagValue)
sb.WriteString(`"`)
return sb.String()
}