[GODRIVER-638] Support encoding for nil pointers Created: 15/Nov/18  Updated: 19/Nov/18  Resolved: 19/Nov/18

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: takumi fujiwara Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates GODRIVER-535 Add support for pointers to reflect.K... Closed

 Description   

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.



 Comments   
Comment by Kristofer Brandow (Inactive) [ 19/Nov/18 ]

hi jiftechnify,

This work will be covered as part of GODRIVER-535, so I'm closing this ticket as a duplicate.

--Kris

Generated at Thu Feb 08 08:34:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.