-
Notifications
You must be signed in to change notification settings - Fork 892
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-3291 Refactor bson.valueReader
with bufio.
#1732
Conversation
bson/value_reader.go
Outdated
if err == nil { | ||
err = io.EOF | ||
} | ||
buf := make([]byte, length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: The length
is frequently static, which allows using static-size arrays or slices. To take advantage of that, consider changing the readBytes
API to accept a []byte
instead of a length.
E.g.
func (vr *valueReader) read(buf []byte) (int, error) { ... }
Then an example call from readi32
would look like:
func (vr *valueReader) readi32() (int32, error) {
var buf [4]byte
_, err := vr.read(buf[:])
// ...
bson/value_reader.go
Outdated
@@ -700,7 +687,7 @@ func (vr *valueReader) ReadElement() (string, ValueReader, error) { | |||
return "", nil, vr.invalidDocumentLengthError() | |||
} | |||
|
|||
vr.pop() | |||
_ = vr.pop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should handle this error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is inside an error-handling block. We don't need to overwrite the original error even when pop()
returns an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ErrEOD
is a sentinel error, similar to io.EOF
, so it's not an error handling block in the sense that "something has gone wrong". However, it does seem like the pop
call here will never read from the underlying reader, so it should be safe to discard. Consider adding a comment that says the pop
call here will never read from the underlying reader, so it's safe to discard the error.
bson/value_reader.go
Outdated
@@ -730,11 +717,11 @@ func (vr *valueReader) ReadValue() (ValueReader, error) { | |||
return nil, vr.invalidDocumentLengthError() | |||
} | |||
|
|||
vr.pop() | |||
_ = vr.pop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should handle this error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is inside an error-handling block. We don't need to overwrite the original error even when pop()
returns an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment above.
API Change ReportNo changes found! |
bson/value_reader.go
Outdated
func (vr *valueReader) read(p []byte) error { | ||
n, err := io.ReadFull(vr.r, p) | ||
if errors.Is(err, io.ErrUnexpectedEOF) { | ||
return io.EOF |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we convert an io.UnexpectedEOF
to io.EOF
here? io.UnexpectedEOF
actually seems more appropriate considering this note from the io.EOF documentation:
Functions should return EOF only to signal a graceful end of input. If the EOF occurs unexpectedly in a structured data stream, the appropriate error is either ErrUnexpectedEOF or some other error giving more detail.
Some other valueReader
methods should also be updated to return io.UnexpectedEOF
if the end of the input was reached before
bson/value_reader.go
Outdated
buf := make([]byte, length) | ||
_, err = io.ReadFull(vr.r, buf) | ||
if errors.Is(err, io.ErrUnexpectedEOF) { | ||
return nil, io.EOF |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question as in read
: Why do we convert an io.UnexpectedEOF
to io.EOF
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! 👍
GODRIVER-3291
Summary
Refactor
bson.valueReader
with bufio.Background & Motivation