Skip to content

Commit

Permalink
GODRIVER-2682 Prepare to rename bson.NewFromIOReader to bson.ReadDocu…
Browse files Browse the repository at this point in the history
…ment. (mongodb#1217)

Co-authored-by: Preston Vasquez <prestonvasquez@icloud.com>
  • Loading branch information
matthewdale and prestonvasquez authored Apr 6, 2023
1 parent 3025384 commit fc24f61
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 11 additions & 3 deletions bson/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down
8 changes: 6 additions & 2 deletions bson/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
})
Expand Down

0 comments on commit fc24f61

Please sign in to comment.