/
data_type.go
904 lines (806 loc) · 20.9 KB
/
data_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
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
package grest
import (
"bytes"
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
var nullBytes = []byte("null")
// NullBool is a nullable bool.
// - It will marshal to null if null, not false.
// - It will unmarshal to true if input value is true, "true", "True", "TRUE", "t", "T", 1, or "1"
// - It will unmarshal to false if input value is false, "false", "False", "FALSE", "f", "F", 0, or "0"
// - Other input value will be considered null, not false and not error.
// - It supports SQL and JSON serialization.
type NullBool struct {
sql.NullBool
}
// Set sets the value for a NullBool.
func (n *NullBool) Set(val bool) {
n.Valid = true
n.Bool = val
}
// Val returns the value of a NullBool.
func (n *NullBool) Val() bool {
return n.Bool
}
// Scan implements the Scanner interface.
func (n *NullBool) Scan(value any) error {
if value == nil {
n.Bool, n.Valid = false, false
return nil
}
n.Valid = true
nb := sql.NullBool{}
err := nb.Scan(value)
if err == nil {
n.Bool = nb.Bool
return nil
}
ni32 := sql.NullInt32{}
err = ni32.Scan(value)
if err == nil && ni32.Int32 == 1 {
n.Bool = true
return nil
}
ni64 := sql.NullInt64{}
err = ni64.Scan(value)
if err == nil && ni64.Int64 == 1 {
n.Bool = true
return nil
}
ns := sql.NullString{}
err = ns.Scan(value)
if err == nil && (ns.String == "1" || ns.String == "t" || ns.String == "T" || ns.String == "true" || ns.String == "True" || ns.String == "TRUE") {
n.Bool = true
}
return nil
}
// Value implements the driver Valuer interface.
func (n NullBool) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
if n.Bool {
return "1", nil
}
return "0", nil
}
// MarshalText implements encoding.TextMarshaler.
// - It will encode blank if this NullBool is null, not false.
func (n NullBool) MarshalText() ([]byte, error) {
if !n.Valid {
return []byte{}, nil
}
if !n.Bool {
return []byte("false"), nil
}
return []byte("true"), nil
}
// MarshalJSON implements json.Marshaler.
// - It will encode null if this Bool is null, not false.
func (n NullBool) MarshalJSON() ([]byte, error) {
if !n.Valid {
return nullBytes, nil
}
return json.Marshal(n.Bool)
}
// UnmarshalText implements encoding.TextUnmarshaler.
// - It will unmarshal to true if input value is true, "true", "True", "TRUE", "t", "T", 1, or "1"
// - It will unmarshal to false if input value is false, "false", "False", "FALSE", "f", "F", 0, or "0"
// - Other input value will be considered null, not false.
func (n *NullBool) UnmarshalText(text []byte) error {
str := string(text)
switch str {
case "1", "t", "T", "true", "TRUE", "True":
n.Bool = true
case "0", "f", "F", "false", "FALSE", "False":
n.Bool = false
default:
n.Valid = false
return nil
}
n.Valid = true
return nil
}
// UnmarshalJSON implements json.Unmarshaler.
// - It will unmarshal to true if input value is true, "true", "True", "TRUE", "t", "T", 1, or "1"
// - It will unmarshal to false if input value is false, "false", "False", "FALSE", "f", "F", 0, or "0"
// - Other input value will be considered null, not false and not error.
func (n *NullBool) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullBytes) {
n.Valid = false
return nil
}
if err := json.Unmarshal(data, &n.Bool); err == nil {
n.Valid = true
} else {
var str string
if err := json.Unmarshal(data, &str); err == nil {
n.Bool, err = strconv.ParseBool(str)
if err == nil {
n.Valid = true
}
} else {
var integer int
if err := json.Unmarshal(data, &integer); err == nil {
if integer == 1 {
n.Bool = true
}
if integer == 0 || integer == 1 {
n.Valid = true
}
}
}
}
return nil
}
// IsZero returns true for invalid bool, for omitempty support
func (n NullBool) IsZero() bool {
return !n.Valid
}
// NullInt64 is a nullable int64.
// - It supports integer number and a string that can be converted to a integer number.
// - Other input value will be considered null, not 0 and not error.
// - It supports SQL and JSON serialization.
type NullInt64 struct {
sql.NullInt64
}
// Set sets the value for a NullInt64.
func (n *NullInt64) Set(val int64) {
n.Valid = true
n.Int64 = val
}
// Val returns the value of a NullInt64.
func (n *NullInt64) Val() int64 {
return n.Int64
}
// MarshalText implements encoding.TextMarshaler.
// - It will encode a blank string if this NullInt64 is null.
func (n NullInt64) MarshalText() ([]byte, error) {
if !n.Valid {
return []byte{}, nil
}
return []byte(strconv.FormatInt(n.Int64, 10)), nil
}
// MarshalJSON implements json.Marshaler.
// - It will encode null if this NullInt64 is null.
func (n NullInt64) MarshalJSON() ([]byte, error) {
if !n.Valid {
return nullBytes, nil
}
return json.Marshal(n.Int64)
}
// UnmarshalText implements encoding.TextUnmarshaler.
// - It supports integer number and a string that can be converted to a integer number.
// - Other input value will be considered null, not 0 and not error.
func (n *NullInt64) UnmarshalText(text []byte) error {
str := string(text)
if str == "" || str == "null" {
return nil
}
var err error
n.Int64, err = strconv.ParseInt(str, 10, 64)
if err == nil {
n.Valid = true
}
return nil
}
// UnmarshalJSON implements json.Unmarshaler.
// - It supports integer number and a string that can be converted to a integer number.
// - Other input value will be considered null, not 0 and not error.
func (n *NullInt64) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullBytes) {
return nil
}
if err := json.Unmarshal(data, &n.Int64); err == nil {
n.Valid = true
} else {
var str string
if err := json.Unmarshal(data, &str); err == nil {
n.Int64, err = strconv.ParseInt(str, 10, 64)
if err == nil {
n.Valid = true
}
}
}
return nil
}
// IsZero returns true for invalid int64, for omitempty support
func (n NullInt64) IsZero() bool {
return !n.Valid
}
// NullFloat64 is a nullable float64.
// - It supports number and a string that can be converted to a number.
// - Other input value will be considered null, not 0 and not error.
// - It supports SQL and JSON serialization.
type NullFloat64 struct {
sql.NullFloat64
}
// Set sets the value for a NullFloat64.
func (n *NullFloat64) Set(val float64) {
n.Valid = true
n.Float64 = val
}
// Val returns the value of a NullFloat64.
func (n *NullFloat64) Val() float64 {
return n.Float64
}
// MarshalText implements encoding.TextMarshaler.
// - It will encode a blank string if this NullFloat64 is null.
func (n NullFloat64) MarshalText() ([]byte, error) {
if !n.Valid {
return []byte{}, nil
}
return []byte(strconv.FormatFloat(n.Float64, 'f', -1, 64)), nil
}
// MarshalJSON implements json.Marshaler.
// - It will encode null if this NullFloat64 is null.
func (n NullFloat64) MarshalJSON() ([]byte, error) {
if !n.Valid {
return nullBytes, nil
}
return json.Marshal(n.Float64)
}
// UnmarshalText implements encoding.TextUnmarshaler.
// - It supports number and a string that can be converted to a number.
// - Other input value will be considered null, not 0 and not error.
func (n *NullFloat64) UnmarshalText(text []byte) error {
str := string(text)
if str == "" || str == "null" {
return nil
}
var err error
n.Float64, err = strconv.ParseFloat(str, 64)
if err == nil {
n.Valid = true
}
return nil
}
// UnmarshalJSON implements json.Unmarshaler.
// - It supports number and a string that can be converted to a number.
// - Other input value will be considered null, not 0 and not error.
func (n *NullFloat64) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullBytes) {
return nil
}
if err := json.Unmarshal(data, &n.Float64); err == nil {
n.Valid = true
} else {
var str string
if err := json.Unmarshal(data, &str); err == nil {
n.Float64, err = strconv.ParseFloat(str, 64)
if err == nil {
n.Valid = true
}
}
}
return nil
}
// IsZero returns true for invalid float64, for omitempty support
func (n NullFloat64) IsZero() bool {
return !n.Valid
}
// NullString is a nullable string.
// - It supports SQL and JSON serialization.
type NullString struct {
sql.NullString
}
// Set sets the value for a NullString.
func (n *NullString) Set(val string) {
n.Valid = true
n.String = val
}
// Val returns the value of a NullString.
func (n *NullString) Val() string {
return n.String
}
// MarshalText implements encoding.TextMarshaler.
// - It will encode a blank string if this NullString is null.
func (n NullString) MarshalText() ([]byte, error) {
if !n.Valid {
return []byte{}, nil
}
return []byte(n.String), nil
}
// MarshalJSON implements json.Marshaler.
// - It will encode null if this NullString is null.
func (n NullString) MarshalJSON() ([]byte, error) {
if !n.Valid {
return nullBytes, nil
}
return json.Marshal(n.String)
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (n *NullString) UnmarshalText(text []byte) error {
str := string(text)
if str == "null" {
return nil
}
n.String = str
n.Valid = true
return nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (n *NullString) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullBytes) {
return nil
}
if err := json.Unmarshal(data, &n.String); err == nil {
n.Valid = true
}
return nil
}
// IsZero returns true for invalid string, for omitempty support
func (n NullString) IsZero() bool {
return !n.Valid
}
// NullDateTime is a nullable time.Time.
// - It supports SQL and JSON serialization.
type NullDateTime struct {
sql.NullTime
}
// Set sets the value for a NullDateTime.
func (n *NullDateTime) Set(val ...time.Time) {
n.Valid = true
if len(val) > 0 {
n.Time = val[0]
} else {
n.Time = time.Now()
}
}
// Val returns the value of a NullDateTime.
func (n *NullDateTime) Val() time.Time {
return n.Time
}
// MarshalJSON implements json.Marshaler.
// - It will encode null if this NullDateTime is null.
func (n NullDateTime) MarshalJSON() ([]byte, error) {
if !n.Valid {
return nullBytes, nil
}
return json.Marshal(n.Time)
}
// UnmarshalJSON implements json.Unmarshaler.
// - It supports a string that can be parsed to a time.Time.
// - Other input value will be considered null, not error.
func (n *NullDateTime) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullBytes) {
n.Valid = false
return nil
}
if err := json.Unmarshal(data, &n.Time); err == nil {
n.Valid = true
}
return nil
}
// IsZero returns true for zero time, for omitempty support
func (n NullDateTime) IsZero() bool {
return n.Time.IsZero()
}
// NullUnixTime is a nullable date.
// - It supports SQL and JSON serialization.
type NullUnixTime struct {
NullDateTime
}
// MarshalJSON implements json.Marshaler.
// - It will encode null if this NullUnixTime is null.
func (n NullUnixTime) MarshalJSON() ([]byte, error) {
if !n.Valid {
return nullBytes, nil
}
return json.Marshal(n.Time.Unix())
}
// UnmarshalJSON implements json.Unmarshaler.
// - It supports a string that can be parsed to a time.Time.
// - Other input value will be considered null, not error.
func (n *NullUnixTime) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullBytes) {
n.Valid = false
return nil
}
val := int64(0)
if err := json.Unmarshal(data, &val); err == nil {
n.Time = time.Unix(val, 0)
n.Valid = val > 0
}
return nil
}
// GormDataType returns gorm common data type. This type is used for the field's column type.
func (NullUnixTime) GormDataType() string {
return "INTEGER"
}
// GormDBDataType returns gorm DB data type based on the current using database.
func (NullUnixTime) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "sqlite":
return "INTEGER"
case "mysql":
return "BIGINT"
case "postgres":
return "bigint"
case "sqlserver":
return "INTEGER"
case "firebird":
return "INTEGER"
default:
return "INTEGER"
}
}
// Scan implements sql.Scanner interface and scans value into Date
func (n *NullUnixTime) Scan(value any) error {
if value == nil {
return nil
}
switch v := value.(type) {
case time.Time:
n.Time, n.Valid = v, true
case int64:
n.Time = time.Unix(v, 0)
n.Valid = v > 0
default:
return NewError(http.StatusInternalServerError, fmt.Sprintf("failed to scan value: %v", v))
}
return nil
}
// Value implements the driver Valuer interface.
func (n NullUnixTime) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Time.Unix(), nil
}
// NullDate is a nullable date.
// - It supports SQL and JSON serialization.
type NullDate struct {
NullDateTime
}
// MarshalJSON implements json.Marshaler.
// - It will encode null if this NullDate is null.
func (n NullDate) MarshalJSON() ([]byte, error) {
if !n.Valid {
return nullBytes, nil
}
return json.Marshal(n.Time.Format("2006-01-02"))
}
// UnmarshalJSON implements json.Unmarshaler.
// - It supports a string that can be parsed to a time.Time.
// - Other input value will be considered null, not error.
func (n *NullDate) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullBytes) {
n.Valid = false
return nil
}
if err := json.Unmarshal(data, &n.Time); err == nil {
n.Valid = true
return nil
}
val := ""
if err := json.Unmarshal(data, &val); err == nil {
n.Time, err = time.Parse("2006-01-02", val)
if err == nil {
n.Valid = true
}
}
return nil
}
// GormDataType returns gorm common data type. This type is used for the field's column type.
func (NullDate) GormDataType() string {
return "date"
}
// GormDBDataType returns gorm DB data type based on the current using database.
func (NullDate) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "sqlite":
return "TEXT"
case "mysql":
return "DATE"
case "postgres":
return "DATE"
case "sqlserver":
return "DATE"
case "firebird":
return "DATE"
default:
return "DATE"
}
}
// Scan implements sql.Scanner interface and scans value into Date
func (n *NullDate) Scan(value any) error {
if value == nil {
return nil
}
switch v := value.(type) {
case time.Time:
n.Time, n.Valid = v, true
case string:
t, err := time.Parse("2006-01-02", v)
n.Time, n.Valid = t, err == nil
case []byte:
t, err := time.Parse("2006-01-02", string(v))
n.Time, n.Valid = t, err == nil
default:
return NewError(http.StatusInternalServerError, fmt.Sprintf("failed to scan value: %v", v))
}
return nil
}
// Value implements the driver Valuer interface.
func (n NullDate) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Time.Format("2006-01-02"), nil
}
// NullTime is a nullable time.
// - It supports SQL and JSON serialization.
type NullTime struct {
NullString
}
// Set sets the value for a NullTime.
func (n *NullTime) Set(val string) {
n.Valid = true
n.String = val
}
// Val returns the value of a NullTime.
func (n *NullTime) Val() string {
return n.String
}
// UnmarshalJSON implements json.Unmarshaler.
// - It supports a string that can be parsed to a time.Time.
// - Other input value will be considered null, not error.
func (n *NullTime) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullBytes) {
n.Valid = false
return nil
}
if err := json.Unmarshal(data, &n.String); err == nil {
_, err := time.Parse("15:04:05", n.String)
if err == nil {
n.Valid = true
}
}
return nil
}
// IsZero returns true for zero time, for omitempty support
func (n NullTime) IsZero() bool {
return !n.Valid
}
// GormDataType returns gorm common data type. This type is used for the field's column type.
func (NullTime) GormDataType() string {
return "time"
}
// GormDBDataType returns gorm DB data type based on the current using database.
func (NullTime) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "sqlite":
return "TEXT"
case "mysql":
return "TIME"
case "postgres":
return "TIME"
case "sqlserver":
return "TIME"
case "firebird":
return "TIME"
default:
return "TIME"
}
}
// Scan implements sql.Scanner interface and scans value into Time
func (t *NullTime) Scan(value any) error {
if value == nil {
t.String, t.Valid = "", false
return nil
}
switch v := value.(type) {
case []byte:
t.String, t.Valid = string(v), true
case string:
t.String, t.Valid = v, true
case time.Time:
t.String, t.Valid = v.Format("15:04:05"), true
default:
return NewError(http.StatusInternalServerError, fmt.Sprintf("failed to scan value: %v", v))
}
return nil
}
// NullText represents a nullable text value.
type NullText struct {
NullString
}
// Set sets the value for a NullText.
func (n *NullText) Set(val string) {
n.Valid = true
n.String = val
}
// Val returns the value of a NullText.
func (n *NullText) Val() string {
return n.String
}
// GormDataType returns gorm common data type. This type is used for the field's column type.
func (NullText) GormDataType() string {
return "text"
}
// GormDBDataType returns gorm DB data type based on the current using database.
func (NullText) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "sqlite":
return "TEXT"
case "mysql":
return "TEXT"
case "postgres":
return "TEXT"
case "sqlserver":
return "NVARCHAR(MAX)"
case "firebird":
return "BLOB SUB_TYPE TEXT"
default:
return "TEXT"
}
}
// NullJSON represents a nullable JSON value.
// - It supports SQL and JSON serialization.
type NullJSON struct {
String string `json:"-"`
Valid bool `json:"-"`
Data any
}
// Set sets the value for a NullJSON.
func (n *NullJSON) Set(val any) {
b, err := json.Marshal(val)
if err == nil {
n.String = string(b)
n.Valid = true
}
}
// Val returns the value of a NullJSON.
func (n *NullJSON) Val() string {
return n.String
}
// MarshalJSON implements json.Marshaler.
func (n NullJSON) MarshalJSON() ([]byte, error) {
if !n.Valid {
return nullBytes, nil
}
return json.Marshal(n.Data)
}
// UnmarshalJSON implements json.Unmarshaler.
func (n *NullJSON) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, nullBytes) {
return nil
}
if err := json.Unmarshal(data, &n.Data); err == nil {
n.Valid = true
n.String = string(data)
}
return nil
}
// Scan implements the Scanner interface.
func (n *NullJSON) Scan(value any) error {
ns := sql.NullString{}
err := ns.Scan(value)
n.String = ns.String
n.Valid = ns.Valid
if n.Valid {
json.Unmarshal([]byte(n.String), &n.Data)
}
return err
}
// Value implements the driver Valuer interface.
func (n NullJSON) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.String, nil
}
// IsZero returns true for invalid string, for omitempty support
func (n NullJSON) IsZero() bool {
return !n.Valid
}
// GormDataType returns gorm common data type. This type is used for the field's column type.
func (NullJSON) GormDataType() string {
return "json"
}
// GormDBDataType returns gorm DB data type based on the current using database.
func (NullJSON) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "sqlite":
return "JSON"
case "mysql":
return "JSON"
case "postgres":
return "JSONB"
case "sqlserver":
return "NVARCHAR(MAX)"
case "firebird":
return "BLOB SUB_TYPE TEXT"
default:
return "TEXT"
}
}
// isNullJSON checks if a reflect type is a NullJSON type.
func isNullJSON(t reflect.Type) bool {
if t.Kind() == reflect.Ptr {
return isNullJSON(t.Elem())
}
if strings.HasSuffix(t.Name(), "NullJSON") {
return true
}
if t.Kind() == reflect.Struct {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldType := field.Type
if fieldType.Kind() == reflect.Struct {
if fieldType.Name() == "NullJSON" {
return true
} else {
return isNullJSON(fieldType)
}
}
}
}
return false
}
// NullUUID represents a nullable UUID value.
type NullUUID struct {
NullString
}
// Set sets the value for a NullUUID.
func (n *NullUUID) Set(val ...string) {
n.Valid = true
if len(val) > 0 {
n.String = val[0]
} else {
n.String = uuid.NewString()
}
}
// Val returns the value of a NullUUID.
func (n *NullUUID) Val() string {
return n.String
}
// GormDataType returns gorm common data type. This type is used for the field's column type.
func (NullUUID) GormDataType() string {
return "char(36)"
}
// GormDBDataType returns gorm DB data type based on the current using database.
func (NullUUID) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Dialector.Name() {
case "postgres":
return "uuid"
default:
return "char(36)"
}
}
// MapSlice of map items.
type MapSlice []map[string]any
// MarshalJSON for map slice.
func (ms MapSlice) MarshalJSON() ([]byte, error) {
buf := &bytes.Buffer{}
buf.Write([]byte{'{'})
for i, mi := range ms {
key, keyOK := mi["key"]
value, valueOK := mi["value"]
if keyOK && valueOK {
b, err := json.Marshal(value)
if err != nil {
return nil, err
}
buf.WriteString(fmt.Sprintf("%q:", fmt.Sprintf("%v", key)))
buf.Write(b)
if i < len(ms)-1 {
buf.Write([]byte{','})
}
}
}
buf.Write([]byte{'}'})
return buf.Bytes(), nil
}