这是indexloc提供的服务,不要输入任何密码
Skip to content

fix(spanner): pointer type custom struct decoder #12496

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

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
18 changes: 11 additions & 7 deletions spanner/row_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2279,6 +2279,7 @@ func TestSelectAll(t *testing.T) {
Col1 int64
COL2 float64
Col3 CustomType[string]
Col4 *CustomType[string]
}

type testStructWithTag struct {
Expand Down Expand Up @@ -2451,7 +2452,7 @@ func TestSelectAll(t *testing.T) {
},
},
{
name: "success: using destination with custom type with custom decoder with some null columns",
name: "success: using destination with custom type with custom decoder with some null columns and pointer decoder",
args: args{
destination: &[]*testStructWithCustom{},
mock: newMockIterator(
Expand All @@ -2460,32 +2461,35 @@ func TestSelectAll(t *testing.T) {
{Name: "Col1", Type: intType()},
{Name: "Col2", Type: floatType()},
{Name: "Col3", Type: stringType()},
{Name: "Col4", Type: stringType()},
},
[]*proto3.Value{intProto(3), floatProto(3.3), stringProto("value3")},
[]*proto3.Value{intProto(3), floatProto(3.3), stringProto("value3"), stringProto("test3")},
},
&Row{
[]*sppb.StructType_Field{
{Name: "Col1", Type: intType()},
{Name: "Col2", Type: floatType()},
{Name: "Col3", Type: stringType()},
{Name: "Col4", Type: stringType()},
},
[]*proto3.Value{intProto(1), floatProto(1.1), nullProto()},
[]*proto3.Value{intProto(1), floatProto(1.1), nullProto(), nullProto()},
},
&Row{
[]*sppb.StructType_Field{
{Name: "Col1", Type: intType()},
{Name: "Col2", Type: floatType()},
{Name: "Col3", Type: stringType()},
{Name: "Col4", Type: stringType()},
},
[]*proto3.Value{intProto(2), floatProto(2.2), stringProto("value2")},
[]*proto3.Value{intProto(2), floatProto(2.2), stringProto("value2"), stringProto("test2")},
},
iterator.Done,
),
},
want: &[]*testStructWithCustom{
{Col1: 3, COL2: 3.3, Col3: CustomType[string]{"value3"}},
{Col1: 1, COL2: 1.1, Col3: CustomType[string]{}},
{Col1: 2, COL2: 2.2, Col3: CustomType[string]{"value2"}},
{Col1: 3, COL2: 3.3, Col3: CustomType[string]{"value3"}, Col4: &CustomType[string]{"test3"}},
{Col1: 1, COL2: 1.1, Col3: CustomType[string]{}, Col4: nil},
{Col1: 2, COL2: 2.2, Col3: CustomType[string]{"value2"}, Col4: &CustomType[string]{"test2"}},
},
},
{
Expand Down
43 changes: 43 additions & 0 deletions spanner/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,41 @@ func parseNullTime(v *proto3.Value, p *NullTime, code sppb.TypeCode, isNull bool
return nil
}

// tryDecodePointerToDecoder attempts to decode a **T where *T implements Decoder
// Returns (handled, error) - handled=true if this case was processed
func tryDecodePointerToDecoder(ptr interface{}, t *sppb.Type, v *proto3.Value, isNull bool) (bool, error) {
rv := reflect.ValueOf(ptr)
if rv.Kind() != reflect.Ptr || rv.Type().Elem().Kind() != reflect.Ptr {
return false, nil
}

elemType := rv.Type().Elem().Elem()
if !reflect.PointerTo(elemType).Implements(reflect.TypeOf((*Decoder)(nil)).Elem()) {
return false, nil
}

if isNull {
rv.Elem().Set(reflect.Zero(rv.Elem().Type()))
return true, nil
}

// Create a new instance of the underlying type
newInstance := reflect.New(elemType)
if decodedVal, ok := newInstance.Interface().(Decoder); ok {
x, err := getGenericValue(t, v)
if err != nil {
return true, err
}
if err := decodedVal.DecodeSpanner(x); err != nil {
return true, err
}
rv.Elem().Set(newInstance)
return true, nil
}

return false, nil
}

// decodeValue decodes a protobuf Value into a pointer to a Go value, as
// specified by sppb.Type.
func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...DecodeOptions) error {
Expand Down Expand Up @@ -2470,6 +2505,10 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...DecodeO
}
return decodedVal.DecodeSpanner(x)
}
// Check if the pointer is a pointer to a pointer, and if the underlying type implements Decoder
if handled, err := tryDecodePointerToDecoder(ptr, t, v, isNull); handled {
return err
}
if p == nil {
return errNilDst(p)
}
Expand Down Expand Up @@ -2721,6 +2760,10 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}, opts ...DecodeO
}
return decodedVal.DecodeSpanner(x)
}
// Check if the pointer is a pointer to a pointer, and if the underlying type implements Decoder
if handled, err := tryDecodePointerToDecoder(ptr, t, v, isNull); handled {
return err
}

// Check if the pointer is a variant of a base type.
decodableType := getDecodableSpannerType(ptr, true)
Expand Down
Loading