-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgen.go
903 lines (844 loc) · 29.1 KB
/
gen.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
package bebop
import (
"bytes"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/200sc/bebop/internal/importgraph"
"github.com/200sc/bebop/iohelp"
)
// GenerateSettings holds customization options for what Generate should do.
type GenerateSettings struct {
// PackageName is optional if the target bebop file defines a go_package
// constant. If both are provided, PackageName will take precedence.
PackageName string
typeByters map[string]string
typeByteReaders map[string]string
typeMarshallers map[string]string
typeUnmarshallers map[string]string
typeLengthers map[string]string
customRecordTypes map[string]struct{}
ImportGenerationMode
imported []File
importTypeAliases map[string]string
nextLength *int
isFirstTopLength *bool
GenerateUnsafeMethods bool
SharedMemoryStrings bool
GenerateFieldTags bool
PrivateDefinitions bool
AlwaysUsePointerReceivers bool
}
type ImportGenerationMode uint8
const (
// ImportGenerationModeSeparate will generate separate go files for
// every bebop file, and will assume that imported files are
// already generated. If imported file types are used and their containing
// files do not contain a go_package constant, this mode will fail.
ImportGenerationModeSeparate ImportGenerationMode = iota
// ImportGenerationModeCombined will generate one go file including
// all definitions from all imports. This does not require go_package
// is defined anywhere, and maintains compatibility with files compilable
// by the original bebopc compiler.
ImportGenerationModeCombined ImportGenerationMode = iota
)
var allImportModes = map[ImportGenerationMode]struct{}{
ImportGenerationModeSeparate: {},
ImportGenerationModeCombined: {},
}
func (gs GenerateSettings) Validate() error {
if _, ok := allImportModes[gs.ImportGenerationMode]; !ok {
return fmt.Errorf("unknown import mode: %d", gs.ImportGenerationMode)
}
return nil
}
// Validate verifies a File can be successfully generated.
func (f File) Validate() error {
allOpCodes := map[uint32]string{}
allConsts := map[string]struct{}{}
for _, c := range f.Consts {
if _, ok := allConsts[c.Name]; ok {
return fmt.Errorf("const has duplicated name %s", c.Name)
}
allConsts[c.Name] = struct{}{}
}
customTypes := map[string]struct{}{}
structTypeUsage := map[string]map[string]bool{}
for _, en := range f.Enums {
if _, ok := primitiveTypes[en.Name]; ok {
return fmt.Errorf("enum shares primitive type name %s", en.Name)
}
if _, ok := customTypes[en.Name]; ok {
return fmt.Errorf("enum has duplicated name %s", en.Name)
}
if en.Namespace != "" {
nameWithoutPrefix := strings.TrimPrefix(en.Name, en.Namespace+".")
customTypes[nameWithoutPrefix] = struct{}{}
}
customTypes[en.Name] = struct{}{}
optionNames := map[string]struct{}{}
optionValues := map[int64]struct{}{}
unsignedOptionValues := map[uint64]struct{}{}
for _, opt := range en.Options {
if _, ok := optionNames[opt.Name]; ok {
return fmt.Errorf("enum %s has duplicate option name %s", en.Name, opt.Name)
}
optionNames[opt.Name] = struct{}{}
if en.Unsigned {
if _, ok := unsignedOptionValues[opt.UintValue]; ok {
return fmt.Errorf("enum %s has duplicate option value %d", en.Name, opt.UintValue)
}
unsignedOptionValues[opt.UintValue] = struct{}{}
} else {
if _, ok := optionValues[opt.Value]; ok {
return fmt.Errorf("enum %s has duplicate option value %d", en.Name, opt.Value)
}
optionValues[opt.Value] = struct{}{}
}
}
}
for _, st := range f.Structs {
if _, ok := primitiveTypes[st.Name]; ok {
return fmt.Errorf("struct shares primitive type name %s", st.Name)
}
if _, ok := customTypes[st.Name]; ok {
return fmt.Errorf("struct has duplicated name %s", st.Name)
}
if st.Namespace != "" {
nameWithoutPrefix := strings.TrimPrefix(st.Name, st.Namespace+".")
customTypes[nameWithoutPrefix] = struct{}{}
structTypeUsage[nameWithoutPrefix] = st.usedTypes()
}
customTypes[st.Name] = struct{}{}
structTypeUsage[st.Name] = st.usedTypes()
stNames := map[string]struct{}{}
for _, fd := range st.Fields {
if _, ok := stNames[fd.Name]; ok {
return fmt.Errorf("struct %s has duplicate field name %s", st.Name, fd.Name)
}
stNames[fd.Name] = struct{}{}
}
if st.OpCode != 0 {
if conflict, ok := allOpCodes[st.OpCode]; ok {
return fmt.Errorf("struct %s has duplicate opcode %02x (duplicated in %s)", st.Name, st.OpCode, conflict)
}
allOpCodes[st.OpCode] = st.Name
}
}
for _, msg := range f.Messages {
if _, ok := primitiveTypes[msg.Name]; ok {
return fmt.Errorf("message shares primitive type name %s", msg.Name)
}
if _, ok := customTypes[msg.Name]; ok {
return fmt.Errorf("message has duplicated name %s", msg.Name)
}
if msg.Namespace != "" {
nameWithoutPrefix := strings.TrimPrefix(msg.Name, msg.Namespace+".")
customTypes[nameWithoutPrefix] = struct{}{}
}
customTypes[msg.Name] = struct{}{}
msgNames := map[string]struct{}{}
for _, fd := range msg.Fields {
if _, ok := msgNames[fd.Name]; ok {
return fmt.Errorf("message %s has duplicate field name %s", msg.Name, fd.Name)
}
msgNames[fd.Name] = struct{}{}
}
if msg.OpCode != 0 {
if conflict, ok := allOpCodes[msg.OpCode]; ok {
return fmt.Errorf("message %s has duplicate opcode %02x (duplicated in %s)", msg.Name, msg.OpCode, conflict)
}
allOpCodes[msg.OpCode] = msg.Name
}
}
for _, un := range f.Unions {
if _, ok := primitiveTypes[un.Name]; ok {
return fmt.Errorf("union shares primitive type name %s", un.Name)
}
if _, ok := customTypes[un.Name]; ok {
return fmt.Errorf("union has duplicated name %s", un.Name)
}
if un.Namespace != "" {
nameWithoutPrefix := strings.TrimPrefix(un.Name, un.Namespace+".")
customTypes[nameWithoutPrefix] = struct{}{}
}
customTypes[un.Name] = struct{}{}
unionNames := map[string]struct{}{}
for _, fd := range un.Fields {
if _, ok := unionNames[fd.name()]; ok {
return fmt.Errorf("union %s has duplicate field name %s", un.Name, fd.name())
}
unionNames[fd.name()] = struct{}{}
}
if un.OpCode != 0 {
if conflict, ok := allOpCodes[un.OpCode]; ok {
return fmt.Errorf("union %s has duplicate opcode %02x (duplicated in %s)", un.Name, un.OpCode, conflict)
}
allOpCodes[un.OpCode] = un.Name
}
}
allTypes := customTypes
for typ := range primitiveTypes {
allTypes[typ] = struct{}{}
}
for _, st := range f.Structs {
for _, fd := range st.Fields {
if err := typeDefined(fd.FieldType, allTypes); err != nil {
return err
}
}
}
for _, msg := range f.Messages {
for _, fd := range msg.Fields {
if err := typeDefined(fd.FieldType, allTypes); err != nil {
return err
}
}
}
// Todo: within a given struct, enum, or message, a field / option cannot
// have a duplicate name
// Determine which structs include themselves as required fields (which would lead
// to the struct taking up infinite size)
delta := true
// This is an unbounded loop because for this case:
// fooStruct:{barStruct}, barStruct{bizzStruct}, bizzStruct{bazStruct}, bazStruct{fooStruct}
// After each iteration we have these updated usages:
// 1. fooStruct:{barStruct, bizzStruct}, barStruct{bizzStruct, bazStruct}, bizzStruct{bazStruct, fooStruct}, bazStruct{fooStruct, barStruct}
// 2. fooStruct:{barStruct, bizzStruct, bazStruct, fooStruct}, barStruct{bizzStruct, bazStruct, fooStruct, barStruct}, bizzStruct{bazStruct, fooStruct, barStruct, bizzStruct}, bazStruct{fooStruct, barStruct, bizzStruct, bazStruct}
// ... and as the chain of structs gets longer the required iterations also increases.
for delta {
delta = false
for stName, usage := range structTypeUsage {
for stName2, usage2 := range structTypeUsage {
if stName == stName2 {
continue
}
// If struct1 includes struct2, it also includes
// all fields that struct2 includes
if usage[stName2] {
for k, v := range usage2 {
if !usage[k] {
delta = true
}
usage[k] = v
}
}
}
structTypeUsage[stName] = usage
}
}
for stName, usage := range structTypeUsage {
if usage[stName] {
return fmt.Errorf("struct %s recursively includes itself as a required field", stName)
}
}
return nil
}
func typeDefined(ft FieldType, allTypes map[string]struct{}) error {
if ft.Array != nil {
return typeDefined(*ft.Array, allTypes)
}
if ft.Map != nil {
if _, ok := allTypes[ft.Map.Key]; !ok {
return fmt.Errorf("map key type %s undefined", ft.Map.Key)
}
return typeDefined(ft.Map.Value, allTypes)
}
if _, ok := allTypes[ft.Simple]; !ok {
return fmt.Errorf("type %s undefined", ft.Simple)
}
return nil
}
// Generate writes a .go file out to w. If f has imports, it will
// parse the files at those imports and generate them according to
// settings.
func (f File) Generate(inputWriter io.Writer, settings GenerateSettings) error {
w := iohelp.NewErrorWriter(inputWriter)
if err := settings.Validate(); err != nil {
return fmt.Errorf("invalid generation settings: %w", err)
}
settings.nextLength = new(int)
settings.isFirstTopLength = new(bool)
if len(f.Imports) != 0 {
thisFilePath := f.FileName
if !path.IsAbs(f.FileName) {
// assume relative to our current directory
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("cannot determine working directory for import lookup: %v", err)
}
thisFilePath = filepath.Join(wd, thisFilePath)
}
thisDir := filepath.Dir(thisFilePath)
type bebopImport struct {
from string
to string
}
imports := make([]bebopImport, len(f.Imports))
importGraph := importgraph.NewDgraph()
for i, imp := range f.Imports {
imports[i] = bebopImport{
from: f.GoPackage,
to: imp,
}
}
// TODO: why are imports not scoped to a namespace?
imported := map[string]struct{}{}
for i := 0; i < len(imports); i++ {
imp := imports[i]
impPath := filepath.Join(thisDir, imp.to)
impF, err := os.Open(impPath)
if err != nil {
return fmt.Errorf("failed to open imported file %s: %w", imp.to, err)
}
impFile, _, err := ReadFile(impF)
if err != nil {
impF.Close()
return fmt.Errorf("failed to parse imported file %s: %w", imp.to, err)
}
impF.Close()
importGraph.AddEdge(imp.from, impFile.GoPackage)
if _, ok := imported[impPath]; ok {
continue
}
settings.imported = append(settings.imported, impFile)
for _, subImp := range impFile.Imports {
imports = append(imports, bebopImport{
from: impFile.GoPackage,
to: subImp,
})
}
imported[impPath] = struct{}{}
}
if settings.ImportGenerationMode == ImportGenerationModeSeparate {
if err := importGraph.FindCycle(); err != nil {
return err
}
}
}
imports := []string{}
potentialImports := []string{}
settings.importTypeAliases = make(map[string]string)
switch settings.ImportGenerationMode {
case ImportGenerationModeSeparate:
for _, imp := range settings.imported {
if imp.GoPackage == "" {
return fmt.Errorf("cannot import %s: file has no %s const", filepath.Base(imp.FileName), goPackage)
}
packageName := imp.GoPackage
packageNamespace := path.Base(packageName)
for _, st := range imp.Structs {
st.Namespace = packageNamespace
namespacedName := packageNamespace + "." + st.Name
settings.importTypeAliases[st.Name] = namespacedName
st.Name = namespacedName
f.Structs = append(f.Structs, st)
}
for _, un := range imp.Unions {
un.Namespace = packageNamespace
namespacedName := packageNamespace + "." + un.Name
settings.importTypeAliases[un.Name] = namespacedName
un.Name = namespacedName
f.Unions = append(f.Unions, un)
}
for _, msg := range imp.Messages {
msg.Namespace = packageNamespace
namespacedName := packageNamespace + "." + msg.Name
settings.importTypeAliases[msg.Name] = namespacedName
msg.Name = namespacedName
f.Messages = append(f.Messages, msg)
}
for _, enm := range imp.Enums {
enm.Namespace = packageNamespace
namespacedName := packageNamespace + "." + enm.Name
settings.importTypeAliases[enm.Name] = namespacedName
enm.Name = namespacedName
f.Enums = append(f.Enums, enm)
}
potentialImports = append(potentialImports, packageName)
}
case ImportGenerationModeCombined:
// treat all imported files as a part of this file. Do not observe GoPackage.
for _, imp := range settings.imported {
f.Consts = append(f.Consts, imp.Consts...)
f.Structs = append(f.Structs, imp.Structs...)
f.Unions = append(f.Unions, imp.Unions...)
f.Messages = append(f.Messages, imp.Messages...)
f.Enums = append(f.Enums, imp.Enums...)
}
}
if err := f.Validate(); err != nil {
return fmt.Errorf("cannot generate file: %w", err)
}
settings.typeMarshallers = f.typeMarshallers()
settings.typeByters = f.typeByters()
settings.typeByteReaders = f.typeByteReaders(settings)
settings.typeUnmarshallers = f.typeUnmarshallers(settings)
settings.typeLengthers = f.typeLengthers()
settings.customRecordTypes = f.customRecordTypes()
usedTypes := f.usedTypes()
if settings.PackageName == "" && f.GoPackage != "" {
settings.PackageName = path.Base(f.GoPackage)
} else if settings.PackageName == "" {
return fmt.Errorf("no package name is defined, provide a %s const or an explicit package name setting", goPackage)
}
for _, imp := range potentialImports {
for typ := range usedTypes {
if alias, ok := settings.importTypeAliases[typ]; ok {
if strings.HasPrefix(alias, path.Base(imp)+".") {
imports = append(imports, imp)
break
}
}
}
}
writeLine(w, "// Code generated by bebopc-go; DO NOT EDIT.")
writeLine(w, "")
writeLine(w, "package %s", settings.PackageName)
writeLine(w, "")
if len(f.Messages)+len(f.Structs)+len(f.Unions) != 0 {
imports = append(imports, "github.com/200sc/bebop")
imports = append(imports, "github.com/200sc/bebop/iohelp")
}
if len(f.Messages)+len(f.Structs)+len(f.Unions) != 0 {
imports = append(imports, "io")
}
for _, c := range f.Consts {
if c.impossibleGoConst() {
imports = append(imports, "math")
break
}
}
if usedTypes[typeDate] {
imports = append(imports, "time")
}
if len(imports) != 0 {
writeLine(w, "import (")
for _, i := range imports {
writeLine(w, "\t%q", i)
}
writeLine(w, ")")
writeLine(w, "")
}
impossibleGoConsts := []Const{}
if len(f.Consts) != 0 {
writeLine(w, "const (")
for _, con := range f.Consts {
if con.impossibleGoConst() {
impossibleGoConsts = append(impossibleGoConsts, con)
} else {
con.Generate(w, settings)
}
}
writeLine(w, ")")
writeLine(w, "")
}
if len(impossibleGoConsts) != 0 {
writeLine(w, "var (")
for _, con := range impossibleGoConsts {
con.Generate(w, settings)
}
writeLine(w, ")")
writeLine(w, "")
}
// Namespaced types are imported from another package, and must not be generated.
// They must, however, be defined up til this point so we know how to create them
// as components of records in this package.
for _, en := range f.Enums {
if en.Namespace != "" {
continue
}
en.Generate(w, settings)
}
for _, st := range f.Structs {
if st.Namespace != "" {
continue
}
st.Generate(w, settings)
}
for _, msg := range f.Messages {
if msg.Namespace != "" {
continue
}
msg.Generate(w, settings)
}
for _, union := range f.Unions {
if union.Namespace != "" {
continue
}
union.Generate(w, settings)
}
return w.Err
}
// Generate writes a .go enum definition out to w.
func (en Enum) Generate(w *iohelp.ErrorWriter, settings GenerateSettings) {
exposedName := exposeName(en.Name, settings)
writeComment(w, 0, en.Comment, settings)
writeLine(w, "type %s %s", exposedName, en.SimpleType)
writeLine(w, "")
if len(en.Options) != 0 {
writeLine(w, "const (")
for _, opt := range en.Options {
writeComment(w, 1, opt.Comment, settings)
if opt.Deprecated {
writeLine(w, "\t// Deprecated: %s", opt.DeprecatedMessage)
}
if en.Unsigned {
writeLine(w, "\t%s_%s %s = %d", exposedName, opt.Name, exposedName, opt.UintValue)
} else {
writeLine(w, "\t%s_%s %s = %d", exposedName, opt.Name, exposedName, opt.Value)
}
}
writeLine(w, ")")
writeLine(w, "")
}
}
func (con Const) impossibleGoConst() bool {
// unique floating point values (inf, nan) cannot be represented as consts in go,
// at least not intuitively. This probably doesn't matter-- its rare to
// rely on inf or nan floating points anyway, and even if you could get these as
// consts you would need to use math.IsInf or math.IsNaN for many use cases.
if con.SimpleType == typeFloat32 || con.SimpleType == typeFloat64 {
switch con.Value {
case "math.Inf(-1)":
return true
case "math.Inf(1)":
return true
case "math.NaN()":
return true
}
}
return false
}
func (con Const) Generate(w io.Writer, settings GenerateSettings) {
ew := iohelp.NewErrorWriter(w)
writeComment(ew, 0, con.Comment, settings)
writeLine(ew, "\t%s = %v", exposeName(con.Name, settings), con.Value)
}
func writeFieldDefinition(fd Field, w *iohelp.ErrorWriter, readOnly bool, message bool, settings GenerateSettings) {
writeComment(w, 1, fd.Comment, settings)
if fd.Deprecated {
writeLine(w, "\t// Deprecated: %s", fd.DeprecatedMessage)
}
name := exposeName(fd.Name, settings)
if readOnly {
name = unexposeName(fd.Name)
}
typ := fd.FieldType.goString(settings)
if message {
typ = "*" + typ
}
if settings.GenerateFieldTags && len(fd.Tags) != 0 {
formattedTags := []string{}
for _, tag := range fd.Tags {
if tag.Boolean {
formattedTags = append(formattedTags, tag.Key)
} else {
formattedTags = append(formattedTags, fmt.Sprintf("%s:%q", tag.Key, tag.Value))
}
}
writeLine(w, "\t%s %s `%s`", name, typ, strings.Join(formattedTags, " "))
} else {
writeLine(w, "\t%s %s", name, typ)
}
}
func depthName(name string, depth int) string {
return name + strconv.Itoa(depth)
}
func lengthName(settings GenerateSettings) string {
(*settings.nextLength)++
return "ln" + strconv.Itoa(*settings.nextLength)
}
func writeFieldByter(name string, typ FieldType, w io.Writer, settings GenerateSettings, depth int) {
if typ.Array != nil {
writeLineWithTabs(w, "iohelp.WriteUint32Bytes(buf[at:], uint32(len(%ASGN)))", depth, name)
writeLineWithTabs(w, "at += 4", depth)
if typ.Array.Simple == typeByte || typ.Array.Simple == typeUint8 {
writeLineWithTabs(w, "copy(buf[at:at+len(%ASGN)], %ASGN)", depth, name)
writeLineWithTabs(w, "at += len(%ASGN)", depth, name)
return
}
writeLineWithTabs(w, "for _, %VNAME := range %ASGN {", depth, name)
writeFieldByter(depthName("v", depth), *typ.Array, w, settings, depth+1)
writeLineWithTabs(w, "}", depth)
} else if typ.Map != nil {
writeLineWithTabs(w, "iohelp.WriteUint32Bytes(buf[at:], uint32(len(%ASGN)))", depth, name)
writeLineWithTabs(w, "at += 4", depth)
writeLineWithTabs(w, "for %KNAME, %VNAME := range %ASGN {", depth, name)
writeLineWithTabs(w, settings.typeByters[typ.Map.Key], depth+1, depthName("k", depth))
writeFieldByter(depthName("v", depth), typ.Map.Value, w, settings, depth+1)
writeLineWithTabs(w, "}", depth)
} else {
simpleTyp := typ.Simple
if alias, ok := settings.importTypeAliases[simpleTyp]; ok {
simpleTyp = alias
}
writeLineWithTabs(w, settings.typeByters[simpleTyp], depth, name, typ.goString(settings))
}
}
func writeLengthCheck(w io.Writer, ln string, depth int, args ...string) {
writeLineWithTabs(w, "if len(buf[at:]) < "+ln+" {", depth, args...)
writeLineWithTabs(w, "\treturn io.ErrUnexpectedEOF", depth, args...)
writeLineWithTabs(w, "}", depth, args...)
}
func writeFieldReadByter(name string, typ FieldType, w *iohelp.ErrorWriter, settings GenerateSettings, depth int, safe bool) {
if typ.Array != nil {
if safe {
writeLengthCheck(w, "4", depth)
}
writeLineWithTabs(w, "%ASGN = make([]%TYPE, iohelp.ReadUint32Bytes(buf[at:]))", depth, name, typ.Array.goString(settings))
writeLineWithTabs(w, "at += 4", depth)
if safe {
if sz, ok := fixedSizeTypes[typ.Array.Simple]; ok {
writeLengthCheck(w, "len(%ASGN)*"+strconv.Itoa(int(sz)), depth, name)
safe = false
}
}
if typ.Array.Simple == typeByte || typ.Array.Simple == typeUint8 {
writeLineWithTabs(w, "copy(%ASGN, buf[at:at+len(%ASGN)])", depth, name)
writeLineWithTabs(w, "at += len(%ASGN)", depth, name)
return
}
iName := depthName("i", depth)
writeLineWithTabs(w, "for "+iName+" := range %ASGN {", depth, name)
writeFieldReadByter("("+name+")["+iName+"]", *typ.Array, w, settings, depth+1, safe)
writeLineWithTabs(w, "}", depth)
} else if typ.Map != nil {
lnName := lengthName(settings)
writeLineWithTabs(w, lnName+" := iohelp.ReadUint32Bytes(buf[at:])", depth)
writeLineWithTabs(w, "at += 4", depth)
writeLineWithTabs(w, "%ASGN = make(%TYPE,"+lnName+")", depth, name, typ.Map.goString(settings))
writeLineWithTabs(w, "for i := uint32(0); i < "+lnName+"; i++ {", depth, name)
var ln string
if format, ok := settings.typeByteReaders[typ.Map.Key+hintSafeKey]; ok && safe {
ln = getLineWithTabs(format, depth+1, depthName("k", depth), simpleGoString(typ.Map.Key, settings))
} else {
if sz, ok := fixedSizeTypes[typ.Map.Key]; ok && safe {
writeLengthCheck(w, strconv.Itoa(int(sz)), depth+1, depthName("k", depth))
}
ln = getLineWithTabs(settings.typeByteReaders[typ.Map.Key], depth+1, depthName("k", depth), typ.goString(settings))
}
w.SafeWrite([]byte(strings.Replace(ln, "=", ":=", 1)))
writeFieldReadByter("("+name+")["+depthName("k", depth)+"]", typ.Map.Value, w, settings, depth+1, safe)
writeLineWithTabs(w, "}", depth)
} else {
simpleTyp := typ.Simple
if alias, ok := settings.importTypeAliases[simpleTyp]; ok {
simpleTyp = alias
}
if format, ok := settings.typeByteReaders[simpleTyp+hintSafeKey]; ok && safe {
writeLineWithTabs(w, format, depth, name, typ.goString(settings))
} else {
if sz, ok := fixedSizeTypes[simpleTyp]; ok && safe {
writeLengthCheck(w, strconv.Itoa(int(sz)), depth, name)
}
writeLineWithTabs(w, settings.typeByteReaders[simpleTyp], depth, name, typ.goString(settings))
}
}
}
func writeFieldMarshaller(name string, typ FieldType, w io.Writer, settings GenerateSettings, depth int) {
if typ.Array != nil {
writeLineWithTabs(w, "iohelp.WriteUint32(w, uint32(len(%ASGN)))", depth, name)
if typ.Array.Simple == typeByte {
writeLineWithTabs(w, "w.Write(%ASGN)", depth, name)
} else {
writeLineWithTabs(w, "for _, elem := range %ASGN {", depth, name)
writeFieldMarshaller("elem", *typ.Array, w, settings, depth+1)
writeLineWithTabs(w, "}", depth)
}
} else if typ.Map != nil {
writeLineWithTabs(w, "iohelp.WriteUint32(w, uint32(len(%ASGN)))", depth, name)
writeLineWithTabs(w, "for %KNAME, %VNAME := range %ASGN {", depth, name)
writeLineWithTabs(w, settings.typeMarshallers[typ.Map.Key], depth+1, depthName("k", depth))
writeFieldMarshaller(depthName("v", depth), typ.Map.Value, w, settings, depth+1)
writeLineWithTabs(w, "}", depth)
} else {
simpleTyp := typ.Simple
if alias, ok := settings.importTypeAliases[simpleTyp]; ok {
simpleTyp = alias
}
writeLineWithTabs(w, settings.typeMarshallers[simpleTyp], depth, name, typ.goString(settings))
}
}
func typeNeedsElem(typ string, settings GenerateSettings) bool {
switch typ {
case "":
return true
case typeString:
return true
}
if _, ok := primitiveTypes[typ]; ok {
return false
}
_, ok := settings.customRecordTypes[typ]
return ok
}
func writeFieldBodyCount(name string, typ FieldType, w io.Writer, settings GenerateSettings, depth int) {
if typ.Array != nil {
writeLineWithTabs(w, "bodyLen += 4", depth)
if sz, ok := fixedSizeTypes[typ.Array.Simple]; ok {
// short circuit-- write length times elem size
writeLineWithTabs(w, "bodyLen += len(%ASGN) * "+strconv.Itoa(int(sz)), depth, name)
return
}
writeLineWithTabs(w, "for _, elem := range %ASGN {", depth, name)
writeFieldBodyCount("elem", *typ.Array, w, settings, depth+1)
writeLineWithTabs(w, "}", depth)
} else if typ.Map != nil {
writeLineWithTabs(w, "bodyLen += 4", depth, name)
useV := typeNeedsElem(typ.Map.Value.Simple, settings)
useK := typ.Map.Key == typeString
if useV && useK {
writeLineWithTabs(w, "for %KNAME, %VNAME := range %ASGN {", depth, name)
} else if useV {
writeLineWithTabs(w, "for _, %VNAME := range %ASGN {", depth, name)
} else if useK {
writeLineWithTabs(w, "for %KNAME := range %ASGN {", depth, name)
} else {
writeLineWithTabs(w, "for range %ASGN {", depth, name)
}
writeLineWithTabs(w, settings.typeLengthers[typ.Map.Key], depth+1, depthName("k", depth))
writeFieldBodyCount(depthName("v", depth), typ.Map.Value, w, settings, depth+1)
writeLineWithTabs(w, "}", depth)
} else {
simpleTyp := typ.Simple
if alias, ok := settings.importTypeAliases[simpleTyp]; ok {
simpleTyp = alias
}
writeLineWithTabs(w, settings.typeLengthers[simpleTyp], depth, name, typ.goString(settings))
}
}
func exposeName(name string, settings GenerateSettings) string {
if settings.PrivateDefinitions {
return unexposeName(name)
}
if name == "" {
return ""
}
return strings.ToUpper(string(name[0])) + name[1:]
}
func unexposeName(name string) string {
if name == "" {
return ""
}
return strings.ToLower(string(name[0])) + name[1:]
}
func writeWrappers(w *iohelp.ErrorWriter, name string, isEmpty bool, settings GenerateSettings) {
writeMarshalBebop(w, name, isEmpty, settings)
writeMake(w, name, isEmpty, settings)
writeMakeFromBytes(w, name, isEmpty, settings)
if settings.GenerateUnsafeMethods {
writeMustMakeFromBytes(w, name, isEmpty, settings)
}
}
func writeMarshalBebop(w *iohelp.ErrorWriter, name string, isEmpty bool, settings GenerateSettings) {
exposedName := exposeName(name, settings)
if settings.AlwaysUsePointerReceivers {
writeLine(w, "func (bbp *%s) MarshalBebop() []byte {", exposedName)
} else {
writeLine(w, "func (bbp %s) MarshalBebop() []byte {", exposedName)
}
if isEmpty {
writeLine(w, "\treturn []byte{}")
} else {
writeLine(w, "\tbuf := make([]byte, bbp.Size())")
writeLine(w, "\tbbp.MarshalBebopTo(buf)")
writeLine(w, "\treturn buf")
}
writeCloseBlock(w)
}
func writeMake(w *iohelp.ErrorWriter, name string, isEmpty bool, settings GenerateSettings) {
exposedName := exposeName(name, settings)
makeName := exposeName("Make", settings)
writeLine(w, "func %[2]s%[1]s(r *iohelp.ErrorReader) (%[1]s, error) {", exposedName, makeName)
if isEmpty {
writeLine(w, "\treturn %s{}, nil", exposedName)
} else {
writeLine(w, "\tv := %s{}", exposedName)
writeLine(w, "\terr := v.DecodeBebop(r)")
writeLine(w, "\treturn v, err")
}
writeCloseBlock(w)
}
func writeMakeFromBytes(w *iohelp.ErrorWriter, name string, isEmpty bool, settings GenerateSettings) {
exposedName := exposeName(name, settings)
makeName := exposeName("Make", settings)
writeLine(w, "func %[2]s%[1]sFromBytes(buf []byte) (%[1]s, error) {", exposedName, makeName)
if isEmpty {
writeLine(w, "\treturn %s{}, nil", exposedName)
} else {
writeLine(w, "\tv := %s{}", exposedName)
writeLine(w, "\terr := v.UnmarshalBebop(buf)")
writeLine(w, "\treturn v, err")
}
writeCloseBlock(w)
}
func writeMustMakeFromBytes(w *iohelp.ErrorWriter, name string, isEmpty bool, settings GenerateSettings) {
exposedName := exposeName(name, settings)
makeName := exposeName("MustMake", settings)
writeLine(w, "func %[2]s%[1]sFromBytes(buf []byte) %[1]s {", exposedName, makeName)
if isEmpty {
writeLine(w, "\treturn %s{}", exposedName)
} else {
writeLine(w, "\tv := %s{}", exposedName)
writeLine(w, "\tv.MustUnmarshalBebop(buf)")
writeLine(w, "\treturn v")
}
writeCloseBlock(w)
}
func writeLine(w *iohelp.ErrorWriter, format string, args ...interface{}) {
fmt.Fprintf(w, format+"\n", args...)
}
func getLineWithTabs(format string, depth int, args ...string) string {
b := new(bytes.Buffer)
writeLineWithTabs(b, format, depth, args...)
return b.String()
}
func writeComment(w *iohelp.ErrorWriter, depth int, comment string, settings GenerateSettings) {
if comment == "" {
return
}
tbs := strings.Repeat("\t", depth)
commentLines := strings.Split(comment, "\n")
for _, cm := range commentLines {
// If you have tag comments and are generating them as tags,
// you probably don't want them showing up in your code as comments too.
if settings.GenerateFieldTags {
if _, ok := parseCommentTag(cm); ok {
continue
}
}
writeLine(w, tbs+"//%s", cm)
}
}
func writeCloseBlock(w *iohelp.ErrorWriter) {
writeLine(w, "}")
writeLine(w, "")
}
func writeOpCode(w *iohelp.ErrorWriter, name string, opCode uint32, settings GenerateSettings) {
if opCode != 0 {
writeLine(w, "const %sOpCode = 0x%x", exposeName(name, settings), opCode)
writeLine(w, "")
}
}
func writeRecordAssertion(w *iohelp.ErrorWriter, name string, settings GenerateSettings) {
writeLine(w, "var _ bebop.Record = &%s{}", exposeName(name, settings))
writeLine(w, "")
}
func writeGoStructDef(w *iohelp.ErrorWriter, name string, settings GenerateSettings) {
writeLine(w, "type %s struct {", exposeName(name, settings))
}
func writeRecordTypeDefinition(w *iohelp.ErrorWriter, name string, opCode uint32, comment string, settings GenerateSettings, fields []fieldWithNumber) {
writeOpCode(w, name, opCode, settings)
writeRecordAssertion(w, name, settings)
writeComment(w, 0, comment, settings)
writeGoStructDef(w, name, settings)
for _, fd := range fields {
writeFieldDefinition(fd.Field, w, false, true, settings)
}
writeCloseBlock(w)
}