Skip to content

Commit

Permalink
feat(spanner): enable custom decoding for list value (#7463)
Browse files Browse the repository at this point in the history
Co-authored-by: rahul2393 <irahul@google.com>
  • Loading branch information
abiabsurd and rahul2393 committed Mar 6, 2023
1 parent a7edf6b commit 3aeadcd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion spanner/value.go
Expand Up @@ -2656,10 +2656,12 @@ func getGenericValue(t *sppb.Type, v *proto3.Value) (interface{}, error) {
return x.BoolValue, nil
case *proto3.Value_StringValue:
return x.StringValue, nil
case *proto3.Value_ListValue:
return x.ListValue, nil
case *proto3.Value_NullValue:
return getTypedNil(t)
default:
return 0, errSrcVal(v, "Number, Bool, String")
return 0, errSrcVal(v, "Number, Bool, String, List")
}
}

Expand Down
28 changes: 28 additions & 0 deletions spanner/value_test.go
Expand Up @@ -22,6 +22,7 @@ import (
"math"
"math/big"
"reflect"
"strconv"
"testing"
"time"

Expand All @@ -30,6 +31,7 @@ import (
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/golang/protobuf/proto"
proto3 "github.com/golang/protobuf/ptypes/struct"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -194,6 +196,30 @@ func (c *customStructToNull) DecodeSpanner(val interface{}) (err error) {
return fmt.Errorf("val mismatch: expected nil, got %v", val)
}

// e.g. a clock face time HH:MM
type customArray [4]uint8

// Convert to customArray from structpb.ListValue<structpb.StringValue>
func (c *customArray) DecodeSpanner(val interface{}) error {
listVal, ok := val.(*structpb.ListValue)
if !ok {
return fmt.Errorf("failed to decode customArray: unexpected type of %v", val)
}
asSlice := listVal.AsSlice()
if len(asSlice) != 4 {
return fmt.Errorf("failed to decode customArray: expected array of length 4")
}
for i, vI := range asSlice {
vStr, ok := vI.(string)
if !ok {
return fmt.Errorf("failed to decode customArray: got non string value: %v", vI)
}
vInt, _ := strconv.Atoi(vStr)
c[i] = uint8(vInt)
}
return nil
}

// Test encoding Values.
func TestEncodeValue(t *testing.T) {
type CustomString string
Expand Down Expand Up @@ -1801,6 +1827,8 @@ func TestDecodeValue(t *testing.T) {
{desc: "decode NULL array of bool to CustomStructToNull", proto: nullProto(), protoType: listType(boolType()), want: customStructToNull{}},
{desc: "decode NULL array of float to CustomStructToNull", proto: nullProto(), protoType: listType(floatType()), want: customStructToNull{}},
{desc: "decode NULL array of string to CustomStructToNull", proto: nullProto(), protoType: listType(stringType()), want: customStructToNull{}},
// CUSTOM ARRAY
{desc: "decode ARRAY<INT64> to CustomArray", proto: listProto(intProto(0), intProto(6), intProto(3), intProto(5)), protoType: listType(intType()), want: customArray([4]uint8{0, 6, 3, 5})},
} {
gotp := reflect.New(reflect.TypeOf(test.want))
v := gotp.Interface()
Expand Down

0 comments on commit 3aeadcd

Please sign in to comment.