Skip to content

Commit

Permalink
Fix numeric to bool conversion in converter (influxdata#7579)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aakso authored May 26, 2020
1 parent 2e4ebe7 commit 09814bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
12 changes: 6 additions & 6 deletions plugins/processors/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 9 additions & 3 deletions plugins/processors/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
Expand All @@ -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),
),
Expand All @@ -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),
),
Expand All @@ -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"},
},
Expand All @@ -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),
),
Expand All @@ -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),
),
Expand Down Expand Up @@ -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"},
},
Expand All @@ -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),
),
Expand All @@ -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),
),
Expand Down

0 comments on commit 09814bc

Please sign in to comment.