diff --git a/flag.go b/flag.go index 7886b93a2c..dcd04b2d9f 100644 --- a/flag.go +++ b/flag.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "slices" + "strconv" "strings" "sync" "time" @@ -21,6 +22,79 @@ 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 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. + 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) + } + if _, err := strconv.Unquote(rest[:end+1]); err != nil { + return nil, fmt.Errorf("could not decode serialized list: %w", err) + } + 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:] + 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) + } + } + 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 +} + +// 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 b3a0d3c20e..1ad9c85143 100644 --- a/flag_impl.go +++ b/flag_impl.go @@ -164,6 +164,19 @@ 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 { + for i, tok := range elems { + elems[i] = listTokenText(tok) + } + 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..48010701ef 100644 --- a/flag_slice_base.go +++ b/flag_slice_base.go @@ -54,8 +54,34 @@ 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. 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 + } + 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.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..fdb5246f71 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,267 @@ 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) + } + }) + } +} + +// 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}, + "withnil": []any{80, nil}, + "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 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))}, + 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: "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}, + {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) +}