Skip to content

Commit 17aeb0b

Browse files
authored
GODRIVER-3420 Clarify docs and comments. (#1897)
1 parent 4f4f715 commit 17aeb0b

File tree

7 files changed

+50
-22
lines changed

7 files changed

+50
-22
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/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).

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
@@ -197,11 +197,11 @@ type BSONOptions struct {
197197

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

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

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)