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

     1  package flag
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"google.golang.org/protobuf/reflect/protoreflect"
     9  
    10  	basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
    11  	"cosmossdk.io/core/coins"
    12  )
    13  
    14  type coinType struct{}
    15  
    16  type coinValue struct {
    17  	value *basev1beta1.Coin
    18  }
    19  
    20  func (c coinType) NewValue(context.Context, *Builder) Value {
    21  	return &coinValue{}
    22  }
    23  
    24  func (c coinType) DefaultValue() string {
    25  	stringCoin, _ := coins.FormatCoins([]*basev1beta1.Coin{}, nil)
    26  	return stringCoin
    27  }
    28  
    29  func (c *coinValue) Get(protoreflect.Value) (protoreflect.Value, error) {
    30  	if c.value == nil {
    31  		return protoreflect.Value{}, nil
    32  	}
    33  	return protoreflect.ValueOfMessage(c.value.ProtoReflect()), nil
    34  }
    35  
    36  func (c *coinValue) String() string {
    37  	return c.value.String()
    38  }
    39  
    40  func (c *coinValue) Set(stringValue string) error {
    41  	if strings.Contains(stringValue, ",") {
    42  		return fmt.Errorf("coin flag must be a single coin, specific multiple coins with multiple flags or spaces")
    43  	}
    44  
    45  	coin, err := coins.ParseCoin(stringValue)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	c.value = coin
    50  	return nil
    51  }
    52  
    53  func (c *coinValue) Type() string {
    54  	return "cosmos.base.v1beta1.Coin"
    55  }