app/vminsert/influx: handle \r\n aka crlf influx line endings from windows world

Such lines exist in real life.
This commit is contained in:
Aliaksandr Valialkin 2019-08-23 18:15:52 +03:00
parent b3502b2b39
commit 0ea21eb9dc
2 changed files with 12 additions and 1 deletions

View file

@ -64,6 +64,10 @@ func (r *Row) reset() {
func (r *Row) unmarshal(s string, tagsPool []Tag, fieldsPool []Field, noEscapeChars bool) ([]Tag, []Field, error) {
r.reset()
// Remove optional \r from the end of s
if len(s) > 0 && s[len(s)-1] == '\r' {
s = s[:len(s)-1]
}
n := nextUnescapedChar(s, ' ', noEscapeChars)
if n < 0 {
return tagsPool, fieldsPool, fmt.Errorf("cannot find Whitespace I in %q", s)
@ -182,6 +186,11 @@ func unmarshalRows(dst []Row, s string, tagsPool []Tag, fieldsPool []Field) ([]R
s = s[1:]
continue
}
if n == 1 && s[0] == '\r' {
// Skip empty line
s = s[2:]
continue
}
if s[0] == '#' {
// Skip comment
if n > 0 {
@ -191,6 +200,7 @@ func unmarshalRows(dst []Row, s string, tagsPool []Tag, fieldsPool []Field) ([]R
}
continue
}
if cap(dst) > len(dst) {
dst = dst[:len(dst)+1]
} else {

View file

@ -146,6 +146,7 @@ func TestRowsUnmarshalSuccess(t *testing.T) {
// Empty line
f("", &Rows{})
f("\n\n", &Rows{})
f("\n\r\n", &Rows{})
// Comment
f("\n# foobar\n", &Rows{})
@ -162,7 +163,7 @@ func TestRowsUnmarshalSuccess(t *testing.T) {
}},
}},
})
f("# comment\nfoo bar=123\n#comment2 sdsf dsf", &Rows{
f("# comment\nfoo bar=123\r\n#comment2 sdsf dsf", &Rows{
Rows: []Row{{
Measurement: "foo",
Fields: []Field{{