[GODRIVER-1980] BSON Type 0x00 (document) missing Created: 23/Apr/21  Updated: 27/Oct/23  Resolved: 10/May/21

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

Type: Bug Priority: Unknown
Reporter: Mike Dotson Assignee: Isabella Siu (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux


Attachments: File main.go    
Issue Links:
Documented
Documentation Changes Summary:

May need to include new Document type in the godoc.


 Description   

bsontype.go does not include `0x00` for top level document types per the bsonspec.org.

This can be seen when using `UnmarshalBSONValue` to determine the document type.

Attached code to duplicate the issue.



 Comments   
Comment by Isabella Siu (Inactive) [ 26/Apr/21 ]

Hi again mike.dotson@its-broke.com! The main reason that we don't have a value for top-level document is because the documentation doesn't include it on either the MongoDB docs or bsonspec.org. The top-level document doesn't have an identifying byte, as the 0x00 byte on the bsonspec is a null-terminal. A bsontype represent the type value for a key/value pair.
To avoid checking for 0x00, you can have both UnmarshalBSON and UnmarshalBSONValue implemented, and documents will go to UnmarshalBSON and values will go to UnmarshalBSONValue.

Comment by Mike Dotson [ 26/Apr/21 ]

Hi @Isabella Siu,

I actually don't know that it's a BSON document, hence the checks.  If it's not a `bsontype.EmbeddedDocument` or (in this case `\x00`, ie `bsontype.Document`?) I fail the marshaling without the need to process the bytes to find it's not something that will result in a valid unmarshal (ie, single double that should result as a map[string]int64)

I also have other types that are unmarshaled based off of types such as `bsontype.Null`, `bsontype.Int32`, `bsontype.Int64`, etc. that work as expected.

Having the go driver return "invalid" string for a valid document seems a bit off and definitely makes the code look a bit awkward when reading and seeing this odd `\x00` check.

Look forward to the conclusion on the 0x00 type.

 

Thanks.

Comment by Isabella Siu (Inactive) [ 26/Apr/21 ]

Hi mike.dotson@its-broke.com!

If you know that the type that you're unmarshaling is a BSON document, we recommend implementing UnmarshalBSON instead. We're still discussing what to do with the 0x00 type, and will update this ticket when when we come to a conclusion.

Comment by Mike Dotson [ 23/Apr/21 ]

output from code:

2021/04/23 12:18:55 -------- test 1 --------
2021/04/23 12:18:55 document -> 00:invalid
2021/04/23 12:18:55 _id -> 07:objectID
2021/04/23 12:18:55 title -> 02:string
2021/04/23 12:18:55 subdoc -> 03:embedded document
2021/04/23 12:18:55 -------- test 2 --------
2021/04/23 12:18:55 document -> 00:invalid
2021/04/23 12:18:55 key1 -> 02:string
2021/04/23 12:18:55 key2 -> 01:double
2021/04/23 12:18:55 key3 -> 07:objectID
2021/04/23 12:18:55 key4 -> 18:64-bit integer
2021/04/23 12:18:55 key5 -> 16:32-bit integer

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