23
23
TestBigArray ,
24
24
TestPointerToStruct ,
25
25
TestSliceOfStruct ,
26
+ TestStructWithPtr ,
27
+ TestStructWithPtrNil ,
26
28
TestInterface ,
27
29
TestSliceOfInterface ,
28
30
TestArrayOfInterface ,
@@ -63,7 +65,8 @@ type Foo interface {
63
65
}
64
66
65
67
type MyInnerStruct struct {
66
- Str string `serialize:"true"`
68
+ Str string `serialize:"true"`
69
+ NumberNotProvided * int32 `serialize:"true":121`
67
70
}
68
71
69
72
func (* MyInnerStruct ) Foo () int {
@@ -86,6 +89,11 @@ type MyInnerStruct3 struct {
86
89
F Foo `serialize:"true"`
87
90
}
88
91
92
+ type MyStructWithPtr struct {
93
+ N1 * int32 `serialize:"true"`
94
+ N2 * int64 `serialize:"true"`
95
+ }
96
+
89
97
type myStruct struct {
90
98
InnerStruct MyInnerStruct `serialize:"true"`
91
99
InnerStruct2 * MyInnerStruct `serialize:"true"`
@@ -145,21 +153,23 @@ func TestStruct(codec GeneralCodec, t testing.TB) {
145
153
myMap7 ["key" ] = "value"
146
154
myMap7 [int32 (1 )] = int32 (2 )
147
155
156
+ number := int32 (8 )
157
+
148
158
myStructInstance := myStruct {
149
- InnerStruct : MyInnerStruct {"hello" },
150
- InnerStruct2 : & MyInnerStruct {"yello" },
159
+ InnerStruct : MyInnerStruct {"hello" , nil },
160
+ InnerStruct2 : & MyInnerStruct {"yello" , nil },
151
161
Member1 : 1 ,
152
162
Member2 : 2 ,
153
163
MySlice : []byte {1 , 2 , 3 , 4 },
154
164
MySlice2 : []string {"one" , "two" , "three" },
155
- MySlice3 : []MyInnerStruct {{"abc" }, {"ab" }, {"c" }},
165
+ MySlice3 : []MyInnerStruct {{"abc" , nil }, {"ab" , & number }, {"c" , nil }},
156
166
MySlice4 : []* MyInnerStruct2 {{true }, {}},
157
167
MySlice5 : []Foo {& MyInnerStruct2 {true }, & MyInnerStruct2 {}},
158
168
MyArray : [4 ]byte {5 , 6 , 7 , 8 },
159
169
MyArray2 : [5 ]string {"four" , "five" , "six" , "seven" },
160
- MyArray3 : [3 ]MyInnerStruct {{"d" }, {"e" }, {"f" }},
170
+ MyArray3 : [3 ]MyInnerStruct {{"d" , nil }, {"e" , nil }, {"f" , nil }},
161
171
MyArray4 : [2 ]* MyInnerStruct2 {{}, {true }},
162
- MyInterface : & MyInnerStruct {"yeet" },
172
+ MyInterface : & MyInnerStruct {"yeet" , & number },
163
173
InnerStruct3 : MyInnerStruct3 {
164
174
Str : "str" ,
165
175
M1 : MyInnerStruct {
@@ -414,20 +424,70 @@ func TestPointerToStruct(codec GeneralCodec, t testing.TB) {
414
424
require .Equal (myPtr , myPtrUnmarshaled )
415
425
}
416
426
427
+ func TestStructWithPtr (codec GeneralCodec , t testing.TB ) {
428
+ require := require .New (t )
429
+ n1 := int32 (5 )
430
+ n2 := int64 (10 )
431
+ struct1 := MyStructWithPtr {
432
+ N1 : & n1 ,
433
+ N2 : & n2 ,
434
+ }
435
+
436
+ require .NoError (codec .RegisterType (& MyStructWithPtr {}))
437
+ manager := NewDefaultManager ()
438
+ require .NoError (manager .RegisterCodec (0 , codec ))
439
+
440
+ bytes , err := manager .Marshal (0 , struct1 )
441
+ require .NoError (err )
442
+
443
+ bytesLen , err := manager .Size (0 , struct1 )
444
+ require .NoError (err )
445
+ require .Len (bytes , bytesLen )
446
+
447
+ var struct1Unmarshaled MyStructWithPtr
448
+ version , err := manager .Unmarshal (bytes , & struct1Unmarshaled )
449
+ require .NoError (err )
450
+ require .Zero (version )
451
+ require .Equal (struct1 , struct1Unmarshaled )
452
+ }
453
+
454
+ func TestStructWithPtrNil (codec GeneralCodec , t testing.TB ) {
455
+ require := require .New (t )
456
+ struct1 := MyStructWithPtr {}
457
+
458
+ require .NoError (codec .RegisterType (& MyStructWithPtr {}))
459
+ manager := NewDefaultManager ()
460
+ require .NoError (manager .RegisterCodec (0 , codec ))
461
+
462
+ bytes , err := manager .Marshal (0 , struct1 )
463
+ require .NoError (err )
464
+
465
+ bytesLen , err := manager .Size (0 , struct1 )
466
+ require .NoError (err )
467
+ require .Len (bytes , bytesLen )
468
+
469
+ var struct1Unmarshaled MyStructWithPtr
470
+ version , err := manager .Unmarshal (bytes , & struct1Unmarshaled )
471
+ require .NoError (err )
472
+ require .Zero (version )
473
+ require .Equal (struct1 , struct1Unmarshaled )
474
+ }
475
+
417
476
// Test marshalling a slice of structs
418
477
func TestSliceOfStruct (codec GeneralCodec , t testing.TB ) {
419
478
require := require .New (t )
420
-
479
+ n1 := int32 (- 1 )
480
+ n2 := int32 (0xff )
421
481
mySlice := []MyInnerStruct3 {
422
482
{
423
483
Str : "One" ,
424
- M1 : MyInnerStruct {"Two" },
425
- F : & MyInnerStruct {"Three" },
484
+ M1 : MyInnerStruct {"Two" , & n1 },
485
+ F : & MyInnerStruct {"Three" , & n2 },
426
486
},
427
487
{
428
488
Str : "Four" ,
429
- M1 : MyInnerStruct {"Five" },
430
- F : & MyInnerStruct {"Six" },
489
+ M1 : MyInnerStruct {"Five" , nil },
490
+ F : & MyInnerStruct {"Six" , nil },
431
491
},
432
492
}
433
493
require .NoError (codec .RegisterType (& MyInnerStruct {}))
0 commit comments