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. #1698

Merged
merged 8 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,6 @@ func noerr(t *testing.T, err error) {
}
}

func TestCompareTimestamp(t *testing.T) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code

testcases := []struct {
name string
tp Timestamp
tp2 Timestamp
expected int
}{
{"equal", Timestamp{T: 12345, I: 67890}, Timestamp{T: 12345, I: 67890}, 0},
{"T greater than", Timestamp{T: 12345, I: 67890}, Timestamp{T: 2345, I: 67890}, 1},
{"I greater than", Timestamp{T: 12345, I: 67890}, Timestamp{T: 12345, I: 7890}, 1},
{"T less than", Timestamp{T: 12345, I: 67890}, Timestamp{T: 112345, I: 67890}, -1},
{"I less than", Timestamp{T: 12345, I: 67890}, Timestamp{T: 12345, I: 167890}, -1},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
result := CompareTimestamp(tc.tp, tc.tp2)
require.Equal(t, tc.expected, result)
})
}
}

func TestTimestamp(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 2 additions & 2 deletions bson/cond_addr_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (cae *condAddrEncoder) EncodeValue(ec EncodeContext, vw ValueWriter, val re
if cae.elseEnc != nil {
return cae.elseEnc.EncodeValue(ec, vw, val)
}
return ErrNoEncoder{Type: val.Type()}
return errNoEncoder{Type: val.Type()}
}

// condAddrDecoder is the decoder used when a pointer to the value has a decoder.
Expand All @@ -57,5 +57,5 @@ func (cad *condAddrDecoder) DecodeValue(dc DecodeContext, vr ValueReader, val re
if cad.elseDec != nil {
return cad.elseDec.DecodeValue(dc, vr, val)
}
return ErrNoDecoder{Type: val.Type()}
return errNoDecoder{Type: val.Type()}
}
4 changes: 2 additions & 2 deletions bson/cond_addr_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestCondAddrCodec(t *testing.T) {
t.Run("error", func(t *testing.T) {
errEncoder := newCondAddrEncoder(encode1, nil)
err := errEncoder.EncodeValue(EncodeContext{}, rw, unaddressable)
want := ErrNoEncoder{Type: unaddressable.Type()}
want := errNoEncoder{Type: unaddressable.Type()}
assert.Equal(t, err, want, "expected error %v, got %v", want, err)
})
})
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestCondAddrCodec(t *testing.T) {
t.Run("error", func(t *testing.T) {
errDecoder := newCondAddrDecoder(decode1, nil)
err := errDecoder.DecodeValue(DecodeContext{}, rw, unaddressable)
want := ErrNoDecoder{Type: unaddressable.Type()}
want := errNoDecoder{Type: unaddressable.Type()}
assert.Equal(t, err, want, "expected error %v, got %v", want, err)
})
})
Expand Down
66 changes: 18 additions & 48 deletions bson/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (
)

// copyDocument handles copying one document from the src to the dst.
//
// Deprecated: Copying BSON documents using the ValueWriter and ValueReader interfaces will not be
// supported in Go Driver 2.0.
func copyDocument(dst ValueWriter, src ValueReader) error {
dr, err := src.ReadDocument()
if err != nil {
Expand All @@ -34,9 +31,6 @@ func copyDocument(dst ValueWriter, src ValueReader) error {

// copyArrayFromBytes copies the values from a BSON array represented as a
// []byte to a ValueWriter.
//
// Deprecated: Copying BSON arrays using the ValueWriter and ValueReader interfaces will not be
// supported in Go Driver 2.0.
func copyArrayFromBytes(dst ValueWriter, src []byte) error {
aw, err := dst.WriteArray()
if err != nil {
Expand All @@ -53,9 +47,6 @@ func copyArrayFromBytes(dst ValueWriter, src []byte) error {

// copyDocumentFromBytes copies the values from a BSON document represented as a
// []byte to a ValueWriter.
//
// Deprecated: Copying BSON documents using the ValueWriter and ValueReader interfaces will not be
// supported in Go Driver 2.0.
func copyDocumentFromBytes(dst ValueWriter, src []byte) error {
dw, err := dst.WriteDocument()
if err != nil {
Expand All @@ -74,9 +65,6 @@ type writeElementFn func(key string) (ValueWriter, error)

// copyBytesToArrayWriter copies the values from a BSON Array represented as a []byte to an
// ArrayWriter.
//
// Deprecated: Copying BSON arrays using the ArrayWriter interface will not be supported in Go
// Driver 2.0.
func copyBytesToArrayWriter(dst ArrayWriter, src []byte) error {
wef := func(_ string) (ValueWriter, error) {
return dst.WriteArrayElement()
Expand All @@ -87,9 +75,6 @@ func copyBytesToArrayWriter(dst ArrayWriter, src []byte) error {

// copyBytesToDocumentWriter copies the values from a BSON document represented as a []byte to a
// DocumentWriter.
//
// Deprecated: Copying BSON documents using the ValueWriter and ValueReader interfaces will not be
// supported in Go Driver 2.0.
func copyBytesToDocumentWriter(dst DocumentWriter, src []byte) error {
wef := func(key string) (ValueWriter, error) {
return dst.WriteDocumentElement(key)
Expand Down Expand Up @@ -149,18 +134,12 @@ func copyBytesToValueWriter(src []byte, wef writeElementFn) error {

// copyDocumentToBytes copies an entire document from the ValueReader and
// returns it as bytes.
//
// Deprecated: Copying BSON documents using the ValueWriter and ValueReader interfaces will not be
// supported in Go Driver 2.0.
func copyDocumentToBytes(src ValueReader) ([]byte, error) {
return appendDocumentBytes(nil, src)
}

// appendDocumentBytes functions the same as CopyDocumentToBytes, but will
// append the result to dst.
//
// Deprecated: Copying BSON documents using the ValueWriter and ValueReader interfaces will not be
// supported in Go Driver 2.0.
func appendDocumentBytes(dst []byte, src ValueReader) ([]byte, error) {
if br, ok := src.(bytesReader); ok {
_, dst, err := br.readValueBytes(dst)
Expand All @@ -178,9 +157,6 @@ func appendDocumentBytes(dst []byte, src ValueReader) ([]byte, error) {
}

// appendArrayBytes copies an array from the ValueReader to dst.
//
// Deprecated: Copying BSON arrays using the ValueWriter and ValueReader interfaces will not be
// supported in Go Driver 2.0.
func appendArrayBytes(dst []byte, src ValueReader) ([]byte, error) {
if br, ok := src.(bytesReader); ok {
_, dst, err := br.readValueBytes(dst)
Expand All @@ -198,8 +174,6 @@ func appendArrayBytes(dst []byte, src ValueReader) ([]byte, error) {
}

// copyValueFromBytes will write the value represtend by t and src to dst.
//
// Deprecated: Use [go.mongodb.org/mongo-driver/bson.UnmarshalValue] instead.
func copyValueFromBytes(dst ValueWriter, t Type, src []byte) error {
if wvb, ok := dst.(bytesWriter); ok {
return wvb.writeValueBytes(t, src)
Expand All @@ -214,44 +188,28 @@ func copyValueFromBytes(dst ValueWriter, t Type, src []byte) error {
return copyValue(dst, vr)
}

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

// appendValueBytes functions the same as CopyValueToBytes, but will append the
// result to dst.
//
// Deprecated: Appending individual BSON elements to an existing slice will not be supported in Go
// Driver 2.0.
func appendValueBytes(dst []byte, src ValueReader) (Type, []byte, error) {
func copyValueToBytes(src ValueReader) (Type, []byte, error) {
if br, ok := src.(bytesReader); ok {
return br.readValueBytes(dst)
return br.readValueBytes(nil)
}

vw := vwPool.Get().(*valueWriter)
defer putValueWriter(vw)

start := len(dst)

vw.reset(dst)
vw.reset(nil)
vw.push(mElement)

err := copyValue(vw, src)
if err != nil {
return 0, dst, err
return 0, nil, err
}

return Type(vw.buf[start]), vw.buf[start+2:], nil
return Type(vw.buf[0]), vw.buf[2:], nil
}

// copyValue will copy a single value from src to dst.
//
// Deprecated: Copying BSON values using the ValueWriter and ValueReader interfaces will not be
// supported in Go Driver 2.0.
func copyValue(dst ValueWriter, src ValueReader) error {
var err error
switch src.Type() {
Expand Down Expand Up @@ -461,3 +419,15 @@ func copyDocumentCore(dw DocumentWriter, dr DocumentReader) error {

return dw.WriteDocumentEnd()
}

// bytesReader is the interface used to read BSON bytes from a valueReader.
//
// The bytes of the value will be appended to dst.
type bytesReader interface {
readValueBytes(dst []byte) (Type, []byte, error)
}

// bytesWriter is the interface used to write BSON bytes to a valueWriter.
type bytesWriter interface {
writeValueBytes(t Type, b []byte) error
}
51 changes: 2 additions & 49 deletions bson/copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func TestCopier(t *testing.T) {
noerr(t, err)
_, _, err = vr.ReadElement()
noerr(t, err)
btype, got, err := CopyValueToBytes(vr)
btype, got, err := copyValueToBytes(vr)
noerr(t, err)
want := bsoncore.AppendString(nil, "world")
if btype != TypeString {
Expand All @@ -467,7 +467,7 @@ func TestCopier(t *testing.T) {
})
t.Run("Non BytesReader", func(t *testing.T) {
llvrw := &TestValueReaderWriter{t: t, bsontype: TypeString, readval: "Hello, world!"}
btype, got, err := CopyValueToBytes(llvrw)
btype, got, err := copyValueToBytes(llvrw)
noerr(t, err)
want := bsoncore.AppendString(nil, "Hello, world!")
if btype != TypeString {
Expand All @@ -478,51 +478,4 @@ func TestCopier(t *testing.T) {
}
})
})
t.Run("AppendValueBytes", func(t *testing.T) {
t.Run("BytesReader", func(t *testing.T) {
var idx int32
b, err := bsoncore.AppendDocumentEnd(
bsoncore.AppendStringElement(
bsoncore.AppendDocumentStartInline(nil, &idx),
"hello", "world",
),
idx,
)
noerr(t, err)
vr := newValueReader(b)
_, err = vr.ReadDocument()
noerr(t, err)
_, _, err = vr.ReadElement()
noerr(t, err)
btype, got, err := appendValueBytes(nil, vr)
noerr(t, err)
want := bsoncore.AppendString(nil, "world")
if btype != TypeString {
t.Errorf("Incorrect type returned. got %v; want %v", btype, TypeString)
}
if !bytes.Equal(got, want) {
t.Errorf("Bytes do not match. got %v; want %v", got, want)
}
})
t.Run("Non BytesReader", func(t *testing.T) {
llvrw := &TestValueReaderWriter{t: t, bsontype: TypeString, readval: "Hello, world!"}
btype, got, err := appendValueBytes(nil, llvrw)
noerr(t, err)
want := bsoncore.AppendString(nil, "Hello, world!")
if btype != TypeString {
t.Errorf("Incorrect type returned. got %v; want %v", btype, TypeString)
}
if !bytes.Equal(got, want) {
t.Errorf("Bytes do not match. got %v; want %v", got, want)
}
})
t.Run("CopyValue error", func(t *testing.T) {
want := errors.New("CopyValue error")
llvrw := &TestValueReaderWriter{t: t, bsontype: TypeString, err: want, errAfter: llvrwReadString}
_, _, got := appendValueBytes(make([]byte, 0), llvrw)
if !assert.CompareErrors(got, want) {
t.Errorf("Errors do not match. got %v; want %v", got, want)
}
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This subtest should be moved to CopyValueToBytes.

})
}
2 changes: 1 addition & 1 deletion bson/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestDecoderv2(t *testing.T) {

cdeih := func(string, string) string { return "certainlydoesntexistelsewhereihope" }
dec := NewDecoder(NewValueReader([]byte{}))
want := ErrNoDecoder{Type: reflect.TypeOf(cdeih)}
want := errNoDecoder{Type: reflect.TypeOf(cdeih)}
got := dec.Decode(&cdeih)
assert.Equal(t, want, got, "Received unexpected error.")
})
Expand Down
4 changes: 2 additions & 2 deletions bson/default_value_decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ func valueUnmarshalerDecodeValue(_ DecodeContext, vr ValueReader, val reflect.Va
val = val.Addr() // If the type doesn't implement the interface, a pointer to it must.
}

t, src, err := CopyValueToBytes(vr)
t, src, err := copyValueToBytes(vr)
if err != nil {
return err
}
Expand All @@ -1206,7 +1206,7 @@ func unmarshalerDecodeValue(_ DecodeContext, vr ValueReader, val reflect.Value)
val.Set(reflect.New(val.Type().Elem()))
}

_, src, err := CopyValueToBytes(vr)
_, src, err := copyValueToBytes(vr)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions bson/default_value_decoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ func TestDefaultValueDecoders(t *testing.T) {
&DecodeContext{Registry: newTestRegistry()},
&valueReaderWriter{},
readDocument,
ErrNoDecoder{Type: reflect.TypeOf("")},
errNoDecoder{Type: reflect.TypeOf("")},
},
{
"ReadElement Error",
Expand Down Expand Up @@ -904,7 +904,7 @@ func TestDefaultValueDecoders(t *testing.T) {
&DecodeContext{Registry: newTestRegistry()},
&valueReaderWriter{BSONType: TypeArray},
readArray,
ErrNoDecoder{Type: reflect.TypeOf("")},
errNoDecoder{Type: reflect.TypeOf("")},
},
{
"ReadValue Error",
Expand Down Expand Up @@ -998,7 +998,7 @@ func TestDefaultValueDecoders(t *testing.T) {
&DecodeContext{Registry: newTestRegistry()},
&valueReaderWriter{BSONType: TypeArray},
readArray,
ErrNoDecoder{Type: reflect.TypeOf("")},
errNoDecoder{Type: reflect.TypeOf("")},
},
{
"ReadValue Error",
Expand Down Expand Up @@ -1558,7 +1558,7 @@ func TestDefaultValueDecoders(t *testing.T) {
},
{
"No Decoder", &wrong, &DecodeContext{Registry: buildDefaultRegistry()}, nil, nothing,
ErrNoDecoder{Type: reflect.TypeOf(wrong)},
errNoDecoder{Type: reflect.TypeOf(wrong)},
},
{
"decode null",
Expand Down Expand Up @@ -3307,7 +3307,7 @@ func TestDefaultValueDecoders(t *testing.T) {
}
val := reflect.New(tEmpty).Elem()
dc := DecodeContext{Registry: newTestRegistry()}
want := ErrNoTypeMapEntry{Type: tc.bsontype}
want := errNoTypeMapEntry{Type: tc.bsontype}
got := (&emptyInterfaceCodec{}).DecodeValue(dc, llvr, val)
if !assert.CompareErrors(got, want) {
t.Errorf("Errors are not equal. got %v; want %v", got, want)
Expand All @@ -3324,7 +3324,7 @@ func TestDefaultValueDecoders(t *testing.T) {
dc := DecodeContext{
Registry: reg,
}
want := ErrNoDecoder{Type: reflect.TypeOf(tc.val)}
want := errNoDecoder{Type: reflect.TypeOf(tc.val)}
got := (&emptyInterfaceCodec{}).DecodeValue(dc, llvr, val)
if !assert.CompareErrors(got, want) {
t.Errorf("Errors are not equal. got %v; want %v", got, want)
Expand Down Expand Up @@ -3389,7 +3389,7 @@ func TestDefaultValueDecoders(t *testing.T) {

t.Run("no type registered", func(t *testing.T) {
llvr := &valueReaderWriter{BSONType: TypeDouble}
want := ErrNoTypeMapEntry{Type: TypeDouble}
want := errNoTypeMapEntry{Type: TypeDouble}
val := reflect.New(tEmpty).Elem()
got := (&emptyInterfaceCodec{}).DecodeValue(DecodeContext{Registry: newTestRegistry()}, llvr, val)
if !assert.CompareErrors(got, want) {
Expand Down Expand Up @@ -3538,7 +3538,7 @@ func TestDefaultValueDecoders(t *testing.T) {
}
stringStructErr := &DecodeError{
keys: []string{"foo"},
wrapped: ErrNoDecoder{reflect.TypeOf("")},
wrapped: errNoDecoder{reflect.TypeOf("")},
}

// Test a deeply nested struct mixed with maps and slices.
Expand Down
Loading
Loading