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-2719 Remove unnecessary errors from bson.Encoder/bson.Decoder. #1525

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,10 @@ func TestMapCodec(t *testing.T) {
mapRegistry := NewRegistry()
mapRegistry.RegisterKindEncoder(reflect.Map, mapCodec)
buf := new(bytes.Buffer)
vw, err := bsonrw.NewBSONValueWriter(buf)
assert.Nil(t, err)
vw := bsonrw.NewValueWriter(buf)
enc := NewEncoder(vw)
enc.SetRegistry(mapRegistry)
err = enc.Encode(mapObj)
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
32 changes: 16 additions & 16 deletions bson/bsoncodec/default_value_decoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,7 @@ func TestDefaultValueDecoders(t *testing.T) {
buildDocument(bsoncore.AppendNullElement(nil, "bar")),
),
)
dvr := bsonrw.NewBSONDocumentReader(b)
dvr := bsonrw.NewValueReader(b)
dr, err := dvr.ReadDocument()
noerr(t, err)
_, vr, err := dr.ReadElement()
Expand Down Expand Up @@ -2488,7 +2488,7 @@ func TestDefaultValueDecoders(t *testing.T) {
noerr(t, err)
doc, err = bsoncore.AppendDocumentEnd(doc, idx)
noerr(t, err)
dvr := bsonrw.NewBSONDocumentReader(doc)
dvr := bsonrw.NewValueReader(doc)
noerr(t, err)
dr, err := dvr.ReadDocument()
noerr(t, err)
Expand Down Expand Up @@ -3132,7 +3132,7 @@ func TestDefaultValueDecoders(t *testing.T) {
t.Run("Decode", func(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
vr := bsonrw.NewBSONDocumentReader(tc.b)
vr := bsonrw.NewValueReader(tc.b)
reg := buildDefaultRegistry()
vtype := reflect.TypeOf(tc.value)
dec, err := reg.LookupDecoder(vtype)
Expand Down Expand Up @@ -3181,7 +3181,7 @@ func TestDefaultValueDecoders(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
vr := bsonrw.NewBSONDocumentReader(tc.b)
vr := bsonrw.NewValueReader(tc.b)
reg := buildDefaultRegistry()
vtype := reflect.TypeOf(tc.value)
dec, err := reg.LookupDecoder(vtype)
Expand Down Expand Up @@ -3403,7 +3403,7 @@ func TestDefaultValueDecoders(t *testing.T) {
})
t.Run("top level document", func(t *testing.T) {
data := bsoncore.BuildDocument(nil, bsoncore.AppendDoubleElement(nil, "pi", 3.14159))
vr := bsonrw.NewBSONDocumentReader(data)
vr := bsonrw.NewValueReader(data)
want := primitive.D{{"pi", 3.14159}}
var got interface{}
val := reflect.ValueOf(&got).Elem()
Expand Down Expand Up @@ -3451,7 +3451,7 @@ func TestDefaultValueDecoders(t *testing.T) {
}
for _, tc := range testCases {
var got interface{}
vr := bsonrw.NewBSONDocumentReader(doc)
vr := bsonrw.NewValueReader(doc)
val := reflect.ValueOf(&got).Elem()

err := defaultEmptyInterfaceCodec.DecodeValue(DecodeContext{Registry: tc.registry}, vr, val)
Expand Down Expand Up @@ -3487,7 +3487,7 @@ func TestDefaultValueDecoders(t *testing.T) {
}

var got primitive.D
vr := bsonrw.NewBSONDocumentReader(doc)
vr := bsonrw.NewValueReader(doc)
val := reflect.ValueOf(&got).Elem()
err := defaultSliceCodec.DecodeValue(DecodeContext{Registry: reg}, vr, val)
noerr(t, err)
Expand Down Expand Up @@ -3577,7 +3577,7 @@ func TestDefaultValueDecoders(t *testing.T) {
// DecodeValue error when decoding into a primitive.D.
"primitive.D slice",
primitive.D{},
bsonrw.NewBSONDocumentReader(docBytes),
bsonrw.NewValueReader(docBytes),
emptyInterfaceErrorRegistry,
defaultSliceCodec,
docEmptyInterfaceErr,
Expand All @@ -3600,7 +3600,7 @@ func TestDefaultValueDecoders(t *testing.T) {
// the decodeD helper function.
"primitive.D array",
[1]primitive.E{},
bsonrw.NewBSONDocumentReader(docBytes),
bsonrw.NewValueReader(docBytes),
emptyInterfaceErrorRegistry,
ValueDecoderFunc(dvd.ArrayDecodeValue),
docEmptyInterfaceErr,
Expand All @@ -3623,7 +3623,7 @@ func TestDefaultValueDecoders(t *testing.T) {
// DecodeValue error when decoding into a map.
"map",
map[string]interface{}{},
bsonrw.NewBSONDocumentReader(docBytes),
bsonrw.NewValueReader(docBytes),
emptyInterfaceErrorRegistry,
defaultMapCodec,
docEmptyInterfaceErr,
Expand All @@ -3632,7 +3632,7 @@ func TestDefaultValueDecoders(t *testing.T) {
// DecodeValue error when decoding into a struct.
"struct - DecodeValue error",
emptyInterfaceStruct{},
bsonrw.NewBSONDocumentReader(docBytes),
bsonrw.NewValueReader(docBytes),
emptyInterfaceErrorRegistry,
defaultTestStructCodec,
emptyInterfaceStructErr,
Expand All @@ -3643,15 +3643,15 @@ func TestDefaultValueDecoders(t *testing.T) {
// no decoder for strings.
"struct - no decoder found",
stringStruct{},
bsonrw.NewBSONDocumentReader(docBytes),
bsonrw.NewValueReader(docBytes),
NewRegistryBuilder().Build(),
defaultTestStructCodec,
stringStructErr,
},
{
"deeply nested struct",
outer{},
bsonrw.NewBSONDocumentReader(outerDoc),
bsonrw.NewValueReader(outerDoc),
nestedRegistry,
defaultTestStructCodec,
nestedErr,
Expand Down Expand Up @@ -3681,7 +3681,7 @@ func TestDefaultValueDecoders(t *testing.T) {
type outer struct{ Foo inner }

dc := DecodeContext{Registry: buildDefaultRegistry()}
vr := bsonrw.NewBSONDocumentReader(outerBytes)
vr := bsonrw.NewValueReader(outerBytes)
val := reflect.New(reflect.TypeOf(outer{})).Elem()
err := defaultTestStructCodec.DecodeValue(dc, vr, val)

Expand Down Expand Up @@ -3714,7 +3714,7 @@ func TestDefaultValueDecoders(t *testing.T) {
reg := rb.RegisterTypeMapEntry(bsontype.Boolean, reflect.TypeOf(mybool(true))).Build()

dc := DecodeContext{Registry: reg}
vr := bsonrw.NewBSONDocumentReader(docBytes)
vr := bsonrw.NewValueReader(docBytes)
val := reflect.New(tD).Elem()
err := defaultValueDecoders.DDecodeValue(dc, vr, val)
assert.Nil(t, err, "DDecodeValue error: %v", err)
Expand All @@ -3733,7 +3733,7 @@ func TestDefaultValueDecoders(t *testing.T) {

type myMap map[string]mybool
dc := DecodeContext{Registry: buildDefaultRegistry()}
vr := bsonrw.NewBSONDocumentReader(docBytes)
vr := bsonrw.NewValueReader(docBytes)
val := reflect.New(reflect.TypeOf(myMap{})).Elem()
err := defaultMapCodec.DecodeValue(dc, vr, val)
assert.Nil(t, err, "DecodeValue error: %v", err)
Expand Down
6 changes: 2 additions & 4 deletions bson/bsoncodec/default_value_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1770,8 +1770,7 @@ func TestDefaultValueEncoders(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b := make(bsonrw.SliceWriter, 0, 512)
vw, err := bsonrw.NewBSONValueWriter(&b)
noerr(t, err)
vw := bsonrw.NewValueWriter(&b)
reg := buildDefaultRegistry()
enc, err := reg.LookupEncoder(reflect.TypeOf(tc.value))
noerr(t, err)
Expand Down Expand Up @@ -1821,8 +1820,7 @@ func TestDefaultValueEncoders(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b := make(bsonrw.SliceWriter, 0, 512)
vw, err := bsonrw.NewBSONValueWriter(&b)
noerr(t, err)
vw := bsonrw.NewValueWriter(&b)
reg := buildDefaultRegistry()
enc, err := reg.LookupEncoder(reflect.TypeOf(tc.value))
noerr(t, err)
Expand Down
14 changes: 4 additions & 10 deletions bson/bsoncodec/registry_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,10 @@ 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.
buf := new(bytes.Buffer)
vw, err := bsonrw.NewBSONValueWriter(buf)
if err != nil {
panic(err)
}
vw := bsonrw.NewValueWriter(buf)
enc := bson.NewEncoder(vw)
enc.SetRegistry(reg)
err = enc.Encode(doc)
err := enc.Encode(doc)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -209,13 +206,10 @@ 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).
buf := new(bytes.Buffer)
vw, err := bsonrw.NewBSONValueWriter(buf)
if err != nil {
panic(err)
}
vw := bsonrw.NewValueWriter(buf)
enc := bson.NewEncoder(vw)
enc.SetRegistry(reg)
err = enc.Encode(doc)
err := enc.Encode(doc)
if err != nil {
panic(err)
}
Expand Down
8 changes: 2 additions & 6 deletions bson/bsonrw/extjson_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,11 @@ type extJSONValueWriter struct {
}

// NewExtJSONValueWriter creates a ValueWriter that writes Extended JSON to w.
func NewExtJSONValueWriter(w io.Writer, canonical, escapeHTML bool) (ValueWriter, error) {
if w == nil {
return nil, errNilWriter
}

func NewExtJSONValueWriter(w io.Writer, canonical, escapeHTML bool) ValueWriter {
// Enable newlines for all Extended JSON value writers created by NewExtJSONValueWriter. We
// expect these value writers to be used with an Encoder, which should add newlines after
// encoded Extended JSON documents.
return newExtJSONWriter(w, canonical, escapeHTML, true), nil
return newExtJSONWriter(w, canonical, escapeHTML, true)
}

func newExtJSONWriter(w io.Writer, canonical, escapeHTML, newlines bool) *extJSONValueWriter {
Expand Down
18 changes: 9 additions & 9 deletions bson/bsonrw/extjson_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package bsonrw

import (
"fmt"
"io/ioutil"
"io"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestExtJSONValueWriter(t *testing.T) {
t.Fatalf("fn must have one return value and it must be an error.")
}
params := make([]reflect.Value, 1, len(tc.params)+1)
ejvw := newExtJSONWriter(ioutil.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true, false)
params[0] = reflect.ValueOf(ejvw)
for _, param := range tc.params {
params = append(params, reflect.ValueOf(param))
Expand All @@ -162,7 +162,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}

t.Run("WriteArray", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw.push(mArray)
want := TransitionError{current: mArray, destination: mArray, parent: mTopLevel,
name: "WriteArray", modes: []mode{mElement, mValue}, action: "write"}
Expand All @@ -172,7 +172,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteCodeWithScope", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw.push(mArray)
want := TransitionError{current: mArray, destination: mCodeWithScope, parent: mTopLevel,
name: "WriteCodeWithScope", modes: []mode{mElement, mValue}, action: "write"}
Expand All @@ -182,7 +182,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteDocument", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw.push(mArray)
want := TransitionError{current: mArray, destination: mDocument, parent: mTopLevel,
name: "WriteDocument", modes: []mode{mElement, mValue, mTopLevel}, action: "write"}
Expand All @@ -192,7 +192,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteDocumentElement", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw.push(mElement)
want := TransitionError{current: mElement,
destination: mElement,
Expand All @@ -206,7 +206,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteDocumentEnd", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw.push(mElement)
want := fmt.Errorf("incorrect mode to end document: %s", mElement)
got := ejvw.WriteDocumentEnd()
Expand All @@ -215,7 +215,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteArrayElement", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw.push(mElement)
want := TransitionError{current: mElement,
destination: mValue,
Expand All @@ -229,7 +229,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteArrayEnd", func(t *testing.T) {
ejvw := newExtJSONWriter(ioutil.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw.push(mElement)
want := fmt.Errorf("incorrect mode to end array: %s", mElement)
got := ejvw.WriteArrayEnd()
Expand Down
4 changes: 2 additions & 2 deletions bson/bsonrw/value_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ type valueReader struct {
frame int64
}

// NewBSONDocumentReader returns a ValueReader using b for the underlying BSON
// NewValueReader returns a ValueReader using b for the underlying BSON
// representation. Parameter b must be a BSON Document.
func NewBSONDocumentReader(b []byte) ValueReader {
func NewValueReader(b []byte) ValueReader {
// TODO(skriptble): There's a lack of symmetry between the reader and writer, since the reader takes a []byte while the
// TODO writer takes an io.Writer. We should have two versions of each, one that takes a []byte and one that takes an
// TODO io.Reader or io.Writer. The []byte version will need to return a thing that can return the finished []byte since
Expand Down
11 changes: 3 additions & 8 deletions bson/bsonrw/value_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ func (bvwp *BSONValueWriterPool) Put(vw ValueWriter) (ok bool) {
// allocating a 4GB slice.
var maxSize = math.MaxInt32

var errNilWriter = errors.New("cannot create a ValueWriter from a nil io.Writer")

type errMaxDocumentSizeExceeded struct {
size int64
}
Expand Down Expand Up @@ -188,15 +186,12 @@ func (vw *valueWriter) pop() {
}
}

// NewBSONValueWriter creates a ValueWriter that writes BSON to w.
// NewValueWriter creates a ValueWriter that writes BSON to w.
//
// This ValueWriter will only write entire documents to the io.Writer and it
// will buffer the document as it is built.
func NewBSONValueWriter(w io.Writer) (ValueWriter, error) {
if w == nil {
return nil, errNilWriter
}
return newValueWriter(w), nil
func NewValueWriter(w io.Writer) ValueWriter {
return newValueWriter(w)
}

func newValueWriter(w io.Writer) *valueWriter {
Expand Down
Loading
Loading