[GODRIVER-577] Add struct tags to choose BSON types Created: 20/Sep/18  Updated: 10/Oct/22

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

Type: Improvement Priority: Major - P3
Reporter: Kristofer Brandow (Inactive) Assignee: Unassigned
Resolution: Unresolved Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by GODRIVER-565 Decode date field to int64 get zero v... Closed

 Description   

The new bsoncodec package handles encoding and decoding of known types, but it doesn't handle encoding known types to their non-default BSON types. For instance, while decoding into an int64 for a DateTime type could be supported, there wouldn't be a way to properly encode the int64 to a DateTime BSON type. This violates the ability to round trip types in the BSON library and it causes information loss by default. For this reason, the BSON decoders do not currently support decoding a DateTime into an int64. Users who need this functionality required to register custom codecs to handle this case, or to handle BSON marshaling on their own.

 

A better solution is to allow users to tag their struct values with a bsontype struct field. This would allow the user to pass information to the encoders and decoders so they can properly handle round trip enabled encoding and decoding without information loss for non standard BSON types. This would enable users to encode a string to essentially any BSON type and also decode it. This is especially useful for ObjectIDs and date time strings.



 Comments   
Comment by Scott Crunkleton [ 26/Jul/21 ]

Another scenario:
We want to keep mongo in our storage layer and abstract mongo away from our business logic. So to our business logic, an ID is a string, but internally that string is converted to/from a BSON ObjectID.

I have copied and modified bsoncodec/struct_codec.go to support our own custom `bsoncodec` struct tag, but I like the proposed `bsontype` paradigm.

I would be happy to do a pull request if someone could point the way.

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