From 0ea21eb9dc29f61443110d2756dad0d04a919b86 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 23 Aug 2019 18:15:52 +0300 Subject: [PATCH] app/vminsert/influx: handle `\r\n` aka `crlf` influx line endings from windows world Such lines exist in real life. --- app/vminsert/influx/parser.go | 10 ++++++++++ app/vminsert/influx/parser_test.go | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/vminsert/influx/parser.go b/app/vminsert/influx/parser.go index e5f21531b..cb9a031b2 100644 --- a/app/vminsert/influx/parser.go +++ b/app/vminsert/influx/parser.go @@ -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 { diff --git a/app/vminsert/influx/parser_test.go b/app/vminsert/influx/parser_test.go index 23bcbd146..0cb914b1e 100644 --- a/app/vminsert/influx/parser_test.go +++ b/app/vminsert/influx/parser_test.go @@ -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{{