Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,10 @@ func TestToStringSliceE(t *testing.T) {
{[]string{"a", "b"}, []string{"a", "b"}, false},
{[]interface{}{1, 3}, []string{"1", "3"}, false},
{interface{}(1), []string{"1"}, false},
{"a", []string{"a"}, false},
{"a b", []string{"a", "b"}, false},
{"a,b", []string{"a", "b"}, false},
{"a b,c", []string{"a", "b,c"}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
Expand Down
9 changes: 8 additions & 1 deletion caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,14 @@ func ToStringSliceE(i interface{}) ([]string, error) {
case []string:
return v, nil
case string:
return strings.Fields(v), nil
slice := strings.Fields(v)
// if the string can be splited by white space
// return slice immediately
if len(slice) > 1 {
return slice, nil
}
// otherwise, try to split string by comma
return strings.Split(v, ","), nil
case interface{}:
str, err := ToStringE(v)
if err != nil {
Expand Down