nullables.go
5.85 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package webutility
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"time"
)
// NullBool is a wrapper for sql.NullBool with added JSON (un)marshalling
type NullBool sql.NullBool
// Scan ...
func (nb *NullBool) Scan(value interface{}) error {
var b sql.NullBool
if err := b.Scan(value); err != nil {
nb.Bool, nb.Valid = false, false
return err
}
nb.Bool, nb.Valid = b.Bool, b.Valid
return nil
}
// Value ...
func (nb *NullBool) Value() (driver.Value, error) {
if !nb.Valid {
return nil, nil
}
return nb.Bool, nil
}
// MarshalJSON ...
func (nb NullBool) MarshalJSON() ([]byte, error) {
if nb.Valid {
return json.Marshal(nb.Bool)
}
return json.Marshal(nil)
}
// UnmarshalJSON ...
func (nb *NullBool) UnmarshalJSON(b []byte) error {
var temp *bool
if err := json.Unmarshal(b, &temp); err != nil {
return err
}
if temp != nil {
nb.Valid = true
nb.Bool = *temp
} else {
nb.Valid = false
}
return nil
}
// SQLCast ...
func (nb *NullBool) SQLCast() sql.NullBool {
return sql.NullBool(*nb)
}
// NullString is a wrapper for sql.NullString with added JSON (un)marshalling
type NullString sql.NullString
// Scan ...
func (ns *NullString) Scan(value interface{}) error {
var s sql.NullString
if err := s.Scan(value); err != nil {
ns.String, ns.Valid = "", false
return err
}
ns.String, ns.Valid = s.String, s.Valid
return nil
}
// Value ...
func (ns *NullString) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return ns.String, nil
}
// MarshalJSON ...
func (ns NullString) MarshalJSON() ([]byte, error) {
if ns.Valid {
return json.Marshal(ns.String)
}
return json.Marshal(nil)
}
// UnmarshalJSON ...
func (ns *NullString) UnmarshalJSON(b []byte) error {
var temp *string
if err := json.Unmarshal(b, &temp); err != nil {
return err
}
if temp != nil {
ns.Valid = true
ns.String = *temp
} else {
ns.Valid = false
}
return nil
}
// SQLCast ...
func (ns *NullString) SQLCast() sql.NullString {
return sql.NullString(*ns)
}
// NullInt64 is a wrapper for sql.NullInt64 with added JSON (un)marshalling
type NullInt64 sql.NullInt64
// Scan ...
func (ni *NullInt64) Scan(value interface{}) error {
var i sql.NullInt64
if err := i.Scan(value); err != nil {
ni.Int64, ni.Valid = 0, false
return err
}
ni.Int64, ni.Valid = i.Int64, i.Valid
return nil
}
// Value ...
func (ni *NullInt64) Value() (driver.Value, error) {
if !ni.Valid {
return nil, nil
}
return ni.Int64, nil
}
// MarshalJSON ...
func (ni NullInt64) MarshalJSON() ([]byte, error) {
if ni.Valid {
return json.Marshal(ni.Int64)
}
return json.Marshal(nil)
}
// UnmarshalJSON ...
func (ni *NullInt64) UnmarshalJSON(b []byte) error {
var temp *int64
if err := json.Unmarshal(b, &temp); err != nil {
return err
}
if temp != nil {
ni.Valid = true
ni.Int64 = *temp
} else {
ni.Valid = false
}
return nil
}
// SQLCast ...
func (ni *NullInt64) SQLCast() sql.NullInt64 {
return sql.NullInt64(*ni)
}
// NullFloat64 is a wrapper for sql.NullFloat64 with added JSON (un)marshalling
type NullFloat64 sql.NullFloat64
// Scan ...
func (nf *NullFloat64) Scan(value interface{}) error {
var f sql.NullFloat64
if err := f.Scan(value); err != nil {
nf.Float64, nf.Valid = 0.0, false
return err
}
nf.Float64, nf.Valid = f.Float64, f.Valid
return nil
}
// Value ...
func (nf *NullFloat64) Value() (driver.Value, error) {
if !nf.Valid {
return nil, nil
}
return nf.Float64, nil
}
// MarshalJSON ...
func (nf NullFloat64) MarshalJSON() ([]byte, error) {
if nf.Valid {
return json.Marshal(nf.Float64)
}
return json.Marshal(nil)
}
// UnmarshalJSON ...
func (nf *NullFloat64) UnmarshalJSON(b []byte) error {
var temp *float64
if err := json.Unmarshal(b, &temp); err != nil {
return err
}
if temp != nil {
nf.Valid = true
nf.Float64 = *temp
} else {
nf.Valid = false
}
return nil
}
// SQLCast ...
func (nf *NullFloat64) SQLCast() sql.NullFloat64 {
return sql.NullFloat64(*nf)
}
// NullTime ...
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}
// Scan ...
func (nt *NullTime) Scan(value interface{}) (err error) {
if value == nil {
nt.Time, nt.Valid = time.Time{}, false
return
}
switch v := value.(type) {
case time.Time:
nt.Time, nt.Valid = v, true
return
case []byte:
nt.Time, err = parseDateTime(string(v), time.UTC)
nt.Valid = (err == nil)
return
case string:
nt.Time, err = parseDateTime(v, time.UTC)
nt.Valid = (err == nil)
return
}
nt.Valid = false
return fmt.Errorf("Can't convert %T to time.Time", value)
}
// Value implements the driver Valuer interface.
func (nt NullTime) Value() (driver.Value, error) {
if !nt.Valid {
return nil, nil
}
return nt.Time, nil
}
// MarshalJSON ...
func (nt NullTime) MarshalJSON() ([]byte, error) {
if nt.Valid {
format := nt.Time.Format("2006-01-02 15:04:05")
return json.Marshal(format)
}
return json.Marshal(nil)
}
// UnmarshalJSON ...
func (nt *NullTime) UnmarshalJSON(b []byte) error {
var temp *time.Time
var t1 time.Time
var err error
s1 := string(b)
s2 := s1[1 : len(s1)-1]
if s1 == "null" {
temp = nil
} else {
t1, err = time.Parse("2006-01-02 15:04:05", s2)
if err != nil {
return err
}
temp = &t1
}
if temp != nil {
nt.Valid = true
nt.Time = *temp
} else {
nt.Valid = false
}
return nil
}
func parseDateTime(str string, loc *time.Location) (t time.Time, err error) {
base := "0000-00-00 00:00:00.0000000"
timeFormat := "2006-01-02 15:04:05.999999"
switch len(str) {
case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
if str == base[:len(str)] {
return
}
t, err = time.Parse(timeFormat[:len(str)], str)
default:
err = fmt.Errorf("invalid time string: %s", str)
return
}
// Adjust location
if err == nil && loc != time.UTC {
y, mo, d := t.Date()
h, mi, s := t.Clock()
t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
}
return
}