From fdfd157ae55c9b57d4761114ab54d4f1e8c358fa Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 27 Jun 2024 08:40:09 -0700 Subject: [PATCH] terraform: Remove strSliceContains and updateStrings util functions These are not currently used, and seem to be here because at the time Go didn't offer functions that are generic over all slice types. Go standard library now has utilities similar to these which we should use if we encounter relevant needs in future. --- internal/terraform/util.go | 32 ------------------ internal/terraform/util_test.go | 58 --------------------------------- 2 files changed, 90 deletions(-) diff --git a/internal/terraform/util.go b/internal/terraform/util.go index 0bd31fb32b..681f1dd300 100644 --- a/internal/terraform/util.go +++ b/internal/terraform/util.go @@ -3,10 +3,6 @@ package terraform -import ( - "sort" -) - // Semaphore is a wrapper around a channel to provide // utility methods to clarify that we are treating the // channel as a semaphore @@ -48,31 +44,3 @@ func (s Semaphore) Release() { panic("release without an acquire") } } - -// strSliceContains checks if a given string is contained in a slice -// When anybody asks why Go needs generics, here you go. -func strSliceContains(haystack []string, needle string) bool { - for _, s := range haystack { - if s == needle { - return true - } - } - return false -} - -// deduplicate a slice of strings -func uniqueStrings(s []string) []string { - if len(s) < 2 { - return s - } - - sort.Strings(s) - result := make([]string, 1, len(s)) - result[0] = s[0] - for i := 1; i < len(s); i++ { - if s[i] != result[len(result)-1] { - result = append(result, s[i]) - } - } - return result -} diff --git a/internal/terraform/util_test.go b/internal/terraform/util_test.go index 253f682749..a094ba3026 100644 --- a/internal/terraform/util_test.go +++ b/internal/terraform/util_test.go @@ -4,8 +4,6 @@ package terraform import ( - "fmt" - "reflect" "testing" "time" ) @@ -36,59 +34,3 @@ func TestSemaphore(t *testing.T) { }() s.Release() } - -func TestStrSliceContains(t *testing.T) { - if strSliceContains(nil, "foo") { - t.Fatalf("Bad") - } - if strSliceContains([]string{}, "foo") { - t.Fatalf("Bad") - } - if strSliceContains([]string{"bar"}, "foo") { - t.Fatalf("Bad") - } - if !strSliceContains([]string{"bar", "foo"}, "foo") { - t.Fatalf("Bad") - } -} - -func TestUniqueStrings(t *testing.T) { - cases := []struct { - Input []string - Expected []string - }{ - { - []string{}, - []string{}, - }, - { - []string{"x"}, - []string{"x"}, - }, - { - []string{"a", "b", "c"}, - []string{"a", "b", "c"}, - }, - { - []string{"a", "a", "a"}, - []string{"a"}, - }, - { - []string{"a", "b", "a", "b", "a", "a"}, - []string{"a", "b"}, - }, - { - []string{"c", "b", "a", "c", "b"}, - []string{"a", "b", "c"}, - }, - } - - for i, tc := range cases { - t.Run(fmt.Sprintf("unique-%d", i), func(t *testing.T) { - actual := uniqueStrings(tc.Input) - if !reflect.DeepEqual(tc.Expected, actual) { - t.Fatalf("Expected: %q\nGot: %q", tc.Expected, actual) - } - }) - } -}