add support for float pointer references

Signed-off-by: Evan Johnson <eljohn1014@gmail.com>
This commit is contained in:
Evan Johnson 2022-12-05 20:01:14 -05:00 committed by Evan Johnson
parent 65044f0439
commit ce686bf20e
2 changed files with 78 additions and 0 deletions

View File

@ -18,6 +18,7 @@ package reference
import (
"context"
"strconv"
kerrors "k8s.io/apimachinery/pkg/api/errors"
@ -50,6 +51,14 @@ func FromPtrValue(v *string) string {
return *v
}
// FromFloatPtrValue adapts a float pointer field for use as a CurrentValue.
func FromFloatPtrValue(v *float64) string {
if v == nil {
return ""
}
return strconv.FormatFloat(*v, 'f', 0, 64)
}
// ToPtrValue adapts a ResolvedValue for use as a string pointer field.
func ToPtrValue(v string) *string {
if v == "" {
@ -58,6 +67,18 @@ func ToPtrValue(v string) *string {
return &v
}
// ToFloatPtrValue adapts a ResolvedValue for use as a float64 pointer field.
func ToFloatPtrValue(v string) *float64 {
if v == "" {
return nil
}
vParsed, err := strconv.ParseFloat(v, 64)
if err != nil {
return nil
}
return &vParsed
}
// FromPtrValues adapts a slice of string pointer fields for use as CurrentValues.
// NOTE: Do not use this utility function unless you have to.
// Using pointer slices does not adhere to our current API practices.
@ -71,6 +92,15 @@ func FromPtrValues(v []*string) []string {
return res
}
// FromFloatPtrValues adapts a slice of float64 pointer fields for use as CurrentValues.
func FromFloatPtrValues(v []*float64) []string {
var res = make([]string, len(v))
for i := 0; i < len(v); i++ {
res[i] = FromFloatPtrValue(v[i])
}
return res
}
// ToPtrValues adapts ResolvedValues for use as a slice of string pointer fields.
// NOTE: Do not use this utility function unless you have to.
// Using pointer slices does not adhere to our current API practices.
@ -84,6 +114,15 @@ func ToPtrValues(v []string) []*string {
return res
}
// ToFloatPtrValues adapts ResolvedValues for use as a slice of float64 pointer fields.
func ToFloatPtrValues(v []string) []*float64 {
var res = make([]*float64, len(v))
for i := 0; i < len(v); i++ {
res[i] = ToFloatPtrValue(v[i])
}
return res
}
// To indicates the kind of managed resource a reference is to.
type To struct {
Managed resource.Managed

View File

@ -66,6 +66,25 @@ func TestToAndFromPtr(t *testing.T) {
}
}
func TestToAndFromFloatPtr(t *testing.T) {
cases := map[string]struct {
want string
}{
"Zero": {want: ""},
"NonZero": {want: "1123581321"},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := FromFloatPtrValue(ToFloatPtrValue(tc.want))
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("FromPtrValue(ToPtrValue(%s): -want, +got: %s", tc.want, diff)
}
})
}
}
func TestToAndFromPtrValues(t *testing.T) {
cases := map[string]struct {
want []string
@ -86,6 +105,26 @@ func TestToAndFromPtrValues(t *testing.T) {
}
}
func TestToAndFromFloatPtrValues(t *testing.T) {
cases := map[string]struct {
want []string
}{
"Nil": {want: []string{}},
"Zero": {want: []string{""}},
"NonZero": {want: []string{"1123581321"}},
"Multiple": {want: []string{"1123581321", "1234567890"}},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := FromFloatPtrValues(ToFloatPtrValues(tc.want))
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("FromPtrValues(ToPtrValues(%s): -want, +got: %s", tc.want, diff)
}
})
}
}
func TestResolve(t *testing.T) {
errBoom := errors.New("boom")
now := metav1.Now()