Add hexadecimal string to integer conversion to converter processor (#5569)

This commit is contained in:
Dimitri Saingre 2019-03-11 23:36:38 +01:00 committed by Daniel Nelson
parent 2566210df1
commit 88e0cb16e1
2 changed files with 22 additions and 8 deletions

View File

@ -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
}
@ -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
}

View File

@ -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"},
@ -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",
@ -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,
},