github.com/batchcorp/thrift-iterator@v0.0.0-20220918180557-4c4a158fc6e9/test/level_2/struct_of_map_test.go (about) 1 package test 2 3 import ( 4 "github.com/apache/thrift/lib/go/thrift" 5 "github.com/batchcorp/thrift-iterator/general" 6 "github.com/batchcorp/thrift-iterator/protocol" 7 "github.com/batchcorp/thrift-iterator/test" 8 "github.com/batchcorp/thrift-iterator/test/level_2/struct_of_map_test" 9 "github.com/stretchr/testify/require" 10 "testing" 11 ) 12 13 func Test_skip_struct_of_map(t *testing.T) { 14 should := require.New(t) 15 for _, c := range test.Combinations { 16 buf, proto := c.CreateProtocol() 17 proto.WriteStructBegin("hello") 18 proto.WriteFieldBegin("field1", thrift.MAP, 1) 19 proto.WriteMapBegin(thrift.I32, thrift.I64, 1) 20 proto.WriteI32(2) 21 proto.WriteI64(2) 22 proto.WriteMapEnd() 23 proto.WriteFieldEnd() 24 proto.WriteFieldStop() 25 proto.WriteStructEnd() 26 iter := c.CreateIterator(buf.Bytes()) 27 should.Equal(buf.Bytes(), iter.SkipStruct(nil)) 28 } 29 } 30 31 func Test_unmarshal_general_struct_of_map(t *testing.T) { 32 should := require.New(t) 33 for _, c := range test.Combinations { 34 buf, proto := c.CreateProtocol() 35 proto.WriteStructBegin("hello") 36 proto.WriteFieldBegin("field1", thrift.MAP, 1) 37 proto.WriteMapBegin(thrift.I32, thrift.I64, 1) 38 proto.WriteI32(2) 39 proto.WriteI64(2) 40 proto.WriteMapEnd() 41 proto.WriteFieldEnd() 42 proto.WriteFieldStop() 43 proto.WriteStructEnd() 44 var val general.Struct 45 should.NoError(c.Unmarshal(buf.Bytes(), &val)) 46 should.Equal(general.Map{ 47 int32(2): int64(2), 48 }, val[protocol.FieldId(1)]) 49 } 50 } 51 52 func Test_unmarshal_struct_of_map(t *testing.T) { 53 should := require.New(t) 54 for _, c := range test.UnmarshalCombinations { 55 buf, proto := c.CreateProtocol() 56 proto.WriteStructBegin("hello") 57 proto.WriteFieldBegin("field1", thrift.MAP, 1) 58 proto.WriteMapBegin(thrift.I32, thrift.I64, 1) 59 proto.WriteI32(2) 60 proto.WriteI64(2) 61 proto.WriteMapEnd() 62 proto.WriteFieldEnd() 63 proto.WriteFieldStop() 64 proto.WriteStructEnd() 65 var val struct_of_map_test.TestObject 66 should.NoError(c.Unmarshal(buf.Bytes(), &val)) 67 should.Equal(struct_of_map_test.TestObject{ 68 map[int32]int64{2: 2}, 69 }, val) 70 } 71 } 72 73 func Test_marshal_general_struct_of_map(t *testing.T) { 74 should := require.New(t) 75 for _, c := range test.Combinations { 76 m := general.Struct{ 77 protocol.FieldId(1): general.Map{ 78 int32(2): int64(2), 79 }, 80 } 81 82 output, err := c.Marshal(m) 83 should.NoError(err) 84 output1, err := c.Marshal(&m) 85 should.NoError(err) 86 should.Equal(output, output1) 87 var val general.Struct 88 should.NoError(c.Unmarshal(output, &val)) 89 should.Equal(general.Map{ 90 int32(2): int64(2), 91 }, val[protocol.FieldId(1)]) 92 } 93 } 94 95 func Test_marshal_struct_of_map(t *testing.T) { 96 should := require.New(t) 97 for _, c := range test.MarshalCombinations { 98 m := struct_of_map_test.TestObject{ 99 map[int32]int64{2: 2}, 100 } 101 102 output, err := c.Marshal(m) 103 should.NoError(err) 104 output1, err := c.Marshal(&m) 105 should.NoError(err) 106 should.Equal(output, output1) 107 var val general.Struct 108 should.NoError(c.Unmarshal(output, &val)) 109 should.Equal(general.Map{ 110 int32(2): int64(2), 111 }, val[protocol.FieldId(1)]) 112 } 113 }