Fix numeric to bool conversion in converter (#7579)

A type switch case with multiple conditions causes the value to remain as
interface which causes toBool to always return true for any numeric values.
This commit is contained in:
Anton Aksola 2020-05-26 21:07:24 +02:00 committed by GitHub
parent f91d0833fb
commit ad97b744a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -327,12 +327,12 @@ func (p *Converter) convertFields(metric telegraf.Metric) {
func toBool(v interface{}) (bool, bool) { func toBool(v interface{}) (bool, bool) {
switch value := v.(type) { switch value := v.(type) {
case int64, uint64, float64: case int64:
if value != 0 { return value != 0, true
return true, true case uint64:
} else { return value != 0, true
return false, false case float64:
} return value != 0, true
case bool: case bool:
return value, true return value, true
case string: case string:

View File

@ -180,7 +180,7 @@ func TestConverter(t *testing.T) {
String: []string{"a"}, String: []string{"a"},
Integer: []string{"b"}, Integer: []string{"b"},
Unsigned: []string{"c", "negative_uint"}, Unsigned: []string{"c", "negative_uint"},
Boolean: []string{"d"}, Boolean: []string{"d", "bool_zero"},
Float: []string{"e"}, Float: []string{"e"},
Tag: []string{"f"}, Tag: []string{"f"},
}, },
@ -196,6 +196,7 @@ func TestConverter(t *testing.T) {
"e": int64(42), "e": int64(42),
"f": int64(42), "f": int64(42),
"negative_uint": int64(-42), "negative_uint": int64(-42),
"bool_zero": int64(0),
}, },
time.Unix(0, 0), time.Unix(0, 0),
), ),
@ -212,6 +213,7 @@ func TestConverter(t *testing.T) {
"d": true, "d": true,
"e": 42.0, "e": 42.0,
"negative_uint": uint64(0), "negative_uint": uint64(0),
"bool_zero": false,
}, },
time.Unix(0, 0), time.Unix(0, 0),
), ),
@ -224,7 +226,7 @@ func TestConverter(t *testing.T) {
String: []string{"a"}, String: []string{"a"},
Integer: []string{"b", "overflow_int"}, Integer: []string{"b", "overflow_int"},
Unsigned: []string{"c"}, Unsigned: []string{"c"},
Boolean: []string{"d"}, Boolean: []string{"d", "bool_zero"},
Float: []string{"e"}, Float: []string{"e"},
Tag: []string{"f"}, Tag: []string{"f"},
}, },
@ -240,6 +242,7 @@ func TestConverter(t *testing.T) {
"e": uint64(42), "e": uint64(42),
"f": uint64(42), "f": uint64(42),
"overflow_int": uint64(math.MaxUint64), "overflow_int": uint64(math.MaxUint64),
"bool_zero": uint64(0),
}, },
time.Unix(0, 0), time.Unix(0, 0),
), ),
@ -256,6 +259,7 @@ func TestConverter(t *testing.T) {
"d": true, "d": true,
"e": 42.0, "e": 42.0,
"overflow_int": int64(math.MaxInt64), "overflow_int": int64(math.MaxInt64),
"bool_zero": false,
}, },
time.Unix(0, 0), time.Unix(0, 0),
), ),
@ -350,7 +354,7 @@ func TestConverter(t *testing.T) {
String: []string{"a"}, String: []string{"a"},
Integer: []string{"b", "too_large_int", "too_small_int"}, Integer: []string{"b", "too_large_int", "too_small_int"},
Unsigned: []string{"c", "negative_uint", "too_large_uint", "too_small_uint"}, Unsigned: []string{"c", "negative_uint", "too_large_uint", "too_small_uint"},
Boolean: []string{"d"}, Boolean: []string{"d", "bool_zero"},
Float: []string{"e"}, Float: []string{"e"},
Tag: []string{"f"}, Tag: []string{"f"},
}, },
@ -370,6 +374,7 @@ func TestConverter(t *testing.T) {
"too_small_int": -math.MaxFloat64, "too_small_int": -math.MaxFloat64,
"too_small_uint": -math.MaxFloat64, "too_small_uint": -math.MaxFloat64,
"negative_uint": -42.0, "negative_uint": -42.0,
"bool_zero": 0.0,
}, },
time.Unix(0, 0), time.Unix(0, 0),
), ),
@ -390,6 +395,7 @@ func TestConverter(t *testing.T) {
"too_small_int": int64(math.MinInt64), "too_small_int": int64(math.MinInt64),
"too_small_uint": uint64(0), "too_small_uint": uint64(0),
"negative_uint": uint64(0), "negative_uint": uint64(0),
"bool_zero": false,
}, },
time.Unix(0, 0), time.Unix(0, 0),
), ),