Skip to content

Commit

Permalink
allow bools in StatSourceFromStruct
Browse files Browse the repository at this point in the history
  • Loading branch information
zeebo committed Nov 26, 2024
1 parent 9db88ad commit 8eee163
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ package monkit

import "reflect"

var f64Type = reflect.TypeOf(float64(0))
var (
f64Type = reflect.TypeOf(float64(0))
boolType = reflect.TypeOf(bool(false))
)

type emptyStatSource struct{}

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

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

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

} else if field_type.ConvertibleTo(f64Type) {
cb(key, typ.Field(i).Name, field.Convert(f64Type).Float())
} else if field_type.ConvertibleTo(boolType) {
if field.Convert(boolType).Bool() {
cb(key, typ.Field(i).Name, 1)
} else {
cb(key, typ.Field(i).Name, 0)
}
}
}
})
Expand Down

0 comments on commit 8eee163

Please sign in to comment.