cosmossdk.io/client/v2@v2.0.0-beta.1/autocli/flag/list.go (about)

     1  package flag
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/spf13/pflag"
     8  	"google.golang.org/protobuf/reflect/protoreflect"
     9  )
    10  
    11  func bindSimpleListFlag(flagSet *pflag.FlagSet, kind protoreflect.Kind, name, shorthand, usage string) HasValue {
    12  	switch kind {
    13  	case protoreflect.StringKind:
    14  		val := flagSet.StringSliceP(name, shorthand, nil, usage)
    15  		return newListValue(val, protoreflect.ValueOfString)
    16  	case protoreflect.Uint32Kind, protoreflect.Fixed32Kind,
    17  		protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
    18  		val := flagSet.UintSliceP(name, shorthand, nil, usage)
    19  		return newListValue(val, func(x uint) protoreflect.Value { return protoreflect.ValueOfUint64(uint64(x)) })
    20  	case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
    21  		val := flagSet.Int32SliceP(name, shorthand, nil, usage)
    22  		return newListValue(val, protoreflect.ValueOfInt32)
    23  	case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
    24  		val := flagSet.Int64SliceP(name, shorthand, nil, usage)
    25  		return newListValue(val, protoreflect.ValueOfInt64)
    26  	case protoreflect.BoolKind:
    27  		val := flagSet.BoolSliceP(name, shorthand, nil, usage)
    28  		return newListValue(val, protoreflect.ValueOfBool)
    29  	default:
    30  		return nil
    31  	}
    32  }
    33  
    34  type listValue[T any] struct {
    35  	array               *[]T
    36  	toProtoreflectValue func(T) protoreflect.Value
    37  }
    38  
    39  func newListValue[T any](array *[]T, toProtoreflectValue func(T) protoreflect.Value) listValue[T] {
    40  	return listValue[T]{array: array, toProtoreflectValue: toProtoreflectValue}
    41  }
    42  
    43  func (v listValue[T]) Get(mutable protoreflect.Value) (protoreflect.Value, error) {
    44  	list := mutable.List()
    45  	for _, x := range *v.array {
    46  		list.Append(v.toProtoreflectValue(x))
    47  	}
    48  	return mutable, nil
    49  }
    50  
    51  type compositeListType struct {
    52  	simpleType Type
    53  }
    54  
    55  func (t compositeListType) NewValue(ctx context.Context, opts *Builder) Value {
    56  	return &compositeListValue{
    57  		simpleType: t.simpleType,
    58  		values:     nil,
    59  		ctx:        ctx,
    60  		opts:       opts,
    61  	}
    62  }
    63  
    64  func (t compositeListType) DefaultValue() string {
    65  	return ""
    66  }
    67  
    68  type compositeListValue struct {
    69  	simpleType Type
    70  	values     []protoreflect.Value
    71  	ctx        context.Context
    72  	opts       *Builder
    73  }
    74  
    75  func (c *compositeListValue) Get(mutable protoreflect.Value) (protoreflect.Value, error) {
    76  	list := mutable.List()
    77  	for _, value := range c.values {
    78  		list.Append(value)
    79  	}
    80  	return mutable, nil
    81  }
    82  
    83  func (c *compositeListValue) String() string {
    84  	if len(c.values) == 0 {
    85  		return ""
    86  	}
    87  
    88  	return fmt.Sprintf("%+v", c.values)
    89  }
    90  
    91  func (c *compositeListValue) Set(val string) error {
    92  	simpleVal := c.simpleType.NewValue(c.ctx, c.opts)
    93  	err := simpleVal.Set(val)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	v, err := simpleVal.Get(protoreflect.Value{})
    98  	if err != nil {
    99  		return err
   100  	}
   101  	c.values = append(c.values, v)
   102  	return nil
   103  }
   104  
   105  func (c *compositeListValue) Type() string {
   106  	return fmt.Sprintf("%s (repeated)", c.simpleType.NewValue(c.ctx, c.opts).Type())
   107  }