Skip to content

Commit

Permalink
Add GetValueAsFloat to force returned value to be casted to float (#132)
Browse files Browse the repository at this point in the history
* Add GetValueAsFloat to force returned value to be casted to float

* Remove unused NanOrNinf
  • Loading branch information
alvarocabanas authored Aug 18, 2022
1 parent f89f039 commit 2c786ec
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
29 changes: 28 additions & 1 deletion gojmx/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ package gojmx

import (
"fmt"
"github.com/newrelic/nrjmx/gojmx/internal/nrprotocol"
"strconv"
"strings"
"unsafe"

"github.com/newrelic/nrjmx/gojmx/internal/nrprotocol"
)

/*
Expand Down Expand Up @@ -56,6 +58,31 @@ func (j *AttributeResponse) GetValue() interface{} {
}
}

// GetValueAsFloat casts the value from AttributeResponse to float based on type.
func (j *AttributeResponse) GetValueAsFloat() (float64, error) {
switch (*j).ResponseType {
case ResponseTypeBool:
if j.BoolValue {
return 1, nil
}
return 0, nil
case ResponseTypeString:
parsedValue, err := strconv.ParseFloat(fmt.Sprintf("%v", j.StringValue), 64)
if err != nil {
return 0, err
}
return parsedValue, nil
case ResponseTypeDouble:
return j.DoubleValue, nil
case ResponseTypeInt:
return float64(j.IntValue), nil
case ResponseTypeErr:
return 0, nil
default:
panic(fmt.Sprintf("unkown value type: %v", j.ResponseType))
}
}

// JMXError is reported when a JMX query fails.
type JMXError nrprotocol.JMXError

Expand Down
75 changes: 75 additions & 0 deletions gojmx/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,78 @@ func Test_JMXAttribute_GetValue(t *testing.T) {
})
}
}

func Test_JMXAttribute_GetValueAsFloat(t *testing.T) {
testCases := []struct {
name string
jmxAttr *AttributeResponse
errorExpected bool
expected float64
}{
{
name: "Incorrect String Value",
jmxAttr: &AttributeResponse{
Name: "test:type=Cat,name=tomas,attr=Name",
ResponseType: nrprotocol.ResponseType_STRING,
StringValue: "aaa",
},
errorExpected: true,
expected: 0,
},
{
name: "Double Value",
jmxAttr: &AttributeResponse{
Name: "test:type=Cat,name=tomas,attr=FloatValue",
ResponseType: nrprotocol.ResponseType_DOUBLE,
DoubleValue: 2.222222,
},
expected: 2.222222,
},
{
name: "Number Value",
jmxAttr: &AttributeResponse{
Name: "test:type=Cat,name=tomas,attr=NumberValue",
ResponseType: nrprotocol.ResponseType_INT,
IntValue: 3,
},
expected: float64(3),
},
{
name: "Bool Value",
jmxAttr: &AttributeResponse{
Name: "test:type=Cat,name=tomas,attr=BoolValue",
ResponseType: nrprotocol.ResponseType_BOOL,
BoolValue: true,
},
expected: 1,
},
{
name: "Double Value",
jmxAttr: &AttributeResponse{
Name: "test:type=Cat,name=tomas,attr=DoubleValue",
ResponseType: nrprotocol.ResponseType_DOUBLE,
DoubleValue: 1.2,
},
expected: 1.2,
},
{
name: "String Value",
jmxAttr: &AttributeResponse{
Name: "test:type=Cat,name=tomas,attr=Name",
ResponseType: nrprotocol.ResponseType_STRING,
StringValue: "1.2",
},
expected: 1.2,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
floatVal, err := testCase.jmxAttr.GetValueAsFloat()
if testCase.errorExpected {
assert.Error(t, err)
}
assert.Equal(t, testCase.expected, floatVal)
})
}
}

0 comments on commit 2c786ec

Please sign in to comment.