Skip to content
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

encoding/csv: document EOF behavior of Reader.Read #17342

Closed
bradfitz opened this issue Oct 4, 2016 · 4 comments
Closed

encoding/csv: document EOF behavior of Reader.Read #17342

bradfitz opened this issue Oct 4, 2016 · 4 comments
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@bradfitz
Copy link
Contributor

bradfitz commented Oct 4, 2016

Reader.Read doesn't say anything about EOF.

Document not only that it returns EOF, but whether that is (nil, EOF) or (Record, EOF) like io.Reader.

/cc @nuss-justin

@bradfitz bradfitz added this to the Go1.8 milestone Oct 4, 2016
@odeke-em
Copy link
Member

odeke-em commented Oct 5, 2016

Aha, interestingly I also ran into this issue this morning, wondering about EOF or what error or when errors are returned.
Thanks for filing the issue, I was planning on filing one on getting back home from work.

@nussjustin
Copy link
Contributor

FWIW I looked at the code and though about this for a few minutes.

Reader.Read will currently always return (Record, nil) on EOF and (nil, EOF)** on the next call if no other error happens. In fact it will always return either a Record or an error except when FieldsPerRecord is set and doesn't match the colums read.

This could be changed to return a Record (if any) even if there was an error (as with the FieldsPerRecord case) but the current implementation of parseRecord (which is used by Read) only returns a Record and an error if the error is io.EOF, which means the only noticeable change would be that Read could now report (Record, EOF) in addition to (nil, EOF).

I'm in favor of keeping the current implementation and just extending the comment with something like

// If Read encounters the end of the input and the row was not empty,
// it will return the record without an error. Otherwise it will return
// no record and io.EOF.

** Note that this is not necessarily true. I haven't looked at the code of bufio.Reader (which csv.Reader uses), but technically the next error will be whatever the bufio.Reader returns on the next call.

@bradfitz
Copy link
Contributor Author

bradfitz commented Oct 5, 2016

Everything in Go returning (T, error) implicitly means either T is valid, or the error is valid. Only cases where both might be valid (for example, the io.Reader interface) should go out of their way to document something.

But... this thing is called Read like an io.Reader, so it invites some doubt.

So let's clarify. But the way we should clarify is that only one value is ever valid. I just haven't audited the code yet to verify that that's the case. Let's make it true if not.

@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 6, 2016
@gopherbot
Copy link

CL https://golang.org/cl/32172 mentions this issue.

@golang golang locked and limited conversation to collaborators Oct 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants