Skip to content

Commit

Permalink
feat(pkg/csv2lp): pass a line number to custom parsing fn #18744
Browse files Browse the repository at this point in the history
  • Loading branch information
sranka committed Jul 2, 2020
1 parent 5c56cc8 commit 91d3129
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/csv2lp/csv_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type CsvTableColumn struct {
// TimeZone of dateTime column, applied when parsing dateTime DataType
TimeZone *time.Location
// ParseF is an optional function used to convert column's string value to interface{}
ParseF func(string) (interface{}, error)
ParseF func(value string, lineNumber int) (interface{}, error)

// escapedLabel contains escaped label that can be directly used in line protocol
escapedLabel string
Expand Down
6 changes: 3 additions & 3 deletions pkg/csv2lp/data_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func toTypedValue(val string, column *CsvTableColumn, lineNumber int) (interface
dataType := column.DataType
dataFormat := column.DataFormat
if column.ParseF != nil {
return column.ParseF(val)
return column.ParseF(val, lineNumber)
}
switch dataType {
case stringDatatype:
Expand Down Expand Up @@ -267,7 +267,7 @@ func CreateDecoder(encoding string) (func(io.Reader) io.Reader, error) {
}

// createBoolParseFn returns a function that converts a string value to boolean according to format "true,yes,1:false,no,0"
func createBoolParseFn(format string) func(string) (interface{}, error) {
func createBoolParseFn(format string) func(string, int) (interface{}, error) {
var err error = nil
truthy := []string{}
falsy := []string{}
Expand All @@ -284,7 +284,7 @@ func createBoolParseFn(format string) func(string) (interface{}, error) {
falsy = strings.Split(f, ",")
}
}
return func(val string) (interface{}, error) {
return func(val string, _lineNumber int) (interface{}, error) {
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/csv2lp/data_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func Test_CreateBoolParseFn(t *testing.T) {
fn := createBoolParseFn(test.format)
for j, pair := range test.pair {
t.Run(fmt.Sprint(i)+"_"+fmt.Sprint(j), func(t *testing.T) {
result, err := fn(pair.value)
result, err := fn(pair.value, 1)
switch pair.expect {
case "true":
require.Equal(t, true, result)
Expand Down

0 comments on commit 91d3129

Please sign in to comment.