Fix sep:"none" and mapsep:"none"
According to the README.md, a value of `"none"` for the `sep` tag (for slices) and `mapsep` (for maps) is meant to disable the separator that would allow multiple entries to be added from a single argument. However, using `"none"` just set the separator to the rune `'n'`. Fix the parsing of the tag, and also update `SplitEscaped` to not split with a separator of `-1`. Add a new test for the new cases.
This commit is contained in:
committed by
Alec Thomas
parent
b11ebc500b
commit
b89354adb0
@@ -636,6 +636,9 @@ func urlMapper() MapperFunc {
|
|||||||
//
|
//
|
||||||
// SplitEscaped(`hello\,there,bob`, ',') == []string{"hello,there", "bob"}
|
// SplitEscaped(`hello\,there,bob`, ',') == []string{"hello,there", "bob"}
|
||||||
func SplitEscaped(s string, sep rune) (out []string) {
|
func SplitEscaped(s string, sep rune) (out []string) {
|
||||||
|
if sep == -1 {
|
||||||
|
return []string{s}
|
||||||
|
}
|
||||||
escaped := false
|
escaped := false
|
||||||
token := ""
|
token := ""
|
||||||
for _, ch := range s {
|
for _, ch := range s {
|
||||||
|
|||||||
@@ -130,6 +130,7 @@ func TestDurationMapper(t *testing.T) {
|
|||||||
func TestSplitEscaped(t *testing.T) {
|
func TestSplitEscaped(t *testing.T) {
|
||||||
require.Equal(t, []string{"a", "b"}, kong.SplitEscaped("a,b", ','))
|
require.Equal(t, []string{"a", "b"}, kong.SplitEscaped("a,b", ','))
|
||||||
require.Equal(t, []string{"a,b", "c"}, kong.SplitEscaped(`a\,b,c`, ','))
|
require.Equal(t, []string{"a,b", "c"}, kong.SplitEscaped(`a\,b,c`, ','))
|
||||||
|
require.Equal(t, []string{"a,b,c"}, kong.SplitEscaped(`a,b,c`, -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJoinEscaped(t *testing.T) {
|
func TestJoinEscaped(t *testing.T) {
|
||||||
@@ -172,6 +173,18 @@ func TestMapWithDifferentSeparator(t *testing.T) {
|
|||||||
require.Equal(t, map[string]string{"a": "b", "c": "d"}, cli.Value)
|
require.Equal(t, map[string]string{"a": "b", "c": "d"}, cli.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMapWithNoSeparator(t *testing.T) {
|
||||||
|
var cli struct {
|
||||||
|
Slice []string `sep:"none"`
|
||||||
|
Value map[string]string `mapsep:"none"`
|
||||||
|
}
|
||||||
|
k := mustNew(t, &cli)
|
||||||
|
_, err := k.Parse([]string{"--slice=a,n,c", "--value=a=b;n=d"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, []string{"a,n,c"}, cli.Slice)
|
||||||
|
require.Equal(t, map[string]string{"a": "b;n=d"}, cli.Value)
|
||||||
|
}
|
||||||
|
|
||||||
func TestURLMapper(t *testing.T) {
|
func TestURLMapper(t *testing.T) {
|
||||||
var cli struct {
|
var cli struct {
|
||||||
URL *url.URL `arg:""`
|
URL *url.URL `arg:""`
|
||||||
|
|||||||
@@ -151,23 +151,23 @@ func parseTag(fv reflect.Value, ft reflect.StructField) *Tag {
|
|||||||
t.Short, _ = t.GetRune("short")
|
t.Short, _ = t.GetRune("short")
|
||||||
t.Hidden = t.Has("hidden")
|
t.Hidden = t.Has("hidden")
|
||||||
t.Format = t.Get("format")
|
t.Format = t.Get("format")
|
||||||
t.Sep, _ = t.GetRune("sep")
|
|
||||||
t.MapSep, _ = t.GetRune("mapsep")
|
|
||||||
t.Group = t.Get("group")
|
t.Group = t.Get("group")
|
||||||
t.Xor = t.Get("xor")
|
t.Xor = t.Get("xor")
|
||||||
t.Prefix = t.Get("prefix")
|
t.Prefix = t.Get("prefix")
|
||||||
t.Embed = t.Has("embed")
|
t.Embed = t.Has("embed")
|
||||||
if t.Sep == 0 {
|
if t.Get("sep") == "none" {
|
||||||
if t.Get("sep") == "none" {
|
t.Sep = -1
|
||||||
t.Sep = -1
|
} else {
|
||||||
} else {
|
t.Sep, _ = t.GetRune("sep")
|
||||||
|
if t.Sep == 0 {
|
||||||
t.Sep = ','
|
t.Sep = ','
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if t.MapSep == 0 {
|
if t.Get("mapsep") == "none" {
|
||||||
if t.Get("mapsep") == "none" {
|
t.MapSep = -1
|
||||||
t.MapSep = -1
|
} else {
|
||||||
} else {
|
t.MapSep, _ = t.GetRune("mapsep")
|
||||||
|
if t.MapSep == 0 {
|
||||||
t.MapSep = ';'
|
t.MapSep = ';'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user