-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
304 lines (253 loc) · 5.46 KB
/
type.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
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
/*
Copyright (C) 2023 Giorgi Kobakhidze
This file is part of go-null.
go-null is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, version 3 of the License.
go-null is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with go-null. If not, see <https://www.gnu.org/licenses/>.
*/
package null
import (
"database/sql"
"database/sql/driver"
"time"
)
// Bool wraps J[bool] and sql.NullBool.
type Bool struct {
J[bool]
}
func NewBool(v bool) (nullV Bool) {
nullV.Val = v
nullV.IsSet = true
return
}
func (v *Bool) Ptr() *BoolP { return (*BoolP)(v.J.Ptr()) }
func (v *Bool) Scan(value any) (err error) {
var sqlV sql.NullBool
if err = sqlV.Scan(value); err != nil {
return
}
v.Val = sqlV.Bool
v.IsSet = sqlV.Valid
return
}
func (v Bool) Value() (driver.Value, error) {
return sql.NullBool{
Bool: v.Val,
Valid: v.IsSet,
}.Value()
}
// Byte wraps J[byte] and sql.NullByte.
type Byte struct {
J[byte]
}
func NewByte(v byte) (nullV Byte) {
nullV.Val = v
nullV.IsSet = true
return
}
func (v *Byte) Ptr() *ByteP { return (*ByteP)(v.J.Ptr()) }
func (v *Byte) Scan(value any) (err error) {
var sqlV sql.NullByte
if err = sqlV.Scan(value); err != nil {
return
}
v.Val = sqlV.Byte
v.IsSet = sqlV.Valid
return
}
func (v Byte) Value() (driver.Value, error) {
return sql.NullByte{
Byte: v.Val,
Valid: v.IsSet,
}.Value()
}
// Float64 wraps J[float64] and sql.NullFloat64.
type Float64 struct {
J[float64]
}
func NewFloat64(v float64) (nullV Float64) {
nullV.Val = v
nullV.IsSet = true
return
}
func (v *Float64) Ptr() *Float64P { return (*Float64P)(v.J.Ptr()) }
func (v *Float64) Scan(value any) (err error) {
var sqlV sql.NullFloat64
if err = sqlV.Scan(value); err != nil {
return
}
v.Val = sqlV.Float64
v.IsSet = sqlV.Valid
return
}
func (v Float64) Value() (driver.Value, error) {
return sql.NullFloat64{
Float64: v.Val,
Valid: v.IsSet,
}.Value()
}
// Int16 wraps J[int16] and sql.NullInt16.
type Int16 struct {
J[int16]
}
func NewInt16(v int16) (nullV Int16) {
nullV.Val = v
nullV.IsSet = true
return
}
func (v *Int16) Ptr() *Int16P { return (*Int16P)(v.J.Ptr()) }
func (v *Int16) Scan(value any) (err error) {
var sqlV sql.NullInt16
if err = sqlV.Scan(value); err != nil {
return
}
v.Val = sqlV.Int16
v.IsSet = sqlV.Valid
return
}
func (v Int16) Value() (driver.Value, error) {
return sql.NullInt16{
Int16: v.Val,
Valid: v.IsSet,
}.Value()
}
// Int32 wraps J[int32] and sql.NullInt32.
type Int32 struct {
J[int32]
}
func NewInt32(v int32) (nullV Int32) {
nullV.Val = v
nullV.IsSet = true
return
}
func (v *Int32) Ptr() *Int32P { return (*Int32P)(v.J.Ptr()) }
func (v *Int32) Scan(value any) (err error) {
var sqlV sql.NullInt32
if err = sqlV.Scan(value); err != nil {
return
}
v.Val = sqlV.Int32
v.IsSet = sqlV.Valid
return
}
func (v Int32) Value() (driver.Value, error) {
return sql.NullInt32{
Int32: v.Val,
Valid: v.IsSet,
}.Value()
}
// Int64 wraps J[int64] and sql.NullInt64.
type Int64 struct {
J[int64]
}
func NewInt64(v int64) (nullV Int64) {
nullV.Val = v
nullV.IsSet = true
return
}
func (v *Int64) Ptr() *Int64P { return (*Int64P)(v.J.Ptr()) }
func (v *Int64) Scan(value any) (err error) {
var sqlV sql.NullInt64
if err = sqlV.Scan(value); err != nil {
return
}
v.Val = sqlV.Int64
v.IsSet = sqlV.Valid
return
}
func (v Int64) Value() (driver.Value, error) {
return sql.NullInt64{
Int64: v.Val,
Valid: v.IsSet,
}.Value()
}
// Str wraps J[string] and sql.NullString.
type Str struct {
J[string]
}
func NewStr(v string) (nullV Str) {
nullV.Val = v
nullV.IsSet = true
return
}
func (v *Str) Ptr() *StrP { return (*StrP)(v.J.Ptr()) }
func (v *Str) Scan(value any) (err error) {
var sqlV sql.NullString
if err = sqlV.Scan(value); err != nil {
return
}
v.Val = sqlV.String
v.IsSet = sqlV.Valid
return
}
func (v Str) Value() (driver.Value, error) {
return sql.NullString{
String: v.Val,
Valid: v.IsSet,
}.Value()
}
// Time wraps J[time.Time] and sql.NullTime.
type Time struct {
J[time.Time]
}
func NewTime(v time.Time) (nullV Time) {
nullV.Val = v
nullV.IsSet = true
return
}
func (v *Time) Ptr() *TimeP { return (*TimeP)(v.J.Ptr()) }
func (v *Time) Scan(value any) (err error) {
var sqlV sql.NullTime
if err = sqlV.Scan(value); err != nil {
return
}
v.Val = sqlV.Time
v.IsSet = sqlV.Valid
return
}
func (v Time) Value() (driver.Value, error) {
return sql.NullTime{
Time: v.Val,
Valid: v.IsSet,
}.Value()
}
type ScannerValuer interface {
sql.Scanner
driver.Valuer
}
// CustomT wraps J[ScannerValuer], sql.Scanner and driver.Valuer.
// Internal type T has to be given as T and as *T.
// *T must support json marshaling and unmarshaling.
type CustomT[
T any,
pT interface {
*T
ScannerValuer
},
] struct {
J[T]
}
func NewCustomT[
T any,
pT interface {
*T
ScannerValuer
},
](v T) (nullV CustomT[T, pT]) {
nullV.Val = v
nullV.IsSet = true
return
}
func (v *CustomT[T, pT]) Scan(value any) (err error) {
if err = (pT)(&v.Val).Scan(value); err != nil {
return
}
v.IsSet = true
return
}
func (v CustomT[T, pT]) Value() (driver.Value, error) {
if !v.IsSet {
return nil, nil
}
return (pT)(&v.Val).Value()
}