Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"slices"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
30 changes: 28 additions & 2 deletions flag_slice_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
35 changes: 35 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 16 additions & 3 deletions value_source.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cli

import (
"encoding/json"
"fmt"
"os"
"reflect"
"strings"
)

Expand Down Expand Up @@ -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
}
Loading
Loading