github.com/batchcorp/thrift-iterator@v0.0.0-20220918180557-4c4a158fc6e9/test/api/api_test.go (about) 1 package test 2 3 import ( 4 "testing" 5 "encoding/hex" 6 "github.com/stretchr/testify/require" 7 "github.com/batchcorp/thrift-iterator" 8 "fmt" 9 "github.com/batchcorp/thrift-iterator/protocol" 10 "bytes" 11 "github.com/batchcorp/thrift-iterator/general" 12 ) 13 14 type combination struct { 15 encoded string 16 api thrifter.API 17 } 18 19 var combinations = []combination{ 20 { 21 encoded: "800100010000000568656c6c6f0000000c0b00010000000a73657373696f6e2d69640c00020c00010a000100000000000000010a000200000000000000000b00030000000f43616c6c46726f6d496e626f756e64000c00020b0001000000093132372e302e302e310a000200000000000004d2000b00030000000568656c6c6f000c00030c00010a000100000000000000020a000200000000000000000b00030000000d52657475726e496e626f756e64000b000200000005776f726c64000f00040c000000010c00020c00010a000100000000000000020a000200000000000000000b00030000000d52657475726e496e626f756e64000b000200000005776f726c64000000", 22 api: thrifter.Config{Protocol: thrifter.ProtocolBinary}.Froze(), 23 }, 24 { 25 encoded: "82210c0568656c6c6f180a73657373696f6e2d69641c1c16021600180f43616c6c46726f6d496e626f756e64001c18093132372e302e302e3116a41300180568656c6c6f001c1c16041600180d52657475726e496e626f756e64001805776f726c6400191c2c1c16041600180d52657475726e496e626f756e64001805776f726c64000000", 26 api: thrifter.Config{Protocol: thrifter.ProtocolCompact}.Froze(), 27 }, 28 } 29 30 func Test_unmarshal_message(t *testing.T) { 31 should := require.New(t) 32 for _, c := range combinations { 33 input, err := hex.DecodeString(c.encoded) 34 should.NoError(err) 35 var msg general.Message 36 err = c.api.Unmarshal(input, &msg) 37 should.NoError(err) 38 fmt.Println(msg.MessageType) 39 fmt.Println(msg.MessageName) 40 for fieldId, fieldValue := range msg.Arguments { 41 fmt.Println("!!!", fieldId, fieldValue) 42 } 43 } 44 } 45 46 func Test_marshal_message(t *testing.T) { 47 should := require.New(t) 48 msg := general.Message{ 49 MessageHeader: protocol.MessageHeader{ 50 MessageType: protocol.MessageTypeCall, 51 MessageName: "hello", 52 SeqId: protocol.SeqId(17), 53 }, 54 Arguments: general.Struct{ 55 protocol.FieldId(1): int64(1), 56 protocol.FieldId(2): int64(2), 57 }, 58 } 59 output, err := thrifter.Marshal(msg) 60 should.Nil(err) 61 var msgRead general.Message 62 err = thrifter.Unmarshal(output, &msgRead) 63 should.NoError(err) 64 fmt.Println(msgRead.MessageType) 65 fmt.Println(msgRead.MessageName) 66 for fieldId, fieldValue := range msgRead.Arguments { 67 fmt.Println(fieldId, fieldValue) 68 } 69 } 70 71 func Test_decode_message(t *testing.T) { 72 should := require.New(t) 73 input, err := hex.DecodeString("800100010000000568656c6c6f0000000c0b00010000000a73657373696f6e2d69640c00020c00010a000100000000000000010a000200000000000000000b00030000000f43616c6c46726f6d496e626f756e64000c00020b0001000000093132372e302e302e310a000200000000000004d2000b00030000000568656c6c6f000c00030c00010a000100000000000000020a000200000000000000000b00030000000d52657475726e496e626f756e64000b000200000005776f726c64000f00040c000000010c00020c00010a000100000000000000020a000200000000000000000b00030000000d52657475726e496e626f756e64000b000200000005776f726c64000000") 74 should.NoError(err) 75 reader := bytes.NewBuffer(input) 76 cfg := thrifter.Config{Protocol: thrifter.ProtocolBinary}.Froze() 77 decoder := cfg.NewDecoder(reader, nil) 78 var msg general.Message 79 should.NoError(decoder.Decode(&msg)) 80 fmt.Println(msg.MessageType) 81 fmt.Println(msg.MessageName) 82 for fieldId, fieldValue := range msg.Arguments { 83 fmt.Println(fieldId, fieldValue) 84 } 85 } 86 87 func Test_encode_message(t *testing.T) { 88 should := require.New(t) 89 msg := general.Message{ 90 MessageHeader: protocol.MessageHeader{ 91 MessageType: protocol.MessageTypeCall, 92 MessageName: "hello", 93 SeqId: protocol.SeqId(17), 94 }, 95 Arguments: general.Struct{ 96 protocol.FieldId(1): int64(1), 97 protocol.FieldId(2): int64(2), 98 }, 99 } 100 var msgRead general.Message 101 buf := bytes.NewBuffer(nil) 102 cfg := thrifter.Config{Protocol: thrifter.ProtocolBinary}.Froze() 103 encoder := cfg.NewEncoder(buf) 104 should.NoError(encoder.Encode(msg)) 105 err := cfg.Unmarshal(buf.Bytes(), &msgRead) 106 should.NoError(err) 107 fmt.Println(msgRead.MessageType) 108 fmt.Println(msgRead.MessageName) 109 for fieldId, fieldValue := range msgRead.Arguments { 110 fmt.Println(fieldId, fieldValue) 111 } 112 } 113 114 type Foo struct { 115 Sa string `thrift:"Sa,1" json:"Sa"` 116 Ib int32 `thrift:"Ib,2" json:"Ib"` 117 Lc []string `thrift:"Lc,3" json:"Lc"` 118 } 119 120 type Example struct { 121 Name string `thrift:"Name,1" json:"Name"` 122 Ia int64 `thrift:"Ia,2" json:"Ia"` 123 Lb []string `thrift:"Lb,3" json:"Lb"` 124 Mc map[string]*Foo `thrift:"Mc,4" json:"Mc"` 125 } 126 127 func TestPanic(t *testing.T) { 128 var example = Example{ 129 Name: "xxxxxxxxxxxxxxxx", 130 Ia: 12345678, 131 Lb: []string{"a", "b", "c", "d", "1", "2", "3", "4", "5"}, 132 Mc: map[string]*Foo{ 133 "t1": &Foo{Sa: "sss", Ib: 987654321, Lc: []string{"1", "2", "3"}}, 134 }, 135 } 136 _, err := thrifter.Marshal(example) 137 if err != nil { 138 t.Fail() 139 } 140 }