Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-3071

Correct uint Encoding BSON Documentation

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.0.0, 1.13.2
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Fully Compatible
    • Not Needed
    • Hide

      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?

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

      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?

            Assignee:
            preston.vasquez@mongodb.com Preston Vasquez
            Reporter:
            preston.vasquez@mongodb.com Preston Vasquez
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: