| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "reflect" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // SliceBase wraps []T to satisfy flag.Value |
| 11 | type SliceBase[T any, C any, VC ValueCreator[T, C]] struct { |
| 12 | slice *[]T |
| 13 | hasBeenSet bool |
| 14 | value Value |
| 15 | sliceSeparator string |
| 16 | disableSliceSeparator bool |
| 17 | } |
| 18 | |
| 19 | func (i SliceBase[T, C, VC]) Create(val []T, p *[]T, c C) Value { |
| 20 | *p = []T{} |
| 21 | *p = append(*p, val...) |
| 22 | var t T |
| 23 | np := new(T) |
| 24 | var vc VC |
| 25 | return &SliceBase[T, C, VC]{ |
| 26 | slice: p, |
| 27 | value: vc.Create(t, np, c), |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // NewSliceBase makes a *SliceBase with default values |
| 32 | func NewSliceBase[T any, C any, VC ValueCreator[T, C]](defaults ...T) *SliceBase[T, C, VC] { |
| 33 | return &SliceBase[T, C, VC]{ |
| 34 | slice: &defaults, |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // configuration of slicing |
| 39 | func (i *SliceBase[T, C, VC]) setMultiValueParsingConfig(c multiValueParsingConfig) { |
| 40 | i.disableSliceSeparator = c.DisableSliceFlagSeparator |
| 41 | i.sliceSeparator = c.SliceFlagSeparator |
| 42 | tracef("set slice parsing config - slice separator '%s', disable separator:%v", i.sliceSeparator, i.disableSliceSeparator) |
| 43 | } |
| 44 | |
| 45 | // Set parses the value and appends it to the list of values |
| 46 | func (i *SliceBase[T, C, VC]) Set(value string) error { |
| 47 | if !i.hasBeenSet { |
| 48 | *i.slice = []T{} |
| 49 | i.hasBeenSet = true |
| 50 | } |
| 51 | |
| 52 | if strings.HasPrefix(value, slPfx) { |
| 53 | // Deserializing assumes overwrite |
| 54 | _ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &i.slice) |
| 55 | i.hasBeenSet = true |
| 56 | return nil |
| 57 | } |
| 58 | |
| 59 | trimSpace := true |
| 60 | // hack. How do we know if we should trim spaces? |
| 61 | // it makes sense only for string slice flags which have |
| 62 | // an option to not trim spaces. So by default we trim spaces |
| 63 | // otherwise we let the underlying value type handle it. |
| 64 | var t T |
| 65 | if reflect.TypeOf(t).Kind() == reflect.String { |
| 66 | trimSpace = false |
| 67 | } |
| 68 | |
| 69 | tracef("splitting slice value '%s', separator '%s', disable separator:%v", value, i.sliceSeparator, i.disableSliceSeparator) |
| 70 | for _, s := range flagSplitMultiValues(value, i.sliceSeparator, i.disableSliceSeparator) { |
| 71 | if trimSpace { |
| 72 | s = strings.TrimSpace(s) |
| 73 | } |
| 74 | if err := i.value.Set(s); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | *i.slice = append(*i.slice, i.value.Get().(T)) |
| 78 | } |
| 79 | |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | // String returns a readable representation of this value (for usage defaults) |
| 84 | func (i *SliceBase[T, C, VC]) String() string { |
| 85 | var defaultVals []string |
| 86 | var v VC |
| 87 | for _, s := range *i.slice { |
| 88 | defaultVals = append(defaultVals, v.ToString(s)) |
| 89 | } |
| 90 | return strings.Join(defaultVals, ", ") |
| 91 | } |
| 92 | |
| 93 | // Serialize allows SliceBase to fulfill Serializer |
| 94 | func (i *SliceBase[T, C, VC]) Serialize() string { |
| 95 | jsonBytes, _ := json.Marshal(i.slice) |
| 96 | return fmt.Sprintf("%s%s", slPfx, string(jsonBytes)) |
| 97 | } |
| 98 | |
| 99 | // Value returns the slice of values set by this flag |
| 100 | func (i *SliceBase[T, C, VC]) Value() []T { |
| 101 | if i.slice == nil { |
| 102 | return nil |
| 103 | } |
| 104 | return *i.slice |
| 105 | } |
| 106 | |
| 107 | // Get returns the slice of values set by this flag |
| 108 | func (i *SliceBase[T, C, VC]) Get() any { |
| 109 | return *i.slice |
| 110 | } |
| 111 | |
| 112 | func (i SliceBase[T, C, VC]) ToString(t []T) string { |
| 113 | i.slice = &t |
| 114 | return i.String() |
| 115 | } |