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.
pull/35400/head
Martin Atkins 2 years ago
parent 27f26bd1b5
commit fdfd157ae5

@ -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
}

@ -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)
}
})
}
}

Loading…
Cancel
Save