Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CASSGO-44: keep nil slices in MapScan #1856

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"context"
"errors"
"fmt"
"github.com/stretchr/testify/require"
"io"
"math"
"math/big"
Expand All @@ -45,6 +44,8 @@ import (
"time"
"unicode"

"github.com/stretchr/testify/require"

"gopkg.in/inf.v0"
)

Expand Down Expand Up @@ -910,6 +911,7 @@ func TestMapScan(t *testing.T) {
fullname text PRIMARY KEY,
age int,
address inet,
data blob,
)`); err != nil {
t.Fatal("create table:", err)
}
Expand All @@ -918,8 +920,8 @@ func TestMapScan(t *testing.T) {
"Grace Hopper", 31, net.ParseIP("10.0.0.1")).Exec(); err != nil {
t.Fatal("insert:", err)
}
if err := session.Query(`INSERT INTO scan_map_table (fullname, age, address) values (?,?,?)`,
"Ada Lovelace", 30, net.ParseIP("10.0.0.2")).Exec(); err != nil {
if err := session.Query(`INSERT INTO scan_map_table (fullname, age, address, data) values (?,?,?,?)`,
"Ada Lovelace", 30, net.ParseIP("10.0.0.2"), []byte(`{"foo": "bar"}`)).Exec(); err != nil {
t.Fatal("insert:", err)
}

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

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

func TestSliceMap(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func getApacheCassandraType(class string) Type {
func (r *RowData) rowMap(m map[string]interface{}) {
for i, column := range r.Columns {
val := dereference(r.Values[i])
if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice {
if valVal := reflect.ValueOf(val); valVal.Kind() == reflect.Slice && !valVal.IsNil() {
valCopy := reflect.MakeSlice(valVal.Type(), valVal.Len(), valVal.Cap())
reflect.Copy(valCopy, valVal)
m[column] = valCopy.Interface()
Expand Down
Loading