-
Type: Question
-
Resolution: Unresolved
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
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?
- is related to
-
GODRIVER-2765 Add an "omitzero" BSON struct tag
- Backlog
-
GODRIVER-2137 empty/nil slices create null field, which can't be used by various functions
- Backlog