From 46565dba4e74085ce80c380d0e6d8ead98258c65 Mon Sep 17 00:00:00 2001 From: Jason Bradshaw Date: Sat, 17 Aug 2024 14:15:37 -0600 Subject: [PATCH] fix(spanner): enable toStruct support for structs with proto message pointer fields --- spanner/value.go | 9 +++++++++ spanner/value_test.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/spanner/value.go b/spanner/value.go index b258678748bf..0f23f9907eca 100644 --- a/spanner/value.go +++ b/spanner/value.go @@ -1259,6 +1259,15 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...DecodeO acode = t.ArrayElementType.Code atypeAnnotation = t.ArrayElementType.TypeAnnotation } + + if code == sppb.TypeCode_PROTO && reflect.TypeOf(ptr).Elem().Kind() == reflect.Ptr { + pve := reflect.ValueOf(ptr).Elem() + if pve.IsNil() { + pve.Set(reflect.New(pve.Type().Elem())) + } + ptr = pve.Interface() + } + _, isNull := v.Kind.(*proto3.Value_NullValue) // Do the decoding based on the type of ptr. diff --git a/spanner/value_test.go b/spanner/value_test.go index 2890b0a27633..c7a3fb1e56e7 100644 --- a/spanner/value_test.go +++ b/spanner/value_test.go @@ -1978,6 +1978,14 @@ func TestDecodeValue(t *testing.T) { }, {desc: "decode ENUM to protoreflect.Enum", proto: protoEnumProto(pb.Genre_ROCK), protoType: protoEnumType(protoEnumfqn), want: singerEnumValue}, {desc: "decode PROTO to NullProto", proto: protoMessageProto(&singerProtoMsg), protoType: protoMessageType(protoMessagefqn), want: NullProtoMessage{&singerProtoMsg, true}}, + {desc: "decode PROTO to *pb.SingerInfo", proto: protoMessageProto(&singerProtoMsg), protoType: protoMessageType(protoMessagefqn), + want: &pb.SingerInfo{ + SingerId: proto.Int64(1), + BirthDate: proto.String("January"), + Nationality: proto.String("Country1"), + Genre: &singerEnumValue, + }, + }, {desc: "decode NULL to NullProto", proto: nullProto(), protoType: protoMessageType(protoMessagefqn), want: NullProtoMessage{}}, {desc: "decode ENUM to NullEnum", proto: protoEnumProto(pb.Genre_ROCK), protoType: protoEnumType(protoEnumfqn), want: NullProtoEnum{&singerEnumValue, true}}, {desc: "decode NULL to NullEnum", proto: nullProto(), protoType: protoEnumType(protoEnumfqn), want: NullProtoEnum{}},