-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
1307 lines (1248 loc) · 30.9 KB
/
writer_test.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
// Copyright (C) 2023 by Posit Software, PBC
package rsf
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"math"
"reflect"
"testing"
"github.com/stretchr/testify/suite"
)
type WriterSuite struct {
suite.Suite
}
func TestWriterSuite(t *testing.T) {
suite.Run(t, &WriterSuite{})
}
func (s *WriterSuite) TestNewWriter() {
buf := &bytes.Buffer{}
w := NewWriterWithVersion(buf, Version2)
s.Assert().Equal(&rsfWriter{writer: buf, version: Version2}, w)
}
func (s *WriterSuite) TestDiscreteWrites() {
buf := &bytes.Buffer{}
w := NewWriterWithVersion(buf, Version2)
sz, err := w.WriteSizeField(0, 4567, buf)
s.Assert().Nil(err)
s.Assert().Equal(4, sz)
// Hex 11d7 equals 4567.
s.Assert().Equal([]byte{0xd7, 0x11, 0x0, 0x0}, buf.Bytes())
buf.Reset()
sz, err = w.WriteFixedStringField(0, 15, "package-manager-too-long", buf)
s.Assert().ErrorContains(err, "size 24 does not match expected size 15")
sz, err = w.WriteFixedStringField(0, 15, "package-manager", buf)
s.Assert().Nil(err)
s.Assert().Equal(15, sz)
s.Assert().Equal([]byte{0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2d, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x72}, buf.Bytes())
buf.Reset()
sz, err = w.WriteStringField(0, "package-manager", buf)
s.Assert().Nil(err)
s.Assert().Equal(19, sz)
// The leading 4 bytes (f000) indicate the size of 15 for the string.
// The following 15 bytes are the same as the fixed string bytes for "package-manager", above.
s.Assert().Equal([]byte{0xf, 0x0, 0x0, 0x0, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2d, 0x6d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x72}, buf.Bytes())
buf.Reset()
// Write `true`.
sz, err = w.WriteBoolField(0, true, buf)
s.Assert().Nil(err)
s.Assert().Equal(1, sz)
s.Assert().Equal([]byte{0x1}, buf.Bytes())
// Append `false`.
sz, err = w.WriteBoolField(sz, false, buf)
s.Assert().Nil(err)
s.Assert().Equal(2, sz)
s.Assert().Equal([]byte{0x1, 0x0}, buf.Bytes())
// Test max int64
buf.Reset()
sz, err = w.WriteInt64Field(0, math.MaxInt64, buf)
s.Assert().Nil(err)
s.Assert().Equal(10, sz)
s.Assert().Equal([]byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1}, buf.Bytes())
intVal, _ := binary.Varint(buf.Bytes())
s.Assert().Equal(int64(math.MaxInt64), intVal)
// Test float
buf.Reset()
sz, err = w.WriteFloatField(0, 697828.28977, buf)
s.Assert().Nil(err)
s.Assert().Equal(8, sz)
s.Assert().Equal([]byte{0xc3, 0xbb, 0x5c, 0x94, 0xc8, 0x4b, 0x25, 0x41}, buf.Bytes())
s.Assert().Equal(697828.28977, math.Float64frombits(binary.LittleEndian.Uint64(buf.Bytes())))
// Test max float
buf.Reset()
sz, err = w.WriteFloatField(0, math.MaxFloat64, buf)
s.Assert().Nil(err)
s.Assert().Equal(8, sz)
s.Assert().Equal([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f}, buf.Bytes())
s.Assert().Equal(math.MaxFloat64, math.Float64frombits(binary.LittleEndian.Uint64(buf.Bytes())))
}
func (s *WriterSuite) TestInternalWriteString() {
buf := &bytes.Buffer{}
w := NewWriterWithVersion(buf, Version2)
// Write a variable-length string
t := &tag{}
sz, err := w.(*rsfWriter).writeString("test", t, buf)
s.Assert().Nil(err)
s.Assert().Equal(8, sz)
// Switch to fixed-length string
t.fixed = 8
_, err = w.(*rsfWriter).writeString("test", t, buf)
s.Assert().ErrorContains(err, "size 4 does not match expected size 8")
sz, err = w.(*rsfWriter).writeString("test-now", t, buf)
s.Assert().Nil(err)
s.Assert().Equal(8, sz)
s.Assert().Equal([]byte{
// 4 bytes in length (header for variable-length string)
0x4, 0x0, 0x0, 0x0,
// "test"
0x74, 0x65, 0x73, 0x74,
// "test-again" (8 byte fixed-length string)
0x74, 0x65, 0x73, 0x74, 0x2d, 0x6e, 0x6f, 0x77,
}, buf.Bytes())
}
func (s *WriterSuite) TestInternalWriteArray() {
buf := &bytes.Buffer{}
w := NewWriterWithVersion(buf, Version2)
a := []struct {
Date string `rsf:"date,fixed:10"`
Name string `rsf:"name"`
Verified bool `rsf:"verified"`
}{
{
Date: "2020-10-01-mistake",
Name: "From 2020",
Verified: false,
},
{
Date: "2021-03-21",
Name: "From 2021",
Verified: true,
},
{
Date: "2022-12-15",
Name: "this is from 2022",
Verified: true,
},
}
t := &tag{
index: "date",
indexSz: 10,
}
// Error
_, err := w.(*rsfWriter).writeArray(reflect.ValueOf(a), t, buf)
s.Assert().ErrorContains(err, "size 18 does not match expected size 10")
// Fix error
a[0].Date = "2020-10-01"
buf.Reset()
sz, err := w.(*rsfWriter).writeArray(reflect.ValueOf(a), t, buf)
s.Assert().Nil(err)
s.Assert().Equal(130, sz)
s.Assert().Equal(130, buf.Len())
s.Assert().Equal([]byte{
//
// Array Header
//
// Array is 130 bytes in size
0x82, 0x0, 0x0, 0x0,
//
// Array has 3 elements
0x3, 0x0, 0x0, 0x0,
//
// Array Index
//
// 2020-10-01 index entry
0x32, 0x30, 0x32, 0x30, 0x2d, 0x31, 0x30, 0x2d, 0x30, 0x31,
// Record is 24 bytes in size
0x18, 0x0, 0x0, 0x0,
// 2021-03-21 index entry
0x32, 0x30, 0x32, 0x31, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31,
// Record is 24 bytes in size
0x18, 0x0, 0x0, 0x0,
// 2022-12-15 index entry
0x32, 0x30, 0x32, 0x32, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x35,
// Record is 32 bytes in size
0x20, 0x0, 0x0, 0x0,
//
// Array data
//
// 2020-10-01 (fixed-length string)
0x32, 0x30, 0x32, 0x30, 0x2d, 0x31, 0x30, 0x2d, 0x30, 0x31,
// 9 byte variable-length string
0x9, 0x0, 0x0, 0x0,
// "From 2020"
0x46, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x30,
// false
0x0,
// 2021-03-21 (fixed-length string)
0x32, 0x30, 0x32, 0x31, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31,
// 9 byte variable-length string
0x9, 0x0, 0x0, 0x0,
// "From 2021"
0x46, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x31,
// true
0x1,
// 2022-12-15 (fixed-length string)
0x32, 0x30, 0x32, 0x32, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x35,
// 17 byte variable-length string
0x11, 0x0, 0x0, 0x0,
// "this is from 2022"
0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x32,
// true
0x1,
}, buf.Bytes())
}
func (s *WriterSuite) TestWriteObjectWithArrayIndex() {
buf := &bytes.Buffer{}
w := NewWriterWithVersion(buf, Version2)
type snap struct {
// Skip this field since we can determine it from the array index:
//
// Since the `List []snap` struct tag includes `index:date`, the
// array will be indexed using the value of this field. Since the
// date is written in the index, there's no need to write it again
// when serializing each array element, so we include `skip` here.
Date string `rsf:"date,skip,fixed:10"`
Name string `rsf:"name"`
Verified bool `rsf:"verified"`
Skip string `rsf:"-"`
Aliases []string `rsf:"aliases"`
}
a := struct {
Skip string `rsf:"-"`
Company string `rsf:"company"`
Ready bool `rsf:"ready"`
List []snap `rsf:"list,index:date"`
Age int `rsf:"age"`
Rating float64 `rsf:"rating"`
}{
Company: "posit",
Ready: true,
Age: 55,
Rating: 92.689,
List: []snap{
{
Date: "2020-10-01",
Name: "From 2020",
Aliases: []string{"from 2020", "before 2021"},
Verified: false,
},
{
Date: "2021-03-21",
Name: "From 2021",
Verified: true,
},
{
Date: "2022-12-15",
Name: "this is from 2022",
Aliases: []string{"from 2022"},
Verified: true,
},
},
}
sz, err := w.WriteObject(a)
s.Assert().Nil(err)
// Object should use 338 bytes.
s.Assert().Equal(338, sz)
s.Assert().Len(buf.Bytes(), 338)
// Verify bytes.
s.Assert().Equal([]byte{
//
// Object index header
//
// Index version
0x0, 0x8, 0x32,
//
// Full size of index header
0x8a, 0x0, 0x0, 0x0,
//
// Fields Index
//
// "company" field is 7 bytes in length
0x7, 0x0, 0x0, 0x0,
// "company" field name
0x63, 0x6f, 0x6d, 0x70, 0x61, 0x6e, 0x79,
// "company" field type 1 indicates variable-length
0x1, 0x0, 0x0, 0x0,
//
// "ready" field is 5 bytes in length
0x5, 0x0, 0x0, 0x0,
// "ready" field name
0x72, 0x65, 0x61, 0x64, 0x79,
// "ready" field type 3 indicates boolean
0x3, 0x0, 0x0, 0x0,
//
// "list" field is 4 bytes in length
0x4, 0x0, 0x0, 0x0,
// "list field name
0x6c, 0x69, 0x73, 0x74,
// "list" field type 4 indicates array
0x4, 0x0, 0x0, 0x0,
// is indexed
0x1,
// index type is string
0x18, 0x0, 0x0, 0x0,
// index size is 10
0xa, 0x0, 0x0, 0x0,
// Array type is struct
0x19, 0x0, 0x0, 0x0,
// "list" field has 3 subfields
0x3, 0x0, 0x0, 0x0,
//
// "name" field is 4 bytes in length
0x4, 0x0, 0x0, 0x0,
// "name" field name
0x6e, 0x61, 0x6d, 0x65,
// "name" field type 1 indicates variable length
0x1, 0x0, 0x0, 0x0,
//
// "verified" field is 8 bytes in length
0x8, 0x0, 0x0, 0x0,
// "verified" field name
0x76, 0x65, 0x72, 0x69, 0x66, 0x69, 0x65, 0x64,
// "verified" field type 3 indicates boolean
0x3, 0x0, 0x0, 0x0,
//
// "aliases" field is 7 bytes in length
0x7, 0x0, 0x0, 0x0,
// "aliases" field name
0x61, 0x6c, 0x69, 0x61, 0x73, 0x65, 0x73,
// "verified" field type 4 indicates array
0x4, 0x0, 0x0, 0x0,
// not indexed
0x0,
// Array is string type.
0x18, 0x0, 0x0, 0x0,
// "verified" has zero subfields
0x0, 0x0, 0x0, 0x0,
//
// "age" field is 3 bytes in length
0x3, 0x0, 0x0, 0x0,
// "age" field name
0x61, 0x67, 0x65,
// "age field type 7 indicates int64
0x7, 0x0, 0x0, 0x0,
//
// "rating" field is 6 bytes in length
0x6, 0x0, 0x0, 0x0,
// "rating" field name
0x72, 0x61, 0x74, 0x69, 0x6e, 0x67,
// "rating field type 6 indicates float
0x6, 0x0, 0x0, 0x0,
//
// -- End of 72-byte object index header ---
//
// Object header
//
// Object size
0xc5, 0x0, 0x0, 0x0,
//
// 5 byte variable-length string
0x5, 0x0, 0x0, 0x0,
// "posit"
0x70, 0x6f, 0x73, 0x69, 0x74,
// ready:true
0x1,
//
// Array Header
//
// Array size
0xa5, 0x0, 0x0, 0x0,
//
// Array has 3 elements
0x3, 0x0, 0x0, 0x0,
//
// Array Index
//
// 2020-10-01 index entry
0x32, 0x30, 0x32, 0x30, 0x2d, 0x31, 0x30, 0x2d, 0x30, 0x31,
// Record size
0x32, 0x0, 0x0, 0x0,
// 2021-03-21 index entry
0x32, 0x30, 0x32, 0x31, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31,
// Record size
0x16, 0x0, 0x0, 0x0,
// 2022-12-15 index entry
0x32, 0x30, 0x32, 0x32, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x35,
// Record size
0x2b, 0x0, 0x0, 0x0,
//
// Array data
//
// 9 byte variable-length string
0x9, 0x0, 0x0, 0x0,
// "From 2020"
0x46, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x30,
// verified:false
0x0,
//
// "aliases" array size
0x24, 0x0, 0x0, 0x0,
//
// "aliases" array length
0x2, 0x0, 0x0, 0x0,
//
// "aliases" array data
//
// element 1 size
0x9, 0x0, 0x0, 0x0,
// "from 2020"
0x66, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x30,
//
// element 2 size
0xb, 0x0, 0x0, 0x0,
// "before 2021"
0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x20, 0x32, 0x30, 0x32, 0x31,
// 9 byte variable-length string
0x9, 0x0, 0x0, 0x0,
// "From 2021"
0x46, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x31,
// verified:true
0x1,
//
// "aliases" array size
0x8, 0x0, 0x0, 0x0,
//
// "aliases" array length (this is a zero length array)
0x0, 0x0, 0x0, 0x0,
//
// 17 byte variable-length string
0x11, 0x0, 0x0, 0x0,
// "this is from 2022"
0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x32,
// verified:true
0x1,
//
// "aliases" array size
0x15, 0x0, 0x0, 0x0,
//
// "aliases" array length
0x1, 0x0, 0x0, 0x0,
//
// "aliases" array data
//
// element 1 size
0x9, 0x0, 0x0, 0x0,
// "from 2022"
0x66, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x32,
//
// Age: 55
0x6e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
// Rating: 92.689
0x6a, 0xbc, 0x74, 0x93, 0x18, 0x2c, 0x57, 0x40,
//
// -- End of object --
}, buf.Bytes())
}
// This is the same as `TestWriteObjectWithArrayIndex` except that
// the array index is an integer value.
func (s *WriterSuite) TestWriteObjectWithArrayIntIndex() {
buf := &bytes.Buffer{}
w := NewWriterWithVersion(buf, Version2)
type snap struct {
// Skip this field since we can determine it from the array index:
//
// Since the `List []snap` struct tag includes `index:date`, the
// array will be indexed using the value of this field. Since the
// date is written in the index, there's no need to write it again
// when serializing each array element, so we include `skip` here.
Date int64 `rsf:"date,skip"`
Name string `rsf:"name"`
Verified bool `rsf:"verified"`
Skip string `rsf:"-"`
Aliases []string `rsf:"aliases"`
}
a := struct {
Skip string `rsf:"-"`
Company string `rsf:"company"`
Ready bool `rsf:"ready"`
List []snap `rsf:"list,index:date"`
Age int `rsf:"age"`
Rating float64 `rsf:"rating"`
}{
Company: "posit",
Ready: true,
Age: 55,
Rating: 92.689,
List: []snap{
{
Date: 20201001,
Name: "From 2020",
Aliases: []string{"from 2020", "before 2021"},
Verified: false,
},
{
Date: 20210321,
Name: "From 2021",
Verified: true,
},
{
Date: 20221215,
Name: "this is from 2022",
Aliases: []string{"from 2022"},
Verified: true,
},
},
}
sz, err := w.WriteObject(a)
s.Assert().Nil(err)
// Object should use 338 bytes.
s.Assert().Equal(338, sz)
s.Assert().Len(buf.Bytes(), 338)
// Verify bytes.
s.Assert().Equal([]byte{
//
// Object index header
//
// Index version
0x0, 0x8, 0x32,
//
// Full size of index header
0x8a, 0x0, 0x0, 0x0,
//
// Fields Index
//
// "company" field is 7 bytes in length
0x7, 0x0, 0x0, 0x0,
// "company" field name
0x63, 0x6f, 0x6d, 0x70, 0x61, 0x6e, 0x79,
// "company" field type 1 indicates variable-length
0x1, 0x0, 0x0, 0x0,
//
// "ready" field is 5 bytes in length
0x5, 0x0, 0x0, 0x0,
// "ready" field name
0x72, 0x65, 0x61, 0x64, 0x79,
// "ready" field type 3 indicates boolean
0x3, 0x0, 0x0, 0x0,
//
// "list" field is 4 bytes in length
0x4, 0x0, 0x0, 0x0,
// "list field name
0x6c, 0x69, 0x73, 0x74,
// "list" field type 4 indicates array
0x4, 0x0, 0x0, 0x0,
// indexed
0x1,
// index type int64
0x6, 0x0, 0x0, 0x0,
// index size 10
0xa, 0x0, 0x0, 0x0,
// array type struct
0x19, 0x0, 0x0, 0x0,
// "list" field has 3 subfields
0x3, 0x0, 0x0, 0x0,
//
// "name" field is 4 bytes in length
0x4, 0x0, 0x0, 0x0,
// "name" field name
0x6e, 0x61, 0x6d, 0x65,
// "name" field type 1 indicates variable length
0x1, 0x0, 0x0, 0x0,
//
// "verified" field is 8 bytes in length
0x8, 0x0, 0x0, 0x0,
// "verified" field name
0x76, 0x65, 0x72, 0x69, 0x66, 0x69, 0x65, 0x64,
// "verified" field type 3 indicates boolean
0x3, 0x0, 0x0, 0x0,
//
// "aliases" field is 7 bytes in length
0x7, 0x0, 0x0, 0x0,
// "aliases" field name
0x61, 0x6c, 0x69, 0x61, 0x73, 0x65, 0x73,
// "verified" field type 4 indicates array
0x4, 0x0, 0x0, 0x0,
// not indexed
0x0,
// string array type
0x18, 0x0, 0x0, 0x0,
// "verified" has zero subfields
0x0, 0x0, 0x0, 0x0,
//
// "age" field is 3 bytes in length
0x3, 0x0, 0x0, 0x0,
// "age" field name
0x61, 0x67, 0x65,
// "age field type 7 indicates int64
0x7, 0x0, 0x0, 0x0,
//
// "rating" field is 6 bytes in length
0x6, 0x0, 0x0, 0x0,
// "rating" field name
0x72, 0x61, 0x74, 0x69, 0x6e, 0x67,
// "rating field type 6 indicates float
0x6, 0x0, 0x0, 0x0,
//
// -- End of 72-byte object index header ---
//
// Object header
//
// Object size
0xc5, 0x0, 0x0, 0x0,
//
// 5 byte variable-length string
0x5, 0x0, 0x0, 0x0,
// "posit"
0x70, 0x6f, 0x73, 0x69, 0x74,
// ready:true
0x1,
//
// Array Header
//
// Array size
0xa5, 0x0, 0x0, 0x0,
//
// Array has 3 elements
0x3, 0x0, 0x0, 0x0,
//
// Array Index
//
// 20201001 index entry
0xd2, 0xf8, 0xa1, 0x13, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
// Record size
0x32, 0x0, 0x0, 0x0,
// 20210321 index entry
0xa2, 0x8a, 0xa3, 0x13, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
// Record size
0x16, 0x0, 0x0, 0x0,
// 20221215 index entry
0xbe, 0xb4, 0xa4, 0x13, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
// Record size
0x2b, 0x0, 0x0, 0x0,
//
// Array data
//
// 9 byte variable-length string
0x9, 0x0, 0x0, 0x0,
// "From 2020"
0x46, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x30,
// verified:false
0x0,
//
// "aliases" array size
0x24, 0x0, 0x0, 0x0,
//
// "aliases" array length
0x2, 0x0, 0x0, 0x0,
//
// "aliases" array data
//
// element 1 size
0x9, 0x0, 0x0, 0x0,
// "from 2020"
0x66, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x30,
//
// element 2 size
0xb, 0x0, 0x0, 0x0,
// "before 2021"
0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x20, 0x32, 0x30, 0x32, 0x31,
// 9 byte variable-length string
0x9, 0x0, 0x0, 0x0,
// "From 2021"
0x46, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x31,
// verified:true
0x1,
//
// "aliases" array size
0x8, 0x0, 0x0, 0x0,
//
// "aliases" array length (this is a zero length array)
0x0, 0x0, 0x0, 0x0,
//
// 17 byte variable-length string
0x11, 0x0, 0x0, 0x0,
// "this is from 2022"
0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x32,
// verified:true
0x1,
//
// "aliases" array size
0x15, 0x0, 0x0, 0x0,
//
// "aliases" array length
0x1, 0x0, 0x0, 0x0,
//
// "aliases" array data
//
// element 1 size
0x9, 0x0, 0x0, 0x0,
// "from 2022"
0x66, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x32,
//
// Age: 55
0x6e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
// Rating: 92.689
0x6a, 0xbc, 0x74, 0x93, 0x18, 0x2c, 0x57, 0x40,
//
// -- End of object --
}, buf.Bytes())
}
func (s *WriterSuite) TestWriteObjectWithArrayIndexNilSubArray() {
buf := &bytes.Buffer{}
w := NewWriterWithVersion(buf, Version2)
type snap struct {
// Skip this field since we can determine it from the array index:
//
// Since the `List []snap` struct tag includes `index:date`, the
// array will be indexed using the value of this field. Since the
// date is written in the index, there's no need to write it again
// when serializing each array element, so we include `skip` here.
Date string `rsf:"date,skip,fixed:10"`
Name string `rsf:"name"`
Verified bool `rsf:"verified"`
Skip string `rsf:"-"`
}
a := struct {
Skip string `rsf:"-"`
Company string `rsf:"company"`
Ready bool `rsf:"ready"`
List []snap `rsf:"list,index:date"`
Age int `rsf:"age"`
Rating float64 `rsf:"rating"`
}{
Company: "posit",
Ready: true,
Age: 55,
Rating: 92.689,
}
sz, err := w.WriteObject(a)
s.Assert().Nil(err)
// Object should use 157 bytes.
s.Assert().Equal(157, sz)
s.Assert().Len(buf.Bytes(), 157)
// Verify bytes.
s.Assert().Equal([]byte{
//
// Object index header
//
// Index version
0x0, 0x8, 0x32,
//
// Full size of index header
0x72, 0x0, 0x0, 0x0,
//
// Fields Index
//
// "company" field is 7 bytes in length
0x7, 0x0, 0x0, 0x0,
// "company" field name
0x63, 0x6f, 0x6d, 0x70, 0x61, 0x6e, 0x79,
// "company" field type 1 indicates variable-length
0x1, 0x0, 0x0, 0x0,
//
// "ready" field is 5 bytes in length
0x5, 0x0, 0x0, 0x0,
// "ready" field name
0x72, 0x65, 0x61, 0x64, 0x79,
// "ready" field type 2 indicates boolean
0x3, 0x0, 0x0, 0x0,
//
// "list" field is 4 bytes in length
0x4, 0x0, 0x0, 0x0,
// "list field name
0x6c, 0x69, 0x73, 0x74,
// "list" field type 4 indicates array
0x4, 0x0, 0x0, 0x0,
// indexed
0x1,
// index field type is string
0x18, 0x0, 0x0, 0x0,
// index size 10
0xa, 0x0, 0x0, 0x0,
// array type is struct
0x19, 0x0, 0x0, 0x0,
// "list" field has 2 subfields
0x2, 0x0, 0x0, 0x0,
//
// "name" field is 4 bytes in length
0x4, 0x0, 0x0, 0x0,
// "name" field name
0x6e, 0x61, 0x6d, 0x65,
// "name" field type 1 indicates variable length
0x1, 0x0, 0x0, 0x0,
//
// "verified" field is 8 bytes in length
0x8, 0x0, 0x0, 0x0,
// "verified" field name
0x76, 0x65, 0x72, 0x69, 0x66, 0x69, 0x65, 0x64,
// "verified" field type 3 indicates boolean
0x3, 0x0, 0x0, 0x0,
//
// "age" field is 3 bytes in length
0x3, 0x0, 0x0, 0x0,
// "age" field name
0x61, 0x67, 0x65,
// "age field type 7 indicates int64
0x7, 0x0, 0x0, 0x0,
//
// "rating" field is 6 bytes in length
0x6, 0x0, 0x0, 0x0,
// "rating" field name
0x72, 0x61, 0x74, 0x69, 0x6e, 0x67,
// "rating field type 6 indicates float
0x6, 0x0, 0x0, 0x0,
//
// -- End of index header ---
//
// Object header
//
// Object size
0x28, 0x0, 0x0, 0x0,
//
// 5 byte variable-length string
0x5, 0x0, 0x0, 0x0,
// "posit"
0x70, 0x6f, 0x73, 0x69, 0x74,
// ready:true
0x1,
//
// Array Header
//
// Array is 8 bytes in size
0x8, 0x0, 0x0, 0x0,
//
// Array has zero elements
0x0, 0x0, 0x0, 0x0,
//
// Age: 55
0x6e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
// Rating: 92.689
0x6a, 0xbc, 0x74, 0x93, 0x18, 0x2c, 0x57, 0x40,
//
// -- End of object --
}, buf.Bytes())
}
func (s *WriterSuite) TestWriteObjectNoArrayIndex() {
buf := &bytes.Buffer{}
w := NewWriterWithVersion(buf, Version2)
type snap struct {
Skip string `rsf:"-"`
// In the `TestWriteObjectWithArrayIndex` test, we included `skip` in the
// `Date` struct tag. Here, since we don't use an array index, we don't skip
// this field since it must be serialized with each array element.
Date string `rsf:"date,fixed:10"`
Name string `rsf:"name"`
Verified bool `rsf:"verified"`
}
a := struct {
Skip string `rsf:"-"`
Company string `rsf:"company"`
Ready bool `rsf:"ready"`
List []snap `rsf:"list"`
}{
Company: "posit",
Ready: true,
List: []snap{
{
Date: "2020-10-01",
Name: "From 2020",
Verified: false,
},
{
Date: "2021-03-21",
Name: "From 2021",
Verified: true,
},
{
Date: "2022-12-15",
Name: "this is from 2022",
Verified: true,
},
},
}
sz, err := w.WriteObject(a)
s.Assert().Nil(err)
// Object should use 202 bytes since there is no array index
s.Assert().Equal(202, sz)
s.Assert().Len(buf.Bytes(), 202)
// Verify bytes.
s.Assert().Equal([]byte{
//
// Object index header
//
// Index version
0x0, 0x8, 0x32,
//
// Full size of index header
0x61, 0x0, 0x0, 0x0,
//
// Fields Index
//
// "company" field is 7 bytes in length
0x7, 0x0, 0x0, 0x0,
// "company" field name
0x63, 0x6f, 0x6d, 0x70, 0x61, 0x6e, 0x79,
// "company" field type 1 indicates variable-length
0x1, 0x0, 0x0, 0x0,
//
// "ready" field is 5 bytes in length
0x5, 0x0, 0x0, 0x0,
// "ready" field name
0x72, 0x65, 0x61, 0x64, 0x79,
// "ready" field type 3 indicates boolean
0x3, 0x0, 0x0, 0x0,
//
// "list" field is 4 bytes in length
0x4, 0x0, 0x0, 0x0,
// "list field name
0x6c, 0x69, 0x73, 0x74,
// "list" field type 4 indicates array
0x4, 0x0, 0x0, 0x0,
// not indexed
0x0,
// Field type is struct.
0x19, 0x0, 0x0, 0x0,
// "list" array has 3 subfields
0x3, 0x0, 0x0, 0x0,
//
// "date" field is 4 bytes in length
0x4, 0x0, 0x0, 0x0,
// "date" field name
0x64, 0x61, 0x74, 0x65,
// "date" field type 2 indicates fixed-length string
0x2, 0x0, 0x0, 0x0,
// "date" field is of fixed size 10
0xa, 0x0, 0x0, 0x0,
//
// "name" field is 4 bytes in length
0x4, 0x0, 0x0, 0x0,
// "name" field name
0x6e, 0x61, 0x6d, 0x65,
// "name" field type 1 indicates variable length
0x1, 0x0, 0x0, 0x0,
//
// "verified" field is 8 bytes in length
0x8, 0x0, 0x0, 0x0,
// "verified" field name
0x76, 0x65, 0x72, 0x69, 0x66, 0x69, 0x65, 0x64,
// "verified" field type 3 indicates boolean
0x3, 0x0, 0x0, 0x0,
//
// -- End of 84-byte object index header ---
//
// Object header
//
// Object size is 102 bytes
0x66, 0x0, 0x0, 0x0,
// 5 byte variable-length string
0x5, 0x0, 0x0, 0x0,
// "posit"
0x70, 0x6f, 0x73, 0x69, 0x74,
// ready:true
0x1,
//
// Array Header
//
// Array is 88 bytes in size
0x58, 0x0, 0x0, 0x0,
//
// Array has 3 elements
0x3, 0x0, 0x0, 0x0,
//
// Array data
//
// 2020-10-01 (fixed-length string)
0x32, 0x30, 0x32, 0x30, 0x2d, 0x31, 0x30, 0x2d, 0x30, 0x31,
// 9 byte variable-length string
0x9, 0x0, 0x0, 0x0,
// "From 2020"
0x46, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x30,
// verified:false
0x0,
// 2021-03-21 (fixed-length string)
0x32, 0x30, 0x32, 0x31, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31,
// 9 byte variable-length string
0x9, 0x0, 0x0, 0x0,
// "From 2021"
0x46, 0x72, 0x6f, 0x6d, 0x20, 0x32, 0x30, 0x32, 0x31,
// verified:true
0x1,
// 2022-12-15 (fixed-length string)
0x32, 0x30, 0x32, 0x32, 0x2d, 0x31, 0x32, 0x2d, 0x31, 0x35,
// 17 byte variable-length string