Details
-
Improvement
-
Resolution: Fixed
-
Major - P3
-
None
-
None
-
None
-
Not Needed
-
Description
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