Skip to content

Commit

Permalink
Add support for float references
Browse files Browse the repository at this point in the history
  • Loading branch information
joakimhew committed Oct 25, 2022
1 parent fa5545f commit d4944bf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pkg/reference/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package reference

import (
"context"
"strconv"

kerrors "k8s.io/apimachinery/pkg/api/errors"

Expand Down Expand Up @@ -50,6 +51,14 @@ func FromPtrValue(v *string) string {
return *v
}

// FromFloatPtrValue adapts a float64 pointer field for use as a CurrentValue.
func FromFloatPtrValue(v *float64) string {
if v == nil {
return ""
}
return strconv.FormatFloat(*v, 'f', -1, 64)
}

// ToPtrValue adapts a ResolvedValue for use as a string pointer field.
func ToPtrValue(v string) *string {
if v == "" {
Expand All @@ -58,6 +67,15 @@ 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
}
f64, _ := strconv.ParseFloat(v, 32)
return &f64
}

// 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.
Expand All @@ -71,6 +89,33 @@ func FromPtrValues(v []*string) []string {
return res
}

// FromFloatPtrValues adapts a slice of float64 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.
// The current use case is where generated code creates reference-able fields in a provider which are
// float pointers and need to be resolved as part of `ResolveMultiple`
func FromFloatPtrValues(v []*float64) []string {
var res = make([]string, len(v))
for i := 0; i < len(v); i++ {
res[i] = strconv.FormatFloat(*v[i], 'f', -1, 64)
}
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.
// The current use case is where generated code creates reference-able fields in a provider which are
// string pointers and need to be resolved as part of `ResolveMultiple`
func ToFloatPtrValues(v []string) []*float64 {
var res = make([]*float64, len(v))
for i := 0; i < len(v); i++ {
f64, _ := strconv.ParseFloat(v[i], 32)
res[i] = &f64
}
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.
Expand Down
19 changes: 19 additions & 0 deletions pkg/reference/reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ func TestToAndFromPtrValues(t *testing.T) {
}
}

func TestToAndFromFloatPtrValues(t *testing.T) {
cases := map[string]struct {
want []string
}{
"Nil": {want: []string{}},
"Zero": {want: []string{"0"}},
"NonZero": {want: []string{"1"}},
"Multiple": {want: []string{"1", "2"}},
}
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("FromFloatPtrValues(ToFloatPtrValues(%s): -want, +got: %s", tc.want, diff)
}
})
}
}

func TestResolve(t *testing.T) {
errBoom := errors.New("boom")
now := metav1.Now()
Expand Down

0 comments on commit d4944bf

Please sign in to comment.