github.com/m3db/m3@v1.5.0/src/dbnode/encoding/proto/custom_marshal_test.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package proto
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  
    27  	"github.com/jhump/protoreflect/dynamic"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestCustomMarshal(t *testing.T) {
    32  	testCases := []struct {
    33  		message *dynamic.Message
    34  	}{
    35  		{
    36  			message: newVL(0, 0, 0, nil, nil),
    37  		},
    38  		{
    39  			message: newVL(1, 1, 1, []byte("some-delivery-id"), nil),
    40  		},
    41  		{
    42  			message: newVL(2, 2, 2, []byte("some-delivery-id-2"), map[string]string{"key": "val"}),
    43  		},
    44  	}
    45  
    46  	marshaller := newCustomMarshaller()
    47  	for i, tc := range testCases {
    48  		t.Run(fmt.Sprintf("test_case_%d", i), func(t *testing.T) {
    49  			marshaller.reset()
    50  			customMarshalVL(t, marshaller, tc.message)
    51  
    52  			unmarshalM := dynamic.NewMessage(testVLSchema)
    53  			require.NoError(t, unmarshalM.Unmarshal(marshaller.bytes()))
    54  
    55  			require.True(t, dynamic.Equal(tc.message, unmarshalM))
    56  		})
    57  	}
    58  }
    59  
    60  func customMarshalVL(t *testing.T, marshaller customFieldMarshaller, m *dynamic.Message) {
    61  	marshaller.encFloat64(1, m.GetFieldByNumber(1).(float64))
    62  	marshaller.encFloat64(2, m.GetFieldByNumber(2).(float64))
    63  	marshaller.encInt64(3, m.GetFieldByNumber(3).(int64))
    64  	marshaller.encBytes(4, m.GetFieldByNumber(4).([]byte))
    65  
    66  	// Fields set to their default value are not marshalled in the Protobuf3 format so to generate
    67  	// the bytes that represent the attributes map we create a new VL message where every field is
    68  	// set to its default value except for the attributes map and then Marshal() it into a byte stream
    69  	// which will create a stream that only includes the attributes field.
    70  	var (
    71  		attributeMapIface      = m.GetFieldByNumber(5).(map[interface{}]interface{})
    72  		attributeMap           = mapInterfaceToMapString(attributeMapIface)
    73  		attributesM            = newVL(0, 0, 0, nil, attributeMap)
    74  		attributeMapBytes, err = attributesM.Marshal()
    75  	)
    76  	require.NoError(t, err)
    77  	marshaller.encPartialProto(attributeMapBytes)
    78  }
    79  
    80  func mapInterfaceToMapString(ifaceMap map[interface{}]interface{}) map[string]string {
    81  	stringMap := make(map[string]string, len(ifaceMap))
    82  	for key, val := range ifaceMap {
    83  		stringMap[key.(string)] = val.(string)
    84  	}
    85  	return stringMap
    86  }