Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spanner): reset array of proto, enum when null from database #7176

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions spanner/testdata/protos/singer.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions spanner/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,7 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...decodeO
// Check if the interface{} is a pointer and is of type array of proto columns
if typ.Kind() == reflect.Ptr && isAnArrayOfProtoColumn(ptr) && code == sppb.TypeCode_ARRAY {
if isNull {
rv.Elem().Set(reflect.Zero(rv.Elem().Type()))
break
}
// Get the user-defined type of the proto array
Expand All @@ -2185,9 +2186,8 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...decodeO
return err
}
return decodeProtoMessagePtrArray(x, t.ArrayElementType, rv)
} else {
return errTypeMismatch(code, acode, ptr)
}
return errTypeMismatch(code, acode, ptr)
}
case sppb.TypeCode_ENUM, sppb.TypeCode_INT64:
if etyp.Implements(protoEnumReflectType) {
Expand All @@ -2197,9 +2197,8 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...decodeO
}
if etyp.Kind() == reflect.Ptr {
return decodeProtoEnumPtrArray(x, t.ArrayElementType, rv)
} else {
return decodeProtoEnumArray(x, t.ArrayElementType, rv, ptr)
}
return decodeProtoEnumArray(x, t.ArrayElementType, rv, ptr)
}
}
}
Expand Down Expand Up @@ -4098,9 +4097,8 @@ func encodeValue(v interface{}) (*proto3.Value, *sppb.Type, error) {
case NullProtoEnum:
if v.Valid {
return encodeValue(v.ProtoEnumVal)
} else {
return nil, nil, errNotValidSrc(v)
}
return nil, nil, errNotValidSrc(v)
case proto.Message:
if v != nil {
if proto.MessageReflect(v).IsValid() {
Expand All @@ -4116,9 +4114,8 @@ func encodeValue(v interface{}) (*proto3.Value, *sppb.Type, error) {
case NullProtoMessage:
if v.Valid {
return encodeValue(v.ProtoMessageVal)
} else {
return nil, nil, errNotValidSrc(v)
}
return nil, nil, errNotValidSrc(v)
default:
// Check if the value is a custom type that implements spanner.Encoder
// interface.
Expand Down Expand Up @@ -4155,9 +4152,8 @@ func encodeValue(v interface{}) (*proto3.Value, *sppb.Type, error) {
if typ.Kind() == reflect.Slice {
if isAnArrayOfProtoColumn(v) {
return encodeProtoArray(v)
} else {
return encodeStructArray(v)
}
return encodeStructArray(v)
}
}
return pb, pt, nil
Expand Down
10 changes: 5 additions & 5 deletions spanner/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2748,7 +2748,7 @@ func TestJSONMarshal_NullTypes(t *testing.T) {
Nationality: proto.String("Country1"),
Genre: &singerProtoEnum,
}
singerProtoMessageJsonStr := `{"singer_id":1,"birth_date":"January","nationality":"Country1","genre":3}`
singerProtoMessageJSONStr := `{"singer_id":1,"birth_date":"January","nationality":"Country1","genre":3}`

type testcase struct {
input interface{}
Expand Down Expand Up @@ -2844,8 +2844,8 @@ func TestJSONMarshal_NullTypes(t *testing.T) {
{
"NullProtoMessage",
[]testcase{
{input: NullProtoMessage{&singerProtoMessage, true}, expect: singerProtoMessageJsonStr},
{input: &NullProtoMessage{&singerProtoMessage, true}, expect: singerProtoMessageJsonStr},
{input: NullProtoMessage{&singerProtoMessage, true}, expect: singerProtoMessageJSONStr},
{input: &NullProtoMessage{&singerProtoMessage, true}, expect: singerProtoMessageJSONStr},
{input: &NullProtoMessage{&singerProtoMessage, false}, expect: "null"},
{input: NullProtoMessage{}, expect: "null"},
},
Expand Down Expand Up @@ -2885,7 +2885,7 @@ func TestJSONUnmarshal_NullTypes(t *testing.T) {
Nationality: proto.String("Country1"),
Genre: &singerProtoEnum,
}
singerProtoMessageJsonStr := `{"singer_id":1,"birth_date":"January","nationality":"Country1","genre":3}`
singerProtoMessageJSONStr := `{"singer_id":1,"birth_date":"January","nationality":"Country1","genre":3}`

type testcase struct {
input []byte
Expand Down Expand Up @@ -2995,7 +2995,7 @@ func TestJSONUnmarshal_NullTypes(t *testing.T) {
{
"NullProtoMessage",
[]testcase{
{input: []byte(singerProtoMessageJsonStr), got: NullProtoMessage{&pb.SingerInfo{}, true}, isNull: false, expect: singerProtoMessage.String(), expectError: false},
{input: []byte(singerProtoMessageJSONStr), got: NullProtoMessage{&pb.SingerInfo{}, true}, isNull: false, expect: singerProtoMessage.String(), expectError: false},
{input: []byte("null"), got: NullProtoMessage{&pb.SingerInfo{}, true}, isNull: true, expect: nullString, expectError: false},
{input: nil, got: NullProtoMessage{&pb.SingerInfo{}, true}, isNull: true, expect: nullString, expectError: true},
{input: []byte(""), got: NullProtoMessage{}, isNull: true, expect: nullString, expectError: true},
Expand Down