forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnull_bool.go
69 lines (56 loc) · 1.15 KB
/
null_bool.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package types
import (
"encoding/json"
"fmt"
"strconv"
)
// NullBool is a wrapper around bool values that can be null or an bool.
// Use IsSet to check if the value is provided, instead of checking against false.
type NullBool struct {
IsSet bool
Value bool
}
// ParseStringValue is used to parse a user provided flag argument.
func (n *NullBool) ParseStringValue(val string) error {
if val == "" {
return nil
}
boolVal, err := strconv.ParseBool(val)
if err != nil {
return err
}
n.Value = boolVal
n.IsSet = true
return nil
}
// ParseBoolValue is used to parse a user provided *bool argument.
func (n *NullBool) ParseBoolValue(val *bool) {
if val == nil {
n.IsSet = false
n.Value = false
return
}
n.Value = *val
n.IsSet = true
}
func (n *NullBool) UnmarshalJSON(rawJSON []byte) error {
var value *bool
err := json.Unmarshal(rawJSON, &value)
if err != nil {
return err
}
if value == nil {
n.Value = false
n.IsSet = false
return nil
}
n.Value = *value
n.IsSet = true
return nil
}
func (n NullBool) MarshalJSON() ([]byte, error) {
if n.IsSet {
return []byte(fmt.Sprint(n.Value)), nil
}
return []byte(JsonNull), nil
}