diff --git a/plugins/processors/converter/converter.go b/plugins/processors/converter/converter.go index 33f2e43c0..55a2a2d09 100644 --- a/plugins/processors/converter/converter.go +++ b/plugins/processors/converter/converter.go @@ -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: diff --git a/plugins/processors/converter/converter_test.go b/plugins/processors/converter/converter_test.go index 1310e698a..efde0bcd9 100644 --- a/plugins/processors/converter/converter_test.go +++ b/plugins/processors/converter/converter_test.go @@ -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), ),