Skip to content

Commit

Permalink
GODRIVER-2835 Change the bson.ValueReader input to an io.Reader.
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Jun 12, 2024
1 parent 269ba66 commit 772efb4
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 177 deletions.
6 changes: 2 additions & 4 deletions bson/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bson

import (
"bytes"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -205,10 +206,7 @@ func copyValueFromBytes(dst ValueWriter, t Type, src []byte) error {
return wvb.WriteValueBytes(t, src)
}

vr := vrPool.Get().(*valueReader)
defer vrPool.Put(vr)

vr.reset(src)
vr := newValueReader(bytes.NewReader(src))
vr.pushElement(t)

return copyValue(dst, vr)
Expand Down
8 changes: 4 additions & 4 deletions bson/copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCopier(t *testing.T) {
doc = bsoncore.AppendStringElement(doc, "Hello", "world")
doc, err := bsoncore.AppendDocumentEnd(doc, idx)
noerr(t, err)
src := newValueReader(doc)
src := newValueReader(bytes.NewReader(doc))
dst := newValueWriterFromSlice(make([]byte, 0))
want := doc
err = copyDocument(dst, src)
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestCopier(t *testing.T) {
noerr(t, err)
doc, err = bsoncore.AppendDocumentEnd(doc, idx)
noerr(t, err)
src := newValueReader(doc)
src := newValueReader(bytes.NewReader(doc))

_, err = src.ReadDocument()
noerr(t, err)
Expand Down Expand Up @@ -450,7 +450,7 @@ func TestCopier(t *testing.T) {
idx,
)
noerr(t, err)
vr := newValueReader(b)
vr := newValueReader(bytes.NewReader(b))
_, err = vr.ReadDocument()
noerr(t, err)
_, _, err = vr.ReadElement()
Expand Down Expand Up @@ -489,7 +489,7 @@ func TestCopier(t *testing.T) {
idx,
)
noerr(t, err)
vr := newValueReader(b)
vr := newValueReader(bytes.NewReader(b))
_, err = vr.ReadDocument()
noerr(t, err)
_, _, err = vr.ReadElement()
Expand Down
6 changes: 3 additions & 3 deletions bson/decoder_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ExampleDecoder() {

// Create a Decoder that reads the marshaled BSON document and use it to
// unmarshal the document into a Product struct.
decoder := bson.NewDecoder(bson.NewValueReader(data))
decoder := bson.NewDecoder(bson.NewValueReader(bytes.NewReader(data)))

type Product struct {
Name string `bson:"name"`
Expand Down Expand Up @@ -66,7 +66,7 @@ func ExampleDecoder_DefaultDocumentM() {

// Create a Decoder that reads the marshaled BSON document and use it to unmarshal the document
// into a City struct.
decoder := bson.NewDecoder(bson.NewValueReader(data))
decoder := bson.NewDecoder(bson.NewValueReader(bytes.NewReader(data)))

type City struct {
Name string `bson:"name"`
Expand Down Expand Up @@ -104,7 +104,7 @@ func ExampleDecoder_UseJSONStructTags() {

// Create a Decoder that reads the marshaled BSON document and use it to
// unmarshal the document into a Product struct.
decoder := bson.NewDecoder(bson.NewValueReader(data))
decoder := bson.NewDecoder(bson.NewValueReader(bytes.NewReader(data)))

type Product struct {
Name string `json:"name"`
Expand Down
28 changes: 14 additions & 14 deletions bson/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestBasicDecode(t *testing.T) {
t.Parallel()

got := reflect.New(tc.sType).Elem()
vr := NewValueReader(tc.data)
vr := NewValueReader(bytes.NewReader(tc.data))
reg := DefaultRegistry
decoder, err := reg.LookupDecoder(reflect.TypeOf(got))
noerr(t, err)
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestDecodingInterfaces(t *testing.T) {

data, receiver, check := tc.stub()
got := reflect.ValueOf(receiver).Elem()
vr := NewValueReader(data)
vr := NewValueReader(bytes.NewReader(data))
reg := DefaultRegistry
decoder, err := reg.LookupDecoder(got.Type())
noerr(t, err)
Expand All @@ -208,7 +208,7 @@ func TestDecoderv2(t *testing.T) {
t.Parallel()

got := reflect.New(tc.sType).Interface()
vr := NewValueReader(tc.data)
vr := NewValueReader(bytes.NewReader(tc.data))
dec := NewDecoder(vr)
err := dec.Decode(got)
noerr(t, err)
Expand All @@ -223,7 +223,7 @@ func TestDecoderv2(t *testing.T) {
_ = certainlydoesntexistelsewhereihope(func(string, string) string { return "" })

cdeih := func(string, string) string { return "certainlydoesntexistelsewhereihope" }
dec := NewDecoder(NewValueReader([]byte{}))
dec := NewDecoder(NewValueReader(bytes.NewReader([]byte{})))
want := ErrNoDecoder{Type: reflect.TypeOf(cdeih)}
got := dec.Decode(&cdeih)
assert.Equal(t, want, got, "Received unexpected error.")
Expand Down Expand Up @@ -285,7 +285,7 @@ func TestDecoderv2(t *testing.T) {

want := bsoncore.BuildDocument(nil, bsoncore.AppendDoubleElement(nil, "pi", 3.14159))
unmarshaler := &testUnmarshaler{}
vr := NewValueReader(want)
vr := NewValueReader(bytes.NewReader(want))
dec := NewDecoder(vr)
err := dec.Decode(unmarshaler)
noerr(t, err)
Expand All @@ -302,7 +302,7 @@ func TestDecoderv2(t *testing.T) {
t.Run("success", func(t *testing.T) {
t.Parallel()

got := NewDecoder(NewValueReader([]byte{}))
got := NewDecoder(NewValueReader(bytes.NewReader([]byte{})))
if got == nil {
t.Errorf("Was expecting a non-nil Decoder, but got <nil>")
}
Expand All @@ -314,7 +314,7 @@ func TestDecoderv2(t *testing.T) {
t.Run("success", func(t *testing.T) {
t.Parallel()

got := NewDecoder(NewValueReader([]byte{}))
got := NewDecoder(NewValueReader(bytes.NewReader([]byte{})))
if got == nil {
t.Errorf("Was expecting a non-nil Decoder, but got <nil>")
}
Expand All @@ -332,7 +332,7 @@ func TestDecoderv2(t *testing.T) {
got.Item = "apple"
got.Bonus = 2
data := docToBytes(D{{"item", "canvas"}, {"qty", 4}})
vr := NewValueReader(data)
vr := NewValueReader(bytes.NewReader(data))
dec := NewDecoder(vr)
err := dec.Decode(&got)
noerr(t, err)
Expand All @@ -342,7 +342,7 @@ func TestDecoderv2(t *testing.T) {
t.Run("Reset", func(t *testing.T) {
t.Parallel()

vr1, vr2 := NewValueReader([]byte{}), NewValueReader([]byte{})
vr1, vr2 := NewValueReader(bytes.NewReader([]byte{})), NewValueReader(bytes.NewReader([]byte{}))
dec := NewDecoder(vr1)
if dec.vr != vr1 {
t.Errorf("Decoder should use the value reader provided. got %v; want %v", dec.vr, vr1)
Expand All @@ -358,7 +358,7 @@ func TestDecoderv2(t *testing.T) {
r1, r2 := DefaultRegistry, NewRegistry()
dc1 := DecodeContext{Registry: r1}
dc2 := DecodeContext{Registry: r2}
dec := NewDecoder(NewValueReader([]byte{}))
dec := NewDecoder(NewValueReader(bytes.NewReader([]byte{})))
if !reflect.DeepEqual(dec.dc, dc1) {
t.Errorf("Decoder should use the Registry provided. got %v; want %v", dec.dc, dc1)
}
Expand All @@ -371,7 +371,7 @@ func TestDecoderv2(t *testing.T) {
t.Parallel()

data := docToBytes(D{{"item", "canvas"}, {"qty", 4}})
vr := NewValueReader(data)
vr := NewValueReader(bytes.NewReader(data))
dec := NewDecoder(vr)

var got *D
Expand Down Expand Up @@ -578,7 +578,7 @@ func TestDecoderConfiguration(t *testing.T) {
t.Run(tc.description, func(t *testing.T) {
t.Parallel()

dec := NewDecoder(NewValueReader(tc.input))
dec := NewDecoder(NewValueReader(bytes.NewReader(tc.input)))

tc.configure(dec)

Expand All @@ -599,7 +599,7 @@ func TestDecoderConfiguration(t *testing.T) {
Build()).
Build()

dec := NewDecoder(NewValueReader(input))
dec := NewDecoder(NewValueReader(bytes.NewReader(input)))

dec.DefaultDocumentM()

Expand All @@ -623,7 +623,7 @@ func TestDecoderConfiguration(t *testing.T) {
Build()).
Build()

dec := NewDecoder(NewValueReader(input))
dec := NewDecoder(NewValueReader(bytes.NewReader(input)))

dec.DefaultDocumentD()

Expand Down
33 changes: 17 additions & 16 deletions bson/default_value_decoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bson

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -2428,7 +2429,7 @@ func TestDefaultValueDecoders(t *testing.T) {
buildDocument(bsoncore.AppendNullElement(nil, "bar")),
),
)
dvr := NewValueReader(b)
dvr := NewValueReader(bytes.NewReader(b))
dr, err := dvr.ReadDocument()
noerr(t, err)
_, vr, err := dr.ReadElement()
Expand Down Expand Up @@ -2481,7 +2482,7 @@ func TestDefaultValueDecoders(t *testing.T) {
noerr(t, err)
doc, err = bsoncore.AppendDocumentEnd(doc, idx)
noerr(t, err)
dvr := NewValueReader(doc)
dvr := NewValueReader(bytes.NewReader(doc))
noerr(t, err)
dr, err := dvr.ReadDocument()
noerr(t, err)
Expand Down Expand Up @@ -3132,7 +3133,7 @@ func TestDefaultValueDecoders(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
vr := NewValueReader(tc.b)
vr := NewValueReader(bytes.NewReader(tc.b))
reg := buildDefaultRegistry()
vtype := reflect.TypeOf(tc.value)
dec, err := reg.LookupDecoder(vtype)
Expand Down Expand Up @@ -3181,7 +3182,7 @@ func TestDefaultValueDecoders(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
vr := NewValueReader(tc.b)
vr := NewValueReader(bytes.NewReader(tc.b))
reg := buildDefaultRegistry()
vtype := reflect.TypeOf(tc.value)
dec, err := reg.LookupDecoder(vtype)
Expand Down Expand Up @@ -3403,7 +3404,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 := NewValueReader(data)
vr := NewValueReader(bytes.NewReader(data))
want := D{{"pi", 3.14159}}
var got interface{}
val := reflect.ValueOf(&got).Elem()
Expand Down Expand Up @@ -3451,7 +3452,7 @@ func TestDefaultValueDecoders(t *testing.T) {
}
for _, tc := range testCases {
var got interface{}
vr := NewValueReader(doc)
vr := NewValueReader(bytes.NewReader(doc))
val := reflect.ValueOf(&got).Elem()

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

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

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

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

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

type myMap map[string]mybool
dc := DecodeContext{Registry: buildDefaultRegistry()}
vr := NewValueReader(docBytes)
vr := NewValueReader(bytes.NewReader(docBytes))
val := reflect.New(reflect.TypeOf(myMap{})).Elem()
err := defaultMapCodec.DecodeValue(dc, vr, val)
assert.Nil(t, err, "DecodeValue error: %v", err)
Expand Down
Loading

0 comments on commit 772efb4

Please sign in to comment.