Context
The documentation for the bson package states the following:
uint, uint32, and uint64 marshal to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32, inclusive, and BSON int64 otherwise.
The logic for this, added in GODRIVER-1358, is contradictory. The default case for the "min int" condition in the UintCodec is false / nil, which means that an type >= 32-bits will be considered int64. This can also be effected by EncodeContext.MinSize.
package main import ( "fmt" "go.mongodb.org/mongo-driver/bson" ) func main() { type foo struct { Bar uint32 } m, err := bson.MarshalExtJSON(foo{2}, true, false) if err != nil { panic(err) } fmt.Println(string(m)) }
Definition of done
The documentation should be updated to the following:
When encoding with EncodeContext.MinSize set to true, uint, uint32, and uint64 will marshal to a BSON int32 if the value falls within the inclusive range of math.MinInt32 to math.MaxInt32. Otherwise, they will marshal to a BSON int64.
Pitfalls
What should the implementer watch out for? What are the risks?