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