-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnull_float.go
341 lines (298 loc) · 7.39 KB
/
null_float.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package typ
import (
"database/sql/driver"
"encoding/json"
)
// Float32Common represents a float32 that may be null.
type Float32Common struct {
P *float32
Error error
}
// Set saves value into current struct
func (n *Float32Common) Set(value float32) {
n.P = &value
}
// V returns value of underlying type if it was set, otherwise default value
func (n Float32Common) V() float32 {
if n.P == nil {
return 0
}
return *n.P
}
// Present determines whether a value has been set
func (n Float32Common) Present() bool {
return n.P != nil
}
// Valid determines whether a value has been valid
func (n Float32Common) Valid() bool {
return n.Err() == nil
}
// Value implements the sql driver Valuer interface.
func (n Float32Common) Value() (driver.Value, error) {
return float64(n.V()), nil
}
// Scan implements the sql Scanner interface.
func (n *Float32Common) Scan(value interface{}) error {
n.P, n.Error = nil, nil
if value == nil {
return nil
}
v := Of(value).Float32()
if v.Err() != nil {
n.Error = v.Err()
return v.Err()
}
n.Set(v.V())
n.Error = v.Err()
return v.Err()
}
// UnmarshalJSON implements the json Unmarshaler interface.
func (n *Float32Common) 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
}
vFloat, ok := uv.(float64)
if !ok {
n.Error = ErrConvert
return n.Err()
}
v := Float32(vFloat)
if v.Err() != nil {
n.Error = ErrConvert
return n.Err()
}
n.Set(v.V())
return nil
}
// MarshalJSON implements the json Marshaler interface.
func (n Float32Common) 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 Float32Common) 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 Float32Common) Err() error {
return n.Error
}
// Float32Accessor accessor of float32 type.
type Float32Accessor interface {
Common
V() float32
Set(value float32)
Clone() Float32Accessor
}
// NullFloat32 represents a float32 that may be null.
type NullFloat32 struct {
Float32Common
}
// Value implements the sql driver Valuer interface.
func (n NullFloat32) Value() (driver.Value, error) {
if n.Err() != nil || !n.Present() {
return nil, n.Err()
}
return n.Float32Common.Value()
}
// MarshalJSON implements the json Marshaler interface.
func (n NullFloat32) MarshalJSON() ([]byte, error) {
if n.Err() != nil || !n.Present() {
return json.Marshal(nil)
}
return n.Float32Common.MarshalJSON()
}
// Clone returns new instance of NullFloat32 with preserved value & error
func (n NullFloat32) Clone() Float32Accessor {
nv := &NullFloat32{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NFloat32 returns NullFloat32 under from float32
func NFloat32(value float32) Float32Accessor {
return &NullFloat32{Float32Common{P: &value}}
}
// NotNullFloat32 represents a float32 that may be null.
type NotNullFloat32 struct {
Float32Common
}
// Clone returns new instance of NotNullFloat32 with preserved value & error
func (n NotNullFloat32) Clone() Float32Accessor {
nv := &NotNullFloat32{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NNFloat32 returns NullFloat32 under Float32Accessor from float32
func NNFloat32(value float32) Float32Accessor {
return &NotNullFloat32{Float32Common{P: &value}}
}
// Float32Slice returns slice of float32 with filled values from slice of Float32Accessor
func Float32Slice(null []Float32Accessor, valid bool) []float32 {
slice := make([]float32, 0, len(null))
for _, v := range null {
if valid && v.Err() != nil {
continue
}
slice = append(slice, v.V())
}
return slice
}
// FloatCommon represents a float64 that may be null.
type FloatCommon struct {
P *float64
Error error
}
// Set saves value into current struct
func (n *FloatCommon) Set(value float64) {
n.P = &value
}
// V returns value of underlying type if it was set, otherwise default value
func (n FloatCommon) V() float64 {
if n.P == nil {
return 0
}
return *n.P
}
// Present determines whether a value has been set
func (n FloatCommon) Present() bool {
return n.P != nil
}
// Valid determines whether a value has been valid
func (n FloatCommon) Valid() bool {
return n.Err() == nil
}
// Value implements the sql driver Valuer interface.
func (n FloatCommon) Value() (driver.Value, error) {
return n.V(), nil
}
// Scan implements the sql Scanner interface.
func (n *FloatCommon) Scan(value interface{}) error {
n.P, n.Error = nil, nil
if value == nil {
return nil
}
v := Of(value).Float()
if v.Err() != nil {
n.Error = v.Err()
return v.Err()
}
n.Set(v.V())
n.Error = v.Err()
return v.Err()
}
// UnmarshalJSON implements the json Unmarshaler interface.
func (n *FloatCommon) 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 err
}
if uv == nil {
return nil
}
v, ok := uv.(float64)
if !ok {
n.Error = ErrConvert
return n.Err()
}
n.P = &v
return nil
}
// MarshalJSON implements the json Marshaler interface.
func (n FloatCommon) 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 FloatCommon) 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 FloatCommon) Err() error {
return n.Error
}
// FloatAccessor accessor of float64 type.
type FloatAccessor interface {
Common
V() float64
Set(value float64)
Clone() FloatAccessor
}
// NullFloat represents a float64 that may be null.
type NullFloat struct {
FloatCommon
}
// Value implements the sql driver Valuer interface.
func (n NullFloat) Value() (driver.Value, error) {
if n.Err() != nil || !n.Present() {
return nil, n.Err()
}
return n.FloatCommon.Value()
}
// MarshalJSON implements the json Marshaler interface.
func (n NullFloat) MarshalJSON() ([]byte, error) {
if n.Err() != nil || !n.Present() {
return json.Marshal(nil)
}
return n.FloatCommon.MarshalJSON()
}
// Clone returns new instance of NullFloat with preserved value & error
func (n NullFloat) Clone() FloatAccessor {
nv := &NullFloat{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NFloat returns NullFloat under FloatAccessor from float64
func NFloat(value float64) FloatAccessor {
return &NullFloat{FloatCommon{P: &value}}
}
// NotNullFloat represents a float64 that may be null.
type NotNullFloat struct {
FloatCommon
}
// Clone returns new instance of NotNullFloat with preserved value & error
func (n NotNullFloat) Clone() FloatAccessor {
nv := &NotNullFloat{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NNFloat returns NullFloat under FloatAccessor from float64
func NNFloat(value float64) FloatAccessor {
return &NotNullFloat{FloatCommon{P: &value}}
}
// FloatSlice returns slice of float64 with filled values from slice of FloatAccessor
func FloatSlice(null []FloatAccessor, valid bool) []float64 {
slice := make([]float64, 0, len(null))
for _, v := range null {
if valid && v.Err() != nil {
continue
}
slice = append(slice, v.V())
}
return slice
}