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
|
// Command pdefgen generates a Go package to process r2 pdata from a pdef.
package main
import (
"bytes"
"fmt"
"go/format"
"os"
"path/filepath"
"runtime/debug"
"sort"
"strconv"
"strings"
"unicode"
"github.com/pg9182/atlas/pkg/pdef"
)
var self string
func init() {
if bi, ok := debug.ReadBuildInfo(); !ok || !strings.Contains(bi.Path, "/") {
panic("failed to get own package name")
} else {
self = bi.Path
}
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "usage: go run %s pdef_version_number\n", self)
os.Exit(2)
}
pdefVersion, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "error: invalid pdef version: %v\n", err)
os.Exit(1)
}
f, err := openSingleFile("*.pdef")
if err != nil {
fmt.Fprintf(os.Stderr, "error: find pdef: %v\n", err)
os.Exit(1)
}
pd, err := pdef.ParsePdef(f)
if err != nil {
fmt.Fprintf(os.Stderr, "error: parse pdef: %v\n", err)
os.Exit(1)
}
var buf bytes.Buffer
pln(&buf, `// Code generated by pdefgen; DO NOT EDIT.`)
pln(&buf, ``)
pln(&buf, `// Package pdata parses Titanfall 2 player data using the schema defined in %s.`, f.Name())
pln(&buf, `//`)
pln(&buf, `// Roundtrip marshal/unmarshal should be byte-identical except for extra data after`)
pln(&buf, `// the null terminator in strings or non-0/1 boolean values. Invalid enum values`)
pln(&buf, `// and trailing data after the pdata root struct are preserved.`)
pln(&buf, `package pdata`)
pln(&buf, ``)
pln(&buf, `//go:generate go run %s %#v`, self, pdefVersion)
pln(&buf, ``)
pln(&buf, `import (`)
pln(&buf, `"bytes"`)
pln(&buf, `_ "embed"`)
pln(&buf, `"encoding"`)
pln(&buf, `"encoding/binary"`)
pln(&buf, `"encoding/json"`)
pln(&buf, `"errors"`)
pln(&buf, `"fmt"`)
pln(&buf, `"io"`)
pln(&buf, `"math"`)
pln(&buf, `"reflect"`)
pln(&buf, `"strconv"`)
pln(&buf, `"strings"`)
pln(&buf, `)`)
pln(&buf, ``)
pln(&buf, `const Version int32 = %#v`, pdefVersion)
pln(&buf, ``)
pln(&buf, `//go:embed %+q`, f.Name())
pln(&buf, `var rawPdef []byte`)
pln(&buf, `func RawPdef() io.Reader { return bytes.NewReader(rawPdef) }`)
pln(&buf, ``)
pln(&buf, `var (`)
pln(&buf, `ErrUnsupportedVersion = errors.New(%#v)`, `unsupported pdata version`)
pln(&buf, `ErrInvalidSize = errors.New(%#v)`, `invalid size`)
pln(&buf, `ErrInvalidEnumValue = errors.New(%#v) // only used when encoding/decoding text/json (binary encode/decode will still preserve unknown values)`, `invalid enum value`)
pln(&buf, `)`)
pln(&buf, `func getString(b []byte) string {`)
pln(&buf, `for i, x := range b {`)
pln(&buf, `if x == '\x00' {`)
pln(&buf, `return string(b[:i])`)
pln(&buf, `}`)
pln(&buf, `}`)
pln(&buf, `return string(b)`)
pln(&buf, `}`)
pln(&buf, `func getInt(b []byte) int32 {`)
pln(&buf, `return int32(binary.LittleEndian.Uint32(b))`)
pln(&buf, `}`)
pln(&buf, `func getFloat(b []byte) float32 {`)
pln(&buf, `return math.Float32frombits(binary.LittleEndian.Uint32(b))`)
pln(&buf, `}`)
pln(&buf, `func getBool(b []byte) bool {`)
pln(&buf, `return b[0] != 0`)
pln(&buf, `}`)
pln(&buf, `func getEnum(b []byte) uint8 {`)
pln(&buf, `return b[0]`)
pln(&buf, `}`)
pln(&buf, `func must(err error) {`)
pln(&buf, `if err != nil {`)
pln(&buf, `panic(err)`)
pln(&buf, `}`)
pln(&buf, `}`)
pln(&buf, `func putString(b []byte, x string) error {`)
pln(&buf, `s := []byte(x)`)
pln(&buf, `if len(s) > len(b) {`)
pln(&buf, `return fmt.Errorf(%#v, len(s), len(b))`, `string length %d too long for field length %d`)
pln(&buf, `}`)
pln(&buf, `for i, c := range s {`)
pln(&buf, `b[i] = c`)
pln(&buf, `}`)
pln(&buf, `for i := len(s); i < len(b); i++ {`)
pln(&buf, `b[i] = '\x00'`)
pln(&buf, `}`)
pln(&buf, `return nil`)
pln(&buf, `}`)
pln(&buf, `func putInt(b []byte, x int32) {`)
pln(&buf, `binary.LittleEndian.PutUint32(b, uint32(x))`)
pln(&buf, `}`)
pln(&buf, `func putFloat(b []byte, x float32) {`)
pln(&buf, `binary.LittleEndian.PutUint32(b, math.Float32bits(x))`)
pln(&buf, `}`)
pln(&buf, `func putBool(b []byte, x bool) {`)
pln(&buf, `if x {`)
pln(&buf, `b[0] = 1`)
pln(&buf, `} else {`)
pln(&buf, `b[0] = 0`)
pln(&buf, `}`)
pln(&buf, `}`)
pln(&buf, `func putEnum(b []byte, x uint8) {`)
pln(&buf, `b[0] = x`)
pln(&buf, `}`)
generateStruct(&buf, "pdata", pd, pd.Root, true)
each(pd.Struct, func(k string, v []pdef.Field) {
generateStruct(&buf, k, pd, v, false)
})
each(pd.Enum, func(k string, v []string) {
generateEnum(&buf, k, v)
})
// this is needed since the default Go marshaler can't filter and doesn't
// handle NaN/Inf floats.
pln(&buf, "%s", `
func pdataMarshalJSONStruct(obj any, filter func(path ...string) bool, path ...string) ([]byte, error) {
objVal := reflect.ValueOf(obj)
objTyp := objVal.Type()
if objTyp.Kind() != reflect.Struct {
panic("not a struct")
}
var b bytes.Buffer
b.WriteByte('{')
needComma := false
for i := 0; i < objTyp.NumField(); i++ {
fldTyp := objTyp.Field(i)
fldVal := objVal.Field(i)
fld := fldVal.Interface()
fldTag := fldTyp.Tag.Get("pdef")
fldTagName, fldTagAttr, _ := strings.Cut(fldTag, ",")
if fldTagName == "" {
continue
}
if fldTagAttr != "" {
panic(fmt.Errorf("unknown pdef field tag attrs %q", fldTagAttr))
}
fldPath := append(path, fldTagName)
if filter != nil && !filter(fldPath...) {
continue
}
if needComma {
b.WriteByte(',')
needComma = false
}
b.WriteString("\"" + fldTagName + "\":")
needComma = true
switch fldTyp.Type.Kind() {
case reflect.Struct:
buf, err := pdataMarshalJSONStruct(fld, filter, fldPath...)
if err != nil {
return nil, err
}
b.Write(buf)
case reflect.Array:
b.WriteByte('[')
for j := 0; j < fldTyp.Type.Len(); j++ {
fldValElemVal := fldVal.Index(j)
fldValElemTyp := fldValElemVal.Type()
fldValElem := fldValElemVal.Interface()
if j != 0 {
b.WriteByte(',')
}
if fldValElemTyp.Kind() == reflect.Struct {
buf, err := pdataMarshalJSONStruct(fldValElem, filter, fldPath...)
if err != nil {
return nil, err
}
b.Write(buf)
} else {
buf, err := pdataMarshalJSONPrimitive(fldValElem)
if err != nil {
return nil, err
}
b.Write(buf)
}
}
b.WriteByte(']')
default:
buf, err := pdataMarshalJSONPrimitive(fld)
if err != nil {
return nil, err
}
b.Write(buf)
}
}
b.WriteByte('}')
return b.Bytes(), nil
}
func pdataMarshalJSONPrimitive(v any) ([]byte, error) {
switch v := v.(type) {
case float32:
if math.IsNaN(float64(v)) || math.IsInf(float64(v), 0) {
return []byte("null"), nil // match the JS behaviour
}
return json.Marshal(v)
case int32, bool, string:
return json.Marshal(v)
case json.Marshaler:
if reflect.TypeOf(v).ConvertibleTo(reflect.TypeOf(uint8(0))) {
return v.MarshalJSON() // enum
}
panic(fmt.Errorf("unhandled type %T", v))
default:
panic(fmt.Errorf("unhandled type %T", v))
}
}
`)
if err := writeGo(strings.TrimSuffix(filepath.Base(f.Name()), filepath.Ext(f.Name()))+".go", buf.Bytes()); err != nil {
fmt.Fprintf(os.Stderr, "error: write generated source: %v\n", err)
os.Exit(1)
}
tfs, err := filepath.Glob(`*.pdata`)
if err != nil {
fmt.Fprintf(os.Stderr, "error: find test pdata: %v\n", err)
os.Exit(1)
}
if len(tfs) != 0 {
var tbuf bytes.Buffer
fmt.Fprintf(&tbuf, `
package pdata
import (
"bytes"
"encoding/json"
"os"
"testing"
)
func TestPdataRoundtrip(t *testing.T) {
for _, fn := range %#v {
fn := fn
t.Run(fn, func(t *testing.T) {
obuf, err := os.ReadFile(fn)
if err != nil {
panic(err)
}
var d1 Pdata
if err := d1.UnmarshalBinary(obuf); err != nil {
t.Fatalf("failed to unmarshal %%q: %%v", fn, err)
}
rbuf, err := d1.MarshalBinary()
if err != nil {
t.Fatalf("failed to marshal %%q: %%v", fn, err)
}
if !bytes.Equal(obuf, rbuf) {
t.Errorf("round-trip failed: re-marshaled data does not match")
}
var d2 Pdata
if err := d2.UnmarshalBinary(rbuf); err != nil {
t.Fatalf("failed to unmarshal marshaled %%q: %%v", fn, err)
}
ebuf, err := d2.MarshalBinary()
if err != nil {
t.Fatalf("failed to marshal unmarshaled marshaled %%q: %%v", fn, err)
}
if !bytes.Equal(rbuf, ebuf) {
t.Errorf("internal round-trip failed: re-marshaled unmarshaled data encoded by marshal does not match")
}
if buf, err := d2.MarshalJSON(); err != nil {
t.Errorf("failed to marshal as JSON: %%v", err)
} else if err = json.Unmarshal(buf, new(map[string]interface{})); err != nil {
t.Errorf("bad json marshal result: %%v", err)
}
})
}
}
`, tfs)
if err := writeGo(strings.TrimSuffix(filepath.Base(f.Name()), filepath.Ext(f.Name()))+"_test.go", tbuf.Bytes()); err != nil {
fmt.Fprintf(os.Stderr, "error: write generated source: %v\n", err)
os.Exit(1)
}
}
}
func generateStruct(buf *bytes.Buffer, name string, pd *pdef.Pdef, fields []pdef.Field, root bool) {
if root && (len(fields) == 0 || fields[0].Type.Int == nil) {
panic("pdef root must have a version field of type int")
}
var size int
for _, v := range fields {
size += pd.TypeSize(v.Type)
}
{
pln(buf, `type %s struct {`, mangle(name, true))
for _, v := range fields {
pln(buf, `%s %s`+" `pdef:%q`", mangle(v.Name, true), pdefGoType(v.Type), v.Name)
}
if root {
pln(buf, `%s %s`, mangle("extraData", true), "[]byte")
}
pln(buf, `}`)
pln(buf, `var _ encoding.BinaryUnmarshaler = (*%s)(nil)`, mangle(name, true))
pln(buf, `var _ encoding.BinaryMarshaler = %s{}`, mangle(name, true))
pln(buf, `var _ json.Unmarshaler = (*%s)(nil)`, mangle(name, true))
pln(buf, `var _ json.Marshaler = %s{}`, mangle(name, true))
}
{
pln(buf, `func (v *%s) UnmarshalBinary(b []byte) error {`, mangle(name, true))
if root {
vs := pd.TypeSize(fields[0].Type)
pln(buf, `if len(b) < %d {`, vs)
pln(buf, `return fmt.Errorf(%#v, %#v, Version, ErrInvalidSize)`, `decode %q (v%d): %w: expected pdef version`, name)
pln(buf, `}`)
pln(buf, `if x := getInt(b[0:%d]); x != Version {`, vs)
pln(buf, `return fmt.Errorf(%#v, %#v, Version, ErrUnsupportedVersion, x)`, `decode %q (v%d): %w: got %d`, name)
pln(buf, `}`)
pln(buf, `if len(b) < %d {`, size)
} else {
pln(buf, `if len(b) != %d {`, size)
}
pln(buf, `return fmt.Errorf(%#v, %#v, Version, ErrInvalidSize, %#v, len(b))`, `decode %q (v%d): %w: expected %d bytes, got %d`, name, size)
pln(buf, `}`)
var offset int
for _, v := range fields {
sz := pd.TypeSize(v.Type)
switch {
case v.Type.Int != nil:
pln(buf, `v.%s = getInt(b[%d:%d])`, mangle(v.Name, true), offset, offset+sz)
case v.Type.Bool != nil:
pln(buf, `v.%s = getBool(b[%d:%d])`, mangle(v.Name, true), offset, offset+sz)
case v.Type.Float != nil:
pln(buf, `v.%s = getFloat(b[%d:%d])`, mangle(v.Name, true), offset, offset+sz)
case v.Type.String != nil:
pln(buf, `v.%s = getString(b[%d:%d])`, mangle(v.Name, true), offset, offset+sz)
case v.Type.Array != nil:
fallthrough
case v.Type.MappedArray != nil:
var arrCount int
var arrType pdef.TypeInfo
switch {
case v.Type.Array != nil:
arrCount = v.Type.Array.Length
arrType = v.Type.Array.Type
case v.Type.MappedArray != nil:
if x, ok := pd.Enum[v.Type.MappedArray.Enum]; ok {
arrCount = len(x)
arrType = v.Type.MappedArray.Type
} else {
panic("undefined enum")
}
default:
panic("bug: missing case")
}
szi := pd.TypeSize(arrType)
for i := 0; i < arrCount; i++ {
switch {
case arrType.Int != nil:
pln(buf, `v.%s[%d] = getInt(b[%d:%d])`, mangle(v.Name, true), i, offset+szi*i, offset+szi*i+szi)
case arrType.Bool != nil:
pln(buf, `v.%s[%d] = getBool(b[%d:%d])`, mangle(v.Name, true), i, offset+szi*i, offset+szi*i+szi)
case arrType.Float != nil:
pln(buf, `v.%s[%d] = getFloat(b[%d:%d])`, mangle(v.Name, true), i, offset+szi*i, offset+szi*i+szi)
case arrType.String != nil:
pln(buf, `v.%s[%d] = getString(b[%d:%d])`, mangle(v.Name, true), i, offset+szi*i, offset+szi*i+szi)
case arrType.Array != nil:
panic("impossible: can't have an array inside an array")
case arrType.MappedArray != nil:
panic("impossible: can't have a mapped array inside an array")
case arrType.Enum != nil:
pln(buf, `v.%s[%d] = %s(getEnum(b[%d:%d]))`, mangle(v.Name, true), i, mangle(arrType.Enum.Name, true), offset+szi*i, offset+szi*i+szi)
case arrType.Struct != nil:
// shouldn't error since we already checked the size
pln(buf, `must(v.%s[%d].UnmarshalBinary(b[%d:%d]))`, mangle(v.Name, true), i, offset+szi*i, offset+szi*i+szi)
default:
panic("bug: unimplemented type in array")
}
}
case v.Type.Enum != nil:
pln(buf, `v.%s = %s(getEnum(b[%d:%d]))`, mangle(v.Name, true), mangle(v.Type.Enum.Name, true), offset, offset+sz)
case v.Type.Struct != nil:
// shouldn't error since we already checked the size
pln(buf, `must(v.%s.UnmarshalBinary(b[%d:%d]))`, mangle(v.Name, true), offset, offset+sz)
default:
panic("bug: unimplemented pdef type")
}
offset += sz
}
if root {
pln(buf, `v.%s = b[%d:]`, mangle("extraData", true), size)
}
pln(buf, `return nil`)
pln(buf, `}`)
}
{
pln(buf, `func (v %s) MarshalBinary() ([]byte, error) {`, mangle(name, true))
if root {
pln(buf, `if x := v.%s; x != Version {`, mangle(fields[0].Name, true))
pln(buf, `return nil, fmt.Errorf(%#v, %#v, Version, ErrUnsupportedVersion, x)`, `encode %q (v%d): %w: got %d`, name)
pln(buf, `}`)
}
pln(buf, `b := make([]byte, %d)`, size)
var offset int
for _, v := range fields {
sz := pd.TypeSize(v.Type)
switch {
case v.Type.Int != nil:
pln(buf, `putInt(b[%d:%d], v.%s)`, offset, offset+sz, mangle(v.Name, true))
case v.Type.Bool != nil:
pln(buf, `putBool(b[%d:%d], v.%s)`, offset, offset+sz, mangle(v.Name, true))
case v.Type.Float != nil:
pln(buf, `putFloat(b[%d:%d], v.%s)`, offset, offset+sz, mangle(v.Name, true))
case v.Type.String != nil:
pln(buf, `if err := putString(b[%d:%d], v.%s); err != nil {`, offset, offset+sz, mangle(v.Name, true))
pln(buf, `return nil, fmt.Errorf(%#v, %#v, Version, ErrInvalidSize, %#v, err)`, `encode %q (v%d): %w: field %q: %v`, name, v.Name)
pln(buf, `}`)
case v.Type.Array != nil:
fallthrough
case v.Type.MappedArray != nil:
var arrCount int
var arrType pdef.TypeInfo
switch {
case v.Type.Array != nil:
arrCount = v.Type.Array.Length
arrType = v.Type.Array.Type
case v.Type.MappedArray != nil:
if x, ok := pd.Enum[v.Type.MappedArray.Enum]; ok {
arrCount = len(x)
arrType = v.Type.MappedArray.Type
} else {
panic("undefined enum")
}
default:
panic("bug: missing case")
}
szi := pd.TypeSize(arrType)
for i := 0; i < arrCount; i++ {
switch {
case arrType.Int != nil:
pln(buf, `putInt(b[%d:%d], v.%s[%d])`, offset+szi*i, offset+szi*i+szi, mangle(v.Name, true), i)
case arrType.Bool != nil:
pln(buf, `putBool(b[%d:%d], v.%s[%d])`, offset+szi*i, offset+szi*i+szi, mangle(v.Name, true), i)
case arrType.Float != nil:
pln(buf, `putFloat(b[%d:%d], v.%s[%d])`, offset+szi*i, offset+szi*i+szi, mangle(v.Name, true), i)
case arrType.String != nil:
pln(buf, `if err := putString(b[%d:%d], v.%s[%d]); err != nil {`, offset+szi*i, offset+szi*i+szi, mangle(v.Name, true), i)
pln(buf, `return nil, fmt.Errorf(%#v, %#v, Version, ErrInvalidSize, %#v, %#v, err)`, `encode %q (v%d): %w: field %q idx %d: %v`, name, v.Name, i)
pln(buf, `}`)
case arrType.Array != nil:
panic("impossible: can't have an array inside an array")
case arrType.MappedArray != nil:
panic("impossible: can't have a mapped array inside an array")
case arrType.Enum != nil:
pln(buf, `putEnum(b[%d:%d], uint8(v.%s[%d]))`, offset+szi*i, offset+szi*i+szi, mangle(v.Name, true), i)
case arrType.Struct != nil:
pln(buf, `if x, err := v.%s[%d].MarshalBinary(); err != nil {`, mangle(v.Name, true), i)
pln(buf, `return nil, fmt.Errorf(%#v, %#v, Version, %#v, %#v, err)`, `encode %q (v%d): field %q idx %d: %w`, name, v.Name, i)
pln(buf, `} else if len(x) != %d {`, szi)
pln(buf, `panic("bug: invalid marshal struct size")`)
pln(buf, `} else {`)
pln(buf, `copy(b[%d:%d], x)`, offset+szi*i, offset+szi*i+szi)
pln(buf, `}`)
default:
panic("bug: unimplemented type in array")
}
}
case v.Type.Enum != nil:
pln(buf, `putEnum(b[%d:%d], uint8(v.%s))`, offset, offset+sz, mangle(v.Name, true))
case v.Type.Struct != nil:
pln(buf, `if x, err := v.%s.MarshalBinary(); err != nil {`, mangle(v.Name, true))
pln(buf, `return nil, fmt.Errorf(%#v, %#v, Version, %#v, err)`, `encode %q (v%d): field %q: %w`, name, v.Name)
pln(buf, `} else if len(x) != %d {`, sz)
pln(buf, `panic("bug: invalid marshal struct size")`)
pln(buf, `} else {`)
pln(buf, `copy(b[%d:%d], x)`, offset, offset+sz)
pln(buf, `}`)
default:
panic("bug: unimplemented pdef type")
}
offset += sz
}
if root {
pln(buf, `b = append(b, v.%s...)`, mangle("extraData", true))
}
pln(buf, `return b, nil`)
pln(buf, `}`)
}
{
pln(buf, `func (v *%s) UnmarshalJSON(b []byte) error {`, mangle(name, true))
// TODO: implement this if we actually have a use for it
pln(buf, `return fmt.Errorf("not implemented")`)
pln(buf, `}`)
}
{
pln(buf, `func (v %s) MarshalJSON() ([]byte, error) {`, mangle(name, true))
pln(buf, `return v.MarshalJSONFilter(nil)`)
pln(buf, `}`)
}
{
pln(buf, `func (v %s) MarshalJSONFilter(filter func(path ...string) bool) ([]byte, error) {`, mangle(name, true))
if root {
pln(buf, `if x := v.%s; x != Version {`, mangle(fields[0].Name, true))
pln(buf, `return nil, fmt.Errorf(%#v, %#v, Version, ErrUnsupportedVersion, x)`, `encode %q (v%d): %w: got %d`, name)
pln(buf, `}`)
}
pln(buf, `return pdataMarshalJSONStruct(v, filter)`)
pln(buf, `}`)
}
}
func generateEnum(buf *bytes.Buffer, name string, values []string) {
pln(buf, `type %s uint8`, mangle(name, true))
{
pln(buf, `const (`)
for i, v := range values {
pln(buf, `%s %s = %d`, mangleEnumValue(name, v), mangle(name, true), i)
}
pln(buf, `%s %s = %d`, mangle(name, true)+"Count", mangle(name, true), len(values))
pln(buf, `)`)
pln(buf, `var _ fmt.Stringer = %s(0)`, mangle(name, true))
pln(buf, `var _ fmt.GoStringer = %s(0)`, mangle(name, true))
pln(buf, `var _ encoding.TextMarshaler = %s(0)`, mangle(name, true))
pln(buf, `var _ encoding.TextUnmarshaler = (*%s)(nil)`, mangle(name, true))
pln(buf, `var _ json.Marshaler = %s(0)`, mangle(name, true))
pln(buf, `var _ json.Unmarshaler = (*%s)(nil)`, mangle(name, true))
}
{
pln(buf, `func (v %s) String() string {`, mangle(name, true))
pln(buf, `if b, err := v.MarshalText(); err == nil {`)
pln(buf, `return string(b)`)
pln(buf, `}`)
pln(buf, `return strconv.Itoa(int(v))`)
pln(buf, `}`)
}
{
pln(buf, `func (v %s) GoString() string {`, mangle(name, true))
pln(buf, `switch v {`)
for _, v := range values {
pln(buf, `case %s:`, mangleEnumValue(name, v))
pln(buf, `return %#v`, mangleEnumValue(name, v))
}
pln(buf, `default:`)
pln(buf, `return fmt.Sprintf(%#v, %#v, int(v))`, `%s(%d)`, mangle(name, true))
pln(buf, `}`)
pln(buf, `}`)
}
{
pln(buf, `func (v %s) MarshalText() ([]byte, error) {`, mangle(name, true))
pln(buf, `switch v {`)
for _, v := range values {
pln(buf, `case %s:`, mangleEnumValue(name, v))
pln(buf, `return []byte(%#v), nil`, v)
}
pln(buf, `default:`)
pln(buf, `return nil, fmt.Errorf(%#v, ErrInvalidEnumValue, int(v), %#v)`, "%w: invalid value %d for enum %q", mangle(name, true))
pln(buf, `}`)
pln(buf, `}`)
}
{
pln(buf, `func (v *%s) UnmarshalText(b []byte) error {`, mangle(name, true))
pln(buf, `switch string(b) {`)
for _, v := range values {
pln(buf, `case %q:`, v)
pln(buf, `*v = %s`, mangleEnumValue(name, v))
}
pln(buf, `default:`)
pln(buf, `return fmt.Errorf(%#v, ErrInvalidEnumValue, string(b), %#v)`, "%w: invalid value %q for enum %q", mangle(name, true))
pln(buf, `}`)
pln(buf, `return nil`)
pln(buf, `}`)
}
{
pln(buf, `func (v %s) MarshalJSON() ([]byte, error) {`, mangle(name, true))
pln(buf, `switch v {`)
for _, v := range values {
pln(buf, `case %s:`, mangleEnumValue(name, v))
pln(buf, `return []byte(%#v), nil`, `"`+v+`"`)
}
pln(buf, `default:`)
pln(buf, `return []byte(strconv.Itoa(int(v))), nil`)
pln(buf, `}`)
pln(buf, `}`)
}
{
pln(buf, `func (v *%s) UnmarshalJSON(b []byte) error {`, mangle(name, true))
pln(buf, `switch string(b) {`)
for _, v := range values {
pln(buf, `case %#v:`, `"`+v+`"`)
pln(buf, `*v = %s`, mangleEnumValue(name, v))
}
pln(buf, `default:`)
pln(buf, `return json.Unmarshal(b, (*uint8)(v))`)
pln(buf, `}`)
pln(buf, `return nil`)
pln(buf, `}`)
}
}
func pdefGoType(t pdef.TypeInfo) string {
switch {
case t.Int != nil:
return "int32"
case t.Float != nil:
return "float32"
case t.Bool != nil:
return "bool"
case t.String != nil:
return "string"
case t.Struct != nil:
return mangle(t.Struct.Name, true)
case t.Enum != nil:
return mangle(t.Enum.Name, true)
case t.Array != nil:
return fmt.Sprintf("[%d]%s", t.Array.Length, pdefGoType(t.Array.Type))
case t.MappedArray != nil:
return fmt.Sprintf("[%sCount]%s", mangle(t.MappedArray.Enum, true), pdefGoType(t.MappedArray.Type))
default:
panic("unimplemented type")
}
}
func mangleEnumValue(name, value string) string {
return mangle(name, true) + "_" + mangle(value, false)
}
func mangle(ident string, upper bool) string {
x := []rune(ident)
if len(x) > 2 && x[0] == 's' && unicode.IsUpper(x[1]) {
x = x[1:] // remove the s/e prefix for struct/enum names
}
if upper {
x[0] = unicode.ToUpper(x[0])
}
return string(x)
}
func pln(b *bytes.Buffer, format string, a ...interface{}) {
fmt.Fprintf(b, format+"\n", a...)
}
func openSingleFile(pattern string) (*os.File, error) {
ms, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
if len(ms) != 1 {
return nil, fmt.Errorf("expected 1 file matching %q, found %d: %q", pattern, len(ms), ms)
}
return os.Open(ms[0])
}
func writeGo(name string, data []byte) error {
var err error
if err = os.WriteFile(name, data, 0666); err != nil {
return err
}
if data, err = format.Source(data); err != nil {
return err
}
if err = os.WriteFile(name, data, 0666); err != nil {
return err
}
return nil
}
func each[T any](m map[string]T, fn func(k string, v T)) {
k := make([]string, 0, len(m))
for x := range m {
k = append(k, x)
}
sort.Strings(k)
for _, x := range k {
fn(x, m[x])
}
}
|