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

     1  package flag
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"google.golang.org/protobuf/reflect/protoreflect"
     8  	"google.golang.org/protobuf/types/known/durationpb"
     9  )
    10  
    11  type durationType struct{}
    12  
    13  func (d durationType) NewValue(context.Context, *Builder) Value {
    14  	return &durationValue{}
    15  }
    16  
    17  func (d durationType) DefaultValue() string {
    18  	return ""
    19  }
    20  
    21  type durationValue struct {
    22  	value *durationpb.Duration
    23  }
    24  
    25  func (d durationValue) Get(protoreflect.Value) (protoreflect.Value, error) {
    26  	if d.value == nil {
    27  		return protoreflect.Value{}, nil
    28  	}
    29  	return protoreflect.ValueOfMessage(d.value.ProtoReflect()), nil
    30  }
    31  
    32  func (d durationValue) String() string {
    33  	if d.value == nil {
    34  		return ""
    35  	}
    36  	return d.value.AsDuration().String()
    37  }
    38  
    39  func (d *durationValue) Set(s string) error {
    40  	dur, err := time.ParseDuration(s)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	d.value = durationpb.New(dur)
    46  	return nil
    47  }
    48  
    49  func (d durationValue) Type() string {
    50  	return "duration"
    51  }