-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnull_complex.go
349 lines (306 loc) · 7.91 KB
/
null_complex.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
342
343
344
345
346
347
348
349
package typ
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
// ComplexCommon represents a complex128 with pointer and error.
type ComplexCommon struct {
P *complex128
Error error
}
// Set saves value into current struct
func (n *ComplexCommon) Set(value complex128) {
n.P = &value
}
// V returns value of underlying type if it was set, otherwise default value
func (n ComplexCommon) V() complex128 {
if n.P == nil {
return 0
}
return *n.P
}
// Present determines whether a value has been set
func (n ComplexCommon) Present() bool {
return n.P != nil
}
// Valid determines whether a value has been valid
func (n ComplexCommon) Valid() bool {
return n.Err() == nil
}
// Value implements the sql driver Valuer interface.
func (n ComplexCommon) Value() (driver.Value, error) {
nv := ComplexFloat64(n.V())
return nv.V(), nv.Err()
}
// Scan implements the sql Scanner interface.
func (n *ComplexCommon) Scan(value interface{}) error {
n.P, n.Error = nil, nil
if value == nil {
return nil
}
v := Of(value).Complex()
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 *ComplexCommon) 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.(string)
if !ok {
n.Error = ErrConvert
return n.Err()
}
vCmplx := StringComplex(v)
if !vCmplx.Valid() {
n.Error = ErrConvert
return n.Err()
}
n.Set(vCmplx.V())
return n.Err()
}
// MarshalJSON implements the json Marshaler interface.
func (n ComplexCommon) MarshalJSON() ([]byte, error) {
return json.Marshal(fmt.Sprintf("%v", n.V()))
}
// Typ returns new instance with himself value.
// If current value is invalid, nil *Type returned
func (n ComplexCommon) 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 ComplexCommon) Err() error {
return n.Error
}
// ComplexAccessor accessor of complex128 type.
type ComplexAccessor interface {
Common
V() complex128
Set(value complex128)
Clone() ComplexAccessor
}
// NullComplex represents a complex128 that may be null.
type NullComplex struct {
ComplexCommon
}
// Value implements the sql driver Valuer interface.
func (n NullComplex) Value() (driver.Value, error) {
if n.Err() != nil || !n.Present() {
return nil, n.Err()
}
return n.ComplexCommon.Value()
}
// MarshalJSON implements the json Marshaler interface.
func (n NullComplex) MarshalJSON() ([]byte, error) {
if n.Err() != nil || !n.Present() {
return json.Marshal(nil)
}
return n.ComplexCommon.MarshalJSON()
}
// Clone returns new instance of NullComplex with preserved value & error
func (n NullComplex) Clone() ComplexAccessor {
nv := &NullComplex{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NComplex returns NullComplex under ComplexAccessor from complex128
func NComplex(value complex128) ComplexAccessor {
return &NullComplex{ComplexCommon{P: &value}}
}
// NotNullComplex represents a complex128 that may be null.
type NotNullComplex struct {
ComplexCommon
}
// Clone returns new instance of NotNullComplex with preserved value & error
func (n NotNullComplex) Clone() ComplexAccessor {
nv := &NotNullComplex{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NNComplex returns NotNullComplex under ComplexAccessor from complex128
func NNComplex(value complex128) ComplexAccessor {
return &NotNullComplex{ComplexCommon{P: &value}}
}
// ComplexSlice returns slice of complex128 with filled values from slice of ComplexAccessor
func ComplexSlice(null []ComplexAccessor, valid bool) []complex128 {
slice := make([]complex128, 0, len(null))
for _, v := range null {
if valid && v.Err() != nil {
continue
}
slice = append(slice, v.V())
}
return slice
}
// Complex64Common represents a complex64 with pointer and error.
type Complex64Common struct {
P *complex64
Error error
}
// Set saves value into current struct
func (n *Complex64Common) Set(value complex64) {
n.P = &value
}
// V returns value of underlying type if it was set, otherwise default value
func (n Complex64Common) V() complex64 {
if n.P == nil {
return 0
}
return *n.P
}
// Present determines whether a value has been set
func (n Complex64Common) Present() bool {
return n.P != nil
}
// Valid determines whether a value has been valid
func (n Complex64Common) Valid() bool {
return n.Err() == nil
}
// Value implements the sql driver Valuer interface.
func (n Complex64Common) Value() (driver.Value, error) {
nv := Complex64Float64(n.V())
return nv.V(), nv.Err()
}
// Scan implements the sql Scanner interface.
func (n *Complex64Common) Scan(value interface{}) error {
n.P, n.Error = nil, nil
if value == nil {
return nil
}
v := Of(value).Complex64()
if v.Err() != nil {
n.Error = v.Err()
return v.Err()
}
n.Set(v.V())
n.Error = v.Err()
return nil
}
// UnmarshalJSON implements the json Unmarshaler interface.
func (n *Complex64Common) 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.(string)
if !ok {
n.Error = ErrConvert
return n.Err()
}
vCmplx := StringComplex64(v)
if !vCmplx.Valid() {
n.Error = ErrConvert
return n.Err()
}
n.Set(vCmplx.V())
return n.Err()
}
// MarshalJSON implements the json Marshaler interface.
func (n Complex64Common) MarshalJSON() ([]byte, error) {
return json.Marshal(fmt.Sprintf("%v", n.V()))
}
// Typ returns new instance with himself value.
// If current value is invalid, nil *Type returned
func (n Complex64Common) 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 Complex64Common) Err() error {
return n.Error
}
// Complex64Accessor accessor of complex64 type.
type Complex64Accessor interface {
Common
V() complex64
Set(value complex64)
Clone() Complex64Accessor
}
// NullComplex64 represents a complex64 that may be null.
type NullComplex64 struct {
Complex64Common
}
// Value implements the sql driver Valuer interface.
func (n NullComplex64) Value() (driver.Value, error) {
if n.Err() != nil || !n.Present() {
return nil, n.Err()
}
return n.Complex64Common.Value()
}
// MarshalJSON implements the json Marshaler interface.
func (n NullComplex64) MarshalJSON() ([]byte, error) {
if n.Err() != nil || !n.Present() {
return json.Marshal(nil)
}
return n.Complex64Common.MarshalJSON()
}
// Clone returns new instance of NullComplex64 with preserved value & error
func (n NullComplex64) Clone() Complex64Accessor {
nv := &NullComplex64{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NComplex64 returns NullComplex64 under Complex64Accessor from complex64
func NComplex64(value complex64) Complex64Accessor {
return &NullComplex64{Complex64Common{P: &value}}
}
// NotNullComplex64 represents a complex64 that may be null.
type NotNullComplex64 struct {
Complex64Common
}
// Clone returns new instance of NotNullComplex64 with preserved value & error
func (n NotNullComplex64) Clone() Complex64Accessor {
nv := &NotNullComplex64{}
if n.Present() {
nv.Set(n.V())
}
nv.Error = n.Error
return nv
}
// NNComplex64 returns NotNullComplex64 under Complex64Accessor from complex64
func NNComplex64(value complex64) Complex64Accessor {
return &NotNullComplex64{Complex64Common{P: &value}}
}
// Complex64Slice returns slice of complex64 with filled values from slice of Complex64Accessor
func Complex64Slice(null []Complex64Accessor, valid bool) []complex64 {
slice := make([]complex64, 0, len(null))
for _, v := range null {
if valid && v.Err() != nil {
continue
}
slice = append(slice, v.V())
}
return slice
}