From af33387144aa318e3c63cd2398899c14fc0e7ff1 Mon Sep 17 00:00:00 2001 From: Preston Vasquez <24281431+prestonvasquez@users.noreply.github.com> Date: Mon, 18 Dec 2023 15:29:52 -0700 Subject: [PATCH 1/4] GODRIVER-3071 Correct uint Encoding BSON Documentation --- bson/doc.go | 5 +++-- bson/encoder_example_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/bson/doc.go b/bson/doc.go index 048b5eb998..8cba7bee03 100644 --- a/bson/doc.go +++ b/bson/doc.go @@ -70,8 +70,9 @@ // otherwise. // 4. int64 marshals to BSON int64. // 5. uint8 and uint16 marshal to a BSON int32. -// 6. uint, uint32, and uint64 marshal to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32, -// inclusive, and BSON int64 otherwise. +// 6. 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. See Encoder examples for usage of EncodeContext.MinSize. // 7. BSON null and undefined values will unmarshal into the zero value of a field (e.g. unmarshalling a BSON null or // undefined value into a string will yield the empty string.). // diff --git a/bson/encoder_example_test.go b/bson/encoder_example_test.go index b52e999d45..057011548b 100644 --- a/bson/encoder_example_test.go +++ b/bson/encoder_example_test.go @@ -252,3 +252,33 @@ func ExampleEncoder_multipleExtendedJSONDocuments() { // {"x":{"$numberInt":"3"},"y":{"$numberInt":"4"}} // {"x":{"$numberInt":"4"},"y":{"$numberInt":"5"}} } + +func ExampleEncoder_IntMinSize() { + // Create an encoder that will marshal integers as the minimum BSON int size + // (either 32 or 64 bits) that can represent the integer value. + type foo struct { + Bar uint32 + } + + buf := new(bytes.Buffer) + vw, err := bsonrw.NewBSONValueWriter(buf) + if err != nil { + panic(err) + } + + enc, err := bson.NewEncoder(vw) + if err != nil { + panic(err) + } + + enc.IntMinSize() + + err = enc.Encode(foo{2}) + if err != nil { + panic(err) + } + + fmt.Println(bson.Raw(buf.Bytes()).String()) + // Output: + // {"bar": {"$numberInt":"2"}} +} From 40abec451da7334cb8b606a6ec776c4b1c9fb4ed Mon Sep 17 00:00:00 2001 From: Preston Vasquez <24281431+prestonvasquez@users.noreply.github.com> Date: Fri, 29 Dec 2023 13:29:51 -0700 Subject: [PATCH 2/4] GODRIVER-3071 Correct documentation --- bson/doc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bson/doc.go b/bson/doc.go index 8cba7bee03..60c2f31942 100644 --- a/bson/doc.go +++ b/bson/doc.go @@ -72,7 +72,7 @@ // 5. uint8 and uint16 marshal to a BSON int32. // 6. 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. See Encoder examples for usage of EncodeContext.MinSize. +// BSON int64. See Encoder examples for usage of Encoder.IntMinSize. // 7. BSON null and undefined values will unmarshal into the zero value of a field (e.g. unmarshalling a BSON null or // undefined value into a string will yield the empty string.). // From 6ba51d39dc1e4f47497023a17ed51d4a88aaf076 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Fri, 5 Jan 2024 12:18:38 -0700 Subject: [PATCH 3/4] Update bson/doc.go Co-authored-by: Matt Dale <9760375+matthewdale@users.noreply.github.com> --- bson/doc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bson/doc.go b/bson/doc.go index 60c2f31942..661bf88893 100644 --- a/bson/doc.go +++ b/bson/doc.go @@ -68,7 +68,7 @@ // 2. int8, int16, and int32 marshal to a BSON int32. // 3. int marshals to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32, inclusive, and a BSON int64 // otherwise. -// 4. int64 marshals to BSON int64. +// 4. int64 marshals to BSON int64 (unless [Encoder.IntMinSize] is set). // 5. uint8 and uint16 marshal to a BSON int32. // 6. 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 From 62cdb0a1dae1c520b38412ad37bd142353d125fe Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Fri, 5 Jan 2024 12:18:43 -0700 Subject: [PATCH 4/4] Update bson/doc.go Co-authored-by: Matt Dale <9760375+matthewdale@users.noreply.github.com> --- bson/doc.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bson/doc.go b/bson/doc.go index 661bf88893..05e33a4412 100644 --- a/bson/doc.go +++ b/bson/doc.go @@ -70,9 +70,7 @@ // otherwise. // 4. int64 marshals to BSON int64 (unless [Encoder.IntMinSize] is set). // 5. uint8 and uint16 marshal to a BSON int32. -// 6. 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. See Encoder examples for usage of Encoder.IntMinSize. +// 6. uint, uint32, and uint64 marshal to a BSON int64 (unless [Encoder.IntMinSize] is set). // 7. BSON null and undefined values will unmarshal into the zero value of a field (e.g. unmarshalling a BSON null or // undefined value into a string will yield the empty string.). //