diff --git a/bson/raw.go b/bson/raw.go index b8fe4cc525..fe990a1771 100644 --- a/bson/raw.go +++ b/bson/raw.go @@ -22,13 +22,21 @@ var ErrNilReader = errors.New("nil reader") // A Raw must be a full BSON document. Use the RawValue type for individual BSON values. type Raw []byte -// NewFromIOReader reads in a document from the given io.Reader and constructs a Raw from -// it. -func NewFromIOReader(r io.Reader) (Raw, error) { +// ReadDocument reads a BSON document from the io.Reader and returns it as a bson.Raw. If the +// reader contains multiple BSON documents, only the first document is read. +func ReadDocument(r io.Reader) (Raw, error) { doc, err := bsoncore.NewDocumentFromReader(r) return Raw(doc), err } +// NewFromIOReader reads a BSON document from the io.Reader and returns it as a bson.Raw. If the +// reader contains multiple BSON documents, only the first document is read. +// +// Deprecated: Use ReadDocument instead. +func NewFromIOReader(r io.Reader) (Raw, error) { + return ReadDocument(r) +} + // Validate validates the document. This method only validates the first document in // the slice, to validate other documents, the slice must be resliced. func (r Raw) Validate() (err error) { return bsoncore.Document(r).Validate() } diff --git a/bson/raw_test.go b/bson/raw_test.go index 12283d6d85..02c9f63136 100644 --- a/bson/raw_test.go +++ b/bson/raw_test.go @@ -264,7 +264,8 @@ func TestRaw(t *testing.T) { }) } }) - t.Run("NewFromIOReader", func(t *testing.T) { + t.Run("ReadDocument", func(t *testing.T) { + t.Parallel() testCases := []struct { name string ioReader io.Reader @@ -338,8 +339,11 @@ func TestRaw(t *testing.T) { } for _, tc := range testCases { + tc := tc // Capture range variable. t.Run(tc.name, func(t *testing.T) { - reader, err := NewFromIOReader(tc.ioReader) + t.Parallel() + + reader, err := ReadDocument(tc.ioReader) require.Equal(t, err, tc.err) require.True(t, bytes.Equal(tc.bsonReader, reader)) })