From 244da895cd6b5ff8feed05e4ca66774afcdfed36 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Sun, 24 Apr 2016 00:59:43 -0500 Subject: [PATCH] core: Remove StringList MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Much celebration may now ensue! ♪┏(°.°)┛┗(°.°)┓┗(°.°)┛┏(°.°)┓ ♪ --- config/string_list.go | 89 -------------------------------------- config/string_list_test.go | 52 ---------------------- 2 files changed, 141 deletions(-) delete mode 100644 config/string_list.go delete mode 100644 config/string_list_test.go diff --git a/config/string_list.go b/config/string_list.go deleted file mode 100644 index e3caea70b..000000000 --- a/config/string_list.go +++ /dev/null @@ -1,89 +0,0 @@ -package config - -import ( - "fmt" - "strings" -) - -// StringList represents the "poor man's list" that terraform uses -// internally -type StringList string - -// This is the delimiter used to recognize and split StringLists -// -// It plays two semantic roles: -// * It introduces a list -// * It terminates each element -// -// Example representations: -// [] => SLD -// [""] => SLDSLD -// [" "] => SLD SLD -// ["foo"] => SLDfooSLD -// ["foo", "bar"] => SLDfooSLDbarSLD -// ["", ""] => SLDSLDSLD -const stringListDelim = `B780FFEC-B661-4EB8-9236-A01737AD98B6` - -// Takes a Stringlist and returns one without empty strings in it -func (sl StringList) Compact() StringList { - parts := sl.Slice() - - newlist := []string{} - // drop the empty strings - for i := range parts { - if parts[i] != "" { - newlist = append(newlist, parts[i]) - } - } - return NewStringList(newlist) -} - -// Build a StringList from a slice -func NewStringList(parts []string) StringList { - // We have to special case the empty list representation - if len(parts) == 0 { - return StringList(stringListDelim) - } - return StringList(fmt.Sprintf("%s%s%s", - stringListDelim, - strings.Join(parts, stringListDelim), - stringListDelim, - )) -} - -// Returns an element at the index, wrapping around the length of the string -// when index > list length -func (sl StringList) Element(index int) string { - if sl.Length() == 0 { - return "" - } - return sl.Slice()[index%sl.Length()] -} - -// Returns the length of the StringList -func (sl StringList) Length() int { - return len(sl.Slice()) -} - -// Returns a slice of strings as represented by this StringList -func (sl StringList) Slice() []string { - parts := strings.Split(string(sl), stringListDelim) - - // split on an empty StringList will have a length of 2, since there is - // always at least one deliminator - if len(parts) <= 2 { - return []string{} - } - - // strip empty elements generated by leading and trailing delimiters - return parts[1 : len(parts)-1] -} - -func (sl StringList) String() string { - return string(sl) -} - -// Determines if a given string represents a StringList -func IsStringList(s string) bool { - return strings.Contains(s, stringListDelim) -} diff --git a/config/string_list_test.go b/config/string_list_test.go deleted file mode 100644 index 3fe57dfe2..000000000 --- a/config/string_list_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package config - -import ( - "reflect" - "testing" -) - -func TestStringList_slice(t *testing.T) { - expected := []string{"apple", "banana", "pear"} - l := NewStringList(expected) - actual := l.Slice() - - if !reflect.DeepEqual(expected, actual) { - t.Fatalf("Expected %q, got %q", expected, actual) - } -} - -func TestStringList_element(t *testing.T) { - list := []string{"apple", "banana", "pear"} - l := NewStringList(list) - actual := l.Element(1) - - expected := "banana" - - if actual != expected { - t.Fatalf("Expected 2nd element from %q to be %q, got %q", - list, expected, actual) - } -} - -func TestStringList_empty_slice(t *testing.T) { - expected := []string{} - l := NewStringList(expected) - actual := l.Slice() - - if !reflect.DeepEqual(expected, actual) { - t.Fatalf("Expected %q, got %q", expected, actual) - } -} - -func TestStringList_empty_slice_length(t *testing.T) { - list := []string{} - l := NewStringList([]string{}) - actual := l.Length() - - expected := 0 - - if actual != expected { - t.Fatalf("Expected length of %q to be %d, got %d", - list, expected, actual) - } -}