|
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?
|