mirror of
https://github.com/wahyd4/telegraf.git
synced 2026-08-09 04:36:05 +10:00
Fix comment and comma csv rune conversion
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user