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): add row.String() and refine error message for decoding a struct array #4431

Merged
merged 4 commits into from Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions spanner/read_test.go
Expand Up @@ -152,11 +152,6 @@ var (
}
)

// String implements fmt.stringer.
func (r *Row) String() string {
return fmt.Sprintf("{fields: %s, val: %s}", r.fields, r.vals)
}

func describeRows(l []*Row) string {
// generate a nice test failure description
var s = "["
Expand Down
5 changes: 5 additions & 0 deletions spanner/row.go
Expand Up @@ -88,6 +88,11 @@ type Row struct {
vals []*proto3.Value // keep decoded for now
}

// String implements fmt.stringer.
func (r *Row) String() string {
return fmt.Sprintf("{fields: %s, values: %s}", r.fields, r.vals)
}

// errNamesValuesMismatch returns error for when columnNames count is not equal
// to columnValues count.
func errNamesValuesMismatch(columnNames []string, columnValues []interface{}) error {
Expand Down
18 changes: 18 additions & 0 deletions spanner/row_test.go
Expand Up @@ -1672,6 +1672,24 @@ func TestToStructEmbedded(t *testing.T) {
}
}

func TestRowToString(t *testing.T) {
r := Row{
[]*sppb.StructType_Field{
{Name: "F1", Type: stringType()},
{Name: "F2", Type: stringType()},
},
[]*proto3.Value{
stringProto("v1"),
stringProto("v2"),
},
}
got := r.String()
want := `{fields: [name:"F1" type:{code:STRING} name:"F2" type:{code:STRING}], values: [string_value:"v1" string_value:"v2"]}`
if !testEqual(r.String(), want) {
t.Errorf("got %+v, want %+v", got, want)
}
}

// Test helpers for getting column names.
func TestColumnNameAndIndex(t *testing.T) {
// Test Row.Size().
Expand Down
4 changes: 2 additions & 2 deletions spanner/value.go
Expand Up @@ -1355,8 +1355,8 @@ func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}) error {
return errNilDst(p)
}
if !isPtrStructPtrSlice(vp.Type()) {
// The container is not a pointer to a struct pointer slice.
return errTypeMismatch(code, acode, ptr)
// The container is not a slice of struct pointers.
return fmt.Errorf("the container is not a slice of struct pointers: %v", errTypeMismatch(code, acode, ptr))
}
// Only use reflection for nil detection on slow path.
// Also, IsNil panics on many types, so check it after the type check.
Expand Down