-
Notifications
You must be signed in to change notification settings - Fork 2
/
methods.go
1977 lines (1519 loc) · 52.6 KB
/
methods.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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/golang/protobuf/proto"
pgd "github.com/golang/protobuf/protoc-gen-go/descriptor"
pgs "github.com/lyft/protoc-gen-star"
pbt "go.appointy.com/protoc-gen-jaal/schema"
)
type Value struct {
Value string
Index int32
}
type enum struct {
Name string
Values []Value
}
type MsgFields struct {
TargetName string
FieldName string
FuncPara string
TargetVal string
}
type UnionObject struct {
UnionName string
UnionFields []string
}
type InputMap struct {
FieldName string
TargetName string
TargetVal string
Key string
Value string
}
type Duration struct {
FieldName string
Name string
}
type Id struct {
FieldName string
Name string
}
type InputClass struct {
Name string
Type string
InputObjName string
Maps []InputMap
Durations []Duration
Fields []MsgFields
Ids []Id
}
func (m *jaalModule) scalarMap(scalar string) string {
// Maps protoc scalars to go scalars
switch scalar {
case "BOOL":
return "bool"
case "INT32":
return "int32"
case "INT64":
return "int64"
case "UINT32":
return "uint32"
case "UINT64":
return "uint64"
case "SINT32":
return "int32"
case "SINT64":
return "int64"
case "FIXED32":
return "uint32"
case "FIXED64":
return "uint64"
case "SFIXED32":
return "int32"
case "SFIXED64":
return "int64"
case "FLOAT":
return "float32"
case "DOUBLE":
return "float64"
case "STRING":
return "string"
case "BYTES":
return "[]byte"
}
return ""
}
func (m *jaalModule) fieldElementType(valKey pgs.FieldTypeElem) string {
// returns Type for a pgs.FieldTypeElem
switch valKey.ProtoType().Proto().String() {
case "TYPE_MESSAGE":
obj := valKey.Embed()
return obj.Name().String()
case "TYPE_ENUM":
enum := valKey.Enum()
return enum.Name().String()
default:
tType := strings.Split(valKey.ProtoType().Proto().String(), "_")
return m.scalarMap(tType[len(tType)-1])
}
}
type PayloadFields struct {
FieldName string
FuncPara string
TargetVal string
}
type OneOfFields struct {
CaseName string
ReturnType string
}
type UnionObjectPayload struct {
FieldName string
FuncReturn string
SwitchName string
Fields []OneOfFields
}
type PayloadMap struct {
FieldName string
TargetVal string
}
type Payload struct {
Name string
Type string
PayloadObjName string
UnionObjects []UnionObjectPayload
Maps []PayloadMap
Durations []Duration
Fields []PayloadFields
Ids []Id
}
func (m *jaalModule) EnumType(enumData pgs.Enum, imports map[string]string, initFunctionsName map[string]bool) (string, error) {
// returns generated template in for a enum type
enumval := enum{Name: enumData.Name().UpperCamelCase().String()}
initFunctionsName["Register"+enumval.Name] = true
for _, val := range enumData.Values() {
enumval.Values = append(enumval.Values, Value{Value: val.Name().String(), Index: val.Value()})
}
tmp := getEnumTemplate()
buf := &bytes.Buffer{}
if err := tmp.Execute(buf, enumval); err != nil {
return "", err
}
return buf.String(), nil
}
type Oneof struct {
Name string
SchemaObjectPara string
FieldFuncPara string
FieldFuncSecondParaFuncPara string
TargetName string
TargetVal string
}
func (m *jaalModule) GetSkipOption(message pgs.Message) (bool, error) {
//returns true if a message have skip flag as true or no skip option
//returns false if message have skip flag as false
opt := message.Descriptor().GetOptions()
x, err := proto.GetExtension(opt, pbt.E_Skip)
if opt == nil {
return false, nil
}
if err != nil {
if err == proto.ErrMissingExtension {
return false, nil
}
return false, err
}
return *x.((*bool)), nil
}
func (m *jaalModule) GetMessageTypeOption(message pgs.Message) (bool, string, error) {
//returns message type if present
opt := message.Descriptor().GetOptions()
x, err := proto.GetExtension(opt, pbt.E_Type)
if opt == nil {
return false, "", nil
}
if err != nil {
if err == proto.ErrMissingExtension {
return false, "", nil
}
return false, "", err
}
return true, *x.((*string)), nil
}
func (m *jaalModule) OneofInputType(inputData pgs.Message, imports map[string]string, initFunctionsName map[string]bool) (string, error) {
/*
returns generated template(Input) in for a Oneof type
Primitive,Enum and object type are handled inside a oneof
*/
var oneOfArr []Oneof
for _, oneof := range inputData.OneOfs() {
for _, fields := range oneof.Fields() {
//checks skip_input field option
if fieldSkip, err := m.GetFieldOptionInput(fields); err != nil {
return "", err
} else if fieldSkip {
continue
}
name := fields.Message().Name().UpperCamelCase().String() + "_" + fields.Name().UpperCamelCase().String()
initFunctionsName["RegisterInput"+name] = true
schemaObjectPara := fields.Message().Name().LowerCamelCase().String() + fields.Name().UpperCamelCase().String()
fieldFuncPara := fields.Name().LowerCamelCase().String()
targetName := fields.Name().UpperCamelCase().String()
fieldFuncSecondParaFuncPara := m.RPCFieldType(fields)
if fieldFuncSecondParaFuncPara[0] == '*' {
fieldFuncSecondParaFuncPara = fieldFuncSecondParaFuncPara[1:len(fieldFuncSecondParaFuncPara)]
}
goPkg := ""
targetVal := ""
if fields.Type().IsEnum() {
goPkg = m.GetGoPackageOfFiles(inputData.File(), fields.Type().Enum().File())
if goPkg != "" {
goPkg += "."
}
targetVal = "*source"
} else if fields.Type().IsEmbed() {
goPkg = m.GetGoPackageOfFiles(inputData.File(), fields.Type().Embed().File())
if goPkg != "" {
goPkg += "."
}
targetVal = "source"
} else {
targetVal = "*source"
}
fieldFuncSecondParaFuncPara = goPkg + fieldFuncSecondParaFuncPara
if fieldFuncSecondParaFuncPara == "field_mask.FieldMask" {
targetVal = "gtypes.ModifyFieldMask(source)"
} else if strings.HasSuffix(fieldFuncSecondParaFuncPara, "duration.Duration") {
fieldFuncSecondParaFuncPara = fieldFuncSecondParaFuncPara[:len(fieldFuncSecondParaFuncPara)-17]
fieldFuncSecondParaFuncPara += "schemabuilder.Duration"
targetVal = "(*duration.Duration)(" + targetVal + ")"
} else if strings.HasSuffix(fieldFuncSecondParaFuncPara, "timestamp.Timestamp") {
fieldFuncSecondParaFuncPara = fieldFuncSecondParaFuncPara[:len(fieldFuncSecondParaFuncPara)-19]
fieldFuncSecondParaFuncPara += "schemabuilder.Timestamp"
targetVal = "(*timestamp.Timestamp)(" + targetVal + ")"
} else if strings.HasSuffix(fieldFuncSecondParaFuncPara, "byte") {
fieldFuncSecondParaFuncPara = "schemabuilder.Bytes"
targetVal = "source.Value"
}
oneOfArr = append(oneOfArr, Oneof{TargetVal: targetVal, Name: name, SchemaObjectPara: schemaObjectPara, FieldFuncPara: fieldFuncPara, TargetName: targetName, FieldFuncSecondParaFuncPara: fieldFuncSecondParaFuncPara})
}
}
tmp := getOneofInputTemplate()
buf := &bytes.Buffer{}
if err := tmp.Execute(buf, oneOfArr); err != nil {
return "", err
}
return buf.String(), nil
}
type OneofPayload struct {
Name string
SchemaObjectPara string
FieldFuncPara string
FieldFuncSecondFuncReturn string
FieldFuncReturn string
}
func (m *jaalModule) OneofPayloadType(inputData pgs.Message, imports map[string]string, initFunctionsName map[string]bool) (string, error) {
/*
returns generated template(Payload) in for all oneOf type
Primitive,Enum and object type are handled inside a oneof
*/
var oneOfArr []OneofPayload
for _, oneof := range inputData.OneOfs() {
for _, fields := range oneof.Fields() {
//checks skip_payload field option
if fieldSkip, err := m.GetFieldOptionPayload(fields); err != nil {
return "", err
} else if fieldSkip {
continue
}
name := fields.Message().Name().UpperCamelCase().String() + "_" + fields.Name().UpperCamelCase().String()
initFunctionsName["RegisterPayload"+name] = true
schemaObjectPara := fields.Message().Name().LowerCamelCase().String() + fields.Name().UpperCamelCase().String()
fieldFuncPara := fields.Name().LowerCamelCase().String()
fieldFuncSecondFuncReturn := m.RPCFieldType(fields)
if fieldFuncSecondFuncReturn[0] == '*' {
fieldFuncSecondFuncReturn = fieldFuncSecondFuncReturn[1:len(fieldFuncSecondFuncReturn)]
}
goPkg := ""
if fields.Type().IsEnum() {
goPkg = m.GetGoPackageOfFiles(inputData.File(), fields.Type().Enum().File())
if goPkg != "" {
goPkg += "."
}
} else if fields.Type().IsEmbed() {
goPkg = m.GetGoPackageOfFiles(inputData.File(), fields.Type().Embed().File())
if goPkg != "" {
goPkg = "*" + goPkg
goPkg += "."
} else {
goPkg = "*"
}
}
fieldFuncSecondFuncReturn = goPkg + fieldFuncSecondFuncReturn
fieldFuncReturn := fields.Name().UpperCamelCase().String()
if fieldFuncSecondFuncReturn == "*field_mask.FieldMask" {
fieldFuncReturn = "gtypes.ModifyFieldMask(in." + fieldFuncReturn + ")"
} else if strings.HasSuffix(fieldFuncSecondFuncReturn, "byte") {
fieldFuncSecondFuncReturn = "*schemabuilder.Bytes"
fieldFuncReturn = "&schemabuilder.Bytes{Value:in." + fieldFuncReturn + "}"
} else {
fieldFuncReturn = "in." + fieldFuncReturn
}
oneOfArr = append(oneOfArr, OneofPayload{Name: name, SchemaObjectPara: schemaObjectPara, FieldFuncPara: fieldFuncPara, FieldFuncReturn: fieldFuncReturn, FieldFuncSecondFuncReturn: fieldFuncSecondFuncReturn})
}
}
tmp := getOneofPayloadTemplate()
buf := &bytes.Buffer{}
if err := tmp.Execute(buf, oneOfArr); err != nil {
return "", err
}
return buf.String(), nil
}
func (m *jaalModule) UnionStruct(inputData pgs.Message, imports map[string]string, PossibleReqObjects map[string]bool, initFunctionsName map[string]bool) (string, error) {
//returns generated template(Union Struct) in for all one of type
var unionObjects []UnionObject
for _, oneof := range inputData.OneOfs() {
unionName := "Union"
msgName := oneof.Message().Name().UpperCamelCase().String()
unionName += msgName
unionName += oneof.Name().UpperCamelCase().String()
var unionField []string
for _, fields := range oneof.Fields() {
unionField = append(unionField, "*"+fields.Message().Name().UpperCamelCase().String()+"_"+fields.Name().UpperCamelCase().String())
}
unionObjects = append(unionObjects, UnionObject{UnionName: unionName, UnionFields: unionField})
}
tmp := getUnionStructTemplate()
buf := &bytes.Buffer{}
if err := tmp.Execute(buf, unionObjects); err != nil {
return "", err
}
return buf.String(), nil
}
func (m *jaalModule) GetMessageName(message pgs.Message) (string, error) {
opt := message.Descriptor().GetOptions() // checks Name option
if opt != nil {
x, err := proto.GetExtension(opt, pbt.E_Name)
if err != nil && err != proto.ErrMissingExtension {
return "", err
}
if x != nil {
return *x.(*string), nil
}
}
return "", nil
}
func (m *jaalModule) InputType(inputData pgs.Message, imports map[string]string, PossibleReqObjects map[string]bool, initFunctionsName map[string]bool, typeCastMap map[string]string) (string, error) {
// returns generated template(Input) in for a message type
if skip, err := m.GetSkipOption(inputData); err != nil {
// checks skip flag
return "", err
} else if skip {
return "", nil
}
// handles embedded messages
fullyQualifiedName := inputData.FullyQualifiedName()
embeddedMessageParent := ""
names := strings.Split(fullyQualifiedName, ".")
if inputData.Parent().Name().String() == names[len(names)-2] {
embeddedMessageParent = strings.Join(names[1:len(names)-1], "_") + "_"
}
msg := InputClass{Name: embeddedMessageParent + inputData.Name().UpperCamelCase().String()}
newName, err := m.GetMessageName(inputData)
if err != nil {
return "", err
} else if newName != "" {
msg.InputObjName = newName + "Input"
} else if PossibleReqObjects[inputData.Name().String()] {
msg.InputObjName = m.InputAppend(inputData.Name().UpperCamelCase().String())
} else {
msg.InputObjName = inputData.Name().UpperCamelCase().String() + "Input"
}
initFunctionsName["RegisterInput"+msg.Name] = true
var maps []InputMap
for _, oneof := range inputData.OneOfs() {
unionName := "Union"
msgName := oneof.Message().Name().UpperCamelCase().String()
unionName += msgName
unionName += oneof.Name().UpperCamelCase().String()
for _, fields := range oneof.Fields() {
//checks skip_input field option
if fieldSkip, err := m.GetFieldOptionInput(fields); err != nil {
return "", err
} else if fieldSkip {
continue
}
msg.Fields = append(msg.Fields, MsgFields{TargetName: oneof.Name().UpperCamelCase().String(), FieldName: oneof.Message().Name().LowerCamelCase().String() + fields.Name().UpperCamelCase().String(), FuncPara: "*" + fields.Message().Name().UpperCamelCase().String() + "_" + fields.Name().UpperCamelCase().String(), TargetVal: "source"})
}
}
for _, fields := range inputData.NonOneOfFields() {
//m.Log(fields.Name(),fields.Type().IsEmbed())
//checks skip_input field option
if fieldSkip, err := m.GetFieldOptionInput(fields); err != nil {
return "", err
} else if fieldSkip {
continue
}
overrideFieldName, nameToBeOverridden, err := m.getFieldNameOption(fields)
if err != nil {
m.Log("In Input")
return "", err
}
msgArg := ""
tVal := ""
flag := true
flag2 := true
flag3 := true
fieldName := fields.Name().LowerCamelCase().String()
targetName := fields.Name().UpperCamelCase().String()
if overrideFieldName {
fieldName = nameToBeOverridden
}
if fields.Type().IsRepeated() {
msgArg += "[]"
flag = false
if !fields.Type().Element().IsEmbed() {
flag3 = false
}
}
if flag3 {
msgArg += "*"
}
idOption, err := m.IdOption(fields)
if err != nil {
return "", err
}
if strings.ToLower(fields.Name().String()) == "id" || idOption {
msgArg += "schemabuilder.ID"
tVal += "source.Value"
flag = false
flag2 = false
} else if fields.Type().IsRepeated() {
msgArg = "[]"
tObj := fields.Type().Element()
if tObj.IsEmbed() {
msgArg += "*"
}
if tObj.IsEmbed() {
if tObj.Embed().File().Descriptor().Options != nil && tObj.Embed().File().Descriptor().Options.GoPackage != nil && inputData.Package().ProtoName().String() != tObj.Embed().Package().ProtoName().String() {
msgArg += m.GetGoPackage(tObj.Embed().File())
msgArg += "."
} else {
if strings.Split(tObj.Embed().FullyQualifiedName(), ".")[len(strings.Split(tObj.Embed().FullyQualifiedName(), "."))-2] == tObj.Embed().Parent().Name().String() {
names := strings.Split(fields.FullyQualifiedName(), ".")
tembeddedMessageParent := strings.Join(names[1:len(names)-1], "_") + "_"
msgArg += tembeddedMessageParent
}
}
}
ttype := m.fieldElementType(tObj)
msgArg += ttype
} else if fields.Type().IsMap() {
// TODO : Repeated case not handled
goPkg := ""
if fields.Type().Element().IsEmbed() {
goPkg = m.GetGoPackageOfFiles(inputData.File(), fields.Type().Element().Embed().File())
if goPkg != "" {
goPkg += "."
}
}
asterik := ""
if fields.Type().Element().IsEmbed() {
asterik = "*"
}
value := asterik + goPkg + m.fieldElementType(fields.Type().Element())
maps = append(maps, InputMap{FieldName: fieldName, TargetVal: "*source", TargetName: targetName, Key: m.fieldElementType(fields.Type().Key()), Value: value})
continue
} else if fields.Descriptor().GetType().String() == "TYPE_MESSAGE" {
if fields.Type().IsEmbed() {
goPkg := m.GetGoPackageOfFiles(inputData.File(), fields.Type().Embed().File())
if goPkg != "" {
msgArg += goPkg
msgArg += "."
} else {
if strings.Split(fields.FullyQualifiedName(), ".")[len(strings.Split(fields.FullyQualifiedName(), "."))-2] == fields.Type().Embed().Parent().Name().String() {
names := strings.Split(fields.FullyQualifiedName(), ".")
tembeddedMessageParent := strings.Join(names[1:len(names)-1], "_") + "_"
msgArg += tembeddedMessageParent
}
}
}
tmsg := strings.Split(fields.Descriptor().GetTypeName(), ".")
msgArg += tmsg[len(tmsg)-1]
flag = false
} else if fields.Descriptor().GetType().String() == "TYPE_ENUM" {
goPkg := m.GetGoPackageOfFiles(inputData.File(), fields.Type().Enum().File())
if goPkg != "" {
goPkg += "."
}
flag = false
tmsg := strings.Split(fields.Descriptor().GetTypeName(), ".")
msgArg = goPkg + tmsg[len(tmsg)-1]
} else {
tmsg := strings.Split(fields.Descriptor().GetType().String(), "_")
msgArg = m.scalarMap(tmsg[len(tmsg)-1])
flag = false
}
if flag {
tVal += "*"
}
if flag2 {
tVal += "source"
}
if strings.HasSuffix(msgArg, "timestamp.Timestamp") {
// handles special case of timestamp
//todo repeated case is not handled
msgArg = msgArg[:len(msgArg)-19]
msgArg += "schemabuilder.Timestamp"
tVal = "(*timestamp.Timestamp)(" + tVal + ")"
} else if strings.HasSuffix(msgArg, "duration.Duration") {
if fields.Type().IsRepeated() {
msgArg = msgArg[:len(msgArg)-17]
msgArg += "schemabuilder.Duration"
msg.Durations = append(msg.Durations, Duration{FieldName: fields.Name().LowerCamelCase().String(), Name: fields.Name().UpperCamelCase().String()})
continue
} else {
msgArg = msgArg[:len(msgArg)-17]
msgArg += "schemabuilder.Duration"
tVal = "(*duration.Duration)(" + tVal + ")"
}
} else if strings.HasSuffix(msgArg, "field_mask.FieldMask") {
tVal = "gtypes.ModifyFieldMask(" + tVal + ")"
} else if strings.HasSuffix(msgArg, "byte") {
msgArg = "*schemabuilder.Bytes"
tVal = "source.Value"
} else if msgArg == "[]schemabuilder.ID" {
//handles repeated ids
msg.Ids = append(msg.Ids, Id{FieldName: fields.Name().LowerCamelCase().String(), Name: fields.Name().UpperCamelCase().String()})
continue
}
msg.Fields = append(msg.Fields, MsgFields{TargetName: targetName, FieldName: fieldName, FuncPara: msgArg, TargetVal: tVal})
}
// adds all maps
msg.Maps = maps
buf := &bytes.Buffer{}
tmp := getInputTemplate()
msg.Type = msg.Name
tbuf := &bytes.Buffer{}
if err := tmp.Execute(tbuf, msg); err != nil {
return "", err
} else {
buf.WriteString(tbuf.String())
}
//if message type is set
if ok, val, err := m.GetMessageTypeOption(inputData); err != nil {
return "", err
} else if ok {
typeCastMap[val] = msg.Name
msg.Type = val
msg.Name = msg.Type
msg.InputObjName = val + "Input"
tbuf := &bytes.Buffer{}
if err := tmp.Execute(tbuf, msg); err != nil {
return "", err
} else {
initFunctionsName["RegisterInput"+msg.Name] = true
buf.WriteString(tbuf.String())
}
}
return buf.String(), nil
}
func (m *jaalModule) PayloadType(payloadData pgs.Message, imports map[string]string, initFunctionsName map[string]bool, typeCastMap map[string]string) (string, error) {
// returns generated template(Payload) in for a message type
if skip, err := m.GetSkipOption(payloadData); err != nil {
// checks skip flag
return "", err
} else if skip {
return "", nil
}
fullyQualifiedName := payloadData.FullyQualifiedName()
names := strings.Split(fullyQualifiedName, ".")
embeddedMessageParent := ""
if payloadData.Parent().Name().String() == names[len(names)-2] {
embeddedMessageParent = strings.Join(names[1:len(names)-1], "_") + "_"
}
msg := Payload{Name: embeddedMessageParent + payloadData.Name().UpperCamelCase().String()}
newName, err := m.GetMessageName(payloadData)
if err != nil {
return "", err
} else if newName != "" {
msg.PayloadObjName = newName
} else {
msg.PayloadObjName = payloadData.Name().UpperCamelCase().String()
}
initFunctionsName["RegisterPayload"+msg.Name] = true
var maps []PayloadMap
for _, oneof := range payloadData.OneOfs() {
var oneofFields []OneOfFields
for _, fields := range oneof.Fields() {
//checks skip_payload field option
if fieldSkip, err := m.GetFieldOptionPayload(fields); err != nil {
return "", err
} else if fieldSkip {
continue
}
caseName := fields.Message().Name().UpperCamelCase().String() + "_" + fields.Name().UpperCamelCase().String()
returnType := "&Union" + oneof.Message().Name().UpperCamelCase().String() + oneof.Name().UpperCamelCase().String()
oneofFields = append(oneofFields, OneOfFields{CaseName: caseName, ReturnType: returnType})
}
funcpara := "*Union" + oneof.Message().Name().UpperCamelCase().String() + oneof.Name().UpperCamelCase().String()
fieldName := oneof.Name().LowerCamelCase().String()
switchName := oneof.Name().UpperCamelCase().String()
msg.UnionObjects = append(msg.UnionObjects, UnionObjectPayload{FieldName: fieldName, SwitchName: switchName, FuncReturn: funcpara, Fields: oneofFields})
}
for _, fields := range payloadData.NonOneOfFields() {
//checks skip_payload field option
if fieldSkip, err := m.GetFieldOptionPayload(fields); err != nil {
return "", err
} else if fieldSkip {
continue
}
overrideFieldName, nameToBeOverridden, err := m.getFieldNameOption(fields)
if err != nil {
return "", err
}
msgArg := ""
tVal := ""
fieldName := fields.Name().LowerCamelCase().String()
if overrideFieldName {
fieldName = nameToBeOverridden
}
if fields.Type().IsRepeated() {
msgArg += "[]"
}
idOption, err := m.IdOption(fields)
if err != nil {
return "", err
}
if strings.ToLower(fields.Name().String()) == "id" || idOption {
msgArg += "schemabuilder.ID"
tVal += "schemabuilder."
tVal += strings.ToUpper(fields.Name().String())
tVal += "{Value: in."
tVal += fields.Name().UpperCamelCase().String()
tVal += "}"
} else if fields.Type().IsRepeated() {
msgArg = "[]"
tObj := fields.Type().Element()
if tObj.IsEmbed() {
msgArg += "*"
}
if tObj.IsEmbed() {
if tObj.Embed().File().Descriptor().Options != nil && tObj.Embed().File().Descriptor().Options.GoPackage != nil && payloadData.Package().ProtoName().String() != tObj.Embed().Package().ProtoName().String() {
msgArg += m.GetGoPackage(tObj.Embed().File())
msgArg += "."
} else {
if strings.Split(tObj.Embed().FullyQualifiedName(), ".")[len(strings.Split(tObj.Embed().FullyQualifiedName(), "."))-2] == tObj.Embed().Parent().Name().String() {
names := strings.Split(fields.FullyQualifiedName(), ".")
tembeddedMessageParent := strings.Join(names[1:len(names)-1], "_") + "_"
msgArg += tembeddedMessageParent
}
}
}
ttype := m.fieldElementType(tObj)
msgArg += ttype
tVal += "in." + fields.Name().UpperCamelCase().String()
} else if fields.Type().IsMap() {
//TODO : Repeated case not handled
tVal += "in."
tVal += fields.Name().UpperCamelCase().String()
maps = append(maps, PayloadMap{FieldName: fieldName, TargetVal: tVal})
continue
} else if fields.Descriptor().GetType().String() == "TYPE_MESSAGE" {
msgArg += "*"
if fields.Type().IsEmbed() {
goPkg := m.GetGoPackageOfFiles(payloadData.File(), fields.Type().Embed().File())
if goPkg != "" {
msgArg += goPkg
msgArg += "."
} else {
if strings.Split(fields.FullyQualifiedName(), ".")[len(strings.Split(fields.FullyQualifiedName(), "."))-2] == fields.Type().Embed().Parent().Name().String() {
names := strings.Split(fields.FullyQualifiedName(), ".")
tembeddedMessageParent := strings.Join(names[1:len(names)-1], "_") + "_"
msgArg += tembeddedMessageParent
}
}
}
tTypeArr := strings.Split(fields.Descriptor().GetTypeName(), ".")
msgArg += tTypeArr[len(tTypeArr)-1]
tVal += "in."
tVal += fields.Name().UpperCamelCase().String()
} else if fields.Descriptor().GetType().String() == "TYPE_ENUM" {
goPkg := m.GetGoPackageOfFiles(payloadData.File(), fields.Type().Enum().File())
if goPkg != "" {
goPkg += "."
}
tTypeArr := strings.Split(fields.Descriptor().GetTypeName(), ".")
msgArg = goPkg + tTypeArr[len(tTypeArr)-1]
tVal += "in."
tVal += fields.Name().UpperCamelCase().String()
} else {
goPkg := m.GetGoPackageOfFiles(payloadData.File(), fields.File())
if goPkg != "" {
goPkg += "."
}
tTypeArr := strings.Split(fields.Descriptor().GetType().String(), "_")
msgArg = goPkg + m.scalarMap(tTypeArr[len(tTypeArr)-1])
tVal += "in."
tVal += fields.Name().UpperCamelCase().String()
}
if strings.HasSuffix(msgArg, "timestamp.Timestamp") {
// handles special case of timestamp
//todo repeated case is not handled
msgArg = msgArg[:len(msgArg)-19]
msgArg += "schemabuilder.Timestamp"
tVal = "(*schemabuilder.Timestamp)(" + tVal + ")"
} else if strings.HasSuffix(msgArg, "duration.Duration") {
if fields.Type().IsRepeated() {
msgArg = msgArg[:len(msgArg)-17]
msgArg += "schemabuilder.Duration"
msg.Durations = append(msg.Durations, Duration{FieldName: fields.Name().LowerCamelCase().String(), Name: fields.Name().UpperCamelCase().String()})
continue
} else {
msgArg = msgArg[:len(msgArg)-17]
msgArg += "schemabuilder.Duration"
tVal = "(*schemabuilder.Duration)(" + tVal + ")"
}
} else if strings.HasSuffix(msgArg, "field_mask.FieldMask") {
tVal = "gtypes.ModifyFieldMask(" + tVal + ")"
} else if strings.HasSuffix(msgArg, "byte") {
msgArg = "*" + "schemabuilder.Bytes"
tVal = "&schemabuilder.Bytes{Value:in." + fields.Name().UpperCamelCase().String() + "}"
} else if msgArg == "[]schemabuilder.ID" {
msg.Ids = append(msg.Ids, Id{FieldName: fields.Name().LowerCamelCase().String(), Name: fields.Name().UpperCamelCase().String()})
continue
}
msg.Fields = append(msg.Fields, PayloadFields{FieldName: fieldName, FuncPara: msgArg, TargetVal: tVal})
}
// adds all maps
msg.Maps = maps
buf := &bytes.Buffer{}
tmp := getPayloadTemplate()
msg.Type = msg.Name
tbuf := &bytes.Buffer{}
if err := tmp.Execute(tbuf, msg); err != nil {
return "", err
} else {
buf.WriteString(tbuf.String())
}
//if message type is set
if ok, val, err := m.GetMessageTypeOption(payloadData); err != nil {
return "", err
} else if ok {
typeCastMap[val] = msg.Name
msg.Type = val
msg.Name = msg.Type
msg.PayloadObjName = val
tbuf := &bytes.Buffer{}
if err := tmp.Execute(tbuf, msg); err != nil {
return "", err
} else {
initFunctionsName["RegisterPayload"+msg.Name] = true
buf.WriteString(tbuf.String())
}
}
return buf.String(), nil
}
type MapData struct {
Name string
Key string
Value string
NewVarName string
}
type Fields struct {
Name string
Type string
}
type Query struct {
FieldName string
InType []Fields
InputName string
ReturnType []Fields
FirstReturnArgType string
ReturnFunc string
MapsData []MapData
Oneofs []OneOfMutation
Durations []Duration
Ids []Id
}
type Mutation struct {
FieldName string
InputType string
FirstReturnArgType string
RequestType string
RequestFields []string
ResponseType string
ReturnType string
OneOfs []OneOfMutation
}