decred.org/dcrwallet/v3@v3.1.0/internal/cfgutil/explicitflags.go (about)

     1  // Copyright (c) 2016 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package cfgutil
     6  
     7  // ExplicitString is a string value implementing the flags.Marshaler and
     8  // flags.Unmarshaler interfaces so it may be used as a config struct field.  It
     9  // records whether the value was explicitly set by the flags package.  This is
    10  // useful when behavior must be modified depending on whether a flag was set by
    11  // the user or left as a default.  Without recording this, it would be
    12  // impossible to determine whether flag with a default value was unmodified or
    13  // explicitly set to the default.
    14  type ExplicitString struct {
    15  	Value         string
    16  	explicitlySet bool
    17  }
    18  
    19  // NewExplicitString creates a string flag with the provided default value.
    20  func NewExplicitString(defaultValue string) *ExplicitString {
    21  	return &ExplicitString{Value: defaultValue, explicitlySet: false}
    22  }
    23  
    24  // ExplicitlySet returns whether the flag was explicitly set through the
    25  // flags.Unmarshaler interface.
    26  func (e *ExplicitString) ExplicitlySet() bool { return e.explicitlySet }
    27  
    28  // MarshalFlag implements the flags.Marshaler interface.
    29  func (e *ExplicitString) MarshalFlag() (string, error) { return e.Value, nil }
    30  
    31  // UnmarshalFlag implements the flags.Unmarshaler interface.
    32  func (e *ExplicitString) UnmarshalFlag(value string) error {
    33  	e.Value = value
    34  	e.explicitlySet = true
    35  	return nil
    36  }
    37  
    38  // String implements the fmt.Stringer interface.
    39  func (e *ExplicitString) String() string { return e.Value }