Skip to content

Commit

Permalink
Merge pull request apache#239 from phillipCouto/nil_in_query
Browse files Browse the repository at this point in the history
Added support for marshalling a nil value.
  • Loading branch information
0x6e6562 committed Sep 10, 2014
2 parents 874844d + b4adfb4 commit f09085c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,3 +1214,25 @@ func TestBatchStats(t *testing.T) {
}
}
}

//TestNilInQuery tests to see that a nil value passed to a query is handled by Cassandra
//TODO validate the nil value by reading back the nil. Need to fix Unmarshalling.
func TestNilInQuery(t *testing.T) {
session := createSession(t)
defer session.Close()

if err := createTable(session, "CREATE TABLE testNilInsert (id int, count int, PRIMARY KEY (id))"); err != nil {
t.Fatalf("failed to create table with error '%v'", err)
}
if err := session.Query("INSERT INTO testNilInsert (id,count) VALUES (?,?)", 1, nil).Exec(); err != nil {
t.Fatalf("failed to insert with err: %v", err)
}

var id int

if err := session.Query("SELECT id FROM testNilInsert").Scan(&id); err != nil {
t.Fatalf("failed to select with err: %v", err)
} else if id != 1 {
t.Fatalf("expected id to be 1, got %v", id)
}
}
4 changes: 4 additions & 0 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Unmarshaler interface {
// Marshal returns the CQL encoding of the value for the Cassandra
// internal type described by the info parameter.
func Marshal(info *TypeInfo, value interface{}) ([]byte, error) {
if value == nil {
return nil, nil
}

if v, ok := value.(Marshaler); ok {
return v.MarshalCQL(info)
}
Expand Down
10 changes: 10 additions & 0 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ func TestMarshal(t *testing.T) {
}
}

func TestMarshalNil(t *testing.T) {
data, err := Marshal(&TypeInfo{Type: TypeInt}, nil)
if err != nil {
t.Errorf("failed to marshal nil with err: %v", err)
}
if data != nil {
t.Errorf("expected nil, got %v", data)
}
}

func TestUnmarshal(t *testing.T) {
for i, test := range marshalTests {
v := reflect.New(reflect.TypeOf(test.Value))
Expand Down

0 comments on commit f09085c

Please sign in to comment.