Skip to content

Commit 3a475c7

Browse files
committed
CASSGO-44: keep nil slices in MapScan
Previously if you read a NULL column as a varchar using MapScan it would store the value []byte{} in the map rather than []byte() which made it impossible to know if the value was NULL or not. Patch by James Hartig; reviewed by João Reis for CASSGO-44
1 parent 8bb171c commit 3a475c7

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424

2525
- Bumped actions/upload-artifact and actions/cache versions to v4 in CI workflow (CASSGO-48)
2626

27+
- Keep nil slices in MapScan (CASSGO-44)
28+
2729
### Fixed
2830

2931
- Retry policy now takes into account query idempotency (CASSGO-27)

cassandra_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"context"
3333
"errors"
3434
"fmt"
35-
"github.com/stretchr/testify/require"
3635
"io"
3736
"math"
3837
"math/big"
@@ -45,6 +44,8 @@ import (
4544
"time"
4645
"unicode"
4746

47+
"github.com/stretchr/testify/require"
48+
4849
"gopkg.in/inf.v0"
4950
)
5051

@@ -910,6 +911,7 @@ func TestMapScan(t *testing.T) {
910911
fullname text PRIMARY KEY,
911912
age int,
912913
address inet,
914+
data blob,
913915
)`); err != nil {
914916
t.Fatal("create table:", err)
915917
}
@@ -918,8 +920,8 @@ func TestMapScan(t *testing.T) {
918920
"Grace Hopper", 31, net.ParseIP("10.0.0.1")).Exec(); err != nil {
919921
t.Fatal("insert:", err)
920922
}
921-
if err := session.Query(`INSERT INTO scan_map_table (fullname, age, address) values (?,?,?)`,
922-
"Ada Lovelace", 30, net.ParseIP("10.0.0.2")).Exec(); err != nil {
923+
if err := session.Query(`INSERT INTO scan_map_table (fullname, age, address, data) values (?,?,?,?)`,
924+
"Ada Lovelace", 30, net.ParseIP("10.0.0.2"), []byte(`{"foo": "bar"}`)).Exec(); err != nil {
923925
t.Fatal("insert:", err)
924926
}
925927

@@ -933,6 +935,7 @@ func TestMapScan(t *testing.T) {
933935
assertEqual(t, "fullname", "Ada Lovelace", row["fullname"])
934936
assertEqual(t, "age", 30, row["age"])
935937
assertEqual(t, "address", "10.0.0.2", row["address"])
938+
assertDeepEqual(t, "data", []byte(`{"foo": "bar"}`), row["data"])
936939

937940
// Second iteration using a new map
938941
row = make(map[string]interface{})
@@ -942,6 +945,7 @@ func TestMapScan(t *testing.T) {
942945
assertEqual(t, "fullname", "Grace Hopper", row["fullname"])
943946
assertEqual(t, "age", 31, row["age"])
944947
assertEqual(t, "address", "10.0.0.1", row["address"])
948+
assertDeepEqual(t, "data", []byte(nil), row["data"])
945949
}
946950

947951
func TestSliceMap(t *testing.T) {

helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func getApacheCassandraType(class string) Type {
305305
func (r *RowData) rowMap(m map[string]interface{}) {
306306
for i, column := range r.Columns {
307307
val := dereference(r.Values[i])
308-
if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice {
308+
if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice && !valVal.IsNil() {
309309
valCopy := reflect.MakeSlice(valVal.Type(), valVal.Len(), valVal.Cap())
310310
reflect.Copy(valCopy, valVal)
311311
m[column] = valCopy.Interface()

0 commit comments

Comments
 (0)