From e8a5d6305804b5661b8f4b23db178e01cc9d5604 Mon Sep 17 00:00:00 2001 From: Evan Johnson Date: Mon, 20 Nov 2023 12:48:47 -0500 Subject: [PATCH] add support for int pointer references Signed-off-by: Evan Johnson --- pkg/reference/reference.go | 38 +++++++++++++++++++++++++++++++++ pkg/reference/reference_test.go | 20 +++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/pkg/reference/reference.go b/pkg/reference/reference.go index 38e905c35..ca8282b2f 100644 --- a/pkg/reference/reference.go +++ b/pkg/reference/reference.go @@ -60,6 +60,14 @@ func FromFloatPtrValue(v *float64) string { return strconv.FormatFloat(*v, 'f', 0, 64) } +// FromIntPtrValue adapts an int pointer field for use as a CurrentValue. +func FromIntPtrValue(v *int64) string { + if v == nil { + return "" + } + return strconv.FormatInt(*v, 10) +} + // ToPtrValue adapts a ResolvedValue for use as a string pointer field. func ToPtrValue(v string) *string { if v == "" { @@ -80,6 +88,18 @@ func ToFloatPtrValue(v string) *float64 { return &vParsed } +// ToIntPtrValue adapts a ResolvedValue for use as an int pointer field. +func ToIntPtrValue(v string) *int64 { + if v == "" { + return nil + } + vParsed, err := strconv.ParseInt(v, 10, 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. @@ -102,6 +122,15 @@ func FromFloatPtrValues(v []*float64) []string { return res } +// FromIntPtrValues adapts a slice of int64 pointer fields for use as CurrentValues. +func FromIntPtrValues(v []*int64) []string { + var res = make([]string, len(v)) + for i := 0; i < len(v); i++ { + res[i] = FromIntPtrValue(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. @@ -124,6 +153,15 @@ func ToFloatPtrValues(v []string) []*float64 { return res } +// ToIntPtrValues adapts ResolvedValues for use as a slice of int64 pointer fields. +func ToIntPtrValues(v []string) []*int64 { + var res = make([]*int64, len(v)) + for i := 0; i < len(v); i++ { + res[i] = ToIntPtrValue(v[i]) + } + return res +} + // To indicates the kind of managed resource a reference is to. type To struct { Managed resource.Managed diff --git a/pkg/reference/reference_test.go b/pkg/reference/reference_test.go index 99f1a1733..e83ad3e8f 100644 --- a/pkg/reference/reference_test.go +++ b/pkg/reference/reference_test.go @@ -125,6 +125,26 @@ func TestToAndFromFloatPtrValues(t *testing.T) { } } +func TestToAndFromIntPtrValues(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 := FromIntPtrValues(ToIntPtrValues(tc.want)) + if diff := cmp.Diff(tc.want, got); diff != "" { + t.Errorf("FromIntPtrValues(ToIntPtrValues(%s): -want, +got: %s", tc.want, diff) + + } + }) + } +} + func TestResolve(t *testing.T) { errBoom := errors.New("boom") now := metav1.Now()