terraform: Adding utility methods

This commit is contained in:
Armon Dadgar 2014-09-17 10:53:24 -07:00
parent bf072c5862
commit 31653fcbcb
2 changed files with 30 additions and 0 deletions

12
terraform/util.go Normal file
View File

@ -0,0 +1,12 @@
package terraform
// 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
}

18
terraform/util_test.go Normal file
View File

@ -0,0 +1,18 @@
package terraform
import "testing"
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")
}
}