diff --git a/plugins/parsers/csv/parser.go b/plugins/parsers/csv/parser.go index 8e0b8b47..cfa91e4e 100644 --- a/plugins/parsers/csv/parser.go +++ b/plugins/parsers/csv/parser.go @@ -17,8 +17,8 @@ type Parser struct { HeaderRowCount int SkipRows int SkipColumns int - Delimiter string - Comment string + Delimiter rune + Comment rune TrimSpace bool ColumnNames []string TagColumns []string @@ -37,11 +37,11 @@ func (p *Parser) compile(r *bytes.Reader) (*csv.Reader, error) { csvReader := csv.NewReader(r) // ensures that the reader reads records of different lengths without an error csvReader.FieldsPerRecord = -1 - if p.Delimiter != "" { - csvReader.Comma = []rune(p.Delimiter)[0] + if p.Delimiter != 0 { + csvReader.Comma = p.Delimiter } - if p.Comment != "" { - csvReader.Comment = []rune(p.Comment)[0] + if p.Comment != 0 { + csvReader.Comment = p.Comment } return csvReader, nil } diff --git a/plugins/parsers/csv/parser_test.go b/plugins/parsers/csv/parser_test.go index e3668d3a..b6eef0ed 100644 --- a/plugins/parsers/csv/parser_test.go +++ b/plugins/parsers/csv/parser_test.go @@ -106,7 +106,7 @@ func TestQuotedCharacter(t *testing.T) { func TestDelimiter(t *testing.T) { p := Parser{ HeaderRowCount: 1, - Delimiter: "%", + Delimiter: '%', ColumnNames: []string{"first", "second", "third"}, MeasurementColumn: "third", TimeFunc: DefaultTime, @@ -122,7 +122,7 @@ func TestDelimiter(t *testing.T) { func TestValueConversion(t *testing.T) { p := Parser{ HeaderRowCount: 0, - Delimiter: ",", + Delimiter: ',', ColumnNames: []string{"first", "second", "third", "fourth"}, MetricName: "test_value", TimeFunc: DefaultTime, @@ -152,7 +152,7 @@ func TestValueConversion(t *testing.T) { func TestSkipComment(t *testing.T) { p := Parser{ HeaderRowCount: 0, - Comment: "#", + Comment: '#', ColumnNames: []string{"first", "second", "third", "fourth"}, MetricName: "test_value", TimeFunc: DefaultTime, diff --git a/plugins/parsers/registry.go b/plugins/parsers/registry.go index 28ff3026..cb5ba71b 100644 --- a/plugins/parsers/registry.go +++ b/plugins/parsers/registry.go @@ -226,20 +226,20 @@ func newCSVParser(metricName string, return nil, fmt.Errorf("there must be a header if `csv_column_names` is not specified") } + var delimiterRune rune if delimiter != "" { - runeStr := []rune(delimiter) - if len(runeStr) > 1 { + if len(delimiter) > 1 { return nil, fmt.Errorf("csv_delimiter must be a single character, got: %s", delimiter) } - delimiter = fmt.Sprintf("%v", runeStr[0]) + delimiterRune = []rune(delimiter)[0] } + var commentRune rune if comment != "" { - runeStr := []rune(comment) - if len(runeStr) > 1 { + if len(comment) > 1 { return nil, fmt.Errorf("csv_delimiter must be a single character, got: %s", comment) } - comment = fmt.Sprintf("%v", runeStr[0]) + commentRune = []rune(comment)[0] } parser := &csv.Parser{ @@ -247,8 +247,8 @@ func newCSVParser(metricName string, HeaderRowCount: headerRowCount, SkipRows: skipRows, SkipColumns: skipColumns, - Delimiter: delimiter, - Comment: comment, + Delimiter: delimiterRune, + Comment: commentRune, TrimSpace: trimSpace, ColumnNames: columnNames, TagColumns: tagColumns,