Respect nil slices, maps in bson encoder by aksentyev · Pull Request #147 · globalsign/mgo (original) (raw)

Expand Up

@@ -1933,3 +1933,59 @@ func (s *S) BenchmarkNewObjectId(c *C) {

bson.NewObjectId()

}

}

func (s *S) TestMarshalRespectNil(c *C) {

type T struct {

Slice []int

SlicePtr *[]int

Ptr *int

Map map[string]interface{}

MapPtr *map[string]interface{}

}

bson.SetRespectNilValues(true)

defer bson.SetRespectNilValues(false)

testStruct1 := T{}

c.Assert(testStruct1.Slice, IsNil)

c.Assert(testStruct1.SlicePtr, IsNil)

c.Assert(testStruct1.Map, IsNil)

c.Assert(testStruct1.MapPtr, IsNil)

c.Assert(testStruct1.Ptr, IsNil)

b, _ := bson.Marshal(testStruct1)

testStruct2 := T{}

bson.Unmarshal(b, &testStruct2)

c.Assert(testStruct2.Slice, IsNil)

c.Assert(testStruct2.SlicePtr, IsNil)

c.Assert(testStruct2.Map, IsNil)

c.Assert(testStruct2.MapPtr, IsNil)

c.Assert(testStruct2.Ptr, IsNil)

testStruct1 = T{

Slice: []int{},

SlicePtr: &[]int{},

Map: map[string]interface{}{},

MapPtr: &map[string]interface{}{},

}

c.Assert(testStruct1.Slice, NotNil)

c.Assert(testStruct1.SlicePtr, NotNil)

c.Assert(testStruct1.Map, NotNil)

c.Assert(testStruct1.MapPtr, NotNil)

b, _ = bson.Marshal(testStruct1)

testStruct2 = T{}

bson.Unmarshal(b, &testStruct2)

c.Assert(testStruct2.Slice, NotNil)

c.Assert(testStruct2.SlicePtr, NotNil)

c.Assert(testStruct2.Map, NotNil)

c.Assert(testStruct2.MapPtr, NotNil)

}