Skip to content

Commit

Permalink
fix(spanner): enable toStruct support for structs with proto message …
Browse files Browse the repository at this point in the history
…pointer fields (#10704)

Co-authored-by: Jason Bradshaw <[email protected]>
Co-authored-by: rahul2393 <[email protected]>
  • Loading branch information
3 people authored Sep 25, 2024
1 parent c342f65 commit 42cdde6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions spanner/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,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.
Expand Down
8 changes: 8 additions & 0 deletions spanner/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,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{}},
Expand Down

0 comments on commit 42cdde6

Please sign in to comment.