github.com/sleungcy/cli@v7.1.0+incompatible/types/null_uint64.go (about)

     1  package types
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // NullUint64 is a wrapper around uint64 values that can be null or an unint64.
     8  // Use IsSet to check if the value is provided, instead of checking against 0.
     9  type NullUint64 struct {
    10  	IsSet bool
    11  	Value uint64
    12  }
    13  
    14  // ParseStringValue is used to parse a user provided flag argument.
    15  func (n *NullUint64) ParseStringValue(val string) error {
    16  	if val == "" {
    17  		n.Value = 0
    18  		n.IsSet = false
    19  		return nil
    20  	}
    21  
    22  	uint64Val, err := strconv.ParseUint(val, 10, 64)
    23  	if err != nil {
    24  		n.Value = 0
    25  		n.IsSet = false
    26  		return err
    27  	}
    28  
    29  	n.Value = uint64Val
    30  	n.IsSet = true
    31  
    32  	return nil
    33  }
    34  
    35  func (n *NullUint64) UnmarshalJSON(rawJSON []byte) error {
    36  	stringValue := string(rawJSON)
    37  
    38  	if stringValue == JsonNull {
    39  		n.Value = 0
    40  		n.IsSet = false
    41  		return nil
    42  	}
    43  
    44  	return n.ParseStringValue(stringValue)
    45  }