Skip to content

Commit

Permalink
marshal: fix nil colleciton handling
Browse files Browse the repository at this point in the history
Nil collections should be marshalled to an empty collection, ie
a collection with 0 elements, it can not be encoded with -1 length
which indicates a null value.
  • Loading branch information
Zariel committed Nov 3, 2015
1 parent e59ea7f commit 65ce6c1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ 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 info.Version() < protoVersion1 {
panic("protocol version not set")
}
Expand Down Expand Up @@ -290,7 +287,16 @@ func marshalInt(info TypeInfo, value interface{}) ([]byte, error) {
}
return encInt(int32(i)), nil
}

if value == nil {
return nil, nil
}

rv := reflect.ValueOf(value)
if rv.IsNil() {
return nil, nil
}

switch rv.Type().Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
v := rv.Int()
Expand Down Expand Up @@ -881,9 +887,6 @@ func marshalList(info TypeInfo, value interface{}) ([]byte, error) {
k := t.Kind()
switch k {
case reflect.Slice, reflect.Array:
if k == reflect.Slice && rv.IsNil() {
return nil, nil
}
buf := &bytes.Buffer{}
n := rv.Len()

Expand Down Expand Up @@ -989,9 +992,7 @@ func marshalMap(info TypeInfo, value interface{}) ([]byte, error) {
if t.Kind() != reflect.Map {
return nil, marshalErrorf("can not marshal %T into %s", value, info)
}
if rv.IsNil() {
return nil, nil
}

buf := &bytes.Buffer{}
n := rv.Len()

Expand Down
8 changes: 4 additions & 4 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ var marshalTests = []struct {
NativeType: NativeType{proto: 2, typ: TypeSet},
Elem: NativeType{proto: 2, typ: TypeInt},
},
[]byte(nil),
[]int(nil),
[]byte{0, 0}, // encoding of a list should always include the size of the collection
[]int{},
},
{
CollectionType{
Expand All @@ -275,8 +275,8 @@ var marshalTests = []struct {
Key: NativeType{proto: 2, typ: TypeVarchar},
Elem: NativeType{proto: 2, typ: TypeInt},
},
[]byte(nil),
map[string]int(nil),
[]byte{0, 0},
map[string]int{},
},
{
CollectionType{
Expand Down

0 comments on commit 65ce6c1

Please sign in to comment.