forked from rclone/rclone
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tristate.go
80 lines (72 loc) · 1.43 KB
/
tristate.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
70
71
72
73
74
75
76
77
78
79
80
package fs
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// Tristate is a boolean that can has the states, true, false and
// unset/invalid/nil
type Tristate struct {
Value bool
Valid bool
}
// String renders the tristate as true/false/unset
func (t Tristate) String() string {
if !t.Valid {
return "unset"
}
if t.Value {
return "true"
}
return "false"
}
// Set the List entries
func (t *Tristate) Set(s string) error {
s = strings.ToLower(s)
if s == "" || s == "nil" || s == "null" || s == "unset" {
t.Valid = false
return nil
}
value, err := strconv.ParseBool(s)
if err != nil {
return fmt.Errorf("failed to parse Tristate %q: %w", s, err)
}
t.Value = value
t.Valid = true
return nil
}
// Type of the value
func (Tristate) Type() string {
return "Tristate"
}
// Scan implements the fmt.Scanner interface
func (t *Tristate) Scan(s fmt.ScanState, ch rune) error {
token, err := s.Token(true, nil)
if err != nil {
return err
}
return t.Set(string(token))
}
// UnmarshalJSON parses it as a bool or nil for unset
func (t *Tristate) UnmarshalJSON(in []byte) error {
var b *bool
err := json.Unmarshal(in, &b)
if err != nil {
return err
}
if b != nil {
t.Valid = true
t.Value = *b
} else {
t.Valid = false
}
return nil
}
// MarshalJSON encodes it as a bool or nil for unset
func (t *Tristate) MarshalJSON() ([]byte, error) {
if !t.Valid {
return json.Marshal(nil)
}
return json.Marshal(t.Value)
}