[GODRIVER-1175] allow BSON null values to decode into non-nilable Go types Created: 01/Jul/19  Updated: 28/Oct/23  Resolved: 30/Jan/20

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

Type: New Feature Priority: Major - P3
Reporter: zzn Assignee: Divjot Arora (Inactive)
Resolution: Fixed Votes: 4
Labels: mgocompat
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Epic Link: MGO BSON compatibility
Case:
Backwards Compatibility: Fully Compatible

 Description   

purpose:  fix `can not decode null into a xxx type` issues.

current possible solution:

  1. change data in mongodb
  2. use pointer type, e.g. define field as type `*string`  but not  `string`
  3. customize the bson decoder

solution 1 is very difficult to do if considering team cooperation.

solution 3 is too heavy for such small requirement.

solution 2 is most acceptable so far, but it will make code ugly, because the compromise between field type and decoder

i propose a new tag property:

`skipnull`: skip null value(don't touch this field, or set to zero value?) when unmarshaling

 

this propose assume that in many cases, null value and non-exist is the same thing in mongodb. feel free to close this issue if the assumption don't make much sense 

 

EDIT: We've gotten multiple requests to allow BSON null values to decode into Go types that can't necessarily be nil (e.g. string). We should change our default decoders to accept BSON null values so there's no user configuration required to enable this feature. I'm adding this to the mgo BSON registry epic to help us track the ticket, but it will be enabled as a default for all codecs, not just in the mgo registry.



 Comments   
Comment by Githook User [ 30/Jan/20 ]

Author:

{'name': 'Divjot Arora', 'username': 'divjotarora', 'email': 'divjot.arora@10gen.com'}

Message: GODRIVER-1175 Allow BSON values to decode into non-nilable Go types (#277)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/e7e0dae2288928068a092ae0ab150940753733ba

Comment by Yao Wei [ 26/Aug/19 ]

Few other suggestions to achieve similar results:

1. Define an Niler interface (like Zeroer but for unmarshaling).

2. (mgo) Define an error SetZero to let decoder set to zero or nil pointer when the error is returned.

Currently other than having custom decoder I cannot set nil pointer when unmarshaling.  I am looking forward to this change as well.

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