Skip to content

Commit

Permalink
Accept interface{} in ByteSlicePayloadConverter (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackieli-tes authored Sep 15, 2021
1 parent 9d143aa commit f39757a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
17 changes: 13 additions & 4 deletions converter/byte_slice_payload_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,20 @@ func (c *ByteSlicePayloadConverter) ToPayload(value interface{}) (*commonpb.Payl

// FromPayload converts single []byte value from payload.
func (c *ByteSlicePayloadConverter) FromPayload(payload *commonpb.Payload, valuePtr interface{}) error {
valueBytes := reflect.ValueOf(valuePtr).Elem()
if !valueBytes.CanSet() {
return fmt.Errorf("type: %T: %w", valuePtr, ErrUnableToSetValue)
rv := reflect.ValueOf(valuePtr)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return fmt.Errorf("type: %T: %w", valuePtr, ErrValuePtrIsNotPointer)
}
v := rv.Elem()
value := payload.Data
if v.Kind() == reflect.Interface {
v.Set(reflect.ValueOf(value))
} else if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 {
// Must be a []byte.
v.SetBytes(value)
} else {
return fmt.Errorf("type %T: %w", valuePtr, ErrTypeIsNotByteSlice)
}
valueBytes.SetBytes(payload.GetData())
return nil
}

Expand Down
73 changes: 73 additions & 0 deletions converter/byte_slice_payload_converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package converter

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestByteSliceConverter(t *testing.T) {
bc := NewByteSlicePayloadConverter()

assert.Equal(t, bc.Encoding(), MetadataEncodingBinary)
payload, err := bc.ToPayload(nil)
assert.Nil(t, err)
assert.Nil(t, payload)

b := []byte("hello world")
payload, err = bc.ToPayload(b)
require.NoError(t, err)
assert.Equal(t, string(payload.Metadata[MetadataEncoding]), MetadataEncodingBinary)
assert.Equal(t, payload.Data, b)

var gotBytes []byte
var gotInterface interface{}

err = bc.FromPayload(payload, &gotBytes)
require.NoError(t, err)
assert.Equal(t, b, gotBytes)

err = bc.FromPayload(payload, &gotInterface)
require.NoError(t, err)
assert.Equal(t, b, gotInterface)

gotString := bc.ToString(payload)
// base64 unpadded encodeing of "hello world"
assert.Equal(t, "aGVsbG8gd29ybGQ", gotString)

// error branches
err = bc.FromPayload(payload, nil)
assert.ErrorIs(t, err, ErrValuePtrIsNotPointer)

var s string
err = bc.FromPayload(payload, s)
assert.ErrorIs(t, err, ErrValuePtrIsNotPointer)

err = bc.FromPayload(payload, &s)
assert.ErrorIs(t, err, ErrTypeIsNotByteSlice)
}
2 changes: 2 additions & 0 deletions converter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ var (
ErrValuePtrIsNotPointer = errors.New("not a pointer type")
// ErrValuePtrMustConcreteType is returned when proto value is of interface type.
ErrValuePtrMustConcreteType = errors.New("must be a concrete type, not interface")
// ErrTypeIsNotByteSlice is returned when value is not of *[]byte type.
ErrTypeIsNotByteSlice = errors.New("type is not *[]byte")
)

0 comments on commit f39757a

Please sign in to comment.