-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add comparisons pkg for reused helpers
This commit moves the helper functions from provider.go to a new comparisons package. This package will be used by provider, tests, and resources. The refactoring will assist with splitting resources into resource specific packages. The previous helper functions have been deprecated and will be removed in a future commit. Signed-off-by: Marques Johansson <mjohansson@equinix.com>
- Loading branch information
Showing
3 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package comparisons | ||
|
||
import ( | ||
"cmp" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
// IntValue returns the value of a given int pointer | ||
// or 0 if the pointer is nil | ||
func intValue(i *int) int { | ||
if i != nil { | ||
return *i | ||
} | ||
return 0 | ||
} | ||
|
||
// StringValue returns the value of a given string pointer | ||
// or empty string if the pointer is nil | ||
func stringValue(s *string) string { | ||
if s != nil { | ||
return *s | ||
} | ||
return "" | ||
} | ||
|
||
// isEmpty returns true if the given value is empty | ||
func IsEmpty(v interface{}) bool { | ||
switch v := v.(type) { | ||
case int: | ||
return v == 0 | ||
case *int: | ||
return intValue(v) == 0 | ||
case string: | ||
return v == "" | ||
case *string: | ||
return stringValue(v) == "" | ||
case nil: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
// Subsets returns true if the first slice is a subset of the second slice | ||
func Subsets[T cmp.Ordered](s1, s2 []T) bool { | ||
// Iterate over the first slice | ||
for _, e := range s1 { | ||
// If the element is not in the second slice, return false | ||
if !slices.Contains(s2, e) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
// comparisons.SlicesMatch returns true if the two slices contain the same elements, regardless of order | ||
func SlicesMatch[T cmp.Ordered](s1, s2 []T) bool { | ||
if len(s1) != len(s2) { | ||
return false | ||
} | ||
|
||
// Create copies of the slices to avoid mutating the input slices | ||
s1Copy := append([]T(nil), s1...) | ||
s2Copy := append([]T(nil), s2...) | ||
|
||
// Sort the slices | ||
slices.Sort(s1Copy) | ||
slices.Sort(s2Copy) | ||
|
||
return slices.Equal(s1Copy, s2Copy) | ||
} | ||
|
||
// caseInsensitiveLess is a comparison function for sorting strings case-insensitively | ||
func caseInsensitiveLess(s1, s2 string) int { | ||
switch { | ||
case strings.ToLower(s1) == strings.ToLower(s2): | ||
return 0 | ||
case strings.ToLower(s1) < strings.ToLower(s2): | ||
return -1 | ||
default: | ||
return 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package comparisons_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/equinix/terraform-provider-equinix/internal/comparisons" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSubsets(t *testing.T) { | ||
// given | ||
needles := []string{"key1", "key5"} | ||
hay := []string{"key1", "key2", "Key3", "key4", "key5"} | ||
// when | ||
result := comparisons.Subsets(needles, hay) | ||
// then | ||
assert.True(t, result, "Given strings were found") | ||
} | ||
|
||
func TestSubsets_negative(t *testing.T) { | ||
// given | ||
needles := []string{"key1", "key6"} | ||
hay := []string{"key1", "key2", "Key3", "key4", "key5"} | ||
// when | ||
result := comparisons.Subsets(hay, needles) | ||
// then | ||
assert.False(t, result, "Given strings were found") | ||
} | ||
|
||
func TestIsEmpty(t *testing.T) { | ||
// given | ||
input := []interface{}{ | ||
"test", | ||
"", | ||
nil, | ||
123, | ||
0, | ||
43.43, | ||
} | ||
expected := []bool{ | ||
false, | ||
true, | ||
true, | ||
false, | ||
true, | ||
false, | ||
true, | ||
} | ||
// when then | ||
for i := range input { | ||
assert.Equal(t, expected[i], comparisons.IsEmpty(input[i]), "Input %v produces expected result %v", input[i], expected[i]) | ||
} | ||
} | ||
|
||
func TestSlicesMatch(t *testing.T) { | ||
// given | ||
input := [][][]string{ | ||
{ | ||
{"DC", "SV", "FR"}, | ||
{"FR", "SV", "DC"}, | ||
}, | ||
{ | ||
{"SV"}, | ||
{}, | ||
}, | ||
{ | ||
{"DC", "DC", "DC"}, | ||
{"DC", "SV", "DC"}, | ||
}, | ||
{ | ||
{}, {}, | ||
}, | ||
} | ||
expected := []bool{ | ||
true, | ||
false, | ||
false, | ||
true, | ||
} | ||
// when | ||
results := make([]bool, len(expected)) | ||
for i := range input { | ||
results[i] = comparisons.SlicesMatch(input[i][0], input[i][1]) | ||
} | ||
// then | ||
for i := range expected { | ||
assert.Equal(t, expected[i], results[i]) | ||
} | ||
} |