-
Notifications
You must be signed in to change notification settings - Fork 929
/
filtered_string.go
57 lines (47 loc) · 981 Bytes
/
filtered_string.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
package types
import (
"encoding/json"
)
// FilteredString is a wrapper around string values that can be null/default or an
// actual value. Use IsSet to check if the value is provided, instead of
// checking against the empty string.
type FilteredString struct {
IsSet bool
Value string
}
// ParseValue is used to parse a user provided flag argument.
func (n *FilteredString) ParseValue(val string) {
if val == "" {
n.IsSet = false
n.Value = ""
return
}
n.IsSet = true
switch val {
case "null", "default":
n.Value = ""
default:
n.Value = val
}
}
func (n *FilteredString) UnmarshalJSON(rawJSON []byte) error {
var value *string
err := json.Unmarshal(rawJSON, &value)
if err != nil {
return err
}
if value != nil {
n.Value = *value
n.IsSet = true
return nil
}
n.Value = ""
n.IsSet = false
return nil
}
func (n FilteredString) MarshalJSON() ([]byte, error) {
if n.IsSet {
return json.Marshal(n.Value)
}
return json.Marshal(nil)
}