|
Encoding a map[string]interface{} contains nil pointer as value or any struct which has nil pointer field causes runtime error.
type Foo struct {
|
Bar *Bar
|
}
|
type Bar struct{}
|
f := Foo{
|
Bar: nil,
|
}
|
// causes "*bsoncodec.StructCodec can only process structs, but got a reflect.Value"
|
_, err := bson.Marshal(f)
|
|
m := map[string]interface{}{
|
"foo": nil,
|
}
|
// causes "cannot perform an encoder or decoder lookup on <nil>"
|
_, err = bson.Marshal(m)
|
To address this problem(maybe a bug?), we should treat nil pointer as BSON null when encoding to BSON, in the same way as json.Marshal() does.
|