From 3b318a7fa6ac8d7fa8f349d4a658a75fe5c4f9b0 Mon Sep 17 00:00:00 2001 From: Paolo Valletta Date: Tue, 3 Nov 2026 13:57:26 +0100 Subject: [PATCH 1/3] fix: hand a list from a map source to a slice flag in its serialized form A slice flag splits its value on commas, and the map value source formatted whatever it found with %+v. A list therefore arrived as "[80 443]": the IntSliceFlag from the issue failed to parse it, and a StringSliceFlag kept the whole string as one element without an error. The value source now returns a slice or array in the serialized form SliceBase already parses, so the flag stops splitting it: an element that contains a comma, a custom SliceFlagSeparator and a disabled separator all arrive intact. --- value_source.go | 19 ++++++-- value_source_test.go | 109 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 4 deletions(-) diff --git a/value_source.go b/value_source.go index 231851e5c2..56ed2f3730 100644 --- a/value_source.go +++ b/value_source.go @@ -1,8 +1,10 @@ package cli import ( + "encoding/json" "fmt" "os" + "reflect" "strings" ) @@ -250,9 +252,20 @@ func (mvs *mapValueSource) GoString() string { } func (mvs *mapValueSource) Lookup() (string, bool) { - if v, ok := mvs.ms.Lookup(mvs.key); !ok { + v, ok := mvs.ms.Lookup(mvs.key) + if !ok { return "", false - } else { - return fmt.Sprintf("%+v", v), true } + + // A slice flag parses a serialized list without splitting it, so the value + // keeps its own separators (a comma inside an element, or a custom + // SliceFlagSeparator) instead of arriving as Go's "[a b]" formatting. + rv := reflect.ValueOf(v) + if rv.Kind() == reflect.Slice || rv.Kind() == reflect.Array { + if b, err := json.Marshal(v); err == nil { + return slPfx() + string(b), true + } + } + + return fmt.Sprintf("%+v", v), true } diff --git a/value_source_test.go b/value_source_test.go index 719148bd09..ab04f61f7c 100644 --- a/value_source_test.go +++ b/value_source_test.go @@ -1,6 +1,7 @@ package cli import ( + "context" "fmt" "os" "path/filepath" @@ -310,7 +311,7 @@ func TestMapValueSource(t *testing.T) { }, }, }, - val: "[10]", + val: slPfx() + "[10]", found: true, }, } @@ -341,3 +342,109 @@ func TestMapValueSourceStringer(t *testing.T) { assert.Equal(t, `&mapValueSource{key:"bar", src:&mapSource{name:"test"}}`, mvs.GoString()) assert.Equal(t, `key "bar" from map source "test"`, mvs.String()) } + +func TestMapValueSourceLists(t *testing.T) { + ms := NewMapSource("config", map[any]any{ + "ports": []any{80, 443}, + "hosts": []string{"a.example", "b.example"}, + "empty": []any{}, + }) + + var ports []int + var hosts []string + cmd := &Command{ + Flags: []Flag{ + &IntSliceFlag{Name: "ports", Sources: NewValueSourceChain(NewMapValueSource("ports", ms))}, + &StringSliceFlag{Name: "hosts", Sources: NewValueSourceChain(NewMapValueSource("hosts", ms))}, + }, + Action: func(_ context.Context, cmd *Command) error { + ports = cmd.IntSlice("ports") + hosts = cmd.StringSlice("hosts") + return nil + }, + } + + if err := cmd.Run(context.Background(), []string{"app"}); err != nil { + t.Fatalf("unexpected error: %v", err) + } + assert.Equal(t, []int{80, 443}, ports) + assert.Equal(t, []string{"a.example", "b.example"}, hosts) + + var empty []string + cmd = &Command{ + Flags: []Flag{ + &StringSliceFlag{Name: "empty", Sources: NewValueSourceChain(NewMapValueSource("empty", ms))}, + }, + Action: func(_ context.Context, cmd *Command) error { + empty = cmd.StringSlice("empty") + return nil + }, + } + if err := cmd.Run(context.Background(), []string{"app"}); err != nil { + t.Fatalf("unexpected error: %v", err) + } + assert.Empty(t, empty) +} + +// A value must survive the separator it is not split on: an element that +// contains a comma, and a command that changes or disables the separator. +func TestMapValueSourceListSeparators(t *testing.T) { + ms := NewMapSource("config", map[any]any{ + "hosts": []string{"a,b.example", "c.example"}, + "ports": []any{80, 443}, + }) + + tests := []struct { + name string + cmd *Command + check func(t *testing.T, cmd *Command) + }{ + { + name: "element with a comma", + cmd: &Command{ + Flags: []Flag{ + &StringSliceFlag{Name: "hosts", Sources: NewValueSourceChain(NewMapValueSource("hosts", ms))}, + }, + }, + check: func(t *testing.T, cmd *Command) { + assert.Equal(t, []string{"a,b.example", "c.example"}, cmd.StringSlice("hosts")) + }, + }, + { + name: "custom separator", + cmd: &Command{ + SliceFlagSeparator: ";", + Flags: []Flag{ + &IntSliceFlag{Name: "ports", Sources: NewValueSourceChain(NewMapValueSource("ports", ms))}, + }, + }, + check: func(t *testing.T, cmd *Command) { + assert.Equal(t, []int{80, 443}, cmd.IntSlice("ports")) + }, + }, + { + name: "separator disabled", + cmd: &Command{ + DisableSliceFlagSeparator: true, + Flags: []Flag{ + &IntSliceFlag{Name: "ports", Sources: NewValueSourceChain(NewMapValueSource("ports", ms))}, + }, + }, + check: func(t *testing.T, cmd *Command) { + assert.Equal(t, []int{80, 443}, cmd.IntSlice("ports")) + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + test.cmd.Action = func(_ context.Context, cmd *Command) error { + test.check(t, cmd) + return nil + } + if err := test.cmd.Run(context.Background(), []string{"app"}); err != nil { + t.Fatalf("unexpected error: %v", err) + } + }) + } +} From 458cde65586cbc04a3dd372884c61dad5bcbaf50 Mon Sep 17 00:00:00 2001 From: Paolo Valletta Date: Fri, 6 Nov 2026 17:45:54 +0100 Subject: [PATCH 2/3] fix: parse each element of a serialized list with the flag's own parser A list from a map source arrives serialized, and Set decoded it straight into the flag's slice with json.Unmarshal, dropping the error. A JSON number in a string slice became "", a JSON string in an int slice became 0, and a negative in an unsigned slice silently became 0 instead of failing. The serialized branch now splits the array and hands each element to the same value parser the split path uses, so mismatched types parse or report instead of turning into zero values. A scalar flag no longer sees the internal prefix: its elements are comma-joined before Set. The split scans the array rather than decoding it: a decode into any concrete type links the whole encoding/json decoder, which adds 300 KB to the example binary and fails `make check-binary-size`. A serialized slice is a flat array of scalars, so the scan covers every element Serialize can write. --- flag.go | 56 ++++++++++++++++ flag_impl.go | 10 +++ flag_slice_base.go | 23 ++++++- value_source_test.go | 150 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 237 insertions(+), 2 deletions(-) diff --git a/flag.go b/flag.go index 7886b93a2c..fc1a1c12bc 100644 --- a/flag.go +++ b/flag.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "slices" + "strconv" "strings" "sync" "time" @@ -21,6 +22,61 @@ var slPfx = sync.OnceValue(func() string { return fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano()) }) +// serializedListElements splits the JSON array `SliceBase.Serialize` writes +// into one string per element. It scans rather than decodes: a decode into a +// concrete Go type links the whole encoding/json decoder, and `make +// check-binary-size` refuses the result. Every element of a serialized slice +// is a JSON string, number, bool or null, since the slice holds scalars. +func serializedListElements(s string) ([]string, error) { + if s == "null" { + // A nil slice serializes to JSON null; keep that distinction. + return nil, nil + } + if len(s) < 2 || s[0] != '[' || s[len(s)-1] != ']' { + return nil, fmt.Errorf("could not decode serialized list: %q is not a JSON array", s) + } + rest := s[1 : len(s)-1] + elems := make([]string, 0, strings.Count(rest, ",")+1) + for rest != "" { + var elem string + if rest[0] == '"' { + end := 1 + for end < len(rest) && rest[end] != '"' { + if rest[end] == '\\' { + end++ + } + end++ + } + if end >= len(rest) { + return nil, fmt.Errorf("could not decode serialized list: unterminated string in %q", s) + } + unquoted, err := strconv.Unquote(rest[:end+1]) + if err != nil { + return nil, fmt.Errorf("could not decode serialized list: %w", err) + } + elem, rest = unquoted, rest[end+1:] + } else { + end := strings.IndexByte(rest, ',') + if end < 0 { + end = len(rest) + } + elem, rest = rest[:end], rest[end:] + if strings.ContainsAny(elem, "[{") { + return nil, fmt.Errorf("could not decode serialized list: %q is not a scalar", elem) + } + } + elems = append(elems, elem) + if rest == "" { + break + } + if rest[0] != ',' { + return nil, fmt.Errorf("could not decode serialized list: %q is not a JSON array", s) + } + rest = rest[1:] + } + return elems, nil +} + // GenerateShellCompletionFlag enables shell completion var GenerateShellCompletionFlag Flag = &BoolFlag{ Name: "generate-shell-completion", diff --git a/flag_impl.go b/flag_impl.go index b3a0d3c20e..e8cc0f9278 100644 --- a/flag_impl.go +++ b/flag_impl.go @@ -164,6 +164,16 @@ func (f *FlagBase[T, C, V]) PostParse() error { for _, source := range f.Sources.Chain { val, found := source.Lookup() + // A list from a map or struct value source arrives in the + // serialized form only a slice flag can decode. A scalar flag + // gets the elements comma-joined, never the internal prefix. + if kind != reflect.Slice && kind != reflect.Array { + if s, ok := strings.CutPrefix(val, slPfx()); ok { + if elems, err := serializedListElements(s); err == nil { + val = strings.Join(elems, defaultSliceFlagSeparator) + } + } + } if trimSpace { val = strings.TrimSpace(val) } diff --git a/flag_slice_base.go b/flag_slice_base.go index 62028b2534..764215f105 100644 --- a/flag_slice_base.go +++ b/flag_slice_base.go @@ -54,8 +54,27 @@ func (i *SliceBase[T, C, VC]) Set(value string) error { } if s, ok := strings.CutPrefix(value, slPfx()); ok { - // Deserializing assumes overwrite - _ = json.Unmarshal([]byte(s), i.slice) + elems, err := serializedListElements(s) + if err != nil { + return err + } + if elems == nil { + *i.slice = nil + i.hasBeenSet = true + return nil + } + // Deserializing assumes overwrite, as it always has. + *i.slice = make([]T, 0, len(elems)) + for _, elem := range elems { + // Each element goes through the same parser as the split path, so + // a JSON number in a string flag reads as its digits and a JSON + // string in an int flag is parsed or reported rather than silently + // becoming the zero value. + if err := i.value.Set(elem); err != nil { + return err + } + *i.slice = append(*i.slice, i.value.Get().(T)) + } i.hasBeenSet = true return nil } diff --git a/value_source_test.go b/value_source_test.go index ab04f61f7c..9a410e1801 100644 --- a/value_source_test.go +++ b/value_source_test.go @@ -448,3 +448,153 @@ func TestMapValueSourceListSeparators(t *testing.T) { }) } } + +// A list from a map source must reach the flag through its own element parser, +// not through a JSON decode that silently produces zero values, and a scalar +// flag must never see the internal serialization prefix. +func TestMapValueSourceListsWithMismatchedTypes(t *testing.T) { + ms := NewMapSource("config", map[any]any{ + "anyints": []any{80, 443}, + "strings": []string{"80", "443"}, + "mixed": []any{"a", 2, true}, + "negative": []any{-1}, + "plain": []string{"x", "y"}, + }) + + for _, tc := range []struct { + name string + flag Flag + want any + wantErr bool + }{ + { + name: "ints in a string slice read as their digits", + flag: &StringSliceFlag{Name: "anyints", Sources: NewValueSourceChain(NewMapValueSource("anyints", ms))}, + want: []string{"80", "443"}, + }, + { + name: "strings in an int slice are parsed", + flag: &IntSliceFlag{Name: "strings", Sources: NewValueSourceChain(NewMapValueSource("strings", ms))}, + want: []int{80, 443}, + }, + { + name: "mixed elements all arrive", + flag: &StringSliceFlag{Name: "mixed", Sources: NewValueSourceChain(NewMapValueSource("mixed", ms))}, + want: []string{"a", "2", "true"}, + }, + { + name: "a negative into an unsigned slice is an error, not a zero", + flag: &UintSliceFlag{Name: "negative", Sources: NewValueSourceChain(NewMapValueSource("negative", ms))}, + wantErr: true, + }, + { + name: "a list into a scalar flag never carries the prefix", + flag: &StringFlag{Name: "plain", Sources: NewValueSourceChain(NewMapValueSource("plain", ms))}, + want: "x,y", + }, + } { + t.Run(tc.name, func(t *testing.T) { + var got any + cmd := &Command{ + Flags: []Flag{tc.flag}, + Action: func(_ context.Context, cmd *Command) error { + switch f := tc.flag.(type) { + case *StringSliceFlag: + got = cmd.StringSlice(f.Name) + case *IntSliceFlag: + got = cmd.IntSlice(f.Name) + case *UintSliceFlag: + got = cmd.UintSlice(f.Name) + case *StringFlag: + got = cmd.String(f.Name) + } + return nil + }, + } + err := cmd.Run(context.Background(), []string{"app"}) + if tc.wantErr { + if err == nil { + t.Fatalf("expected an error, got %v", got) + } + return + } + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + assert.Equal(t, tc.want, got) + }) + } +} + +// The serialized form is scanned without a JSON decoder, so the elements that +// a decoder would unescape have to arrive intact: the separator inside a +// value, quotes, backslashes, a newline, empty strings and brackets. +func TestMapValueSourceListElementsSurviveTheSerializedForm(t *testing.T) { + want := []string{ + "a,b", + `he said "hi"`, + `back\slash`, + "emoji 🎉", + "", + "[brackets]", + "line\nbreak", + "tab\there", + } + ms := NewMapSource("tricky", map[any]any{"tricky": want}) + + var got []string + cmd := &Command{ + Flags: []Flag{ + &StringSliceFlag{Name: "tricky", Sources: NewValueSourceChain(NewMapValueSource("tricky", ms))}, + }, + Action: func(_ context.Context, cmd *Command) error { + got = cmd.StringSlice("tricky") + return nil + }, + } + require.NoError(t, cmd.Run(context.Background(), []string{"app"})) + assert.Equal(t, want, got) +} + +// The serialized scanner is hand-written, so its refusals are pinned here: a +// value that is not the flat array Serialize writes must come back as an +// error, never as a silently truncated list. +func TestSerializedListElements(t *testing.T) { + for _, tc := range []struct { + name string + in string + want []string + wantErr bool + }{ + {name: "strings", in: `["a","b"]`, want: []string{"a", "b"}}, + {name: "empty array", in: `[]`, want: []string{}}, + {name: "null", in: `null`}, + {name: "numbers and bools", in: `[80,-1,true]`, want: []string{"80", "-1", "true"}}, + {name: "escapes", in: `["a,b","he said \"hi\"","back\\slash",""]`, want: []string{"a,b", `he said "hi"`, `back\slash`, ""}}, + {name: "object", in: `{"a":1}`, wantErr: true}, + {name: "unterminated string", in: `["abc]`, wantErr: true}, + {name: "bad escape", in: `["\q"]`, wantErr: true}, + {name: "nested value", in: `[{"a":1}]`, wantErr: true}, + {name: "missing comma", in: `["a" "b"]`, wantErr: true}, + } { + t.Run(tc.name, func(t *testing.T) { + got, err := serializedListElements(tc.in) + if tc.wantErr { + require.Error(t, err, "got %q", got) + return + } + require.NoError(t, err) + assert.Equal(t, tc.want, got) + }) + } +} + +// A malformed serialized value reaches the caller as an error, not as an empty +// list. +func TestSliceBaseRejectsAMalformedSerializedValue(t *testing.T) { + var dest []string + v := StringSlice{}.Create([]string{"old"}, &dest, StringConfig{}) + require.NoError(t, v.Set("a,b")) + require.Error(t, v.Set(slPfx()+`["c"`)) + assert.Equal(t, []string{"a", "b"}, dest) +} From 0683c24df1f912dda18888b41be081ba39f872ec Mon Sep 17 00:00:00 2001 From: Paolo Valletta Date: Mon, 16 Nov 2026 10:26:50 +0100 Subject: [PATCH 3/3] fix: decode a serialized number in the element type, not the input base `Serialize` writes canonical JSON, so a serialized number is always decimal. Feeding each token back through the flag's own parser made the round trip depend on `IntegerConfig.Base`: `16` came back as `22` with base 16, and as a parse error with base 2. A quoted token is text and still goes to the parser, which keeps the mixed-type cases the previous commit added. A bare number or bool is a value, decoded in `T`, so the input base never reads it. A `null` or empty element is now refused by the scanner instead of becoming a zero value or a panic, and a rejected element leaves the current list untouched. Text input is unchanged: with base 16, `--ports 10` still reads 16. --- flag.go | 34 ++++++++++++++++++++++++++-------- flag_impl.go | 3 +++ flag_slice_base.go | 25 ++++++++++++++++--------- flag_test.go | 35 +++++++++++++++++++++++++++++++++++ value_source_test.go | 12 ++++++++++-- 5 files changed, 90 insertions(+), 19 deletions(-) diff --git a/flag.go b/flag.go index fc1a1c12bc..dcd04b2d9f 100644 --- a/flag.go +++ b/flag.go @@ -23,10 +23,10 @@ var slPfx = sync.OnceValue(func() string { }) // serializedListElements splits the JSON array `SliceBase.Serialize` writes -// into one string per element. It scans rather than decodes: a decode into a -// concrete Go type links the whole encoding/json decoder, and `make -// check-binary-size` refuses the result. Every element of a serialized slice -// is a JSON string, number, bool or null, since the slice holds scalars. +// into one token per element, keeping each token in its JSON form. It scans +// rather than decodes so a broken list is reported with the token that broke +// it, and so an element that is not a scalar value is refused: every element of +// a serialized slice is a JSON string, number or bool. func serializedListElements(s string) ([]string, error) { if s == "null" { // A nil slice serializes to JSON null; keep that distinction. @@ -50,18 +50,25 @@ func serializedListElements(s string) ([]string, error) { if end >= len(rest) { return nil, fmt.Errorf("could not decode serialized list: unterminated string in %q", s) } - unquoted, err := strconv.Unquote(rest[:end+1]) - if err != nil { + if _, err := strconv.Unquote(rest[:end+1]); err != nil { return nil, fmt.Errorf("could not decode serialized list: %w", err) } - elem, rest = unquoted, rest[end+1:] + elem, rest = rest[:end+1], rest[end+1:] } else { end := strings.IndexByte(rest, ',') if end < 0 { end = len(rest) } elem, rest = rest[:end], rest[end:] - if strings.ContainsAny(elem, "[{") { + switch { + case elem == "": + return nil, fmt.Errorf("could not decode serialized list: empty element in %q", s) + case elem == "null": + // json.Unmarshal turns a null into the zero value of any type + // without an error, which is the silent zero this path exists to + // avoid. + return nil, fmt.Errorf("could not decode serialized list: null element in %q", s) + case strings.ContainsAny(elem, "[{"): return nil, fmt.Errorf("could not decode serialized list: %q is not a scalar", elem) } } @@ -77,6 +84,17 @@ func serializedListElements(s string) ([]string, error) { return elems, nil } +// listTokenText returns the text a serialized token stands for: a JSON string +// is unquoted, and a bare number or bool is already text. The escapes are +// validated by serializedListElements, so the unquote cannot fail here. +func listTokenText(tok string) string { + if tok == "" || tok[0] != '"' { + return tok + } + text, _ := strconv.Unquote(tok) + return text +} + // GenerateShellCompletionFlag enables shell completion var GenerateShellCompletionFlag Flag = &BoolFlag{ Name: "generate-shell-completion", diff --git a/flag_impl.go b/flag_impl.go index e8cc0f9278..1ad9c85143 100644 --- a/flag_impl.go +++ b/flag_impl.go @@ -170,6 +170,9 @@ func (f *FlagBase[T, C, V]) PostParse() error { if kind != reflect.Slice && kind != reflect.Array { if s, ok := strings.CutPrefix(val, slPfx()); ok { if elems, err := serializedListElements(s); err == nil { + for i, tok := range elems { + elems[i] = listTokenText(tok) + } val = strings.Join(elems, defaultSliceFlagSeparator) } } diff --git a/flag_slice_base.go b/flag_slice_base.go index 764215f105..48010701ef 100644 --- a/flag_slice_base.go +++ b/flag_slice_base.go @@ -63,18 +63,25 @@ func (i *SliceBase[T, C, VC]) Set(value string) error { i.hasBeenSet = true return nil } - // Deserializing assumes overwrite, as it always has. - *i.slice = make([]T, 0, len(elems)) - for _, elem := range elems { - // Each element goes through the same parser as the split path, so - // a JSON number in a string flag reads as its digits and a JSON - // string in an int flag is parsed or reported rather than silently - // becoming the zero value. - if err := i.value.Set(elem); err != nil { + // Deserializing assumes overwrite, as it always has. The elements are + // built aside so a rejected one leaves the current list untouched. + parsed := make([]T, 0, len(elems)) + for _, tok := range elems { + // A JSON string is text input for the flag's parser. A bare number or + // bool is a value, decoded in T, so the flag's input config (e.g. + // IntegerConfig.Base) cannot re-read a decimal token in another base. + var elem T + if tok[0] != '"' && json.Unmarshal([]byte(tok), &elem) == nil { + parsed = append(parsed, elem) + + continue + } + if err := i.value.Set(listTokenText(tok)); err != nil { return err } - *i.slice = append(*i.slice, i.value.Get().(T)) + parsed = append(parsed, i.value.Get().(T)) } + *i.slice = parsed i.hasBeenSet = true return nil } diff --git a/flag_test.go b/flag_test.go index 130340f2a3..52ee13ab30 100644 --- a/flag_test.go +++ b/flag_test.go @@ -2572,6 +2572,41 @@ func TestIntSlice_Serialized_Set(t *testing.T) { require.Equal(t, sl0.String(), sl1.String(), "pre and post serialization do not match") } +// Serialize writes canonical JSON, so a decimal token must not be re-read in +// the flag's text-input base on the way back in. +func TestIntSlice_Serialized_Set_NonDecimalBase(t *testing.T) { + for _, tc := range []struct{ base, ten int }{{2, 2}, {16, 16}} { + base := tc.base + t.Run(fmt.Sprintf("base %d", base), func(t *testing.T) { + cfg := IntegerConfig{Base: base} + + var original, restored []int + value := IntSlice{}.Create([]int{16}, &original, cfg) + encoded := value.(Serializer).Serialize() + + receiver := IntSlice{}.Create(nil, &restored, cfg) + require.NoError(t, receiver.Set(encoded)) + require.Equal(t, []int{16}, restored) + + // Text input still goes through the flag's parser, base included. + var fromText []int + require.NoError(t, IntSlice{}.Create(nil, &fromText, cfg).Set("10")) + require.Equal(t, []int{tc.ten}, fromText) + }) + } +} + +func TestUintSlice_Serialized_Set_NonDecimalBase(t *testing.T) { + cfg := IntegerConfig{Base: 16} + + var original, restored []uint + value := UintSlice{}.Create([]uint{16}, &original, cfg) + encoded := value.(Serializer).Serialize() + + require.NoError(t, UintSlice{}.Create(nil, &restored, cfg).Set(encoded)) + require.Equal(t, []uint{16}, restored) +} + func TestUintSlice_Serialized_Set(t *testing.T) { sl0 := NewUint64Slice(1, 2) ser0 := sl0.Serialize() diff --git a/value_source_test.go b/value_source_test.go index 9a410e1801..fdb5246f71 100644 --- a/value_source_test.go +++ b/value_source_test.go @@ -458,6 +458,7 @@ func TestMapValueSourceListsWithMismatchedTypes(t *testing.T) { "strings": []string{"80", "443"}, "mixed": []any{"a", 2, true}, "negative": []any{-1}, + "withnil": []any{80, nil}, "plain": []string{"x", "y"}, }) @@ -487,6 +488,11 @@ func TestMapValueSourceListsWithMismatchedTypes(t *testing.T) { flag: &UintSliceFlag{Name: "negative", Sources: NewValueSourceChain(NewMapValueSource("negative", ms))}, wantErr: true, }, + { + name: "a null element is an error, not a zero", + flag: &IntSliceFlag{Name: "withnil", Sources: NewValueSourceChain(NewMapValueSource("withnil", ms))}, + wantErr: true, + }, { name: "a list into a scalar flag never carries the prefix", flag: &StringFlag{Name: "plain", Sources: NewValueSourceChain(NewMapValueSource("plain", ms))}, @@ -566,11 +572,13 @@ func TestSerializedListElements(t *testing.T) { want []string wantErr bool }{ - {name: "strings", in: `["a","b"]`, want: []string{"a", "b"}}, + {name: "strings", in: `["a","b"]`, want: []string{`"a"`, `"b"`}}, {name: "empty array", in: `[]`, want: []string{}}, {name: "null", in: `null`}, {name: "numbers and bools", in: `[80,-1,true]`, want: []string{"80", "-1", "true"}}, - {name: "escapes", in: `["a,b","he said \"hi\"","back\\slash",""]`, want: []string{"a,b", `he said "hi"`, `back\slash`, ""}}, + {name: "null element", in: `[80,null]`, wantErr: true}, + {name: "empty element", in: `[1,,2]`, wantErr: true}, + {name: "escapes", in: `["a,b","he said \"hi\"","back\\slash",""]`, want: []string{`"a,b"`, `"he said \"hi\""`, `"back\\slash"`, `""`}}, {name: "object", in: `{"a":1}`, wantErr: true}, {name: "unterminated string", in: `["abc]`, wantErr: true}, {name: "bad escape", in: `["\q"]`, wantErr: true},