Skip to content

Commit

Permalink
Add hexadecimal string to integer conversion to converter processor (i…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri Saingre authored and Helge Waastad committed Jun 13, 2019
1 parent 8db5b38 commit 20b7241
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
22 changes: 16 additions & 6 deletions plugins/processors/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,16 @@ func toInteger(v interface{}) (int64, bool) {
return 0, true
}
case string:
result, err := strconv.ParseFloat(value, 64)
result, err := strconv.ParseInt(value, 0, 64)

if err != nil {
return 0, false
result, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, false
}
return toInteger(result)
}
return toInteger(result)
return result, true
}
return 0, false
}
Expand Down Expand Up @@ -379,11 +384,16 @@ func toUnsigned(v interface{}) (uint64, bool) {
return 0, true
}
case string:
result, err := strconv.ParseFloat(value, 64)
result, err := strconv.ParseUint(value, 0, 64)

if err != nil {
return 0, false
result, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, false
}
return toUnsigned(result)
}
return toUnsigned(result)
return result, true
}
return 0, false
}
Expand Down
8 changes: 6 additions & 2 deletions plugins/processors/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func TestConverter(t *testing.T) {
converter: &Converter{
Fields: &Conversion{
String: []string{"a"},
Integer: []string{"b", "b1", "b2"},
Unsigned: []string{"c", "c1", "c2"},
Integer: []string{"b", "b1", "b2", "b3"},
Unsigned: []string{"c", "c1", "c2", "c3"},
Boolean: []string{"d"},
Float: []string{"e"},
Tag: []string{"f"},
Expand All @@ -145,9 +145,11 @@ func TestConverter(t *testing.T) {
"b": "42",
"b1": "42.2",
"b2": "42.5",
"b3": "0x2A",
"c": "42",
"c1": "42.2",
"c2": "42.5",
"c3": "0x2A",
"d": "true",
"e": "42.0",
"f": "foo",
Expand All @@ -166,9 +168,11 @@ func TestConverter(t *testing.T) {
"b": int64(42),
"b1": int64(42),
"b2": int64(43),
"b3": int64(42),
"c": uint64(42),
"c1": uint64(42),
"c2": uint64(43),
"c3": uint64(42),
"d": true,
"e": 42.0,
},
Expand Down

0 comments on commit 20b7241

Please sign in to comment.