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

Add bson.UnmarshalValue function

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.12.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • 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?

      When marshalling Go types to BSON, users can choose between Marshal(), which assumes the value is a document, and MarshalValue(), which works for all values and returns both a type and a slice of bytes.

      For going from BSON bytes to Go, the Unmarshal() function mirrors Marshal() and assumes the input is a document. If the input is some other value like an array, users currently need to do:

      val := bson.RawValue{
          Type: bsontype.Array, // or whatever the type is
          Value: bsonBytes,
      }
      var unmarshalled bson.A
      if err := val.Unmarshal(&unmarshalled); err != nil {
         ...
      }

      We should consider adding a UnmarshalValue() function to mirror MarshalValue().

      Definition of done:

      • Add a function bson.UnmarshalValue that, given a BSON type and a byte slice, unmarshals the BSON value into a Go value. Its signature should mirror the bson.MarshalValue API.
        For example:
        func UnmarshalValue(t bsontype.Type, data []byte, val interface{}) error
        

            Assignee:
            qingyang.hu@mongodb.com Qingyang Hu
            Reporter:
            divjot.arora@mongodb.com Divjot Arora (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: