Skip to content

Commit

Permalink
fix(spanner): allow toStruct to decode value into struct with unequal…
Browse files Browse the repository at this point in the history
… number of fields compared to Spanner row fields
  • Loading branch information
rahul2393 committed Nov 18, 2021
1 parent c615922 commit b2fadc7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion spanner/row_test.go
Expand Up @@ -559,7 +559,7 @@ func TestToStructInvalidDst(t *testing.T) {
errDupGoField(&struct {
PK1 string `spanner:"STRING"`
PK2 string `spanner:"STRING"`
}{}),
}{}, "STRING"),
},
{
"Decode row as STRUCT to Go struct with no field for the PK column",
Expand Down
13 changes: 6 additions & 7 deletions spanner/value.go
Expand Up @@ -2872,8 +2872,8 @@ func errNilSpannerStructType() error {
}

// errDupGoField returns error for duplicated Go STRUCT field names
func errDupGoField(s interface{}) error {
return spannerErrorf(codes.InvalidArgument, "Go struct %+v(type %T) has duplicate fields for GO STRUCT field", s, s)
func errDupGoField(s interface{}, name string) error {
return spannerErrorf(codes.InvalidArgument, "Go struct %+v(type %T) has duplicate fields for GO STRUCT field %s", s, s, name)
}

// errUnnamedField returns error for decoding a Cloud Spanner STRUCT with
Expand Down Expand Up @@ -2930,7 +2930,7 @@ func decodeStruct(ty *sppb.StructType, pb *proto3.ListValue, ptr interface{}) er
for _, f := range fieldNames {
sf := fields.Match(f)
if sf == nil {
return errDupGoField(ptr)
return errDupGoField(ptr, f)
}
}
seen := map[string]bool{}
Expand Down Expand Up @@ -3009,10 +3009,9 @@ func decodeStructArray(ty *sppb.StructType, pb *proto3.ListValue, ptr interface{

func getAllFieldNames(v reflect.Value) []string {
var names []string
s := v
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
typeOfT := v.Type()
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
fieldType := typeOfT.Field(i)
exported := (fieldType.PkgPath == "")
// If a named field is unexported, ignore it. An anonymous
Expand Down

0 comments on commit b2fadc7

Please sign in to comment.