Skip to content

Commit 2cbf8d9

Browse files
authored
Merge pull request #610 from eljohnson92/support_int_pointers
add support for int pointer references
2 parents 013a48d + e8a5d63 commit 2cbf8d9

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

pkg/reference/reference.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ func FromFloatPtrValue(v *float64) string {
6060
return strconv.FormatFloat(*v, 'f', 0, 64)
6161
}
6262

63+
// FromIntPtrValue adapts an int pointer field for use as a CurrentValue.
64+
func FromIntPtrValue(v *int64) string {
65+
if v == nil {
66+
return ""
67+
}
68+
return strconv.FormatInt(*v, 10)
69+
}
70+
6371
// ToPtrValue adapts a ResolvedValue for use as a string pointer field.
6472
func ToPtrValue(v string) *string {
6573
if v == "" {
@@ -80,6 +88,18 @@ func ToFloatPtrValue(v string) *float64 {
8088
return &vParsed
8189
}
8290

91+
// ToIntPtrValue adapts a ResolvedValue for use as an int pointer field.
92+
func ToIntPtrValue(v string) *int64 {
93+
if v == "" {
94+
return nil
95+
}
96+
vParsed, err := strconv.ParseInt(v, 10, 64)
97+
if err != nil {
98+
return nil
99+
}
100+
return &vParsed
101+
}
102+
83103
// FromPtrValues adapts a slice of string pointer fields for use as CurrentValues.
84104
// NOTE: Do not use this utility function unless you have to.
85105
// Using pointer slices does not adhere to our current API practices.
@@ -102,6 +122,15 @@ func FromFloatPtrValues(v []*float64) []string {
102122
return res
103123
}
104124

125+
// FromIntPtrValues adapts a slice of int64 pointer fields for use as CurrentValues.
126+
func FromIntPtrValues(v []*int64) []string {
127+
var res = make([]string, len(v))
128+
for i := 0; i < len(v); i++ {
129+
res[i] = FromIntPtrValue(v[i])
130+
}
131+
return res
132+
}
133+
105134
// ToPtrValues adapts ResolvedValues for use as a slice of string pointer fields.
106135
// NOTE: Do not use this utility function unless you have to.
107136
// Using pointer slices does not adhere to our current API practices.
@@ -124,6 +153,15 @@ func ToFloatPtrValues(v []string) []*float64 {
124153
return res
125154
}
126155

156+
// ToIntPtrValues adapts ResolvedValues for use as a slice of int64 pointer fields.
157+
func ToIntPtrValues(v []string) []*int64 {
158+
var res = make([]*int64, len(v))
159+
for i := 0; i < len(v); i++ {
160+
res[i] = ToIntPtrValue(v[i])
161+
}
162+
return res
163+
}
164+
127165
// To indicates the kind of managed resource a reference is to.
128166
type To struct {
129167
Managed resource.Managed

pkg/reference/reference_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ func TestToAndFromFloatPtrValues(t *testing.T) {
125125
}
126126
}
127127

128+
func TestToAndFromIntPtrValues(t *testing.T) {
129+
cases := map[string]struct {
130+
want []string
131+
}{
132+
"Nil": {want: []string{}},
133+
"Zero": {want: []string{""}},
134+
"NonZero": {want: []string{"1123581321"}},
135+
"Multiple": {want: []string{"1123581321", "1234567890"}},
136+
}
137+
for name, tc := range cases {
138+
t.Run(name, func(t *testing.T) {
139+
got := FromIntPtrValues(ToIntPtrValues(tc.want))
140+
if diff := cmp.Diff(tc.want, got); diff != "" {
141+
t.Errorf("FromIntPtrValues(ToIntPtrValues(%s): -want, +got: %s", tc.want, diff)
142+
143+
}
144+
})
145+
}
146+
}
147+
128148
func TestResolve(t *testing.T) {
129149
errBoom := errors.New("boom")
130150
now := metav1.Now()

0 commit comments

Comments
 (0)