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,12 +345,17 @@ func toInteger(v interface{}) (int64, bool) {
return 0, true return 0, true
} }
case string: case string:
result, err := strconv.ParseInt(value, 0, 64)
if err != nil {
result, err := strconv.ParseFloat(value, 64) result, err := strconv.ParseFloat(value, 64)
if err != nil { if err != nil {
return 0, false return 0, false
} }
return toInteger(result) return toInteger(result)
} }
return result, true
}
return 0, false return 0, false
} }
@ -379,12 +384,17 @@ func toUnsigned(v interface{}) (uint64, bool) {
return 0, true return 0, true
} }
case string: case string:
result, err := strconv.ParseUint(value, 0, 64)
if err != nil {
result, err := strconv.ParseFloat(value, 64) result, err := strconv.ParseFloat(value, 64)
if err != nil { if err != nil {
return 0, false return 0, false
} }
return toUnsigned(result) return toUnsigned(result)
} }
return result, true
}
return 0, false return 0, false
} }

View File

@ -129,8 +129,8 @@ func TestConverter(t *testing.T) {
converter: &Converter{ converter: &Converter{
Fields: &Conversion{ Fields: &Conversion{
String: []string{"a"}, String: []string{"a"},
Integer: []string{"b", "b1", "b2"}, Integer: []string{"b", "b1", "b2", "b3"},
Unsigned: []string{"c", "c1", "c2"}, Unsigned: []string{"c", "c1", "c2", "c3"},
Boolean: []string{"d"}, Boolean: []string{"d"},
Float: []string{"e"}, Float: []string{"e"},
Tag: []string{"f"}, Tag: []string{"f"},
@ -145,9 +145,11 @@ func TestConverter(t *testing.T) {
"b": "42", "b": "42",
"b1": "42.2", "b1": "42.2",
"b2": "42.5", "b2": "42.5",
"b3": "0x2A",
"c": "42", "c": "42",
"c1": "42.2", "c1": "42.2",
"c2": "42.5", "c2": "42.5",
"c3": "0x2A",
"d": "true", "d": "true",
"e": "42.0", "e": "42.0",
"f": "foo", "f": "foo",
@ -166,9 +168,11 @@ func TestConverter(t *testing.T) {
"b": int64(42), "b": int64(42),
"b1": int64(42), "b1": int64(42),
"b2": int64(43), "b2": int64(43),
"b3": int64(42),
"c": uint64(42), "c": uint64(42),
"c1": uint64(42), "c1": uint64(42),
"c2": uint64(43), "c2": uint64(43),
"c3": uint64(42),
"d": true, "d": true,
"e": 42.0, "e": 42.0,
}, },