Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-2976 Remove deprecated BSON code (phase 1) #1410

Merged
merged 14 commits into from
Jan 10, 2024
16 changes: 12 additions & 4 deletions bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/google/go-cmp/cmp"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonoptions"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
Expand Down Expand Up @@ -180,10 +181,17 @@ func TestMapCodec(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mapCodec := bsoncodec.NewMapCodec(tc.opts)
mapRegistry := NewRegistryBuilder().RegisterDefaultEncoder(reflect.Map, mapCodec).Build()
val, err := MarshalWithRegistry(mapRegistry, mapObj)
assert.Nil(t, err, "Marshal error: %v", err)
assert.True(t, strings.Contains(string(val), tc.key), "expected result to contain %v, got: %v", tc.key, string(val))
mapRegistry := NewRegistry()
mapRegistry.RegisterKindEncoder(reflect.Map, mapCodec)
buf := new(bytes.Buffer)
vw, err := bsonrw.NewBSONValueWriter(buf)
assert.Nil(t, err)
enc := NewEncoder(vw)
enc.SetRegistry(mapRegistry)
err = enc.Encode(mapObj)
assert.Nil(t, err, "Encode error: %v", err)
str := buf.String()
assert.True(t, strings.Contains(str, tc.key), "expected result to contain %v, got: %v", tc.key, str)
})
}
})
Expand Down
23 changes: 19 additions & 4 deletions bson/bsoncodec/registry_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bsoncodec_test

import (
"bytes"
"fmt"
"math"
"reflect"
Expand Down Expand Up @@ -66,11 +67,18 @@ func ExampleRegistry_customEncoder() {

// Marshal the document as BSON. Expect that the int field is encoded to the
// same value and that the negatedInt field is encoded as the negated value.
b, err := bson.MarshalWithRegistry(reg, doc)
buf := new(bytes.Buffer)
vw, err := bsonrw.NewBSONValueWriter(buf)
if err != nil {
panic(err)
}
fmt.Println(bson.Raw(b).String())
enc := bson.NewEncoder(vw)
enc.SetRegistry(reg)
err = enc.Encode(doc)
if err != nil {
panic(err)
}
fmt.Println(bson.Raw(buf.Bytes()).String())
// Output: {"int": {"$numberInt":"1"},"negatedint": {"$numberInt":"-1"}}
}

Expand Down Expand Up @@ -200,11 +208,18 @@ func ExampleRegistry_RegisterKindEncoder() {

// Marshal the document as BSON. Expect that all fields are encoded as BSON
// int64 (represented as "$numberLong" when encoded as Extended JSON).
b, err := bson.MarshalWithRegistry(reg, doc)
buf := new(bytes.Buffer)
vw, err := bsonrw.NewBSONValueWriter(buf)
if err != nil {
panic(err)
}
enc := bson.NewEncoder(vw)
enc.SetRegistry(reg)
err = enc.Encode(doc)
if err != nil {
panic(err)
}
fmt.Println(bson.Raw(b).String())
fmt.Println(bson.Raw(buf.Bytes()).String())
// Output: {"myint": {"$numberLong":"1"},"int32": {"$numberLong":"1"},"int64": {"$numberLong":"1"}}
}

Expand Down
35 changes: 2 additions & 33 deletions bson/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,6 @@ func NewDecoder(vr bsonrw.ValueReader) (*Decoder, error) {
}, nil
}

// NewDecoderWithContext returns a new decoder that uses DecodeContext dc to read from vr.
//
// Deprecated: Use [NewDecoder] and use the Decoder configuration methods set the desired unmarshal
// behavior instead.
func NewDecoderWithContext(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader) (*Decoder, error) {
if dc.Registry == nil {
dc.Registry = DefaultRegistry
}
if vr == nil {
return nil, errors.New("cannot create a new Decoder with a nil ValueReader")
}

return &Decoder{
dc: dc,
vr: vr,
}, nil
}

// Decode reads the next BSON document from the stream and decodes it into the
// value pointed to by val.
//
Expand Down Expand Up @@ -136,26 +118,13 @@ func (d *Decoder) Decode(val interface{}) error {

// Reset will reset the state of the decoder, using the same *DecodeContext used in
// the original construction but using vr for reading.
func (d *Decoder) Reset(vr bsonrw.ValueReader) error {
// TODO:(GODRIVER-2719): Remove error return value.
func (d *Decoder) Reset(vr bsonrw.ValueReader) {
d.vr = vr
return nil
}

// SetRegistry replaces the current registry of the decoder with r.
func (d *Decoder) SetRegistry(r *bsoncodec.Registry) error {
// TODO:(GODRIVER-2719): Remove error return value.
func (d *Decoder) SetRegistry(r *bsoncodec.Registry) {
d.dc.Registry = r
return nil
}

// SetContext replaces the current registry of the decoder with dc.
//
// Deprecated: Use the Decoder configuration methods to set the desired unmarshal behavior instead.
func (d *Decoder) SetContext(dc bsoncodec.DecodeContext) error {
// TODO:(GODRIVER-2719): Remove error return value.
d.dc = dc
return nil
}

// DefaultDocumentM causes the Decoder to always unmarshal documents into the primitive.M type. This
Expand Down
40 changes: 7 additions & 33 deletions bson/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestDecoderv2(t *testing.T) {

got := reflect.New(tc.sType).Interface()
vr := bsonrw.NewBSONDocumentReader(tc.data)
dec, err := NewDecoderWithContext(bsoncodec.DecodeContext{Registry: DefaultRegistry}, vr)
dec, err := NewDecoder(vr)
noerr(t, err)
err = dec.Decode(got)
noerr(t, err)
Expand Down Expand Up @@ -177,8 +177,7 @@ func TestDecoderv2(t *testing.T) {
t.Run("errors", func(t *testing.T) {
t.Parallel()

dc := bsoncodec.DecodeContext{Registry: DefaultRegistry}
_, got := NewDecoderWithContext(dc, nil)
_, got := NewDecoder(nil)
want := errors.New("cannot create a new Decoder with a nil ValueReader")
if !cmp.Equal(got, want, cmp.Comparer(compareErrors)) {
t.Errorf("Was expecting error but got different error. got %v; want %v", got, want)
Expand All @@ -187,13 +186,7 @@ func TestDecoderv2(t *testing.T) {
t.Run("success", func(t *testing.T) {
t.Parallel()

got, err := NewDecoderWithContext(bsoncodec.DecodeContext{}, bsonrw.NewBSONDocumentReader([]byte{}))
noerr(t, err)
if got == nil {
t.Errorf("Was expecting a non-nil Decoder, but got <nil>")
}
dc := bsoncodec.DecodeContext{Registry: DefaultRegistry}
got, err = NewDecoderWithContext(dc, bsonrw.NewBSONDocumentReader([]byte{}))
got, err := NewDecoder(bsonrw.NewBSONDocumentReader([]byte{}))
noerr(t, err)
if got == nil {
t.Errorf("Was expecting a non-nil Decoder, but got <nil>")
Expand Down Expand Up @@ -224,47 +217,28 @@ func TestDecoderv2(t *testing.T) {
t.Parallel()

vr1, vr2 := bsonrw.NewBSONDocumentReader([]byte{}), bsonrw.NewBSONDocumentReader([]byte{})
dc := bsoncodec.DecodeContext{Registry: DefaultRegistry}
dec, err := NewDecoderWithContext(dc, vr1)
dec, err := NewDecoder(vr1)
noerr(t, err)
if dec.vr != vr1 {
t.Errorf("Decoder should use the value reader provided. got %v; want %v", dec.vr, vr1)
}
err = dec.Reset(vr2)
noerr(t, err)
dec.Reset(vr2)
if dec.vr != vr2 {
t.Errorf("Decoder should use the value reader provided. got %v; want %v", dec.vr, vr2)
}
})
t.Run("SetContext", func(t *testing.T) {
t.Parallel()

dc1 := bsoncodec.DecodeContext{Registry: DefaultRegistry}
dc2 := bsoncodec.DecodeContext{Registry: NewRegistryBuilder().Build()}
dec, err := NewDecoderWithContext(dc1, bsonrw.NewBSONDocumentReader([]byte{}))
noerr(t, err)
if !reflect.DeepEqual(dec.dc, dc1) {
t.Errorf("Decoder should use the Registry provided. got %v; want %v", dec.dc, dc1)
}
err = dec.SetContext(dc2)
noerr(t, err)
if !reflect.DeepEqual(dec.dc, dc2) {
t.Errorf("Decoder should use the Registry provided. got %v; want %v", dec.dc, dc2)
}
})
t.Run("SetRegistry", func(t *testing.T) {
t.Parallel()

r1, r2 := DefaultRegistry, NewRegistryBuilder().Build()
r1, r2 := DefaultRegistry, NewRegistry()
dc1 := bsoncodec.DecodeContext{Registry: r1}
dc2 := bsoncodec.DecodeContext{Registry: r2}
dec, err := NewDecoder(bsonrw.NewBSONDocumentReader([]byte{}))
noerr(t, err)
if !reflect.DeepEqual(dec.dc, dc1) {
t.Errorf("Decoder should use the Registry provided. got %v; want %v", dec.dc, dc1)
}
err = dec.SetRegistry(r2)
noerr(t, err)
dec.SetRegistry(r2)
if !reflect.DeepEqual(dec.dc, dc2) {
t.Errorf("Decoder should use the Registry provided. got %v; want %v", dec.dc, dc2)
}
Expand Down
43 changes: 3 additions & 40 deletions bson/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package bson

import (
"errors"
"reflect"
"sync"

Expand Down Expand Up @@ -41,34 +40,11 @@ type Encoder struct {
}

// NewEncoder returns a new encoder that uses the DefaultRegistry to write to vw.
func NewEncoder(vw bsonrw.ValueWriter) (*Encoder, error) {
// TODO:(GODRIVER-2719): Remove error return value.
if vw == nil {
return nil, errors.New("cannot create a new Encoder with a nil ValueWriter")
}

func NewEncoder(vw bsonrw.ValueWriter) *Encoder {
return &Encoder{
ec: bsoncodec.EncodeContext{Registry: DefaultRegistry},
vw: vw,
}, nil
}

// NewEncoderWithContext returns a new encoder that uses EncodeContext ec to write to vw.
//
// Deprecated: Use [NewEncoder] and use the Encoder configuration methods to set the desired marshal
// behavior instead.
func NewEncoderWithContext(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter) (*Encoder, error) {
if ec.Registry == nil {
ec = bsoncodec.EncodeContext{Registry: DefaultRegistry}
}
if vw == nil {
return nil, errors.New("cannot create a new Encoder with a nil ValueWriter")
}

return &Encoder{
ec: ec,
vw: vw,
}, nil
}

// Encode writes the BSON encoding of val to the stream.
Expand Down Expand Up @@ -121,26 +97,13 @@ func (e *Encoder) Encode(val interface{}) error {

// Reset will reset the state of the Encoder, using the same *EncodeContext used in
// the original construction but using vw.
func (e *Encoder) Reset(vw bsonrw.ValueWriter) error {
// TODO:(GODRIVER-2719): Remove error return value.
func (e *Encoder) Reset(vw bsonrw.ValueWriter) {
e.vw = vw
return nil
}

// SetRegistry replaces the current registry of the Encoder with r.
func (e *Encoder) SetRegistry(r *bsoncodec.Registry) error {
// TODO:(GODRIVER-2719): Remove error return value.
func (e *Encoder) SetRegistry(r *bsoncodec.Registry) {
e.ec.Registry = r
return nil
}

// SetContext replaces the current EncodeContext of the encoder with ec.
//
// Deprecated: Use the Encoder configuration methods set the desired marshal behavior instead.
func (e *Encoder) SetContext(ec bsoncodec.EncodeContext) error {
// TODO:(GODRIVER-2719): Remove error return value.
e.ec = ec
return nil
}

// ErrorOnInlineDuplicates causes the Encoder to return an error if there is a duplicate field in
Expand Down
30 changes: 6 additions & 24 deletions bson/encoder_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ func ExampleEncoder() {
if err != nil {
panic(err)
}
encoder, err := bson.NewEncoder(vw)
if err != nil {
panic(err)
}
encoder := bson.NewEncoder(vw)

type Product struct {
Name string `bson:"name"`
Expand Down Expand Up @@ -67,10 +64,7 @@ func ExampleEncoder_StringifyMapKeysWithFmt() {
if err != nil {
panic(err)
}
encoder, err := bson.NewEncoder(vw)
if err != nil {
panic(err)
}
encoder := bson.NewEncoder(vw)

// Configure the Encoder to convert Go map keys to BSON document field names
// using fmt.Sprintf instead of the default string conversion logic.
Expand Down Expand Up @@ -98,10 +92,7 @@ func ExampleEncoder_UseJSONStructTags() {
if err != nil {
panic(err)
}
encoder, err := bson.NewEncoder(vw)
if err != nil {
panic(err)
}
encoder := bson.NewEncoder(vw)

type Product struct {
Name string `json:"name"`
Expand Down Expand Up @@ -137,10 +128,7 @@ func ExampleEncoder_multipleBSONDocuments() {
if err != nil {
panic(err)
}
encoder, err := bson.NewEncoder(vw)
if err != nil {
panic(err)
}
encoder := bson.NewEncoder(vw)

type Coordinate struct {
X int
Expand Down Expand Up @@ -187,10 +175,7 @@ func ExampleEncoder_extendedJSON() {
if err != nil {
panic(err)
}
encoder, err := bson.NewEncoder(vw)
if err != nil {
panic(err)
}
encoder := bson.NewEncoder(vw)

type Product struct {
Name string `bson:"name"`
Expand Down Expand Up @@ -222,10 +207,7 @@ func ExampleEncoder_multipleExtendedJSONDocuments() {
if err != nil {
panic(err)
}
encoder, err := bson.NewEncoder(vw)
if err != nil {
panic(err)
}
encoder := bson.NewEncoder(vw)

type Coordinate struct {
X int
Expand Down
9 changes: 3 additions & 6 deletions bson/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func TestEncoderEncode(t *testing.T) {
got := make(bsonrw.SliceWriter, 0, 1024)
vw, err := bsonrw.NewBSONValueWriter(&got)
noerr(t, err)
enc, err := NewEncoder(vw)
noerr(t, err)
enc := NewEncoder(vw)
err = enc.Encode(tc.val)
noerr(t, err)

Expand Down Expand Up @@ -105,8 +104,7 @@ func TestEncoderEncode(t *testing.T) {
vw, err = bsonrw.NewBSONValueWriter(&b)
noerr(t, err)
}
enc, err := NewEncoder(vw)
noerr(t, err)
enc := NewEncoder(vw)
got := enc.Encode(marshaler)
want := tc.wanterr
if !compareErrors(got, want) {
Expand Down Expand Up @@ -285,8 +283,7 @@ func TestEncoderConfiguration(t *testing.T) {
got := new(bytes.Buffer)
vw, err := bsonrw.NewBSONValueWriter(got)
require.NoError(t, err, "bsonrw.NewBSONValueWriter error")
enc, err := NewEncoder(vw)
require.NoError(t, err, "NewEncoder error")
enc := NewEncoder(vw)

tc.configure(enc)

Expand Down
Loading
Loading