diff --git a/api/nodes.go b/api/nodes.go index 71bb1051e..b43a06368 100644 --- a/api/nodes.go +++ b/api/nodes.go @@ -6,8 +6,6 @@ import ( "sort" "strconv" "time" - - "github.com/hashicorp/nomad/helper" ) // Nodes is used to query node-related API endpoints @@ -661,9 +659,9 @@ func (v *StatValue) String() string { case v.StringVal != nil: return *v.StringVal case v.FloatNumeratorVal != nil: - str := helper.FormatFloat(*v.FloatNumeratorVal, 3) + str := formatFloat(*v.FloatNumeratorVal, 3) if v.FloatDenominatorVal != nil { - str += " / " + helper.FormatFloat(*v.FloatDenominatorVal, 3) + str += " / " + formatFloat(*v.FloatDenominatorVal, 3) } if v.Unit != "" { diff --git a/api/resources.go b/api/resources.go index c8bf9b079..bad52c711 100644 --- a/api/resources.go +++ b/api/resources.go @@ -2,8 +2,6 @@ package api import ( "strconv" - - "github.com/hashicorp/nomad/helper" ) // Resources encapsulates the required resources of @@ -169,7 +167,7 @@ type Attribute struct { func (a Attribute) String() string { switch { case a.FloatVal != nil: - str := helper.FormatFloat(*a.FloatVal, 3) + str := formatFloat(*a.FloatVal, 3) if a.Unit != "" { str += " " + a.Unit } diff --git a/api/utils.go b/api/utils.go index fa0b76bd2..67b409a8c 100644 --- a/api/utils.go +++ b/api/utils.go @@ -1,6 +1,10 @@ package api -import "time" +import ( + "strconv" + "strings" + "time" +) // boolToPtr returns the pointer to a boolean func boolToPtr(b bool) *bool { @@ -26,3 +30,24 @@ func stringToPtr(str string) *string { func timeToPtr(t time.Duration) *time.Duration { return &t } + +// formatFloat converts the floating-point number f to a string, +// after rounding it to the passed unit. +// +// Uses 'f' format (-ddd.dddddd, no exponent), and uses at most +// maxPrec digits after the decimal point. +func formatFloat(f float64, maxPrec int) string { + v := strconv.FormatFloat(f, 'f', -1, 64) + + idx := strings.LastIndex(v, ".") + if idx == -1 { + return v + } + + sublen := idx + maxPrec + 1 + if sublen > len(v) { + sublen = len(v) + } + + return v[:sublen] +} diff --git a/api/utils_test.go b/api/utils_test.go new file mode 100644 index 000000000..14c50544e --- /dev/null +++ b/api/utils_test.go @@ -0,0 +1,39 @@ +package api + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestFormatRoundedFloat(t *testing.T) { + cases := []struct { + input float64 + expected string + }{ + { + 1323, + "1323", + }, + { + 10.321, + "10.321", + }, + { + 100000.31324324, + "100000.313", + }, + { + 100000.3, + "100000.3", + }, + { + 0.7654321, + "0.765", + }, + } + + for _, c := range cases { + require.Equal(t, c.expected, formatFloat(c.input, 3)) + } +} diff --git a/helper/funcs.go b/helper/funcs.go index b9a77db96..0a280e522 100644 --- a/helper/funcs.go +++ b/helper/funcs.go @@ -4,8 +4,6 @@ import ( "crypto/sha512" "fmt" "regexp" - "strconv" - "strings" "time" multierror "github.com/hashicorp/go-multierror" @@ -359,24 +357,3 @@ func CheckHCLKeys(node ast.Node, valid []string) error { return result } - -// FormatFloat converts the floating-point number f to a string, -// after rounding it to the passed unit. -// -// Uses 'f' format (-ddd.dddddd, no exponent), and uses at most -// maxPrec digits after the decimal point. -func FormatFloat(f float64, maxPrec int) string { - v := strconv.FormatFloat(f, 'f', -1, 64) - - idx := strings.LastIndex(v, ".") - if idx == -1 { - return v - } - - sublen := idx + maxPrec + 1 - if sublen > len(v) { - sublen = len(v) - } - - return v[:sublen] -} diff --git a/helper/funcs_test.go b/helper/funcs_test.go index e9c9cdcd7..774030be1 100644 --- a/helper/funcs_test.go +++ b/helper/funcs_test.go @@ -4,8 +4,6 @@ import ( "reflect" "sort" "testing" - - "github.com/stretchr/testify/require" ) func TestSliceStringIsSubset(t *testing.T) { @@ -89,35 +87,3 @@ func BenchmarkCleanEnvVar(b *testing.B) { CleanEnvVar(in, replacement) } } - -func TestFormatRoundedFloat(t *testing.T) { - cases := []struct { - input float64 - expected string - }{ - { - 1323, - "1323", - }, - { - 10.321, - "10.321", - }, - { - 100000.31324324, - "100000.313", - }, - { - 100000.3, - "100000.3", - }, - { - 0.7654321, - "0.765", - }, - } - - for _, c := range cases { - require.Equal(t, c.expected, FormatFloat(c.input, 3)) - } -}