Skip to content

Commit

Permalink
feat(spanner): add methods to return Row fields (#8953)
Browse files Browse the repository at this point in the history
* feat(spanner): add code and tests

* feat(spanner): code fix

---------

Co-authored-by: rahul2393 <irahul@google.com>
  • Loading branch information
harshachinta and rahul2393 committed Nov 2, 2023
1 parent 411a51e commit e22e70f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spanner/row.go
Expand Up @@ -174,6 +174,22 @@ func (r *Row) ColumnNames() []string {
return n
}

// ColumnType returns the Cloud Spanner Type of column i, or nil for invalid column.
func (r *Row) ColumnType(i int) *sppb.Type {
if i < 0 || i >= len(r.fields) {
return nil
}
return r.fields[i].Type
}

// ColumnValue returns the Cloud Spanner Value of column i, or nil for invalid column.
func (r *Row) ColumnValue(i int) *proto3.Value {
if i < 0 || i >= len(r.vals) {
return nil
}
return r.vals[i]
}

// errColIdxOutOfRange returns error for requested column index is out of the
// range of the target Row's columns.
func errColIdxOutOfRange(i int, r *Row) error {
Expand Down
24 changes: 24 additions & 0 deletions spanner/row_test.go
Expand Up @@ -1909,6 +1909,30 @@ func TestColumnNameAndIndex(t *testing.T) {
}
}

// Test helpers for getting column type and value.
func TestColumnTypeAndValue(t *testing.T) {
// Test Row.ColumnType()
for i, col := range row.fields {
if ct := row.ColumnType(i); ct != col.Type {
t.Errorf("row.ColumnType(%v) returns %q, want %q", i, ct, col.Type)
}
}
// Test Row.ColumnValue()
for i, val := range row.vals {
if cv := row.ColumnValue(i); cv != val {
t.Errorf("row.ColumnValue(%v) returns %q, want %q", i, cv, val)
}
}
// Test Row.ColumnType on empty Row.
if ct := (&Row{}).ColumnType(0); ct != nil {
t.Errorf("empty_row.ColumnType(%v) returns %v, want %v", 0, ct, nil)
}
// Test Row.ColumnValue on empty Row.
if cv := (&Row{}).ColumnValue(0); cv != nil {
t.Errorf("empty_row.ColumnValue(%v) returns %v, want %v", 0, cv, nil)
}
}

func TestNewRow(t *testing.T) {
for _, test := range []struct {
names []string
Expand Down

0 comments on commit e22e70f

Please sign in to comment.