Skip to content

Commit e22e70f

Browse files
feat(spanner): add methods to return Row fields (#8953)
* feat(spanner): add code and tests * feat(spanner): code fix --------- Co-authored-by: rahul2393 <[email protected]>
1 parent 411a51e commit e22e70f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

spanner/row.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,22 @@ func (r *Row) ColumnNames() []string {
174174
return n
175175
}
176176

177+
// ColumnType returns the Cloud Spanner Type of column i, or nil for invalid column.
178+
func (r *Row) ColumnType(i int) *sppb.Type {
179+
if i < 0 || i >= len(r.fields) {
180+
return nil
181+
}
182+
return r.fields[i].Type
183+
}
184+
185+
// ColumnValue returns the Cloud Spanner Value of column i, or nil for invalid column.
186+
func (r *Row) ColumnValue(i int) *proto3.Value {
187+
if i < 0 || i >= len(r.vals) {
188+
return nil
189+
}
190+
return r.vals[i]
191+
}
192+
177193
// errColIdxOutOfRange returns error for requested column index is out of the
178194
// range of the target Row's columns.
179195
func errColIdxOutOfRange(i int, r *Row) error {

spanner/row_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,30 @@ func TestColumnNameAndIndex(t *testing.T) {
19091909
}
19101910
}
19111911

1912+
// Test helpers for getting column type and value.
1913+
func TestColumnTypeAndValue(t *testing.T) {
1914+
// Test Row.ColumnType()
1915+
for i, col := range row.fields {
1916+
if ct := row.ColumnType(i); ct != col.Type {
1917+
t.Errorf("row.ColumnType(%v) returns %q, want %q", i, ct, col.Type)
1918+
}
1919+
}
1920+
// Test Row.ColumnValue()
1921+
for i, val := range row.vals {
1922+
if cv := row.ColumnValue(i); cv != val {
1923+
t.Errorf("row.ColumnValue(%v) returns %q, want %q", i, cv, val)
1924+
}
1925+
}
1926+
// Test Row.ColumnType on empty Row.
1927+
if ct := (&Row{}).ColumnType(0); ct != nil {
1928+
t.Errorf("empty_row.ColumnType(%v) returns %v, want %v", 0, ct, nil)
1929+
}
1930+
// Test Row.ColumnValue on empty Row.
1931+
if cv := (&Row{}).ColumnValue(0); cv != nil {
1932+
t.Errorf("empty_row.ColumnValue(%v) returns %v, want %v", 0, cv, nil)
1933+
}
1934+
}
1935+
19121936
func TestNewRow(t *testing.T) {
19131937
for _, test := range []struct {
19141938
names []string

0 commit comments

Comments
 (0)