Skip to content

Commit 239e3f0

Browse files
Merge branch 'GODRIVER-3434' of github.com:prestonvasquez/mongo-go-driver into GODRIVER-3434
2 parents fa6cb74 + 0b8261e commit 239e3f0

File tree

9 files changed

+60
-28
lines changed

9 files changed

+60
-28
lines changed

bson/decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (d *Decoder) SetRegistry(r *Registry) {
8787
d.dc.Registry = r
8888
}
8989

90-
// DefaultDocumentM causes the Decoder to always unmarshal documents into the primitive.M type. This
90+
// DefaultDocumentM causes the Decoder to always unmarshal documents into the bson.M type. This
9191
// behavior is restricted to data typed as "interface{}" or "map[string]interface{}".
9292
func (d *Decoder) DefaultDocumentM() {
9393
d.dc.defaultDocumentType = reflect.TypeOf(M{})
@@ -101,7 +101,7 @@ func (d *Decoder) AllowTruncatingDoubles() {
101101
}
102102

103103
// BinaryAsSlice causes the Decoder to unmarshal BSON binary field values that are the "Generic" or
104-
// "Old" BSON binary subtype as a Go byte slice instead of a primitive.Binary.
104+
// "Old" BSON binary subtype as a Go byte slice instead of a bson.Binary.
105105
func (d *Decoder) BinaryAsSlice() {
106106
d.dc.binaryAsSlice = true
107107
}

bson/decoder_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ func TestDecoderConfiguration(t *testing.T) {
647647

648648
var got objectIDTest
649649
err := dec.Decode(&got)
650-
assert.EqualError(t, err, "error decoding key id: decoding an object ID to a non-hexadecimal string representation is not supported")
650+
const want = "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)"
651+
assert.EqualError(t, err, want)
651652
})
652653
t.Run("DefaultDocumentM top-level", func(t *testing.T) {
653654
t.Parallel()

bson/doc.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@
4747
// 5. BSON boolean unmarshals to a bool.
4848
// 6. BSON embedded document unmarshals to the parent type (i.e. D for a D, M for an M).
4949
// 7. BSON array unmarshals to a bson.A.
50-
// 8. BSON ObjectId unmarshals to a primitive.ObjectID.
51-
// 9. BSON datetime unmarshals to a primitive.DateTime.
52-
// 10. BSON binary unmarshals to a primitive.Binary.
53-
// 11. BSON regular expression unmarshals to a primitive.Regex.
54-
// 12. BSON JavaScript unmarshals to a primitive.JavaScript.
55-
// 13. BSON code with scope unmarshals to a primitive.CodeWithScope.
56-
// 14. BSON timestamp unmarshals to an primitive.Timestamp.
57-
// 15. BSON 128-bit decimal unmarshals to an primitive.Decimal128.
58-
// 16. BSON min key unmarshals to an primitive.MinKey.
59-
// 17. BSON max key unmarshals to an primitive.MaxKey.
60-
// 18. BSON undefined unmarshals to a primitive.Undefined.
50+
// 8. BSON ObjectId unmarshals to a bson.ObjectID.
51+
// 9. BSON datetime unmarshals to a bson.DateTime.
52+
// 10. BSON binary unmarshals to a bson.Binary.
53+
// 11. BSON regular expression unmarshals to a bson.Regex.
54+
// 12. BSON JavaScript unmarshals to a bson.JavaScript.
55+
// 13. BSON code with scope unmarshals to a bson.CodeWithScope.
56+
// 14. BSON timestamp unmarshals to an bson.Timestamp.
57+
// 15. BSON 128-bit decimal unmarshals to an bson.Decimal128.
58+
// 16. BSON min key unmarshals to an bson.MinKey.
59+
// 17. BSON max key unmarshals to an bson.MaxKey.
60+
// 18. BSON undefined unmarshals to a bson.Undefined.
6161
// 19. BSON null unmarshals to nil.
62-
// 20. BSON DBPointer unmarshals to a primitive.DBPointer.
63-
// 21. BSON symbol unmarshals to a primitive.Symbol.
62+
// 20. BSON DBPointer unmarshals to a bson.DBPointer.
63+
// 21. BSON symbol unmarshals to a bson.Symbol.
6464
//
6565
// The above mappings also apply when marshaling a D or M to BSON. Some other useful marshaling mappings are:
6666
//

bson/primitive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (d DateTime) MarshalJSON() ([]byte, error) {
6060
return d.Time().UTC().MarshalJSON()
6161
}
6262

63-
// UnmarshalJSON creates a primitive.DateTime from a JSON string.
63+
// UnmarshalJSON creates a bson.DateTime from a JSON string.
6464
func (d *DateTime) UnmarshalJSON(data []byte) error {
6565
// Ignore "null" so that we can distinguish between a "null" value and
6666
// valid value that is the zero time (as reported by time.Time.IsZero).

bson/string_codec.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package bson
88

99
import (
10+
"errors"
1011
"fmt"
1112
"reflect"
1213
)
@@ -50,14 +51,16 @@ func (sc *stringCodec) decodeType(dc DecodeContext, vr ValueReader, t reflect.Ty
5051
return emptyValue, err
5152
}
5253
case TypeObjectID:
53-
oid, err := vr.ReadObjectID()
54-
if err != nil {
55-
return emptyValue, err
56-
}
5754
if dc.objectIDAsHexString {
55+
oid, err := vr.ReadObjectID()
56+
if err != nil {
57+
return emptyValue, err
58+
}
5859
str = oid.Hex()
5960
} else {
60-
return emptyValue, fmt.Errorf("decoding an object ID to a non-hexadecimal string representation is not supported")
61+
const msg = "decoding an object ID into a string is not supported by default " +
62+
"(set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal string)"
63+
return emptyValue, errors.New(msg)
6164
}
6265
case TypeSymbol:
6366
str, err = vr.ReadSymbol()

docs/migration-2.0.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,34 @@ The `NewRegistryBuilder` function has been removed along with the `bsoncodec.Reg
867867

868868
### Decoder
869869

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+
870898
#### NewDecoder
871899

872900
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.
10431071

10441072
#### DefaultDocumentD / DefaultDocumentM
10451073

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.
10471075

10481076
#### ObjectIDAsHexString
10491077

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.
10511079

10521080
### Encoder
10531081

mongo/gridfs_download_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type GridFSDownloadStream struct {
5252
// GridFSDownloadStream.GetFile method.
5353
type GridFSFile struct {
5454
// ID is the file's ID. This will match the file ID specified when uploading the file. If an upload helper that
55-
// does not require a file ID was used, this field will be a primitive.ObjectID.
55+
// does not require a file ID was used, this field will be a bson.ObjectID.
5656
ID interface{}
5757

5858
// Length is the length of this file in bytes.

mongo/options/clientoptions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ type BSONOptions struct {
198198

199199
// BinaryAsSlice causes the driver to unmarshal BSON binary field values
200200
// that are the "Generic" or "Old" BSON binary subtype as a Go byte slice
201-
// instead of a primitive.Binary.
201+
// instead of a bson.Binary.
202202
BinaryAsSlice bool
203203

204204
// DefaultDocumentM causes the driver to always unmarshal documents into the
205-
// primitive.M type. This behavior is restricted to data typed as
205+
// bson.M type. This behavior is restricted to data typed as
206206
// "interface{}" or "map[string]interface{}".
207207
DefaultDocumentM bool
208208

mongo/options/encryptoptions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func Encrypt() *EncryptOptionsBuilder {
128128
return &EncryptOptionsBuilder{}
129129
}
130130

131-
// SetKeyID specifies an _id of a data key. This should be a UUID (a primitive.Binary with subtype 4).
131+
// SetKeyID specifies an _id of a data key. This should be a UUID (a bson.Binary with subtype 4).
132132
func (e *EncryptOptionsBuilder) SetKeyID(keyID bson.Binary) *EncryptOptionsBuilder {
133133
e.Opts = append(e.Opts, func(opts *EncryptOptions) error {
134134
opts.KeyID = &keyID

0 commit comments

Comments
 (0)