Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix numeric to bool conversion in converter #7579

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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