Skip to content

Commit 0326fae

Browse files
committed
Fix Iter.RowData Values to point to concrete types
There were multiple issues with the previous RowData method. First, it was returning invalid values because it called c.Zero() instead of Zero on column. Second, it would've returned an interface to a pointer to an interface which is frustrating to work with. Now it restores the previous v1 behavior and returns pointers to the underlying types wrapped in an interface. Added tests to ensure the behavior is as expected. Patch by James Hartig; reviewed by João Reis for CASSGO-95
1 parent 22ab88e commit 0326fae

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Fixed
1616

1717
- Prevent panic with queries during session init (CASSGO-92)
18+
- Return correct values from RowData (CASSGO-95)
1819

1920
## [2.0.0]
2021

helpers.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,14 @@ func (iter *Iter) RowData() (RowData, error) {
6060

6161
for _, column := range iter.Columns() {
6262
if c, ok := column.TypeInfo.(TupleTypeInfo); !ok {
63-
val := c.Zero()
63+
val := reflect.New(reflect.TypeOf(column.TypeInfo.Zero()))
6464
columns = append(columns, column.Name)
65-
values = append(values, &val)
65+
values = append(values, val.Interface())
6666
} else {
6767
for i, elem := range c.Elems {
6868
columns = append(columns, TupleColumnName(column.Name, i))
69-
var val interface{}
70-
val = elem.Zero()
71-
values = append(values, &val)
69+
val := reflect.New(reflect.TypeOf(elem.Zero()))
70+
values = append(values, val.Interface())
7271
}
7372
}
7473
}

helpers_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,50 @@ func TestGetCassandraTypeInfo(t *testing.T) {
259259
})
260260
}
261261
}
262+
263+
func TestIter_RowData(t *testing.T) {
264+
iter := &Iter{
265+
meta: resultMetadata{
266+
columns: []ColumnInfo{
267+
{Name: "id", TypeInfo: intTypeInfo{}},
268+
{Name: "name", TypeInfo: varcharLikeTypeInfo{typ: TypeText}},
269+
{Name: "coords", TypeInfo: TupleTypeInfo{
270+
Elems: []TypeInfo{
271+
floatTypeInfo{},
272+
floatTypeInfo{},
273+
},
274+
}},
275+
{Name: "active", TypeInfo: booleanTypeInfo{}},
276+
},
277+
},
278+
}
279+
280+
rowData, err := iter.RowData()
281+
if err != nil {
282+
t.Fatal(err)
283+
}
284+
285+
expectedColumns := []string{"id", "name", "coords[0]", "coords[1]", "active"}
286+
if !reflect.DeepEqual(rowData.Columns, expectedColumns) {
287+
t.Fatalf("expected columns %v got %v", expectedColumns, rowData.Columns)
288+
}
289+
290+
if len(rowData.Values) != len(expectedColumns) {
291+
t.Fatalf("expected %d values got %d", len(expectedColumns), len(rowData.Values))
292+
}
293+
294+
expectedTypes := []reflect.Type{
295+
reflect.TypeOf((*int)(nil)),
296+
reflect.TypeOf((*string)(nil)),
297+
reflect.TypeOf((*float32)(nil)),
298+
reflect.TypeOf((*float32)(nil)),
299+
reflect.TypeOf((*bool)(nil)),
300+
}
301+
302+
for i, val := range rowData.Values {
303+
gotType := reflect.TypeOf(val)
304+
if gotType != expectedTypes[i] {
305+
t.Fatalf("value[%d]: expected type %v got %v", i, expectedTypes[i], gotType)
306+
}
307+
}
308+
}

0 commit comments

Comments
 (0)