Skip to content

Commit

Permalink
Add function to datapoint package's cast metric values functions (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsignalfx authored Mar 25, 2019
1 parent ed6b82f commit 01b8a11
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 102 deletions.
15 changes: 15 additions & 0 deletions datapoint/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,18 @@ func CastMetricValue(value interface{}) (metricValue Value, err error) {
}
return
}

// CastMetricValueWithBool casts an interface to datapoint Value and handles bool
func CastMetricValueWithBool(value interface{}) (metricValue Value, err error) {
switch value.(type) {
case bool:
if value.(bool) {
metricValue, err = intWire(int64(1)), nil
} else {
metricValue, err = intWire(int64(0)), nil
}
default:
metricValue, err = CastMetricValue(value)
}
return
}
240 changes: 138 additions & 102 deletions datapoint/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,109 +5,113 @@ import (
"testing"
)

type args struct {
value interface{}
}

type CastMetricValueTest struct {
name string
args args
wantMetricValue Value
wantErr bool
}

var castMetricValueTests = []CastMetricValueTest{
{
name: "cast float32 to int value",
args: args{
value: float32(1),
},
wantMetricValue: NewFloatValue(float64(1)),
},
{
name: "cast float64 to int value",
args: args{
value: float64(2),
},
wantMetricValue: NewFloatValue(float64(2)),
},
{
name: "cast uint to int value",
args: args{
value: uint(4),
},
wantMetricValue: NewIntValue(int64(4)),
},
{
name: "cast uint8 to int value",
args: args{
value: uint8(5),
},
wantMetricValue: NewIntValue(int64(5)),
},
{
name: "cast uint16 to int value",
args: args{
value: uint16(6),
},
wantMetricValue: NewIntValue(int64(6)),
},
{
name: "cast uint32 to int value",
args: args{
value: uint32(7),
},
wantMetricValue: NewIntValue(int64(7)),
},
{
name: "cast uint64 to int value",
args: args{
value: uint64(8),
},
wantMetricValue: NewIntValue(int64(8)),
},
{
name: "cast int to int value",
args: args{
value: int(10),
},
wantMetricValue: NewIntValue(int64(10)),
},
{
name: "cast int8 to int value",
args: args{
value: int8(11),
},
wantMetricValue: NewIntValue(int64(11)),
},
{
name: "cast int16 to int value",
args: args{
value: int16(12),
},
wantMetricValue: NewIntValue(int64(12)),
},
{
name: "cast int32 to int value",
args: args{
value: int32(13),
},
wantMetricValue: NewIntValue(int64(13)),
},
{
name: "cast int64 to int value",
args: args{
value: int64(14),
},
wantMetricValue: NewIntValue(int64(14)),
},
{
name: "cast metric value error",
args: args{
value: "hello world",
},
wantErr: true,
},
}

func TestCastMetricValue(t *testing.T) {
type args struct {
value interface{}
}
tests := []struct {
name string
args args
wantMetricValue Value
wantErr bool
}{
{
name: "cast float32 to int value",
args: args{
value: float32(1),
},
wantMetricValue: NewFloatValue(float64(1)),
},
{
name: "cast float64 to int value",
args: args{
value: float64(2),
},
wantMetricValue: NewFloatValue(float64(2)),
},
{
name: "cast uint to int value",
args: args{
value: uint(4),
},
wantMetricValue: NewIntValue(int64(4)),
},
{
name: "cast uint8 to int value",
args: args{
value: uint8(5),
},
wantMetricValue: NewIntValue(int64(5)),
},
{
name: "cast uint16 to int value",
args: args{
value: uint16(6),
},
wantMetricValue: NewIntValue(int64(6)),
},
{
name: "cast uint32 to int value",
args: args{
value: uint32(7),
},
wantMetricValue: NewIntValue(int64(7)),
},
{
name: "cast uint64 to int value",
args: args{
value: uint64(8),
},
wantMetricValue: NewIntValue(int64(8)),
},
{
name: "cast int to int value",
args: args{
value: int(10),
},
wantMetricValue: NewIntValue(int64(10)),
},
{
name: "cast int8 to int value",
args: args{
value: int8(11),
},
wantMetricValue: NewIntValue(int64(11)),
},
{
name: "cast int16 to int value",
args: args{
value: int16(12),
},
wantMetricValue: NewIntValue(int64(12)),
},
{
name: "cast int32 to int value",
args: args{
value: int32(13),
},
wantMetricValue: NewIntValue(int64(13)),
},
{
name: "cast int64 to int value",
args: args{
value: int64(14),
},
wantMetricValue: NewIntValue(int64(14)),
},
{
name: "cast metric value error",
args: args{
value: "hello world",
},
wantErr: true,
},
}
for _, tt := range tests {
for _, tt := range castMetricValueTests {
t.Run(tt.name, func(t *testing.T) {
gotMetricValue, err := CastMetricValue(tt.args.value)
if (err != nil) != tt.wantErr {
Expand All @@ -121,6 +125,38 @@ func TestCastMetricValue(t *testing.T) {
}
}

var castMetricValueWithBoolTests = append(castMetricValueTests,
CastMetricValueTest{
name: "cast bool true to int 1",
args: args{
value: true,
},
wantMetricValue: NewIntValue(int64(1)),
},
CastMetricValueTest{
name: "cast bool false to int 0",
args: args{
value: false,
},
wantMetricValue: NewIntValue(int64(0)),
},
)

func TestCastMetricValueWithBool(t *testing.T) {
for _, tt := range castMetricValueWithBoolTests {
t.Run(tt.name, func(t *testing.T) {
gotMetricValue, err := CastMetricValueWithBool(tt.args.value)
if (err != nil) != tt.wantErr {
t.Errorf("CastMetricValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotMetricValue, tt.wantMetricValue) {
t.Errorf("CastMetricValue() = %v, want %v", gotMetricValue, tt.wantMetricValue)
}
})
}
}

func TestCastFloatValue(t *testing.T) {
type args struct {
value interface{}
Expand Down

0 comments on commit 01b8a11

Please sign in to comment.