-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
types.go
560 lines (478 loc) · 13.2 KB
/
types.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package sqlxx
import (
"bytes"
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/tidwall/gjson"
"github.com/pkg/errors"
)
// Duration represents a JSON and SQL compatible time.Duration.
// swagger:type string
type Duration time.Duration
// MarshalJSON returns m as the JSON encoding of m.
func (ns Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(ns).String())
}
// UnmarshalJSON sets *m to a copy of data.
func (ns *Duration) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
p, err := time.ParseDuration(s)
if err != nil {
return err
}
*ns = Duration(p)
return nil
}
// StringSliceJSONFormat represents []string{} which is encoded to/from JSON for SQL storage.
type StringSliceJSONFormat []string
// Scan implements the Scanner interface.
func (m *StringSliceJSONFormat) Scan(value interface{}) error {
val := fmt.Sprintf("%s", value)
if len(val) == 0 {
val = "[]"
}
if parsed := gjson.Parse(val); parsed.Type == gjson.Null {
val = "[]"
} else if !parsed.IsArray() {
return errors.Errorf("expected JSON value to be an array but got type: %s", parsed.Type.String())
}
return errors.WithStack(json.Unmarshal([]byte(val), &m))
}
// Value implements the driver Valuer interface.
func (m StringSliceJSONFormat) Value() (driver.Value, error) {
if len(m) == 0 {
return "[]", nil
}
encoded, err := json.Marshal(&m)
return string(encoded), errors.WithStack(err)
}
// StringSlicePipeDelimiter de/encodes the string slice to/from a SQL string.
type StringSlicePipeDelimiter []string
// Scan implements the Scanner interface.
func (n *StringSlicePipeDelimiter) Scan(value interface{}) error {
var s sql.NullString
if err := s.Scan(value); err != nil {
return err
}
*n = scanStringSlice('|', s.String)
return nil
}
// Value implements the driver Valuer interface.
func (n StringSlicePipeDelimiter) Value() (driver.Value, error) {
return valueStringSlice('|', n), nil
}
func scanStringSlice(delimiter rune, value interface{}) []string {
escaped := false
s := fmt.Sprintf("%s", value)
splitted := strings.FieldsFunc(s, func(r rune) bool {
if r == '\\' {
escaped = !escaped
} else if escaped && r != delimiter {
escaped = false
}
return !escaped && r == delimiter
})
for k, v := range splitted {
splitted[k] = strings.ReplaceAll(v, "\\"+string(delimiter), string(delimiter))
}
return splitted
}
func valueStringSlice(delimiter rune, value []string) string {
replace := make([]string, len(value))
for k, v := range value {
replace[k] = strings.ReplaceAll(v, string(delimiter), "\\"+string(delimiter))
}
return strings.Join(replace, string(delimiter))
}
// NullBool represents a bool that may be null.
// NullBool implements the Scanner interface so
// swagger:type bool
// swagger:model nullBool
type NullBool struct {
Bool bool
Valid bool // Valid is true if Bool is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullBool) Scan(value interface{}) error {
var d = sql.NullBool{}
if err := d.Scan(value); err != nil {
return err
}
ns.Bool = d.Bool
ns.Valid = d.Valid
return nil
}
// Value implements the driver Valuer interface.
func (ns NullBool) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return ns.Bool, nil
}
// MarshalJSON returns m as the JSON encoding of m.
func (ns NullBool) MarshalJSON() ([]byte, error) {
if !ns.Valid {
return []byte("null"), nil
}
return json.Marshal(ns.Bool)
}
// UnmarshalJSON sets *m to a copy of data.
func (ns *NullBool) UnmarshalJSON(data []byte) error {
if ns == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
if len(data) == 0 || string(data) == "null" {
return nil
}
ns.Valid = true
return errors.WithStack(json.Unmarshal(data, &ns.Bool))
}
// FalsyNullBool represents a bool that may be null.
// It JSON decodes to false if null.
//
// swagger:type bool
// swagger:model falsyNullBool
type FalsyNullBool struct {
Bool bool
Valid bool // Valid is true if Bool is not NULL
}
// Scan implements the Scanner interface.
func (ns *FalsyNullBool) Scan(value interface{}) error {
var d = sql.NullBool{}
if err := d.Scan(value); err != nil {
return err
}
ns.Bool = d.Bool
ns.Valid = d.Valid
return nil
}
// Value implements the driver Valuer interface.
func (ns FalsyNullBool) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return ns.Bool, nil
}
// MarshalJSON returns m as the JSON encoding of m.
func (ns FalsyNullBool) MarshalJSON() ([]byte, error) {
if !ns.Valid {
return []byte("false"), nil
}
return json.Marshal(ns.Bool)
}
// UnmarshalJSON sets *m to a copy of data.
func (ns *FalsyNullBool) UnmarshalJSON(data []byte) error {
if ns == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
if len(data) == 0 || string(data) == "null" {
return nil
}
ns.Valid = true
return errors.WithStack(json.Unmarshal(data, &ns.Bool))
}
// swagger:type string
// swagger:model nullString
type NullString string
// MarshalJSON returns m as the JSON encoding of m.
func (ns NullString) MarshalJSON() ([]byte, error) {
return json.Marshal(string(ns))
}
// UnmarshalJSON sets *m to a copy of data.
func (ns *NullString) UnmarshalJSON(data []byte) error {
if ns == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
if len(data) == 0 {
return nil
}
return errors.WithStack(json.Unmarshal(data, (*string)(ns)))
}
// Scan implements the Scanner interface.
func (ns *NullString) Scan(value interface{}) error {
var v sql.NullString
if err := (&v).Scan(value); err != nil {
return err
}
*ns = NullString(v.String)
return nil
}
// Value implements the driver Valuer interface.
func (ns NullString) Value() (driver.Value, error) {
if len(ns) == 0 {
return sql.NullString{}.Value()
}
return sql.NullString{Valid: true, String: string(ns)}.Value()
}
// String implements the Stringer interface.
func (ns NullString) String() string {
return string(ns)
}
// NullTime implements sql.NullTime functionality.
//
// swagger:model nullTime
// required: false
type NullTime time.Time
// Scan implements the Scanner interface.
func (ns *NullTime) Scan(value interface{}) error {
var v sql.NullTime
if err := (&v).Scan(value); err != nil {
return err
}
*ns = NullTime(v.Time)
return nil
}
// MarshalJSON returns m as the JSON encoding of m.
func (ns NullTime) MarshalJSON() ([]byte, error) {
var t *time.Time
if !time.Time(ns).IsZero() {
tt := time.Time(ns)
t = &tt
}
return json.Marshal(t)
}
// UnmarshalJSON sets *m to a copy of data.
func (ns *NullTime) UnmarshalJSON(data []byte) error {
var t time.Time
if err := json.Unmarshal(data, &t); err != nil {
return err
}
*ns = NullTime(t)
return nil
}
// Value implements the driver Valuer interface.
func (ns NullTime) Value() (driver.Value, error) {
return sql.NullTime{Valid: !time.Time(ns).IsZero(), Time: time.Time(ns)}.Value()
}
// MapStringInterface represents a map[string]interface that works well with JSON, SQL, and Swagger.
type MapStringInterface map[string]interface{}
// Scan implements the Scanner interface.
func (n *MapStringInterface) Scan(value interface{}) error {
v := fmt.Sprintf("%s", value)
if len(v) == 0 {
return nil
}
return errors.WithStack(json.Unmarshal([]byte(v), n))
}
// Value implements the driver Valuer interface.
func (n MapStringInterface) Value() (driver.Value, error) {
value, err := json.Marshal(n)
if err != nil {
return nil, errors.WithStack(err)
}
return string(value), nil
}
// JSONArrayRawMessage represents a json.RawMessage which only accepts arrays that works well with JSON, SQL, and Swagger.
type JSONArrayRawMessage json.RawMessage
// Scan implements the Scanner interface.
func (m *JSONArrayRawMessage) Scan(value interface{}) error {
val := fmt.Sprintf("%s", value)
if len(val) == 0 {
val = "[]"
}
if parsed := gjson.Parse(val); parsed.Type == gjson.Null {
val = "[]"
} else if !parsed.IsArray() {
return errors.Errorf("expected JSON value to be an array but got type: %s", parsed.Type.String())
}
*m = []byte(val)
return nil
}
// Value implements the driver Valuer interface.
func (m JSONArrayRawMessage) Value() (driver.Value, error) {
if len(m) == 0 {
return "[]", nil
}
if parsed := gjson.ParseBytes(m); parsed.Type == gjson.Null {
return "[]", nil
} else if !parsed.IsArray() {
return nil, errors.Errorf("expected JSON value to be an array but got type: %s", parsed.Type.String())
}
return string(m), nil
}
// JSONRawMessage represents a json.RawMessage that works well with JSON, SQL, and Swagger.
type JSONRawMessage json.RawMessage
// Scan implements the Scanner interface.
func (m *JSONRawMessage) Scan(value interface{}) error {
*m = []byte(fmt.Sprintf("%s", value))
return nil
}
// Value implements the driver Valuer interface.
func (m JSONRawMessage) Value() (driver.Value, error) {
if len(m) == 0 {
return "null", nil
}
return string(m), nil
}
// MarshalJSON returns m as the JSON encoding of m.
func (m JSONRawMessage) MarshalJSON() ([]byte, error) {
if len(m) == 0 {
return []byte("null"), nil
}
return m, nil
}
// UnmarshalJSON sets *m to a copy of data.
func (m *JSONRawMessage) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
*m = append((*m)[0:0], data...)
return nil
}
// NullJSONRawMessage represents a json.RawMessage that works well with JSON, SQL, and Swagger and is NULLable-
//
// swagger:model nullJsonRawMessage
type NullJSONRawMessage json.RawMessage
// Scan implements the Scanner interface.
func (m *NullJSONRawMessage) Scan(value interface{}) error {
if value == nil {
value = "null"
}
*m = []byte(fmt.Sprintf("%s", value))
return nil
}
// Value implements the driver Valuer interface.
func (m NullJSONRawMessage) Value() (driver.Value, error) {
if len(m) == 0 {
return nil, nil
}
return string(m), nil
}
// MarshalJSON returns m as the JSON encoding of m.
func (m NullJSONRawMessage) MarshalJSON() ([]byte, error) {
if len(m) == 0 {
return []byte("null"), nil
}
return m, nil
}
// UnmarshalJSON sets *m to a copy of data.
func (m *NullJSONRawMessage) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
*m = append((*m)[0:0], data...)
return nil
}
// JSONScan is a generic helper for storing a value as a JSON blob in SQL.
func JSONScan(dst interface{}, value interface{}) error {
if value == nil {
value = "null"
}
if err := json.Unmarshal([]byte(fmt.Sprintf("%s", value)), &dst); err != nil {
return fmt.Errorf("unable to decode payload to: %s", err)
}
return nil
}
// JSONValue is a generic helper for retrieving a SQL JSON-encoded value.
func JSONValue(src interface{}) (driver.Value, error) {
if src == nil {
return nil, nil
}
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(&src); err != nil {
return nil, err
}
return b.String(), nil
}
// NullInt64 represents an int64 that may be null.
// swagger:model nullInt64
type NullInt64 struct {
Int int64
Valid bool // Valid is true if Duration is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullInt64) Scan(value interface{}) error {
var d = sql.NullInt64{}
if err := d.Scan(value); err != nil {
return err
}
ns.Int = d.Int64
ns.Valid = d.Valid
return nil
}
// Value implements the driver Valuer interface.
func (ns NullInt64) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return ns.Int, nil
}
// MarshalJSON returns m as the JSON encoding of m.
func (ns NullInt64) MarshalJSON() ([]byte, error) {
if !ns.Valid {
return []byte("null"), nil
}
return json.Marshal(ns.Int)
}
// UnmarshalJSON sets *m to a copy of data.
func (ns *NullInt64) UnmarshalJSON(data []byte) error {
if ns == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
if len(data) == 0 || string(data) == "null" {
return nil
}
ns.Valid = true
return errors.WithStack(json.Unmarshal(data, &ns.Int))
}
// NullDuration represents a nullable JSON and SQL compatible time.Duration.
//
// swagger:type string
// swagger:model nullDuration
type NullDuration struct {
Duration time.Duration
Valid bool
}
// Scan implements the Scanner interface.
func (ns *NullDuration) Scan(value interface{}) error {
var d = sql.NullInt64{}
if err := d.Scan(value); err != nil {
return err
}
ns.Duration = time.Duration(d.Int64)
ns.Valid = d.Valid
return nil
}
// Value implements the driver Valuer interface.
func (ns NullDuration) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return int64(ns.Duration), nil
}
// MarshalJSON returns m as the JSON encoding of m.
func (ns NullDuration) MarshalJSON() ([]byte, error) {
if !ns.Valid {
return []byte("null"), nil
}
return json.Marshal(ns.Duration.String())
}
// UnmarshalJSON sets *m to a copy of data.
func (ns *NullDuration) UnmarshalJSON(data []byte) error {
if ns == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
if len(data) == 0 || string(data) == "null" {
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
p, err := time.ParseDuration(s)
if err != nil {
return err
}
ns.Duration = p
ns.Valid = true
return nil
}