github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/types/null_uint64.go (about) 1 package types 2 3 import ( 4 "encoding/json" 5 "strconv" 6 ) 7 8 // NullUint64 is a wrapper around uint64 values that can be null or an unint64. 9 // Use IsSet to check if the value is provided, instead of checking against 0. 10 type NullUint64 struct { 11 IsSet bool 12 Value uint64 13 } 14 15 // ParseStringValue is used to parse a user provided flag argument. 16 func (n *NullUint64) ParseStringValue(val string) error { 17 if val == "" { 18 return nil 19 } 20 21 uint64Val, err := strconv.ParseUint(val, 10, 64) 22 if err != nil { 23 return err 24 } 25 26 n.Value = uint64Val 27 n.IsSet = true 28 29 return nil 30 } 31 32 func (n *NullUint64) UnmarshalJSON(rawJSON []byte) error { 33 var value json.Number 34 err := json.Unmarshal(rawJSON, &value) 35 if err != nil { 36 return err 37 } 38 39 if value.String() == "" { 40 n.Value = 0 41 n.IsSet = false 42 return nil 43 } 44 45 valueInt, err := strconv.ParseUint(value.String(), 10, 64) 46 if err != nil { 47 return err 48 } 49 50 n.Value = valueInt 51 n.IsSet = true 52 53 return nil 54 }