Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(spanner): add row.String() and refine error message for decoding…
… a struct array (#4431)

* fix(spanner): add row.String() and refine error message for decoding a struct array
* Update the comment.
  • Loading branch information
hengfengli committed Jul 15, 2021
1 parent 09879ea commit f6258a4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
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

0 comments on commit f6258a4

Please sign in to comment.