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