api: move formatFloat function

`helpers.FormatFloat` function is only used in `api`.  Moving it and
marking it as private.  We can re-export it if we find value later.
This commit is contained in:
Mahmood Ali
2019-01-18 13:36:49 -05:00
parent 41c3250ee5
commit b1293a8993
6 changed files with 68 additions and 65 deletions

View File

@@ -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 != "" {

View File

@@ -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
}

View File

@@ -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]
}

39
api/utils_test.go Normal file
View File

@@ -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))
}
}

View File

@@ -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]
}

View File

@@ -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))
}
}