From 31653fcbcb3c86b2bfaef53fdc22adc8bf6c64e1 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Wed, 17 Sep 2014 10:53:24 -0700 Subject: [PATCH] terraform: Adding utility methods --- terraform/util.go | 12 ++++++++++++ terraform/util_test.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 terraform/util.go create mode 100644 terraform/util_test.go diff --git a/terraform/util.go b/terraform/util.go new file mode 100644 index 000000000..f2654141f --- /dev/null +++ b/terraform/util.go @@ -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 +} diff --git a/terraform/util_test.go b/terraform/util_test.go new file mode 100644 index 000000000..3b9818872 --- /dev/null +++ b/terraform/util_test.go @@ -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") + } +}