-
Notifications
You must be signed in to change notification settings - Fork 34
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
Make line separator customisable #31
Conversation
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.
decode.go
Outdated
@@ -21,7 +21,8 @@ func Unmarshal(data []byte, v interface{}) error { | |||
|
|||
// A Decoder reads and decodes fixed width data from an input stream. | |||
type Decoder struct { | |||
data *bufio.Reader | |||
scanner *bufio.Scanner | |||
separator string |
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.
separator string | |
lineTerminator []byte |
decode.go
Outdated
line, err := d.data.ReadString('\n') | ||
if err != nil && err != io.EOF { | ||
return err, false | ||
func (d *Decoder) SetSeparator(separator string) { |
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.
func (d *Decoder) SetSeparator(separator string) { | |
func (d *Decoder) SetLineTerminator(lineTerminator []byte) { |
decode.go
Outdated
d.separator = separator | ||
} | ||
|
||
func (d Decoder) Separator() string { |
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.
I don't see any reason this should be exported. Also is there any reason it is not a pointer receiver e.g. func (d *Decoder)
?
decode.go
Outdated
return "\n" | ||
} | ||
|
||
func (d *Decoder) Scan(data []byte, atEOF bool) (advance int, token []byte, err 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.
Why implement the custom scan function? I think data.ReadString(d.lineTerminator)
would work fine.
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.
@ianlopshire ReadString
only takes a single byte as argument: https://golang.org/pkg/bufio/#Reader.ReadString
So in the case of "\r\n" this wouldn't work
9b59077
to
0998b83
Compare
0998b83
to
7c5918c
Compare
@ianlopshire I've added your suggestions. The suggestion about using So in the case of I'm happy to edit the implementation but this was the best I could come up with besides a custom implementation of a |
@LeonB, thanks for making the changes! merged! |
No description provided.