[GODRIVER-3071] Correct uint Encoding BSON Documentation Created: 14/Dec/23  Updated: 16/Jan/24  Resolved: 12/Jan/24

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.0.0, 1.13.2

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

Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

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?



 Comments   
Comment by Githook User [ 16/Jan/24 ]

Author:

{'name': 'Steven Silvester', 'email': 'steven.silvester@ieee.org', 'username': 'blink1073'}

Message: GODRIVER-3071 [master] Correct uint Encoding BSON Documentation (#1516)

Co-authored-by: Preston Vasquez <prestonvasquez@icloud.com>
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/91b9075a5334e221c571408eaaf7fb801a9adefc

Comment by Githook User [ 12/Jan/24 ]

Author:

{'name': 'Preston Vasquez', 'email': 'prestonvasquez@icloud.com', 'username': 'prestonvasquez'}

Message: GODRIVER-3071 Correct uint Encoding BSON Documentation (#1500)

Co-authored-by: Matt Dale <9760375+matthewdale@users.noreply.github.com>
Branch: v1
https://github.com/mongodb/mongo-go-driver/commit/6740ce0fb5a9c53803ee6310a6b6d43763d6545d

Comment by KeAna Moutra [ 18/Dec/23 ]

Example usage : https://www.mongodb.com/community/forums/t/mongo-go-driver-uint-always-marshals-to-bson-int64/257633

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