Skip to content

Commit 42cdde6

Browse files
fix(spanner): enable toStruct support for structs with proto message pointer fields (#10704)
Co-authored-by: Jason Bradshaw <jason@j2ceb.com> Co-authored-by: rahul2393 <irahul@google.com>
1 parent c342f65 commit 42cdde6

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

spanner/value.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,15 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...DecodeO
12651265
acode = t.ArrayElementType.Code
12661266
atypeAnnotation = t.ArrayElementType.TypeAnnotation
12671267
}
1268+
1269+
if code == sppb.TypeCode_PROTO && reflect.TypeOf(ptr).Elem().Kind() == reflect.Ptr {
1270+
pve := reflect.ValueOf(ptr).Elem()
1271+
if pve.IsNil() {
1272+
pve.Set(reflect.New(pve.Type().Elem()))
1273+
}
1274+
ptr = pve.Interface()
1275+
}
1276+
12681277
_, isNull := v.Kind.(*proto3.Value_NullValue)
12691278

12701279
// Do the decoding based on the type of ptr.

spanner/value_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,14 @@ func TestDecodeValue(t *testing.T) {
19821982
},
19831983
{desc: "decode ENUM to protoreflect.Enum", proto: protoEnumProto(pb.Genre_ROCK), protoType: protoEnumType(protoEnumfqn), want: singerEnumValue},
19841984
{desc: "decode PROTO to NullProto", proto: protoMessageProto(&singerProtoMsg), protoType: protoMessageType(protoMessagefqn), want: NullProtoMessage{&singerProtoMsg, true}},
1985+
{desc: "decode PROTO to *pb.SingerInfo", proto: protoMessageProto(&singerProtoMsg), protoType: protoMessageType(protoMessagefqn),
1986+
want: &pb.SingerInfo{
1987+
SingerId: proto.Int64(1),
1988+
BirthDate: proto.String("January"),
1989+
Nationality: proto.String("Country1"),
1990+
Genre: &singerEnumValue,
1991+
},
1992+
},
19851993
{desc: "decode NULL to NullProto", proto: nullProto(), protoType: protoMessageType(protoMessagefqn), want: NullProtoMessage{}},
19861994
{desc: "decode ENUM to NullEnum", proto: protoEnumProto(pb.Genre_ROCK), protoType: protoEnumType(protoEnumfqn), want: NullProtoEnum{&singerEnumValue, true}},
19871995
{desc: "decode NULL to NullEnum", proto: nullProto(), protoType: protoEnumType(protoEnumfqn), want: NullProtoEnum{}},

0 commit comments

Comments
 (0)