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:
parent
f91d0833fb
commit
ad97b744a3
|
@ -327,12 +327,12 @@ func (p *Converter) convertFields(metric telegraf.Metric) {
|
|||
|
||||
func toBool(v interface{}) (bool, bool) {
|
||||
switch value := v.(type) {
|
||||
case int64, uint64, float64:
|
||||
if value != 0 {
|
||||
return true, true
|
||||
} else {
|
||||
return false, false
|
||||
}
|
||||
case int64:
|
||||
return value != 0, true
|
||||
case uint64:
|
||||
return value != 0, true
|
||||
case float64:
|
||||
return value != 0, true
|
||||
case bool:
|
||||
return value, true
|
||||
case string:
|
||||
|
|
|
@ -180,7 +180,7 @@ func TestConverter(t *testing.T) {
|
|||
String: []string{"a"},
|
||||
Integer: []string{"b"},
|
||||
Unsigned: []string{"c", "negative_uint"},
|
||||
Boolean: []string{"d"},
|
||||
Boolean: []string{"d", "bool_zero"},
|
||||
Float: []string{"e"},
|
||||
Tag: []string{"f"},
|
||||
},
|
||||
|
@ -196,6 +196,7 @@ func TestConverter(t *testing.T) {
|
|||
"e": int64(42),
|
||||
"f": int64(42),
|
||||
"negative_uint": int64(-42),
|
||||
"bool_zero": int64(0),
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
|
@ -212,6 +213,7 @@ func TestConverter(t *testing.T) {
|
|||
"d": true,
|
||||
"e": 42.0,
|
||||
"negative_uint": uint64(0),
|
||||
"bool_zero": false,
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
|
@ -224,7 +226,7 @@ func TestConverter(t *testing.T) {
|
|||
String: []string{"a"},
|
||||
Integer: []string{"b", "overflow_int"},
|
||||
Unsigned: []string{"c"},
|
||||
Boolean: []string{"d"},
|
||||
Boolean: []string{"d", "bool_zero"},
|
||||
Float: []string{"e"},
|
||||
Tag: []string{"f"},
|
||||
},
|
||||
|
@ -240,6 +242,7 @@ func TestConverter(t *testing.T) {
|
|||
"e": uint64(42),
|
||||
"f": uint64(42),
|
||||
"overflow_int": uint64(math.MaxUint64),
|
||||
"bool_zero": uint64(0),
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
|
@ -256,6 +259,7 @@ func TestConverter(t *testing.T) {
|
|||
"d": true,
|
||||
"e": 42.0,
|
||||
"overflow_int": int64(math.MaxInt64),
|
||||
"bool_zero": false,
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
|
@ -350,7 +354,7 @@ func TestConverter(t *testing.T) {
|
|||
String: []string{"a"},
|
||||
Integer: []string{"b", "too_large_int", "too_small_int"},
|
||||
Unsigned: []string{"c", "negative_uint", "too_large_uint", "too_small_uint"},
|
||||
Boolean: []string{"d"},
|
||||
Boolean: []string{"d", "bool_zero"},
|
||||
Float: []string{"e"},
|
||||
Tag: []string{"f"},
|
||||
},
|
||||
|
@ -370,6 +374,7 @@ func TestConverter(t *testing.T) {
|
|||
"too_small_int": -math.MaxFloat64,
|
||||
"too_small_uint": -math.MaxFloat64,
|
||||
"negative_uint": -42.0,
|
||||
"bool_zero": 0.0,
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
|
@ -390,6 +395,7 @@ func TestConverter(t *testing.T) {
|
|||
"too_small_int": int64(math.MinInt64),
|
||||
"too_small_uint": uint64(0),
|
||||
"negative_uint": uint64(0),
|
||||
"bool_zero": false,
|
||||
},
|
||||
time.Unix(0, 0),
|
||||
),
|
||||
|
|
Loading…
Reference in New Issue