forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bool.go
201 lines (176 loc) · 4.33 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package null
import (
"bytes"
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jwriter"
)
var (
nullLiteral = []byte("null")
falseLiteral = []byte("false")
trueLiteral = []byte("true")
)
// Bool is a nullable bool.
// It does not consider false values to be null.
// It will decode to null, not false, if null.
type Bool struct {
sql.NullBool
}
func True() Bool { return B(true) }
func False() Bool { return B(false) }
// NewBool creates a new Bool
func NewBool[T ~bool](b T, valid bool) Bool {
return Bool{
NullBool: sql.NullBool{
Bool: bool(b),
Valid: valid,
},
}
}
// B creates a new Bool that will always be valid.
func B[T ~bool](b T) Bool {
return BoolFrom(b)
}
// BoolFrom creates a new Bool that will always be valid.
func BoolFrom[T ~bool](b T) Bool {
return NewBool(b, true)
}
// BoolFromPtr creates a new Bool that will be null if f is nil.
func BoolFromPtr[T ~bool](b *T) Bool {
if b == nil {
return NewBool(false, false)
}
return NewBool(*b, true)
}
// ValueOrZero returns the inner value if valid, otherwise false.
func (b Bool) ValueOrZero() bool {
return b.Valid && b.Bool
}
// UnmarshalJSON implements json.Unmarshaler.
// It supports number and null input.
// 0 will not be considered a null Bool.
func (b *Bool) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullLiteral) || len(data) == 0 {
b.Valid = false
return nil
}
if data[0] == '{' {
if err := json.Unmarshal(data, &b.NullBool); err != nil {
return fmt.Errorf("null: couldn't unmarshal JSON: %w", err)
}
return nil
}
if err := json.Unmarshal(data, &b.Bool); err != nil {
return fmt.Errorf("null: couldn't unmarshal JSON: %w", err)
}
b.Valid = true
return nil
}
// UnmarshalEasyJSON is an easy-JSON specific decoder, that should be more efficient than the standard one.
// We expect the value to be either `null` or `true`, but we also unmarshal if we receive
// `{"Valid":true,"Bool":false}`
func (b *Bool) UnmarshalEasyJSON(w *jlexer.Lexer) {
if w.IsNull() {
w.Skip()
b.Valid = false
return
}
if w.IsDelim('{') {
w.Skip()
for !w.IsDelim('}') {
key := w.UnsafeString()
w.WantColon()
if w.IsNull() {
w.Skip()
w.WantComma()
continue
}
switch key {
case "bool", "Bool":
b.Bool = w.Bool()
case "valid", "Valid":
b.Valid = w.Bool()
}
w.WantComma()
}
return
}
b.Bool = w.Bool()
b.Valid = (w.Error() == nil)
}
// UnmarshalText implements encoding.TextUnmarshaler.
// It will unmarshal to a null Bool if the input is blank.
// It will return an error if the input is not an integer, blank, or "null".
func (b *Bool) UnmarshalText(text []byte) error {
str := string(text)
switch str {
case "", "null":
b.Valid = false
return nil
case "true":
b.Bool = true
case "false":
b.Bool = false
default:
return errors.New("null: invalid input for UnmarshalText:" + str)
}
b.Valid = true
return nil
}
// MarshalJSON implements json.Marshaler.
// It will encode null if this Bool is null.
func (b Bool) MarshalJSON() ([]byte, error) {
if !b.Valid {
return nullLiteral, nil
}
if !b.Bool {
return falseLiteral, nil
}
return trueLiteral, nil
}
func (b Bool) MarshalEasyJSON(w *jwriter.Writer) {
if !b.Valid {
w.RawString("null")
return
}
w.Bool(b.Bool)
}
// MarshalText implements encoding.TextMarshaler.
// It will encode a blank string if this Bool is null.
func (b Bool) MarshalText() ([]byte, error) {
if !b.Valid {
return []byte{}, nil
}
if !b.Bool {
return falseLiteral, nil
}
return trueLiteral, nil
}
// SetValid changes this Bool's value and also sets it to be non-null.
func (b *Bool) SetValid(v bool) {
b.Bool = v
b.Valid = true
}
// Ptr returns a pointer to this Bool's value, or a nil pointer if this Bool is null.
func (b Bool) Ptr() *bool {
if !b.Valid {
return nil
}
return &b.Bool
}
// IsZero returns true for invalid Bools, for future omitempty support (Go 1.4?)
// A non-null Bool with a 0 value will not be considered zero.
func (b Bool) IsZero() bool {
return !b.Valid
}
// IsDefined implements the easyjson.Optional interface for omitempty-ing.
func (b Bool) IsDefined() bool {
return !b.IsZero()
}
// Equal returns true if both booleans have the same value or are both null.
func (b Bool) Equal(other Bool) bool {
return b.Valid == other.Valid && (!b.Valid || b.Bool == other.Bool)
}