fix(flag): honor full separator length when splitting map key=value pairs
splitKeyVal sliced the value from index+1, skipping only one byte of the key/value separator. With a multi-character sep tag (e.g. sep:"=>") the remaining separator bytes leaked into the value. Slice from index+len(sep) so the whole separator is consumed.
This commit is contained in:
1 parent
8a9f09fc09
commit
55cba65a99
2 files changed
+22
-1
No files matched your search
+21
@@ -727,3 +727,24 @@ func TestDecoder(t *testing.T) {
|
|||||||
assert.Nil(t, flagSet.err)
|
assert.Nil(t, flagSet.err)
|
||||||
assert.Equal(t, v.D, customT{K1: "string", K2: 2})
|
assert.Equal(t, v.D, customT{K1: "string", K2: 2})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMapMultiCharSep(t *testing.T) {
|
||||||
|
// A multi-character separator must not leak its tail into the value.
|
||||||
|
if k, v, _ := splitKeyVal("key=>val", "=>"); k != "key" || v != "val" {
|
||||||
|
t.Errorf(`splitKeyVal("key=>val", "=>") = (%q, %q), want ("key", "val")`, k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Public API: a map flag configured with a multi-char sep.
|
||||||
|
type T struct {
|
||||||
|
Map map[string]string `cli:"m" sep:"=>"`
|
||||||
|
}
|
||||||
|
clr := color.Color{}
|
||||||
|
v := new(T)
|
||||||
|
flagSet := parseArgv([]string{"-mkey=>val"}, v, clr)
|
||||||
|
if flagSet.err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", flagSet.err)
|
||||||
|
}
|
||||||
|
if got := v.Map["key"]; got != "val" {
|
||||||
|
t.Errorf(`map value = %q, want "val"`, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -436,7 +436,7 @@ func splitKeyVal(s, sep string) (key, val string, err error) {
|
|||||||
if index == -1 {
|
if index == -1 {
|
||||||
return s, "", nil
|
return s, "", nil
|
||||||
}
|
}
|
||||||
return s[:index], s[index+1:], nil
|
return s[:index], s[index+len(sep):], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func minmaxIntCheck(kind reflect.Kind, v int64) bool {
|
func minmaxIntCheck(kind reflect.Kind, v int64) bool {
|
||||||
|
|||||||
Reference in new issue
Block a user