Skip to content
Permalink
Browse files
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 f6258a47a4dfadc02dcdd75b53fd5f88c5dcca30
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
@@ -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 = "["
@@ -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 {
@@ -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().
@@ -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.

0 comments on commit f6258a4

Please sign in to comment.