Merge pull request #244 from smcavallo/reference_ptrvalue_helpers

add pointer helper functions for slices for referencers
This commit is contained in:
Daniel Mangum 2021-03-01 16:00:09 -06:00 committed by GitHub
commit 3cd26cccdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

@ -56,6 +56,32 @@ func ToPtrValue(v string) *string {
return &v
}
// 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.
// 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 FromPtrValues(v []*string) []string {
var res = make([]string, len(v))
for i := 0; i < len(v); i++ {
res[i] = FromPtrValue(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.
// 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 ToPtrValues(v []string) []*string {
var res = make([]*string, len(v))
for i := 0; i < len(v); i++ {
res[i] = ToPtrValue(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,26 @@ func TestToAndFromPtr(t *testing.T) {
}
}
func TestToAndFromPtrValues(t *testing.T) {
cases := map[string]struct {
want []string
}{
"Nil": {want: []string{}},
"Zero": {want: []string{""}},
"NonZero": {want: []string{"pointy"}},
"Multiple": {want: []string{"pointy", "pointers"}},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := FromPtrValues(ToPtrValues(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()