Skip to content

Commit

Permalink
fix: ConvertNumeric now handles bools (#1336)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

- Closes #1335 

## Short description of the changes

- Add bool case to ConvertNumeric
- Add test for ConvertNumeric function
  • Loading branch information
kentquirk authored and MikeGoldsmith committed Oct 10, 2024
1 parent 51f5941 commit e09d23b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
7 changes: 5 additions & 2 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ func ConvertNumeric(val interface{}) float64 {
return n
case float32:
return float64(n)
default:
return 0
case bool:
if n {
return 1
}
}
return 0
}

func PrefixMetricName(prefix string, name string) string {
Expand Down
34 changes: 34 additions & 0 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
package metrics

import "testing"

func TestConvertNumeric(t *testing.T) {
tests := []struct {
name string
val interface{}
want float64
}{
// TODO: Add test cases.
{"int", 17, 17},
{"uint", uint(17), 17},
{"int64", int64(17), 17},
{"uint64", uint64(17), 17},
{"int32", int32(17), 17},
{"uint32", uint32(17), 17},
{"int16", int16(17), 17},
{"uint16", uint16(17), 17},
{"int8", int8(17), 17},
{"uint8", uint8(17), 17},
{"float64", float64(17), 17},
{"float32", float32(17), 17},
{"bool", true, 1},
{"bool", false, 0},
{"string", "17", 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ConvertNumeric(tt.val); got != tt.want {
t.Errorf("ConvertNumeric() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit e09d23b

Please sign in to comment.