[GODRIVER-2920] Should empty bson.Raw follow the json.RawMessage encoding conventions? Created: 26/Jul/23  Updated: 31/Jul/23

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

Type: Question Priority: Unknown
Reporter: Preston Vasquez Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-2765 Add an "omitnil" BSON struct tag Backlog
is related to GODRIVER-2137 empty/nil slices create null field, w... Backlog

 Description   

If a user tries to encode a struct with a reference to an empty bson.Raw, the marshaler will return the following error:

couldn't read length from src, not enough bytes. length=0

This is because the encoder is attempting to marshal an empty byte slice into a BSON document which is invalid. You can use a pointer reference to avoid this error:

	type rd2 struct {
		Foo *bson.Raw
	}

The user can also use the "omitempty" tag:

	type rd2 struct {
		Foo bson.Raw `bson:",omitempty"`
	}

It’s interesting to note that the encoding/json package does not follow this convention and simply converts the RawMessage field to “null”: https://go.dev/play/p/fLnMX-nstJr?v=goprev

Should the bson package determine if a "Raw" value is zero and then marshal the data to "null" as the default?


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