diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 9b26c24ff2d8..e872508d3e52 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -251,6 +251,9 @@ func interpolationFuncSplit() ast.Function { Callback: func(args []interface{}) (interface{}, error) { sep := args[0].(string) s := args[1].(string) + if s == "" { + return NewStringList([]string{}).String(), nil + } return NewStringList(strings.Split(s, sep)).String(), nil }, } diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 3258bda5c449..ee9d9ec0eed5 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -312,6 +312,11 @@ func TestInterpolateFuncLength(t *testing.T) { }, // Lists + { + `${length(split(",", ""))}`, + "0", + false, + }, { `${length(split(",", "a"))}`, "1", @@ -352,7 +357,7 @@ func TestInterpolateFuncSplit(t *testing.T) { { `${split(",", "")}`, - NewStringList([]string{""}).String(), + NewStringList([]string{}).String(), false, }, diff --git a/config/string_list.go b/config/string_list.go index 70d43d1e4bc4..4057eed43b5b 100644 --- a/config/string_list.go +++ b/config/string_list.go @@ -53,14 +53,13 @@ func (sl StringList) Length() int { // Returns a slice of strings as represented by this StringList func (sl StringList) Slice() []string { - parts := strings.Split(string(sl), stringListDelim) - - switch len(parts) { - case 0, 1: + if string(sl) == stringListDelim { return []string{} - case 2: + } + if string(sl) == strings.Join([]string{stringListDelim, stringListDelim}, "") { return []string{""} } + parts := strings.Split(string(sl), stringListDelim) // strip empty elements generated by leading and trailing delimiters return parts[1 : len(parts)-1] diff --git a/config/string_list_test.go b/config/string_list_test.go new file mode 100644 index 000000000000..cfc4b89db1a6 --- /dev/null +++ b/config/string_list_test.go @@ -0,0 +1,29 @@ +package config + +import ( + "testing" +) + +func TestStringListLength(t *testing.T) { + empty := NewStringList([]string{}) + l := empty.Length() + if l != 0 { + t.Errorf("L != 0") + } +} + +func TestStringListSlice(t *testing.T) { + empty := NewStringList([]string{}) + s := empty.Slice() + if len(s) != 0 { + t.Errorf("Empty StringList Slice not zero length") + } + oneelem_empty := NewStringList([]string{""}) + s = oneelem_empty.Slice() + if len(s) != 1 { + t.Errorf("Stringlist of empty element not length 1") + } + if s[0] != "" { + t.Errorf("Stringlist of empty element first element not empty") + } +}