Skip to content

Commit

Permalink
Set TrimLeadingSpace when TrimSpace is on in csv parser (influxdata#6773
Browse files Browse the repository at this point in the history
)
  • Loading branch information
DSpeichert authored and danielnelson committed Dec 11, 2019
1 parent 942bbda commit d762186
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/parsers/csv/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (p *Parser) compile(r *bytes.Reader) (*csv.Reader, error) {
if p.Comment != "" {
csvReader.Comment = []rune(p.Comment)[0]
}
csvReader.TrimLeadingSpace = p.TrimSpace
return csvReader, nil
}

Expand Down
24 changes: 24 additions & 0 deletions plugins/parsers/csv/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,30 @@ func TestTrimSpace(t *testing.T) {
require.Equal(t, expectedFields, metrics[0].Fields())
}

func TestTrimSpaceDelimetedBySpace(t *testing.T) {
p := Parser{
Delimiter: " ",
HeaderRowCount: 1,
TrimSpace: true,
TimeFunc: DefaultTime,
}
testCSV := ` first second third fourth
abcdefgh 0 2 false
abcdef 3.3 4 true
f 0 2 false`

expectedFields := map[string]interface{}{
"first": "abcdef",
"second": 3.3,
"third": int64(4),
"fourth": true,
}

metrics, err := p.Parse([]byte(testCSV))
require.NoError(t, err)
require.Equal(t, expectedFields, metrics[1].Fields())
}

func TestSkipRows(t *testing.T) {
p := Parser{
HeaderRowCount: 1,
Expand Down

0 comments on commit d762186

Please sign in to comment.