Skip to content

Commit

Permalink
update how scaling factor is applied (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski authored Oct 19, 2018
1 parent 928f440 commit 79f6b2e
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 55 deletions.
74 changes: 44 additions & 30 deletions sdk/type.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package sdk

import (
"encoding/json"
"strconv"
"strings"

"encoding/json"

log "github.com/Sirupsen/logrus"
"github.com/vapor-ware/synse-sdk/sdk/errors"
"github.com/vapor-ware/synse-server-grpc/go"
)
Expand Down Expand Up @@ -94,34 +94,48 @@ func (outputType *OutputType) Apply(value interface{}) interface{} { // nolint:
return value
}

// Do not permit a scaling factor of 0.
if scalingFactor != 0 {
switch t := value.(type) {
case float64:
value = t * scalingFactor
case float32:
value = float32(float64(t) * scalingFactor)
case int64:
value = int64(float64(t) * scalingFactor)
case int32:
value = int32(float64(t) * scalingFactor)
case int16:
value = int16(float64(t) * scalingFactor)
case int8:
value = int8(float64(t) * scalingFactor)
case int:
value = int(float64(t) * scalingFactor)
case uint64:
value = uint64(float64(t) * scalingFactor)
case uint32:
value = uint32(float64(t) * scalingFactor)
case uint16:
value = uint16(float64(t) * scalingFactor)
case uint8:
value = uint8(float64(t) * scalingFactor)
case uint:
value = uint(float64(t) * scalingFactor)
}
// If the scaling factor is 0, log a warning and just return the original value.
if scalingFactor == 0 {
log.WithField("value", value).Warn(
"[type] got scaling factor of 0; will not apply",
)
return value
}

// If the scaling factor is 1, there is nothing to do. Return the value.
if scalingFactor == 1 {
return value
}

// Otherwise, the scaling factor is non-zero and not 1, so it will
// need to be applied.
switch t := value.(type) {
case float64:
value = t * scalingFactor
case float32:
value = float64(t) * scalingFactor
case int64:
value = float64(t) * scalingFactor
case int32:
value = float64(t) * scalingFactor
case int16:
value = float64(t) * scalingFactor
case int8:
value = float64(t) * scalingFactor
case int:
value = float64(t) * scalingFactor
case uint64:
value = float64(t) * scalingFactor
case uint32:
value = float64(t) * scalingFactor
case uint16:
value = float64(t) * scalingFactor
case uint8:
value = float64(t) * scalingFactor
case uint:
value = float64(t) * scalingFactor
default:
log.Warnf("[type] Unable to apply scaling factor %v to value %v of type %v", scalingFactor, value, t)
}
return value
}
Expand Down
106 changes: 81 additions & 25 deletions sdk/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,23 @@ func TestOutputType_Apply(t *testing.T) {
ScalingFactor: "3",
},
value: 0,
expected: 0,
expected: float64(0),
},
{
desc: "multiply factor by value 1",
output: OutputType{
ScalingFactor: "3",
},
value: 1,
expected: 3,
expected: float64(3),
},
{
desc: "multiply value by 0.5 factor",
output: OutputType{
ScalingFactor: "0.5",
},
value: float64(1),
expected: 0.5,
expected: float64(0.5),
},
{
desc: "value is a float64, factor is > 1",
Expand All @@ -288,175 +288,231 @@ func TestOutputType_Apply(t *testing.T) {
ScalingFactor: "2",
},
value: float32(3),
expected: float32(6),
expected: float64(6),
},
{
desc: "value is a float32, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: float32(3),
expected: float32(1.5),
expected: float64(1.5),
},
{
desc: "value is a int64, factor is > 1",
output: OutputType{
ScalingFactor: "2",
},
value: int64(3),
expected: int64(6),
expected: float64(6),
},
{
desc: "value is a int64, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: int64(3),
expected: int64(1),
expected: float64(1.5),
},
{
desc: "value is a int32, factor is > 1",
output: OutputType{
ScalingFactor: "2",
},
value: int32(3),
expected: int32(6),
expected: float64(6),
},
{
desc: "value is a int32, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: int32(3),
expected: int32(1),
expected: float64(1.5),
},
{
desc: "value is a int16, factor is > 1",
output: OutputType{
ScalingFactor: "2",
},
value: int16(3),
expected: int16(6),
expected: float64(6),
},
{
desc: "value is a int16, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: int16(3),
expected: int16(1),
expected: float64(1.5),
},
{
desc: "value is a int8, factor is > 1",
output: OutputType{
ScalingFactor: "2",
},
value: int8(3),
expected: int8(6),
expected: float64(6),
},
{
desc: "value is a int8, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: int8(3),
expected: int8(1),
expected: float64(1.5),
},
{
desc: "value is a int, factor is > 1",
output: OutputType{
ScalingFactor: "2",
},
value: int(3),
expected: int(6),
expected: float64(6),
},
{
desc: "value is a int, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: int(3),
expected: int(1),
expected: float64(1.5),
},
{
desc: "value is a uint64, factor is > 1",
output: OutputType{
ScalingFactor: "2",
},
value: uint64(3),
expected: uint64(6),
expected: float64(6),
},
{
desc: "value is a uint64, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: uint64(3),
expected: uint64(1),
expected: float64(1.5),
},
{
desc: "value is a uint32, factor is > 1",
output: OutputType{
ScalingFactor: "2",
},
value: uint32(3),
expected: uint32(6),
expected: float64(6),
},
{
desc: "value is a uint32, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: uint32(3),
expected: uint32(1),
expected: float64(1.5),
},
{
desc: "value is a uint16, factor is > 1",
output: OutputType{
ScalingFactor: "2",
},
value: uint32(3),
expected: uint32(6),
expected: float64(6),
},
{
desc: "value is a uint16, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: uint16(3),
expected: uint16(1),
expected: float64(1.5),
},
{
desc: "value is a uint8, factor is > 1",
output: OutputType{
ScalingFactor: "2",
},
value: uint8(3),
expected: uint8(6),
expected: float64(6),
},
{
desc: "value is a uint8, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: uint8(3),
expected: uint8(1),
expected: float64(1.5),
},
{
desc: "value is a uint, factor is > 1",
output: OutputType{
ScalingFactor: "2",
},
value: uint(3),
expected: uint(6),
expected: float64(6),
},
{
desc: "value is a uint, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: uint(3),
expected: uint(1),
expected: float64(1.5),
},
{
desc: "value is a bool, factor is < 1",
output: OutputType{
ScalingFactor: "0.5",
},
value: true,
expected: true,
},
{
desc: "value is a uint, factor is 1",
output: OutputType{
ScalingFactor: "1",
},
value: uint(3),
expected: uint(3),
},
{
desc: "value is a float32, factor is 1",
output: OutputType{
ScalingFactor: "1",
},
value: float32(3),
expected: float32(3),
},
{
desc: "value is a bool, factor is 1",
output: OutputType{
ScalingFactor: "1",
},
value: true,
expected: true,
},
{
desc: "value is a uint, factor is 0",
output: OutputType{
ScalingFactor: "0",
},
value: uint(3),
expected: uint(3),
},
{
desc: "value is a float32, factor is 0",
output: OutputType{
ScalingFactor: "0",
},
value: float32(3),
expected: float32(3),
},
{
desc: "value is a bool, factor is 0",
output: OutputType{
ScalingFactor: "0",
},
value: false,
expected: false,
},
}

Expand Down

0 comments on commit 79f6b2e

Please sign in to comment.