Skip to content

Commit 0e5e704

Browse files
authored
fix: When given a string for a bytes entity/sort key filter, attempt to convert as base64 string (#285)
1 parent e196c6a commit 0e5e704

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

go/types/typeconversion.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
"encoding/base64"
45
"fmt"
56
"google.golang.org/protobuf/types/known/timestamppb"
67
"math"
@@ -797,6 +798,15 @@ func valueTypeToGoTypeTimestampAsString(value *types.Value, timestampAsString bo
797798
}
798799
}
799800

801+
func transformStringToBytes(str string) []byte {
802+
bytes, decodeErr := base64.StdEncoding.DecodeString(str)
803+
if decodeErr != nil {
804+
// If base64 decoding fails, try as a byte string
805+
bytes = []byte(str)
806+
}
807+
return bytes
808+
}
809+
800810
func ConvertToValueType(value *types.Value, valueType types.ValueType_Enum) (*types.Value, error) {
801811
if valueType != types.ValueType_NULL {
802812
if value == nil || value.Val == nil {
@@ -821,7 +831,7 @@ func ConvertToValueType(value *types.Value, valueType types.ValueType_Enum) (*ty
821831
case *types.Value_BytesVal:
822832
return value, nil
823833
case *types.Value_StringVal:
824-
return &types.Value{Val: &types.Value_BytesVal{BytesVal: []byte(value.GetStringVal())}}, nil
834+
return &types.Value{Val: &types.Value_BytesVal{BytesVal: transformStringToBytes(value.GetStringVal())}}, nil
825835
}
826836
case types.ValueType_INT32:
827837
switch value.Val.(type) {
@@ -884,7 +894,7 @@ func ConvertToValueType(value *types.Value, valueType types.ValueType_Enum) (*ty
884894
stringList := value.GetStringListVal().GetVal()
885895
bytesList := make([][]byte, len(stringList))
886896
for i, str := range stringList {
887-
bytesList[i] = []byte(str)
897+
bytesList[i] = transformStringToBytes(str)
888898
}
889899
return &types.Value{Val: &types.Value_BytesListVal{BytesListVal: &types.BytesList{Val: bytesList}}}, nil
890900
}

go/types/typeconversion_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ func TestConvertToValueType_Bytes(t *testing.T) {
500500
}{
501501
{input: &types.Value{Val: &types.Value_BytesVal{BytesVal: []byte{1, 2, 3}}}, expected: []byte{1, 2, 3}},
502502
{input: &types.Value{Val: &types.Value_BytesVal{BytesVal: nil}}, expected: []byte(nil)},
503-
{input: &types.Value{Val: &types.Value_StringVal{StringVal: "test"}}, expected: []byte("test")},
503+
{input: &types.Value{Val: &types.Value_StringVal{StringVal: "\u0001\u0002\u0003"}}, expected: []byte{1, 2, 3}},
504+
{input: &types.Value{Val: &types.Value_StringVal{StringVal: "dGVzdA=="}}, expected: []byte("test")},
504505
}
505506

506507
for _, tc := range testCases {
@@ -632,7 +633,8 @@ func TestConvertToValueType_BytesList(t *testing.T) {
632633
}{
633634
{input: &types.Value{Val: &types.Value_BytesListVal{BytesListVal: &types.BytesList{Val: [][]byte{{1, 2}, {3, 4}}}}}, expected: [][]byte{{1, 2}, {3, 4}}},
634635
{input: &types.Value{Val: &types.Value_BytesListVal{BytesListVal: &types.BytesList{Val: [][]byte{}}}}, expected: [][]byte{}},
635-
{input: &types.Value{Val: &types.Value_StringListVal{StringListVal: &types.StringList{Val: []string{"a", "b", "c"}}}}, expected: [][]byte{[]byte("a"), []byte("b"), []byte("c")}},
636+
{input: &types.Value{Val: &types.Value_StringListVal{StringListVal: &types.StringList{Val: []string{"\u0001\u0002", "\u0003\u0004"}}}}, expected: [][]byte{{1, 2}, {3, 4}}},
637+
{input: &types.Value{Val: &types.Value_StringListVal{StringListVal: &types.StringList{Val: []string{"YQ==", "Yg==", "Yw=="}}}}, expected: [][]byte{[]byte("a"), []byte("b"), []byte("c")}},
636638
{input: &types.Value{Val: &types.Value_StringListVal{StringListVal: &types.StringList{Val: []string{}}}}, expected: [][]byte{}},
637639
}
638640

0 commit comments

Comments
 (0)