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

Add IsValidValue method to stats package. #116

Merged
merged 3 commits into from
Jan 3, 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
2 changes: 1 addition & 1 deletion buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (b HistogramBuckets) Set(key string, buckets ...interface{}) {
v := make([]Value, len(buckets))

for i, b := range buckets {
v[i] = ValueOf(b)
v[i] = MustValueOf(ValueOf(b))
}

b[makeKey(key)] = v
Expand Down
2 changes: 1 addition & 1 deletion field.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Field struct {

// MakeField constructs and returns a new Field from name, value, and ftype.
func MakeField(name string, value interface{}, ftype FieldType) Field {
f := Field{Name: name, Value: ValueOf(value)}
f := Field{Name: name, Value: MustValueOf(ValueOf(value))}
f.setType(ftype)
return f
}
Expand Down
12 changes: 11 additions & 1 deletion value.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ type Value struct {
bits uint64
}

func MustValueOf(v Value) Value {
if v.Type() == Invalid {
panic("stats.MustValueOf received a value of unsupported type")
}
return v
}

func ValueOf(v interface{}) Value {
switch x := v.(type) {
case Value:
return x
case nil:
return Value{}
case bool:
Expand Down Expand Up @@ -47,7 +56,7 @@ func ValueOf(v interface{}) Value {
case time.Duration:
return durationValue(x)
default:
panic("stats.ValueOf received a value of unsupported type")
return Value{typ: Invalid}
}
}

Expand Down Expand Up @@ -182,6 +191,7 @@ const (
Uint
Float
Duration
Invalid
)

func (t Type) String() string {
Expand Down
42 changes: 42 additions & 0 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,50 @@ import (
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestMustValueOf(t *testing.T) {
tests := []struct {
name string
in interface{}
out interface{}
panic bool
}{
{
name: "should not panic",
in: 42,
out: ValueOf(42),
},
{
name: "should panic",
in: struct{}{},
panic: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.panic {
require.PanicsWithValue(t, "stats.MustValueOf received a value of unsupported type", func() {
MustValueOf(ValueOf(test.in))
})
} else {
out := MustValueOf(ValueOf(test.in))
require.EqualValues(t, test.out, out)
}
})
}
}

func TestValueOfIdentity(t *testing.T) {
v1 := ValueOf(3.14)
v2 := ValueOf(v1)
if !reflect.DeepEqual(v1, v2) {
t.Fatalf("Expected %+v to be equal to %+v", v2, v1)
}
}

func TestValueOf(t *testing.T) {
tests := []struct {
in interface{}
Expand Down