2015-06-10 18:29:44 +02:00
|
|
|
package config
|
|
|
|
|
2015-06-26 00:55:51 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2015-06-10 18:29:44 +02:00
|
|
|
|
|
|
|
// StringList represents the "poor man's list" that terraform uses
|
|
|
|
// internally
|
|
|
|
type StringList string
|
|
|
|
|
|
|
|
// This is the delimiter used to recognize and split StringLists
|
2015-06-26 00:55:51 +02:00
|
|
|
//
|
|
|
|
// 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`
|
2015-06-10 18:29:44 +02:00
|
|
|
|
2015-09-16 11:08:58 +02:00
|
|
|
// Takes a Stringlist and returns one without empty strings in it
|
|
|
|
func (sl StringList) CompactStringList() StringList {
|
|
|
|
parts := sl.Slice()
|
|
|
|
|
|
|
|
var newlist []string
|
|
|
|
// drop the empty strings
|
|
|
|
for i := range parts {
|
|
|
|
if parts[i] != "" {
|
|
|
|
newlist = append(newlist, parts[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NewStringList(newlist)
|
|
|
|
}
|
|
|
|
|
2015-06-10 18:29:44 +02:00
|
|
|
// Build a StringList from a slice
|
|
|
|
func NewStringList(parts []string) StringList {
|
2015-06-26 00:55:51 +02:00
|
|
|
// 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,
|
|
|
|
))
|
2015-06-10 18:29:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an element at the index, wrapping around the length of the string
|
|
|
|
// when index > list length
|
|
|
|
func (sl StringList) Element(index int) string {
|
2015-06-26 00:55:51 +02:00
|
|
|
if sl.Length() == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2015-06-10 18:29:44 +02:00
|
|
|
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 {
|
2015-06-26 00:55:51 +02:00
|
|
|
parts := strings.Split(string(sl), stringListDelim)
|
2015-06-10 18:29:44 +02:00
|
|
|
|
2015-06-26 00:55:51 +02:00
|
|
|
switch len(parts) {
|
|
|
|
case 0, 1:
|
2015-06-10 18:29:44 +02:00
|
|
|
return []string{}
|
2015-06-26 00:55:51 +02:00
|
|
|
case 2:
|
|
|
|
return []string{""}
|
2015-06-10 18:29:44 +02:00
|
|
|
}
|
2015-06-26 00:55:51 +02:00
|
|
|
|
|
|
|
// strip empty elements generated by leading and trailing delimiters
|
|
|
|
return parts[1 : len(parts)-1]
|
2015-06-10 18:29:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sl StringList) String() string {
|
|
|
|
return string(sl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determines if a given string represents a StringList
|
|
|
|
func IsStringList(s string) bool {
|
2015-06-26 00:55:51 +02:00
|
|
|
return strings.Contains(s, stringListDelim)
|
2015-06-10 18:29:44 +02:00
|
|
|
}
|