diff --git a/castx/castx.go b/castx/castx.go index 6cccf3d4..f7778835 100644 --- a/castx/castx.go +++ b/castx/castx.go @@ -1,13 +1,11 @@ package castx import ( + "encoding/csv" "fmt" "reflect" "strings" - "github.com/ory/x/stringslice" - "github.com/ory/x/stringsx" - "github.com/spf13/cast" ) @@ -56,10 +54,14 @@ func ToStringSlice(i interface{}) []string { func ToStringSliceE(i interface{}) ([]string, error) { switch s := i.(type) { case string: - if strings.Contains(s, ",") { - return stringslice.TrimSpaceEmptyFilter(stringsx.Splitx(s, ",")), nil - } + return parseCSV(s) } return cast.ToStringSliceE(i) } + +func parseCSV(v string) ([]string, error) { + stringReader := strings.NewReader(v) + csvReader := csv.NewReader(stringReader) + return csvReader.Read() +} diff --git a/castx/castx_test.go b/castx/castx_test.go index 338ca97c..c32b3297 100644 --- a/castx/castx_test.go +++ b/castx/castx_test.go @@ -45,4 +45,10 @@ func TestToFloatSliceE(t *testing.T) { func TestToStringSlice(t *testing.T) { assert.Equal(t, []string{"foo", "bar"}, ToStringSlice("foo,bar")) + assert.NotEqual(t, []string{"foo bar baz"}, ToStringSlice("foo bar baz,")) + assert.Equal(t, []string{"foo bar baz", ""}, ToStringSlice("foo bar baz,")) + assert.NotEqual(t, []string{"foo", "bar", "baz"}, ToStringSlice("foo bar baz")) + assert.Equal(t, []string{"foo bar baz"}, ToStringSlice("foo bar baz")) + assert.Equal(t, []string{"foo", "bar", "baz,", " asdf"}, ToStringSlice("foo,bar,\"baz,\", asdf")) + assert.Equal(t, []string{"'foo'", "x\"bar", "baz"}, ToStringSlice("'foo',\"x\"\"bar\",baz")) }