Skip to content

Commit 2ba80e8

Browse files
committed
update
1 parent 8091e36 commit 2ba80e8

21 files changed

+577
-556
lines changed

bson/bsoncodec.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44
// not use this file except in compliance with the License. You may obtain
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66

7+
// # ValueEncoders and ValueDecoders
8+
//
9+
// The ValueEncoder interface is implemented by types that can encode a provided Go type to BSON.
10+
// The value to encode is provided as a reflect.Value and a bsonrw.ValueWriter is used within the
11+
// EncodeValue method to actually create the BSON representation. For convenience, ValueEncoderFunc
12+
// is provided to allow use of a function with the correct signature as a ValueEncoder. An
13+
// EncodeContext instance is provided to allow implementations to lookup further ValueEncoders and
14+
// to provide configuration information.
15+
//
16+
// The ValueDecoder interface is the inverse of the ValueEncoder. Implementations should ensure that
17+
// the value they receive is settable. Similar to ValueEncoderFunc, ValueDecoderFunc is provided to
18+
// allow the use of a function with the correct signature as a ValueDecoder. A DecodeContext
19+
// instance is provided and serves similar functionality to the EncodeContext.
20+
//
21+
// # DefaultValueEncoders and DefaultValueDecoders
22+
//
23+
// The DefaultValueEncoders and DefaultValueDecoders types provide a full set of ValueEncoders and
24+
// ValueDecoders for handling a wide range of Go types, including all of the types within the
25+
// primitive package. To make registering these codecs easier, a helper method on each type is
26+
// provided. For the DefaultValueEncoders type the method is called RegisterDefaultEncoders and for
27+
// the DefaultValueDecoders type the method is called RegisterDefaultDecoders, this method also
28+
// handles registering type map entries for each BSON type.
29+
730
package bson
831

932
import (
File renamed without changes.

bson/copier.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ func copyValueFromBytes(dst ValueWriter, t Type, src []byte) error {
214214
return copyValue(dst, vr)
215215
}
216216

217-
// copyValueToBytes copies a value from src and returns it as a Type and a
217+
// CopyValueToBytes copies a value from src and returns it as a Type and a
218218
// []byte.
219219
//
220220
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.MarshalValue] instead.
221-
func copyValueToBytes(src ValueReader) (Type, []byte, error) {
221+
func CopyValueToBytes(src ValueReader) (Type, []byte, error) {
222222
return appendValueBytes(nil, src)
223223
}
224224

bson/copier_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func TestCopier(t *testing.T) {
454454
noerr(t, err)
455455
_, _, err = vr.ReadElement()
456456
noerr(t, err)
457-
btype, got, err := copyValueToBytes(vr)
457+
btype, got, err := CopyValueToBytes(vr)
458458
noerr(t, err)
459459
want := bsoncore.AppendString(nil, "world")
460460
if btype != TypeString {
@@ -466,7 +466,7 @@ func TestCopier(t *testing.T) {
466466
})
467467
t.Run("Non BytesReader", func(t *testing.T) {
468468
llvrw := &TestValueReaderWriter{t: t, bsontype: TypeString, readval: "Hello, world!"}
469-
btype, got, err := copyValueToBytes(llvrw)
469+
btype, got, err := CopyValueToBytes(llvrw)
470470
noerr(t, err)
471471
want := bsoncore.AppendString(nil, "Hello, world!")
472472
if btype != TypeString {

bson/decimal.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import (
1818
"strconv"
1919
"strings"
2020

21-
"go.mongodb.org/mongo-driver/bson/util"
21+
"go.mongodb.org/mongo-driver/internal/bsonutil/primitive"
2222
)
2323

2424
// These constants are the maximum and minimum values for the exponent field in a decimal128 value.
2525
const (
26-
MaxDecimal128Exp = util.MaxDecimal128Exp
27-
MinDecimal128Exp = util.MinDecimal128Exp
26+
MaxDecimal128Exp = 6111
27+
MinDecimal128Exp = -6176
2828
)
2929

3030
// These errors are returned when an invalid value is parsed as a big.Int.
@@ -52,7 +52,7 @@ func (d Decimal128) GetBytes() (uint64, uint64) {
5252

5353
// String returns a string representation of the decimal value.
5454
func (d Decimal128) String() string {
55-
return util.Decimal128String(d.h, d.l)
55+
return primitive.Decimal128String(d.h, d.l)
5656
}
5757

5858
// BigInt returns significand as big.Int and exponent, bi * 10 ^ exp.

bson/default_value_decoders.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ func (dvd DefaultValueDecoders) ValueUnmarshalerDecodeValue(_ DecodeContext, vr
15321532
val = val.Addr() // If the type doesn't implement the interface, a pointer to it must.
15331533
}
15341534

1535-
t, src, err := copyValueToBytes(vr)
1535+
t, src, err := CopyValueToBytes(vr)
15361536
if err != nil {
15371537
return err
15381538
}
@@ -1561,7 +1561,7 @@ func (dvd DefaultValueDecoders) UnmarshalerDecodeValue(_ DecodeContext, vr Value
15611561
val.Set(reflect.New(val.Type().Elem()))
15621562
}
15631563

1564-
_, src, err := copyValueToBytes(vr)
1564+
_, src, err := CopyValueToBytes(vr)
15651565
if err != nil {
15661566
return err
15671567
}

bson/doc.go

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@
135135
//
136136
// Manually marshaling and unmarshaling can be done with the Marshal and Unmarshal family of functions.
137137
//
138-
// ---
139-
//
140138
// bsoncodec code provides a system for encoding values to BSON representations and decoding
141139
// values from BSON representations. This package considers both binary BSON and ExtendedJSON as
142140
// BSON representations. The types in this package enable a flexible system for handling this
@@ -150,81 +148,5 @@
150148
// 2) A Registry that holds these ValueEncoders and ValueDecoders and provides methods for
151149
// retrieving them.
152150
//
153-
// # ValueEncoders and ValueDecoders
154-
//
155-
// The ValueEncoder interface is implemented by types that can encode a provided Go type to BSON.
156-
// The value to encode is provided as a reflect.Value and a bsonrw.ValueWriter is used within the
157-
// EncodeValue method to actually create the BSON representation. For convenience, ValueEncoderFunc
158-
// is provided to allow use of a function with the correct signature as a ValueEncoder. An
159-
// EncodeContext instance is provided to allow implementations to lookup further ValueEncoders and
160-
// to provide configuration information.
161-
//
162-
// The ValueDecoder interface is the inverse of the ValueEncoder. Implementations should ensure that
163-
// the value they receive is settable. Similar to ValueEncoderFunc, ValueDecoderFunc is provided to
164-
// allow the use of a function with the correct signature as a ValueDecoder. A DecodeContext
165-
// instance is provided and serves similar functionality to the EncodeContext.
166-
//
167-
// # Registry
168-
//
169-
// A Registry is a store for ValueEncoders, ValueDecoders, and a type map. See the Registry type
170-
// documentation for examples of registering various custom encoders and decoders. A Registry can
171-
// have three main types of codecs:
172-
//
173-
// 1. Type encoders/decoders - These can be registered using the RegisterTypeEncoder and
174-
// RegisterTypeDecoder methods. The registered codec will be invoked when encoding/decoding a value
175-
// whose type matches the registered type exactly.
176-
// If the registered type is an interface, the codec will be invoked when encoding or decoding
177-
// values whose type is the interface, but not for values with concrete types that implement the
178-
// interface.
179-
//
180-
// 2. Hook encoders/decoders - These can be registered using the RegisterHookEncoder and
181-
// RegisterHookDecoder methods. These methods only accept interface types and the registered codecs
182-
// will be invoked when encoding or decoding values whose types implement the interface. An example
183-
// of a hook defined by the driver is bson.Marshaler. The driver will call the MarshalBSON method
184-
// for any value whose type implements bson.Marshaler, regardless of the value's concrete type.
185-
//
186-
// 3. Type map entries - This can be used to associate a BSON type with a Go type. These type
187-
// associations are used when decoding into a bson.D/bson.M or a struct field of type interface{}.
188-
// For example, by default, BSON int32 and int64 values decode as Go int32 and int64 instances,
189-
// respectively, when decoding into a bson.D. The following code would change the behavior so these
190-
// values decode as Go int instances instead:
191-
//
192-
// intType := reflect.TypeOf(int(0))
193-
// registry.RegisterTypeMapEntry(bson.TypeInt32, intType).RegisterTypeMapEntry(bson.TypeInt64, intType)
194-
//
195-
// 4. Kind encoder/decoders - These can be registered using the RegisterDefaultEncoder and
196-
// RegisterDefaultDecoder methods. The registered codec will be invoked when encoding or decoding
197-
// values whose reflect.Kind matches the registered reflect.Kind as long as the value's type doesn't
198-
// match a registered type or hook encoder/decoder first. These methods should be used to change the
199-
// behavior for all values for a specific kind.
200-
//
201-
// # Registry Lookup Procedure
202-
//
203-
// When looking up an encoder in a Registry, the precedence rules are as follows:
204-
//
205-
// 1. A type encoder registered for the exact type of the value.
206-
//
207-
// 2. A hook encoder registered for an interface that is implemented by the value or by a pointer to
208-
// the value. If the value matches multiple hooks (e.g. the type implements bsoncodec.Marshaler and
209-
// bsoncodec.ValueMarshaler), the first one registered will be selected. Note that registries
210-
// constructed using bson.NewRegistry have driver-defined hooks registered for the
211-
// bsoncodec.Marshaler, bsoncodec.ValueMarshaler, and bsoncodec.Proxy interfaces, so those will take
212-
// precedence over any new hooks.
213-
//
214-
// 3. A kind encoder registered for the value's kind.
215-
//
216-
// If all of these lookups fail to find an encoder, an error of type ErrNoEncoder is returned. The
217-
// same precedence rules apply for decoders, with the exception that an error of type ErrNoDecoder
218-
// will be returned if no decoder is found.
219-
//
220-
// # DefaultValueEncoders and DefaultValueDecoders
221-
//
222-
// The DefaultValueEncoders and DefaultValueDecoders types provide a full set of ValueEncoders and
223-
// ValueDecoders for handling a wide range of Go types, including all of the types within the
224-
// primitive package. To make registering these codecs easier, a helper method on each type is
225-
// provided. For the DefaultValueEncoders type the method is called RegisterDefaultEncoders and for
226-
// the DefaultValueDecoders type the method is called RegisterDefaultDecoders, this method also
227-
// handles registering type map entries for each BSON type.
228-
//
229151
// [Work with BSON]: https://www.mongodb.com/docs/drivers/go/current/fundamentals/bson/
230152
package bson

bson/encoder_example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func ExampleEncoder_multipleBSONDocuments() {
137137
// Read each marshaled BSON document from the buffer and print them as
138138
// Extended JSON by converting them to bson.Raw.
139139
for {
140-
doc, err := bson.ReadRawDocument(buf)
140+
doc, err := bson.ReadDocument(buf)
141141
if errors.Is(err, io.EOF) {
142142
return
143143
}

0 commit comments

Comments
 (0)