-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_replace_contract_test.go
More file actions
1115 lines (1038 loc) · 32.9 KB
/
Copy pathdecoder_replace_contract_test.go
File metadata and controls
1115 lines (1038 loc) · 32.9 KB
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 vibejson
import (
"reflect"
"testing"
"time"
"unsafe"
)
type replaceJSONState struct {
hidden int
Seen int
}
func (s *replaceJSONState) UnmarshalJSON(data []byte) error {
s.Seen = s.hidden
return nil
}
type replaceTextState struct {
hidden int
Seen int
}
func (s *replaceTextState) UnmarshalText(text []byte) error {
s.Seen = s.hidden
return nil
}
type replaceHookState struct {
hidden int
Seen int
}
type replaceContainingInner struct {
Marker int `json:"marker"`
Parent *replaceContainingOuter `json:"parent"`
}
type replaceContainingOuter struct {
Prefix int `json:"prefix"`
Child replaceContainingInner `json:"child"`
}
type ReplaceWideScalars struct {
F00 int `json:"f00"`
F01 int `json:"f01"`
F02 int `json:"f02"`
F03 int `json:"f03"`
F04 int `json:"f04"`
F05 int `json:"f05"`
F06 int `json:"f06"`
F07 int `json:"f07"`
F08 int `json:"f08"`
F09 int `json:"f09"`
F10 int `json:"f10"`
F11 int `json:"f11"`
F12 int `json:"f12"`
F13 int `json:"f13"`
F14 int `json:"f14"`
F15 int `json:"f15"`
F16 int `json:"f16"`
F17 int `json:"f17"`
F18 int `json:"f18"`
F19 int `json:"f19"`
F20 int `json:"f20"`
F21 int `json:"f21"`
F22 int `json:"f22"`
F23 int `json:"f23"`
F24 int `json:"f24"`
F25 int `json:"f25"`
F26 int `json:"f26"`
F27 int `json:"f27"`
F28 int `json:"f28"`
F29 int `json:"f29"`
F30 int `json:"f30"`
F31 int `json:"f31"`
F32 int `json:"f32"`
F33 int `json:"f33"`
F34 int `json:"f34"`
F35 int `json:"f35"`
F36 int `json:"f36"`
F37 int `json:"f37"`
F38 int `json:"f38"`
F39 int `json:"f39"`
F40 int `json:"f40"`
F41 int `json:"f41"`
F42 int `json:"f42"`
F43 int `json:"f43"`
F44 int `json:"f44"`
F45 int `json:"f45"`
F46 int `json:"f46"`
F47 int `json:"f47"`
F48 int `json:"f48"`
F49 int `json:"f49"`
F50 int `json:"f50"`
F51 int `json:"f51"`
F52 int `json:"f52"`
F53 int `json:"f53"`
F54 int `json:"f54"`
F55 int `json:"f55"`
F56 int `json:"f56"`
F57 int `json:"f57"`
F58 int `json:"f58"`
F59 int `json:"f59"`
F60 int `json:"f60"`
F61 int `json:"f61"`
F62 int `json:"f62"`
F63 int `json:"f63"`
F64 int `json:"f64"`
}
type ReplaceWideTail struct {
Nested []int `json:"nested"`
}
type replaceWideDocument struct {
ReplaceWideScalars
*ReplaceWideTail
Direct []int `json:"direct"`
}
func (s *replaceHookState) UnmarshalVibeJSON(cursor DecodeCursor) (DecodeCursor, error) {
s.Seen = s.hidden
return cursor, cursor.Skip()
}
func TestReplaceCustomDecodersReceiveZeroState(t *testing.T) {
t.Run("json", func(t *testing.T) {
decoder := mustCompileTestDecoder[replaceJSONState](t, DecoderOptions{Replace: true})
got := replaceJSONState{hidden: 7, Seen: 7}
if err := decoder.Decode([]byte(`{}`), &got); err != nil {
t.Fatal(err)
}
if got.hidden != 0 || got.Seen != 0 {
t.Fatalf("Replace JSON receiver = %#v, want zero-state receiver", got)
}
})
t.Run("text", func(t *testing.T) {
decoder := mustCompileTestDecoder[replaceTextState](t, DecoderOptions{Replace: true})
got := replaceTextState{hidden: 7, Seen: 7}
if err := decoder.Decode([]byte(`"value"`), &got); err != nil {
t.Fatal(err)
}
if got.hidden != 0 || got.Seen != 0 {
t.Fatalf("Replace text receiver = %#v, want zero-state receiver", got)
}
})
t.Run("native hook", func(t *testing.T) {
decoder := mustCompileTestDecoder[replaceHookState](t, DecoderOptions{Replace: true})
got := replaceHookState{hidden: 7, Seen: 7}
if err := decoder.Decode([]byte(`0`), &got); err != nil {
t.Fatal(err)
}
if got.hidden != 0 || got.Seen != 0 {
t.Fatalf("Replace native-hook receiver = %#v, want zero-state receiver", got)
}
})
}
func TestReplaceClearsAbsentCustomDecoderField(t *testing.T) {
type document struct {
Value replaceJSONState `json:"value"`
Other int `json:"other"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
got := document{
Value: replaceJSONState{hidden: 7, Seen: 7},
}
if err := decoder.Decode([]byte(`{"other":1}`), &got); err != nil {
t.Fatal(err)
}
if got.Value.hidden != 0 || got.Value.Seen != 0 {
t.Fatalf("absent custom field = %#v, want zero value", got.Value)
}
}
func TestReplaceClearsFieldsExcludedFromJSON(t *testing.T) {
type embeddedValue struct {
Kept int `json:"kept"`
Ignored int `json:"-"`
hidden int
}
type EmbeddedPointer struct {
Other int `json:"other"`
Ignored int `json:"-"`
hidden int
}
type leftConflict struct {
Conflict int
}
type rightConflict struct {
Conflict int
}
type document struct {
embeddedValue
*EmbeddedPointer
leftConflict
rightConflict
Visible int `json:"visible"`
Ignored int `json:"-"`
hidden int
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
got := document{
embeddedValue: embeddedValue{Kept: 7, Ignored: 8, hidden: 9},
EmbeddedPointer: &EmbeddedPointer{Other: 10, Ignored: 11, hidden: 12},
leftConflict: leftConflict{Conflict: 13},
rightConflict: rightConflict{Conflict: 14},
Visible: 15,
Ignored: 16,
hidden: 17,
}
if err := decoder.Decode([]byte(`{"kept":1,"other":2,"visible":3}`), &got); err != nil {
t.Fatal(err)
}
want := document{
embeddedValue: embeddedValue{Kept: 1},
EmbeddedPointer: &EmbeddedPointer{Other: 2},
Visible: 3,
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("Replace retained state excluded from JSON:\n got %#v\n want %#v", got, want)
}
}
func TestReplaceNullClearsTimeValue(t *testing.T) {
decoder := mustCompileTestDecoder[time.Time](t, DecoderOptions{Replace: true})
got := time.Date(2026, 7, 28, 12, 0, 0, 0, time.UTC)
if err := decoder.Decode([]byte(`null`), &got); err != nil {
t.Fatal(err)
}
if !got.IsZero() {
t.Fatalf("Replace null retained time %v, want zero", got)
}
}
func TestReplaceQuotedNullClearsScalar(t *testing.T) {
type document struct {
Number int `json:"number,string"`
Pointer *int `json:"pointer,string"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
seven := 7
got := document{Number: 7, Pointer: &seven}
if err := decoder.Decode([]byte(`{"number":"null","pointer":"null"}`), &got); err != nil {
t.Fatal(err)
}
if got.Number != 0 || got.Pointer != nil {
t.Fatalf("quoted null retained Number=%d Pointer=%v, want zero values", got.Number, got.Pointer)
}
}
func TestReplaceBreaksQuotedNumberPointerAliases(t *testing.T) {
type document struct {
First *int `json:"first,string"`
Second *int `json:"second,string"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
shared := 7
got := document{First: &shared, Second: &shared}
if err := decoder.Decode([]byte(`{"first":"1","second":"2"}`), &got); err != nil {
t.Fatal(err)
}
if got.First == nil || got.Second == nil || *got.First != 1 || *got.Second != 2 {
t.Fatalf("Replace quoted aliased pointers decoded as %#v", got)
}
if got.First == got.Second {
t.Fatalf("Replace retained quoted pointer alias %p", got.First)
}
}
func TestReplaceBreaksQuotedBoolPointerAliases(t *testing.T) {
type document struct {
First *bool `json:"first,string"`
Second *bool `json:"second,string"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
shared := true
got := document{First: &shared, Second: &shared}
if err := decoder.Decode([]byte(`{"first":"false","second":"true"}`), &got); err != nil {
t.Fatal(err)
}
if got.First == nil || got.Second == nil || *got.First || !*got.Second {
t.Fatalf("Replace quoted aliased bool pointers decoded as %#v", got)
}
if got.First == got.Second {
t.Fatalf("Replace retained quoted bool pointer alias %p", got.First)
}
}
func TestReplaceDynamicInterfaceIgnoresExistingPointer(t *testing.T) {
type pointee struct {
Value int `json:"value"`
}
type document struct {
Dynamic any `json:"dynamic"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
got := document{Dynamic: &pointee{Value: 7}}
if err := decoder.Decode([]byte(`{"dynamic":{"value":1}}`), &got); err != nil {
t.Fatal(err)
}
want := document{Dynamic: map[string]any{"value": float64(1)}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("Replace dynamic interface:\n got %#v\n want %#v", got, want)
}
root := mustCompileTestDecoder[any](t, DecoderOptions{Replace: true})
var rootGot any = &pointee{Value: 7}
if err := root.Decode([]byte(`{"value":1}`), &rootGot); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(rootGot, want.Dynamic) {
t.Fatalf("Replace root interface = %#v, want %#v", rootGot, want.Dynamic)
}
}
type replaceNonEmpty interface {
replaceMarker()
}
type replaceNonEmptyValue struct {
Value int `json:"value"`
}
func (*replaceNonEmptyValue) replaceMarker() {}
func TestReplaceNonEmptyInterfaceDoesNotMergeExistingPointer(t *testing.T) {
type document struct {
Value replaceNonEmpty `json:"value"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
got := document{Value: &replaceNonEmptyValue{Value: 7}}
if err := decoder.Decode([]byte(`{"value":{"value":1}}`), &got); err == nil {
t.Fatalf("Replace decoded into stale non-empty interface: %#v", got)
}
}
type replaceEmbeddedValue struct {
Value int `json:"value"`
}
type replaceEmbeddedDocument struct {
*replaceEmbeddedValue
Other int `json:"other"`
}
func TestReplaceClearsAbsentEmbeddedPointer(t *testing.T) {
decoder := mustCompileTestDecoder[replaceEmbeddedDocument](t, DecoderOptions{Replace: true})
got := replaceEmbeddedDocument{
replaceEmbeddedValue: &replaceEmbeddedValue{Value: 7},
}
if err := decoder.Decode([]byte(`{"other":1}`), &got); err != nil {
t.Fatal(err)
}
if got.replaceEmbeddedValue != nil {
t.Fatalf("absent embedded pointer = %#v, want nil", got.replaceEmbeddedValue)
}
got.replaceEmbeddedValue = &replaceEmbeddedValue{Value: 7}
if err := decoder.Decode([]byte(`{"value":1}`), &got); err == nil {
t.Fatal("Replace reused an unexported embedded pointer that is nil in a fresh destination")
}
}
func TestReplaceReusesPresentEmbeddedPointerStorage(t *testing.T) {
type Embedded struct {
First []int `json:"first"`
}
type document struct {
*Embedded
Second []int `json:"second"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
first := make([]int, 1)
second := make([]int, 1)
got := document{
Embedded: &Embedded{First: first},
Second: second,
}
src := []byte(`{"first":[1],"second":[2]}`)
decode := func() {
if err := decoder.Decode(src, &got); err != nil {
panic(err)
}
if got.Embedded == nil ||
len(got.First) != 1 || got.First[0] != 1 ||
len(got.Second) != 1 || got.Second[0] != 2 {
panic("unexpected Replace result")
}
}
decode()
embeddedPointer := got.Embedded
firstPointer := &got.First[0]
secondPointer := &got.Second[0]
if allocs := testing.AllocsPerRun(100, decode); allocs != 0 {
t.Fatalf("present embedded pointer reuse: %.2f allocs/decode, want 0", allocs)
}
if got.Embedded != embeddedPointer ||
&got.First[0] != firstPointer ||
&got.Second[0] != secondPointer {
t.Fatal("Replace discarded unique storage behind a present embedded pointer")
}
}
func TestReplaceBreaksAliasesAcrossEmbeddedPointerFields(t *testing.T) {
type Embedded struct {
First []int `json:"first"`
}
type document struct {
*Embedded
Second []int `json:"second"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
backing := []int{7}
got := document{
Embedded: &Embedded{First: backing},
Second: backing,
}
if err := decoder.Decode([]byte(`{"first":[1],"second":[2]}`), &got); err != nil {
t.Fatal(err)
}
if got.Embedded == nil ||
!reflect.DeepEqual(got.First, []int{1}) ||
!reflect.DeepEqual(got.Second, []int{2}) {
t.Fatalf("Replace retained alias across embedded pointer fields: %#v", got)
}
if &got.First[0] == &got.Second[0] {
t.Fatal("Replace kept shared storage across an embedded pointer")
}
}
func TestReplaceResetsOnlyAbsentEmbeddedPointerPrefixes(t *testing.T) {
type Inner struct {
Leaf int `json:"leaf"`
}
type Middle struct {
*Inner
Branch int `json:"branch"`
}
type document struct {
*Middle
Top int `json:"top"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
got := document{
Middle: &Middle{
Inner: &Inner{Leaf: 7},
Branch: 8,
},
Top: 9,
}
middle := got.Middle
if err := decoder.Decode([]byte(`{"branch":1,"top":2}`), &got); err != nil {
t.Fatal(err)
}
if got.Middle != middle || got.Inner != nil || got.Branch != 1 || got.Top != 2 {
t.Fatalf("Replace reset the wrong embedded pointer prefix: %#v", got)
}
got.Inner = &Inner{Leaf: 7}
inner := got.Inner
if err := decoder.Decode([]byte(`{"leaf":3,"top":4}`), &got); err != nil {
t.Fatal(err)
}
if got.Middle != middle || got.Inner != inner ||
got.Leaf != 3 || got.Branch != 0 || got.Top != 4 {
t.Fatalf("Replace failed to preserve present embedded pointer prefixes: %#v", got)
}
}
func TestReplaceBreaksStalePointerAliases(t *testing.T) {
type document struct {
First *int `json:"first"`
Second *int `json:"second"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
shared := 7
original := &shared
got := document{First: original, Second: original}
if err := decoder.Decode([]byte(`{"first":1,"second":2}`), &got); err != nil {
t.Fatal(err)
}
if got.First == nil || got.Second == nil || *got.First != 1 || *got.Second != 2 {
t.Fatalf("Replace aliased pointers decoded as %#v", got)
}
if got.First == got.Second {
t.Fatalf("Replace retained stale pointer alias %p", got.First)
}
if got.First != original {
t.Fatalf("Replace discarded unique first pointee: got %p, want reuse of %p", got.First, original)
}
}
func TestReplacePointerReuseStaysAllocationFree(t *testing.T) {
type document struct {
First *int `json:"first"`
Second *int `json:"second"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
first, second := 1, 2
got := document{First: &first, Second: &second}
src := []byte(`{"first":3,"second":4}`)
if err := decoder.Decode(src, &got); err != nil {
t.Fatal(err)
}
firstPointer, secondPointer := got.First, got.Second
allocs := testing.AllocsPerRun(100, func() {
if err := decoder.Decode(src, &got); err != nil {
t.Fatal(err)
}
})
if allocs != 0 {
t.Fatalf("Replace pointer reuse allocated %.1f times per decode, want 0", allocs)
}
if got.First != firstPointer || got.Second != secondPointer {
t.Fatal("Replace discarded unique pointees during allocation-free reuse")
}
}
func BenchmarkReplacePointerReuse(b *testing.B) {
type document struct {
Value *int `json:"value"`
}
decoder, err := CompileDecoder[document](DecoderOptions{Replace: true})
if err != nil {
b.Fatal(err)
}
src := []byte(`{"value":1}`)
value := 0
dst := document{Value: &value}
b.ReportAllocs()
b.SetBytes(int64(len(src)))
b.ResetTimer()
for b.Loop() {
if err := decoder.Decode(src, &dst); err != nil {
b.Fatal(err)
}
}
if dst.Value == nil || *dst.Value != 1 {
b.Fatal("unexpected Replace result")
}
}
func TestReplaceWideReferenceTrackingStaysAllocationFree(t *testing.T) {
decoder := mustCompileTestDecoder[[][]int](t, DecoderOptions{Replace: true})
got := make([][]int, 20)
for i := range got {
got[i] = []int{i}
}
src := []byte(`[[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19]]`)
if err := decoder.Decode(src, &got); err != nil {
t.Fatal(err)
}
allocs := testing.AllocsPerRun(100, func() {
if err := decoder.Decode(src, &got); err != nil {
t.Fatal(err)
}
})
if allocs != 0 {
t.Fatalf("wide Replace reference tracking allocated %.1f times per decode, want 0", allocs)
}
}
func TestReplaceWideStructReusesPresentLateReferences(t *testing.T) {
decoder := mustCompileTestDecoder[replaceWideDocument](t, DecoderOptions{Replace: true})
tail := &ReplaceWideTail{Nested: make([]int, 2, 4)}
got := replaceWideDocument{
ReplaceWideScalars: ReplaceWideScalars{F00: 7, F64: 9},
ReplaceWideTail: tail,
Direct: make([]int, 2, 4),
}
src := []byte(`{"nested":[1,2],"direct":[3,4]}`)
if err := decoder.Decode(src, &got); err != nil {
t.Fatal(err)
}
nestedBacking := unsafe.SliceData(got.Nested)
directBacking := unsafe.SliceData(got.Direct)
if got.ReplaceWideTail != tail || nestedBacking == nil || directBacking == nil {
t.Fatalf("wide Replace warmup discarded present storage: %#v", got)
}
decode := func() {
if err := decoder.Decode(src, &got); err != nil {
panic(err)
}
}
if allocs := testing.AllocsPerRun(100, decode); allocs != 0 {
t.Fatalf("wide struct Replace allocated %.2f times/decode, want 0", allocs)
}
if got.F00 != 0 || got.F64 != 0 ||
!reflect.DeepEqual(got.Nested, []int{1, 2}) ||
!reflect.DeepEqual(got.Direct, []int{3, 4}) {
t.Fatal("wide Replace result diverged from a fresh destination")
}
if got.ReplaceWideTail != tail ||
unsafe.SliceData(got.Nested) != nestedBacking ||
unsafe.SliceData(got.Direct) != directBacking {
t.Fatal("wide Replace discarded a unique present pointer or slice backing")
}
}
func TestReplaceDuplicateReferenceFieldStaysAllocationFree(t *testing.T) {
type document struct {
First []int `json:"first"`
Second []int `json:"second"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
got := document{
First: make([]int, 1, 2),
Second: make([]int, 1, 2),
}
src := []byte(`{"first":[1],"first":[2],"second":[3]}`)
if err := decoder.Decode(src, &got); err != nil {
t.Fatal(err)
}
allocs := testing.AllocsPerRun(100, func() {
if err := decoder.Decode(src, &got); err != nil {
t.Fatal(err)
}
})
if allocs != 0 {
t.Fatalf("duplicate Replace reference field allocated %.1f times per decode, want 0", allocs)
}
if !reflect.DeepEqual(got.First, []int{2}) || !reflect.DeepEqual(got.Second, []int{3}) {
t.Fatalf("duplicate Replace reference field decoded as %#v", got)
}
}
func TestReplaceReusesSliceStorageReleasedByNull(t *testing.T) {
type document struct {
First []int `json:"first"`
Second []int `json:"second"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
backing := make([]int, 1, 2)
base := unsafe.SliceData(backing)
got := document{First: backing, Second: backing}
if err := decoder.Decode([]byte(`{"first":null,"second":[2]}`), &got); err != nil {
t.Fatal(err)
}
if got.First != nil || !reflect.DeepEqual(got.Second, []int{2}) {
t.Fatalf("Replace released-slice decode = %#v", got)
}
if unsafe.SliceData(got.Second) != base {
t.Fatal("Replace detached storage released by an earlier null field")
}
}
func TestReplaceReusesSliceStorageReleasedByGrowth(t *testing.T) {
type document struct {
First []int `json:"first"`
Second []int `json:"second"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
backing := make([]int, 0, 1)
base := unsafe.SliceData(backing)
got := document{First: backing, Second: backing}
if err := decoder.Decode([]byte(`{"first":[1,2],"second":[3]}`), &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got.First, []int{1, 2}) || !reflect.DeepEqual(got.Second, []int{3}) {
t.Fatalf("Replace grown-slice decode = %#v", got)
}
if unsafe.SliceData(got.Second) != base {
t.Fatal("Replace detached storage released when an earlier slice grew")
}
}
func TestReplaceDuplicateNullReleasesPointerStorage(t *testing.T) {
type document struct {
First *int `json:"first"`
Second *int `json:"second"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
shared := 7
original := &shared
got := document{First: original, Second: original}
if err := decoder.Decode([]byte(`{"first":1,"first":null,"second":2}`), &got); err != nil {
t.Fatal(err)
}
if got.First != nil || got.Second == nil || *got.Second != 2 {
t.Fatalf("Replace duplicate-null pointer decode = %#v", got)
}
if got.Second != original {
t.Fatal("Replace detached pointee released by a duplicate null field")
}
}
func TestReplaceDuplicateQuotedNullReleasesPointerStorage(t *testing.T) {
type document struct {
First *int `json:"first,string"`
Second *int `json:"second,string"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
shared := 7
original := &shared
got := document{First: original, Second: original}
if err := decoder.Decode([]byte(`{"first":"1","first":"null","second":"2"}`), &got); err != nil {
t.Fatal(err)
}
if got.First != nil || got.Second == nil || *got.Second != 2 {
t.Fatalf("Replace duplicate quoted-null pointer decode = %#v", got)
}
if got.Second != original {
t.Fatal("Replace detached pointee released by a duplicate quoted null field")
}
}
func TestReplaceDuplicateNestedSliceReusesRelocatedElement(t *testing.T) {
type document struct {
Values [][]int `json:"values"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
inner := make([]int, 1, 2)
innerBase := unsafe.SliceData(inner)
outer := make([][]int, 1, 1)
outer[0] = inner
got := document{Values: outer}
src := []byte(`{"values":[[1],[2]],"values":[[3],[4]]}`)
if err := decoder.Decode(src, &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got.Values, [][]int{{3}, {4}}) {
t.Fatalf("Replace duplicate nested-slice decode = %#v", got)
}
if unsafe.SliceData(got.Values[0]) != innerBase {
t.Fatal("Replace detached a nested slice after its owner slot relocated")
}
allocs := testing.AllocsPerRun(100, func() {
if err := decoder.Decode(src, &got); err != nil {
t.Fatal(err)
}
})
if allocs != 0 {
t.Fatalf("duplicate nested Replace field allocated %.1f times per warm decode, want 0", allocs)
}
}
func TestReplaceDuplicateStructNullReleasesNestedStorage(t *testing.T) {
type nested struct {
Values []int `json:"values"`
}
type document struct {
First nested `json:"first"`
Second []int `json:"second"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
backing := make([]int, 1, 2)
base := unsafe.SliceData(backing)
got := document{First: nested{Values: backing}, Second: backing}
src := []byte(`{"first":{"values":[1]},"first":null,"second":[2]}`)
if err := decoder.Decode(src, &got); err != nil {
t.Fatal(err)
}
if got.First.Values != nil || !reflect.DeepEqual(got.Second, []int{2}) {
t.Fatalf("Replace duplicate struct-null decode = %#v", got)
}
if unsafe.SliceData(got.Second) != base {
t.Fatal("Replace detached nested storage released by a duplicate struct null")
}
}
func TestReplaceDuplicateStructMissingFieldReleasesNestedStorage(t *testing.T) {
type nested struct {
Values []int `json:"values"`
Spare []int `json:"spare"`
}
type document struct {
Nested nested `json:"nested"`
Reuse []int `json:"reuse"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
src := []byte(`{"nested":{"values":[1]},"nested":{},"reuse":[2]}`)
storage := make([]int, 1)
var dst document
decode := func() {
dst.Nested.Values = storage[:1]
dst.Nested.Spare = nil
dst.Reuse = storage[:1]
if err := decoder.Decode(src, &dst); err != nil {
panic(err)
}
if dst.Nested.Values != nil || dst.Nested.Spare != nil ||
len(dst.Reuse) != 1 || dst.Reuse[0] != 2 {
panic("unexpected Replace result")
}
}
decode()
if allocs := testing.AllocsPerRun(100, decode); allocs != 0 {
t.Fatalf("duplicate object missing a reference field: %.2f allocs/decode, want 0", allocs)
}
}
func TestReplaceBreaksStaleSliceAndMapAliases(t *testing.T) {
type document struct {
FirstSlice []int `json:"firstSlice"`
SecondSlice []int `json:"secondSlice"`
FirstMap map[string]int `json:"firstMap"`
SecondMap map[string]int `json:"secondMap"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
backing := []int{7, 7, 7}
sharedMap := map[string]int{"stale": 7}
got := document{
FirstSlice: backing[:2],
SecondSlice: backing[1:],
FirstMap: sharedMap,
SecondMap: sharedMap,
}
if err := decoder.Decode([]byte(
`{"firstSlice":[1,2],"secondSlice":[3,4],"firstMap":{"a":1},"secondMap":{"b":2}}`,
), &got); err != nil {
t.Fatal(err)
}
want := document{
FirstSlice: []int{1, 2},
SecondSlice: []int{3, 4},
FirstMap: map[string]int{"a": 1},
SecondMap: map[string]int{"b": 2},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("Replace retained stale slice/map aliases:\n got %#v\n want %#v", got, want)
}
}
func TestReplaceBreaksPointerSliceAliases(t *testing.T) {
type document struct {
Slice []int `json:"slice"`
Value *int `json:"value"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
backing := []int{7}
got := document{Slice: backing, Value: &backing[0]}
if err := decoder.Decode([]byte(`{"slice":[1],"value":2}`), &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got.Slice, []int{1}) || got.Value == nil || *got.Value != 2 {
t.Fatalf("Replace retained pointer/slice alias: %#v", got)
}
if got.Value == &got.Slice[0] {
t.Fatal("Replace kept a pointer into sibling slice storage")
}
}
func TestReplaceBreaksPointerAliasesIntoDestination(t *testing.T) {
type document struct {
Scalar int `json:"scalar"`
Value *int `json:"value"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
check := func(t *testing.T, got *document) {
t.Helper()
if got.Scalar != 1 || got.Value == nil || *got.Value != 2 {
t.Fatalf("Replace retained pointer into destination: %#v", *got)
}
if got.Value == &got.Scalar {
t.Fatal("Replace kept a pointer into sibling destination storage")
}
}
t.Run("Decode", func(t *testing.T) {
got := document{Scalar: 7}
got.Value = &got.Scalar
if err := decoder.Decode([]byte(`{"scalar":1,"value":2}`), &got); err != nil {
t.Fatal(err)
}
check(t, &got)
})
t.Run("DecodePrefix", func(t *testing.T) {
got := document{Scalar: 7}
got.Value = &got.Scalar
n, err := decoder.DecodePrefix([]byte(`{"scalar":1,"value":2} trailing`), &got)
if err != nil {
t.Fatal(err)
}
if n != len(`{"scalar":1,"value":2}`) {
t.Fatalf("DecodePrefix consumed %d bytes", n)
}
check(t, &got)
})
t.Run("DecodeArray", func(t *testing.T) {
storage := make([]document, 1)
storage[0].Scalar = 7
storage[0].Value = &storage[0].Scalar
got, err := decoder.DecodeArray([]byte(`[{"scalar":1,"value":2}]`), storage[:0])
if err != nil {
t.Fatal(err)
}
if len(got) != 1 {
t.Fatalf("DecodeArray length = %d", len(got))
}
check(t, &got[0])
})
}
func TestReplaceBreaksSliceAliasesIntoDestination(t *testing.T) {
type document struct {
Fixed [2]int `json:"fixed"`
Values []int `json:"values"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
got := document{Fixed: [2]int{7, 7}}
got.Values = got.Fixed[:]
if err := decoder.Decode([]byte(`{"fixed":[1,2],"values":[3,4]}`), &got); err != nil {
t.Fatal(err)
}
if got.Fixed != [2]int{1, 2} || !reflect.DeepEqual(got.Values, []int{3, 4}) {
t.Fatalf("Replace retained slice backing inside destination: %#v", got)
}
if &got.Values[0] == &got.Fixed[0] {
t.Fatal("Replace kept a slice backed by a sibling destination array")
}
}
func TestReplaceBreaksPointerToObjectContainingDestination(t *testing.T) {
decoder := mustCompileTestDecoder[replaceContainingInner](t, DecoderOptions{Replace: true})
var storage replaceContainingOuter
storage.Child.Marker = 7
storage.Child.Parent = &storage
if err := decoder.Decode(
[]byte(`{"marker":1,"parent":{"prefix":2}}`),
&storage.Child,
); err != nil {
t.Fatal(err)
}
got := storage.Child
if got.Marker != 1 || got.Parent == nil || got.Parent.Prefix != 2 {
t.Fatalf("Replace retained a pointer to the object containing dst: %#v", got)
}
if got.Parent == &storage {
t.Fatal("Replace kept a pointer to the object containing its destination")
}
}
func TestReplaceBreaksPointerAliasesInsideSliceElements(t *testing.T) {
type element struct {
Scalar int `json:"scalar"`
Value *int `json:"value"`
}
decoder := mustCompileTestDecoder[[]element](t, DecoderOptions{Replace: true})
got := make([]element, 1)
got[0].Scalar = 7
got[0].Value = &got[0].Scalar
if err := decoder.Decode([]byte(`[{"scalar":1,"value":2}]`), &got); err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].Scalar != 1 || got[0].Value == nil || *got[0].Value != 2 {
t.Fatalf("Replace retained a pointer into slice element storage: %#v", got)
}
if got[0].Value == &got[0].Scalar {
t.Fatal("Replace kept a pointer into its reused slice element")
}
}
func TestReplaceBreaksPointerAliasesInsideReusedPointee(t *testing.T) {
type nested struct {
Scalar int `json:"scalar"`
Value *int `json:"value"`
}
type document struct {
Nested *nested `json:"nested"`
}
decoder := mustCompileTestDecoder[document](t, DecoderOptions{Replace: true})
storage := &nested{Scalar: 7}
storage.Value = &storage.Scalar
got := document{Nested: storage}
if err := decoder.Decode([]byte(`{"nested":{"scalar":1,"value":2}}`), &got); err != nil {
t.Fatal(err)
}
if got.Nested == nil || got.Nested.Scalar != 1 ||
got.Nested.Value == nil || *got.Nested.Value != 2 {
t.Fatalf("Replace retained a pointer into reused pointee storage: %#v", got)
}
if got.Nested != storage {
t.Fatalf("Replace discarded unique outer pointee: got %p, want %p", got.Nested, storage)
}
if got.Nested.Value == &got.Nested.Scalar {
t.Fatal("Replace kept a pointer into its reused parent pointee")
}
}
func TestReplaceBreaksQuotedPointerAliasesIntoDestination(t *testing.T) {
type document struct {
Scalar int `json:"scalar"`
Value *int `json:"value,string"`
}