Skip to content

Commit 8eee163

Browse files
committed
allow bools in StatSourceFromStruct
1 parent 9db88ad commit 8eee163

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

struct.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ package monkit
1616

1717
import "reflect"
1818

19-
var f64Type = reflect.TypeOf(float64(0))
19+
var (
20+
f64Type = reflect.TypeOf(float64(0))
21+
boolType = reflect.TypeOf(bool(false))
22+
)
2023

2124
type emptyStatSource struct{}
2225

2326
func (emptyStatSource) Stats(cb func(key SeriesKey, field string, val float64)) {}
2427

2528
// StatSourceFromStruct uses the reflect package to implement the Stats call
26-
// across all float64-castable fields of the struct.
29+
// across all float64-castable and bool-castable fields of the struct.
2730
func StatSourceFromStruct(key SeriesKey, structData interface{}) StatSource {
2831
val := deref(reflect.ValueOf(structData))
2932

@@ -45,6 +48,12 @@ func StatSourceFromStruct(key SeriesKey, structData interface{}) StatSource {
4548

4649
} else if field_type.ConvertibleTo(f64Type) {
4750
cb(key, typ.Field(i).Name, field.Convert(f64Type).Float())
51+
} else if field_type.ConvertibleTo(boolType) {
52+
if field.Convert(boolType).Bool() {
53+
cb(key, typ.Field(i).Name, 1)
54+
} else {
55+
cb(key, typ.Field(i).Name, 0)
56+
}
4857
}
4958
}
5059
})

0 commit comments

Comments
 (0)