-
Notifications
You must be signed in to change notification settings - Fork 4
/
null_bool.go
166 lines (144 loc) · 3.46 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
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
package typ
import (
"database/sql/driver"
"encoding/json"
)
// BoolCommon represents a bool with pointer and error.
type BoolCommon struct {
P *bool
Error error
}
// Set saves value into current struct
func (n *BoolCommon) Set(value bool) {
n.P = &value
}
// V returns value of underlying type if it was set, otherwise default value
func (n BoolCommon) V() bool {
if n.P == nil {
return false
}
return *n.P
}
// Present determines whether a value has been set
func (n BoolCommon) Present() bool {
return n.P != nil
}
// Valid determines whether a value has been valid
func (n BoolCommon) Valid() bool {
return n.Err() == nil
}
// Value implements the sql driver Valuer interface.
func (n BoolCommon) Value() (driver.Value, error) {
return n.V(), n.Err()
}
// Scan implements the sql Scanner interface.
func (n *BoolCommon) Scan(value interface{}) error {
n.P, n.Error = nil, nil
if value == nil {
return nil
}
v := Of(value).Bool().V()
n.P = &v
return nil
}
// UnmarshalJSON implements the json Unmarshaler interface.
func (n *BoolCommon) UnmarshalJSON(b []byte) error {
n.P, n.Error = nil, nil
var uv interface{}
if err := json.Unmarshal(b, &uv); err != nil {
n.Error = err
return n.Err()
}
if uv == nil {
return nil
}
v, ok := uv.(bool)
if !ok {
n.Error = ErrConvert
return ErrConvert
}
n.P = &v
return nil
}
// MarshalJSON implements the json Marshaler interface.
func (n BoolCommon) MarshalJSON() ([]byte, error) {
return json.Marshal(n.V())
}
// Typ returns new instance with himself value.
// If current value is invalid, nil *Type returned
func (n BoolCommon) Typ(options ...Option) *Type {
if n.Err() != nil {
return NewType(nil, n.Err())
}
return NewType(n.V(), n.Err(), options...)
}
// Err returns underlying error.
func (n BoolCommon) Err() error {
return n.Error
}
// BoolAccessor accessor of bool type.
type BoolAccessor interface {
Common
V() bool
Set(value bool)
Clone() BoolAccessor
}
// NullBool represents a bool that may be null.
type NullBool struct {
BoolCommon
}
// Value implements the sql driver Valuer interface.
func (n NullBool) Value() (driver.Value, error) {
if n.Err() != nil || !n.Present() {
return nil, n.Err()
}
return n.BoolCommon.Value()
}
// MarshalJSON implements the json Marshaler interface.
func (n NullBool) MarshalJSON() ([]byte, error) {
if n.Err() != nil || !n.Present() {
return json.Marshal(nil)
}
return n.BoolCommon.MarshalJSON()
}
// Clone returns new instance of NullBool with preserved value & error
func (n NullBool) Clone() BoolAccessor {
nv := &NullBool{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NBool returns NullBool under BoolAccessor from bool
func NBool(value bool) BoolAccessor {
return &NullBool{BoolCommon{P: &value}}
}
// NotNullBool represents a bool with accessor.
type NotNullBool struct {
BoolCommon
}
// Clone returns new instance of NullBool with preserved value & error
func (n NotNullBool) Clone() BoolAccessor {
nv := &NotNullBool{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NNBool returns NotNullBool under BoolAccessor from bool
func NNBool(value bool) BoolAccessor {
return &NotNullBool{BoolCommon{P: &value}}
}
// BoolSlice returns slice of bool with filled values from slice of BoolAccessor
func BoolSlice(null []BoolAccessor, valid bool) []bool {
slice := make([]bool, 0, len(null))
for _, v := range null {
if valid && v.Err() != nil {
continue
}
slice = append(slice, v.V())
}
return slice
}