Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Aug 30, 2024
1 parent 35e236f commit c202948
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
12 changes: 4 additions & 8 deletions bson/value_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ func (vr *valueReader) appendNextElement(dst []byte) ([]byte, error) {

buf := make([]byte, length)
_, err = io.ReadFull(vr.r, buf)
if errors.Is(err, io.ErrUnexpectedEOF) {
return nil, io.EOF
} else if err != nil {
if err != nil {
return nil, err
}
dst = append(dst, buf...)
Expand Down Expand Up @@ -682,7 +680,7 @@ func (vr *valueReader) ReadElement() (string, ValueReader, error) {
return "", nil, vr.invalidDocumentLengthError()
}

_ = vr.pop()
_ = vr.pop() // Ignore the error because the call here never reads from the underlying reader.
return "", nil, ErrEOD
}

Expand Down Expand Up @@ -712,7 +710,7 @@ func (vr *valueReader) ReadValue() (ValueReader, error) {
return nil, vr.invalidDocumentLengthError()
}

_ = vr.pop()
_ = vr.pop() // Ignore the error because the call here never reads from the underlying reader.
return nil, ErrEOA
}

Expand All @@ -726,9 +724,7 @@ func (vr *valueReader) ReadValue() (ValueReader, error) {

func (vr *valueReader) read(p []byte) error {
n, err := io.ReadFull(vr.r, p)
if errors.Is(err, io.ErrUnexpectedEOF) {
return io.EOF
} else if err != nil {
if err != nil {
return err
}
vr.offset += int64(n)
Expand Down
10 changes: 5 additions & 5 deletions bson/value_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func TestValueReader(t *testing.T) {
// invalid length
vr.r = bufio.NewReader(bytes.NewReader([]byte{0x00, 0x00}))
_, err := vr.ReadDocument()
if !errors.Is(err, io.EOF) {
t.Errorf("Expected io.EOF with document length too small. got %v; want %v", err, io.EOF)
if !errors.Is(err, io.ErrUnexpectedEOF) {
t.Errorf("Expected io.ErrUnexpectedEOF with document length too small. got %v; want %v", err, io.EOF)
}
if vr.offset != 0 {
t.Errorf("Expected 0 offset. got %d", vr.offset)
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestValueReader(t *testing.T) {

vr.frame--
_, err = vr.ReadDocument()
if !errors.Is(err, io.EOF) {
if !errors.Is(err, io.ErrUnexpectedEOF) {
t.Errorf("Should return error when attempting to read length with not enough bytes. got %v; want %v", err, io.EOF)
}
})
Expand Down Expand Up @@ -778,7 +778,7 @@ func TestValueReader(t *testing.T) {
append([]byte{0x40, 0x27, 0x00, 0x00}, testcstring...),
(*valueReader).ReadString,
"",
io.EOF,
io.ErrUnexpectedEOF,
TypeString,
},
{
Expand Down Expand Up @@ -1417,7 +1417,7 @@ func TestValueReader(t *testing.T) {
"append bytes",
[]byte{0x01, 0x02, 0x03, 0x04},
Type(0),
io.EOF,
io.ErrUnexpectedEOF,
},
}

Expand Down

0 comments on commit c202948

Please sign in to comment.