github.com/batchcorp/thrift-iterator@v0.0.0-20220918180557-4c4a158fc6e9/test/api/raw_message_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"github.com/stretchr/testify/require"
     6  	"github.com/batchcorp/thrift-iterator"
     7  	"github.com/batchcorp/thrift-iterator/general"
     8  	"fmt"
     9  	"github.com/batchcorp/thrift-iterator/raw"
    10  	"github.com/batchcorp/thrift-iterator/protocol"
    11  )
    12  
    13  func Test_decode_struct_of_raw_message(t *testing.T) {
    14  	should := require.New(t)
    15  	api := thrifter.Config{Protocol: thrifter.ProtocolBinary, StaticCodegen: false}.Froze()
    16  	output, err := api.Marshal(general.Struct{
    17  		0: general.Map{
    18  			"key1": "value1",
    19  		},
    20  		1: "hello",
    21  	})
    22  	should.Nil(err)
    23  	rawStruct := raw.Struct{}
    24  	should.NoError(api.Unmarshal(output, &rawStruct))
    25  	// parse arg1
    26  	var arg1 string
    27  	should.NoError(api.Unmarshal(rawStruct[protocol.FieldId(1)].Buffer, &arg1))
    28  	should.Equal("hello", arg1)
    29  	// parse arg0
    30  	var arg0 map[string]string
    31  	should.NoError(api.Unmarshal(rawStruct[protocol.FieldId(0)].Buffer, &arg0))
    32  	should.Equal(map[string]string{"key1": "value1"}, arg0)
    33  	// modify arg0
    34  	arg0["key2"] = "value2"
    35  	encodedArg0, err := api.Marshal(arg0)
    36  	should.NoError(err)
    37  	// set arg0 back
    38  	rawStruct[protocol.FieldId(0)] = raw.StructField{
    39  		Buffer: encodedArg0,
    40  		Type:   protocol.TypeMap,
    41  	}
    42  	encodedArgs, err := api.Marshal(rawStruct)
    43  	should.NoError(err)
    44  	// verify it is changed
    45  	var val general.Struct
    46  	should.NoError(api.Unmarshal(encodedArgs, &val))
    47  	fmt.Println(val)
    48  }