You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
assert.EqualError(t, err, "error decoding key id: decoding an object ID to a non-hexadecimal string representation is not supported")
650
+
constwant="error decoding key id: decoding an object ID into a string is not supported by default (set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal string)"
Copy file name to clipboardExpand all lines: docs/migration-2.0.md
+44-4Lines changed: 44 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -867,6 +867,34 @@ The `NewRegistryBuilder` function has been removed along with the `bsoncodec.Reg
867
867
868
868
### Decoder
869
869
870
+
The BSON decoding logic has changed to decode into a `bson.D` by default.
871
+
872
+
The example shows the behavior change.
873
+
874
+
```go
875
+
// v1
876
+
877
+
b1:= bson.M{"a": 1, "b": bson.M{"c": 2}}
878
+
b2, _:= bson.Marshal(b1)
879
+
b3:= bson.M{}
880
+
bson.Unmarshal(b2, &b3)
881
+
fmt.Printf("b3.b type: %T\n", b3["b"])
882
+
// Output: b3.b type: primitive.M
883
+
```
884
+
885
+
```go
886
+
// v2
887
+
888
+
b1:= bson.M{"a": 1, "b": bson.M{"c": 2}}
889
+
b2, _:= bson.Marshal(b1)
890
+
b3:= bson.M{}
891
+
bson.Unmarshal(b2, &b3)
892
+
fmt.Printf("b3.b type: %T\n", b3["b"])
893
+
// Output: b3.b type: bson.D
894
+
```
895
+
896
+
Use `Decoder.DefaultDocumentM()` or set the `DefaultDocumentM` field of `options.BSONOptions` to always decode documents into the `bson.M` type.
897
+
870
898
#### NewDecoder
871
899
872
900
The signature of `NewDecoder` has been updated without an error being returned.
@@ -1043,11 +1071,11 @@ The signature of `Reset` has been updated without an error being returned.
1043
1071
1044
1072
#### DefaultDocumentD / DefaultDocumentM
1045
1073
1046
-
`Decoder.DefaultDocumentD` has been removed since a document, including a top-level value (e.g. you pass in an empty interface value to Decode), is always decoded into a `bson.D` by default. Therefore, use `Decoder.DefaultDocumentM` to always decode a document into a `bson.M` to avoid unexpected decode results.
1074
+
`Decoder.DefaultDocumentD()` has been removed since a document, including a top-level value (e.g. you pass in an empty interface value to Decode), is always decoded into a `bson.D` by default. Therefore, use `Decoder.DefaultDocumentM()` to always decode a document into a `bson.M` to avoid unexpected decode results.
1047
1075
1048
1076
#### ObjectIDAsHexString
1049
1077
1050
-
`Decoder.ObjectIDAsHexString` method enables decoding a BSON ObjectId as a hexadecimal string. Otherwise, the decoder returns an error by default instead of decoding as the UTF-8 representation of the raw ObjectId bytes, which results in a garbled and unusable string.
1078
+
`Decoder.ObjectIDAsHexString()` method enables decoding a BSON ObjectId as a hexadecimal string. Otherwise, the decoder returns an error by default instead of decoding as the UTF-8 representation of the raw ObjectId bytes, which results in a garbled and unusable string.
1051
1079
1052
1080
### Encoder
1053
1081
@@ -1183,8 +1211,20 @@ A new `RawArray` type has been added to the `bson` package as a primitive type t
1183
1211
1184
1212
### ValueMarshaler
1185
1213
1186
-
The `MarshalBSONValue` method of the `ValueMarshaler` interface is only required to return a byte type value representing the BSON type to avoid importing the `bsontype` package.
1214
+
The `MarshalBSONValue` method of the [ValueMarshaler](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#ValueMarshaler) interface now returns a `byte` value representing the [BSON type](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#Type). That allows external packages to implement the `ValueMarshaler` interface without having to import the `bson` package. Convert a returned `byte` value to [bson.Type](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#Type) to compare with the BSON type constants. For example:
1215
+
1216
+
```go
1217
+
btype, _, _:= m.MarshalBSONValue()
1218
+
fmt.Println("type of data: %s: ", bson.Type(btype))
1219
+
fmt.Println("type of data is an array: %v", bson.Type(btype) == bson.TypeArray)
1220
+
```
1187
1221
1188
1222
### ValueUnmarshaler
1189
1223
1190
-
The `UnmarshalBSONValue` method of the `ValueUnmarshaler` interface is only required to take a byte type argument representing the BSON type to avoid importing the Go driver package.
1224
+
The `UnmarshalBSONValue` method of the [ValueUnmarshaler](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#ValueUnmarshaler) interface now accepts a `byte` value representing the [BSON type](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/bson#Type) for the first argument. That allows packages to implement `ValueUnmarshaler` without having to import the `bson` package. For example:
0 commit comments