-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patharray_types.go
More file actions
541 lines (461 loc) · 16.9 KB
/
array_types.go
File metadata and controls
541 lines (461 loc) · 16.9 KB
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
// Copyright 2025 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gorm
import (
"database/sql/driver"
"fmt"
"time"
"cloud.google.com/go/civil"
"cloud.google.com/go/spanner"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
// NullStringArray is a named type for storing string arrays in Spanner.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
// ARRAY<STRING> is by default mapped to []spanner.NullString in the Spanner
// database/sql driver. This is because Spanner always allows arrays to contain
// null elements, even if the column itself is defined as NOT NULL.
type NullStringArray []spanner.NullString
func (a NullStringArray) Value() (driver.Value, error) {
return []spanner.NullString(a), nil
}
func (a NullStringArray) GormDataType() string {
return "ARRAY<STRING(MAX)>"
}
func (a NullStringArray) GormDBDataType(_ *gorm.DB, field *schema.Field) string {
if field.Size > 0 {
return fmt.Sprintf("ARRAY<STRING(%v)>", field.Size)
}
return "ARRAY<STRING(MAX)>"
}
// StringArray is a named type for storing string arrays in Spanner.
// This type cannot contain any NULL elements.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
type StringArray []string
//goland:noinspection GoMixedReceiverTypes
func (a StringArray) Value() (driver.Value, error) {
return []string(a), nil
}
//goland:noinspection GoMixedReceiverTypes
func (a *StringArray) Scan(v any) error {
if val, ok := v.([]string); ok {
*a = val
return nil
}
if val, ok := v.([]spanner.NullString); ok {
*a = make([]string, len(val))
for i, b := range val {
if !b.Valid {
return fmt.Errorf("index %d of string array contains a null value", i)
}
(*a)[i] = b.StringVal
}
return nil
}
return fmt.Errorf("invalid value for StringArray: %v", v)
}
//goland:noinspection GoMixedReceiverTypes
func (a StringArray) GormDataType() string {
return "ARRAY<STRING(MAX)>"
}
//goland:noinspection GoMixedReceiverTypes
func (a StringArray) GormDBDataType(_ *gorm.DB, field *schema.Field) string {
if field.Size > 0 {
return fmt.Sprintf("ARRAY<STRING(%v)>", field.Size)
}
return "ARRAY<STRING(MAX)>"
}
// NullBoolArray is a named type for storing bool arrays in Spanner.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
// ARRAY<BOOL> is by default mapped to []spanner.NullBool in the Spanner
// database/sql driver. This is because Spanner always allows arrays to contain
// null elements, even if the column itself is defined as NOT NULL.
type NullBoolArray []spanner.NullBool
func (a NullBoolArray) Value() (driver.Value, error) {
return []spanner.NullBool(a), nil
}
func (a NullBoolArray) GormDataType() string {
return "ARRAY<BOOL>"
}
func (a NullBoolArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<BOOL>"
}
// BoolArray is a named type for storing bool arrays in Spanner.
// This type cannot contain any NULL elements.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
type BoolArray []bool
//goland:noinspection GoMixedReceiverTypes
func (a BoolArray) Value() (driver.Value, error) {
return []bool(a), nil
}
//goland:noinspection GoMixedReceiverTypes
func (a *BoolArray) Scan(v any) error {
if val, ok := v.([]bool); ok {
*a = val
return nil
}
if val, ok := v.([]spanner.NullBool); ok {
*a = make([]bool, len(val))
for i, b := range val {
if !b.Valid {
return fmt.Errorf("index %d of bool array contains a null value", i)
}
(*a)[i] = b.Bool
}
return nil
}
return fmt.Errorf("invalid value for BoolArray: %v", v)
}
//goland:noinspection GoMixedReceiverTypes
func (a BoolArray) GormDataType() string {
return "ARRAY<BOOL>"
}
//goland:noinspection GoMixedReceiverTypes
func (a BoolArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<BOOL>"
}
// BytesArray is a named type for storing bytes arrays in Spanner.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
type BytesArray [][]byte
//goland:noinspection GoMixedReceiverTypes
func (a BytesArray) Value() (driver.Value, error) {
return [][]byte(a), nil
}
//goland:noinspection GoMixedReceiverTypes
func (a *BytesArray) Scan(v any) error {
if val, ok := v.([][]byte); ok {
*a = val
return nil
}
return fmt.Errorf("invalid value for BytesArray: %v", v)
}
//goland:noinspection GoMixedReceiverTypes
func (a BytesArray) GormDataType() string {
return "ARRAY<BYTES>"
}
//goland:noinspection GoMixedReceiverTypes
func (a BytesArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<BYTES>"
}
// NullBytesArray is a synonym for BytesArray. It is only defined for consistency
// with the other array data types.
type NullBytesArray BytesArray
//goland:noinspection GoMixedReceiverTypes
func (a NullBytesArray) Value() (driver.Value, error) {
return BytesArray(a).Value()
}
//goland:noinspection GoMixedReceiverTypes
func (a *NullBytesArray) Scan(v any) error {
return (*BytesArray)(a).Scan(v)
}
//goland:noinspection GoMixedReceiverTypes
func (a NullBytesArray) GormDataType() string {
return BytesArray(a).GormDataType()
}
//goland:noinspection GoMixedReceiverTypes
func (a NullBytesArray) GormDBDataType(db *gorm.DB, field *schema.Field) string {
return BytesArray(a).GormDBDataType(db, field)
}
// NullInt64Array is a named type for storing int64 arrays in Spanner.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
// ARRAY<INT64> is by default mapped to []spanner.NullInt64 in the Spanner
// database/sql driver. This is because Spanner always allows arrays to contain
// null elements, even if the column itself is defined as NOT NULL.
type NullInt64Array []spanner.NullInt64
func (a NullInt64Array) Value() (driver.Value, error) {
return []spanner.NullInt64(a), nil
}
func (a NullInt64Array) GormDataType() string {
return "ARRAY<INT64>"
}
func (a NullInt64Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<INT64>"
}
// Int64Array is a named type for storing int64 arrays in Spanner.
// This type cannot contain any NULL elements.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
type Int64Array []int64
//goland:noinspection GoMixedReceiverTypes
func (a Int64Array) Value() (driver.Value, error) {
return []int64(a), nil
}
//goland:noinspection GoMixedReceiverTypes
func (a *Int64Array) Scan(v any) error {
if val, ok := v.([]int64); ok {
*a = val
return nil
}
if val, ok := v.([]spanner.NullInt64); ok {
*a = make([]int64, len(val))
for i, b := range val {
if !b.Valid {
return fmt.Errorf("index %d of int64 array contains a null value", i)
}
(*a)[i] = b.Int64
}
return nil
}
return fmt.Errorf("invalid value for Int64Array: %v", v)
}
//goland:noinspection GoMixedReceiverTypes
func (a Int64Array) GormDataType() string {
return "ARRAY<INT64>"
}
//goland:noinspection GoMixedReceiverTypes
func (a Int64Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<INT64>"
}
// NullFloat32Array is a named type for storing float32 arrays in Spanner.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
// ARRAY<FLOAT32> is by default mapped to []spanner.NullFloat32 in the Spanner
// database/sql driver. This is because Spanner always allows arrays to contain
// null elements, even if the column itself is defined as NOT NULL.
type NullFloat32Array []spanner.NullFloat32
func (a NullFloat32Array) Value() (driver.Value, error) {
return []spanner.NullFloat32(a), nil
}
func (a NullFloat32Array) GormDataType() string {
return "ARRAY<FLOAT32>"
}
func (a NullFloat32Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<FLOAT32>"
}
// Float32Array is a named type for storing float32 arrays in Spanner.
// This type cannot contain any NULL elements.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
type Float32Array []float32
//goland:noinspection GoMixedReceiverTypes
func (a Float32Array) Value() (driver.Value, error) {
return []float32(a), nil
}
//goland:noinspection GoMixedReceiverTypes
func (a *Float32Array) Scan(v any) error {
if val, ok := v.([]float32); ok {
*a = val
return nil
}
if val, ok := v.([]spanner.NullFloat32); ok {
*a = make([]float32, len(val))
for i, b := range val {
if !b.Valid {
return fmt.Errorf("index %d of float32 array contains a null value", i)
}
(*a)[i] = b.Float32
}
return nil
}
return fmt.Errorf("invalid value for Float32Array: %v", v)
}
//goland:noinspection GoMixedReceiverTypes
func (a Float32Array) GormDataType() string {
return "ARRAY<FLOAT32>"
}
//goland:noinspection GoMixedReceiverTypes
func (a Float32Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<FLOAT32>"
}
// NullFloat64Array is a named type for storing float64 arrays in Spanner.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
// ARRAY<FLOAT64> is by default mapped to []spanner.NullFloat64 in the Spanner
// database/sql driver. This is because Spanner always allows arrays to contain
// null elements, even if the column itself is defined as NOT NULL.
type NullFloat64Array []spanner.NullFloat64
func (a NullFloat64Array) Value() (driver.Value, error) {
return []spanner.NullFloat64(a), nil
}
func (a NullFloat64Array) GormDataType() string {
return "ARRAY<FLOAT64>"
}
func (a NullFloat64Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<FLOAT64>"
}
// Float64Array is a named type for storing float64 arrays in Spanner.
// This type cannot contain any NULL elements.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
type Float64Array []float64
//goland:noinspection GoMixedReceiverTypes
func (a Float64Array) Value() (driver.Value, error) {
return []float64(a), nil
}
//goland:noinspection GoMixedReceiverTypes
func (a *Float64Array) Scan(v any) error {
if val, ok := v.([]float64); ok {
*a = val
return nil
}
if val, ok := v.([]spanner.NullFloat64); ok {
*a = make([]float64, len(val))
for i, b := range val {
if !b.Valid {
return fmt.Errorf("index %d of float64 array contains a null value", i)
}
(*a)[i] = b.Float64
}
return nil
}
return fmt.Errorf("invalid value for FLoat64Array: %v", v)
}
//goland:noinspection GoMixedReceiverTypes
func (a Float64Array) GormDataType() string {
return "ARRAY<FLOAT64>"
}
//goland:noinspection GoMixedReceiverTypes
func (a Float64Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<FLOAT64>"
}
// NullDateArray is a named type for storing date arrays in Spanner.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
// ARRAY<DATE> is by default mapped to []spanner.NullDate in the Spanner
// database/sql driver. This is because Spanner always allows arrays to contain
// null elements, even if the column itself is defined as NOT NULL.
type NullDateArray []spanner.NullDate
func (a NullDateArray) Value() (driver.Value, error) {
return []spanner.NullDate(a), nil
}
func (a NullDateArray) GormDataType() string {
return "ARRAY<DATE>"
}
func (a NullDateArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<DATE>"
}
// DateArray is a named type for storing date arrays in Spanner.
// This type cannot contain any NULL elements.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
type DateArray []civil.Date
//goland:noinspection GoMixedReceiverTypes
func (a DateArray) Value() (driver.Value, error) {
return []civil.Date(a), nil
}
//goland:noinspection GoMixedReceiverTypes
func (a *DateArray) Scan(v any) error {
if val, ok := v.([]civil.Date); ok {
*a = val
return nil
}
if val, ok := v.([]spanner.NullDate); ok {
*a = make([]civil.Date, len(val))
for i, b := range val {
if !b.Valid {
return fmt.Errorf("index %d of date array contains a null value", i)
}
(*a)[i] = b.Date
}
return nil
}
return fmt.Errorf("invalid value for DateArray: %v", v)
}
//goland:noinspection GoMixedReceiverTypes
func (a DateArray) GormDataType() string {
return "ARRAY<DATE>"
}
//goland:noinspection GoMixedReceiverTypes
func (a DateArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<DATE>"
}
// NullTimeArray is a named type for storing timestamp arrays in Spanner.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
// ARRAY<TIMESTAMP> is by default mapped to []spanner.NullTime in the Spanner
// database/sql driver. This is because Spanner always allows arrays to contain
// null elements, even if the column itself is defined as NOT NULL.
type NullTimeArray []spanner.NullTime
func (a NullTimeArray) Value() (driver.Value, error) {
return []spanner.NullTime(a), nil
}
func (a NullTimeArray) GormDataType() string {
return "ARRAY<TIMESTAMP>"
}
func (a NullTimeArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<TIMESTAMP>"
}
// TimeArray is a named type for storing date arrays in Spanner.
// This type cannot contain any NULL elements.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
type TimeArray []time.Time
//goland:noinspection GoMixedReceiverTypes
func (a TimeArray) Value() (driver.Value, error) {
return []time.Time(a), nil
}
//goland:noinspection GoMixedReceiverTypes
func (a *TimeArray) Scan(v any) error {
if val, ok := v.([]time.Time); ok {
*a = val
return nil
}
if val, ok := v.([]spanner.NullTime); ok {
*a = make([]time.Time, len(val))
for i, b := range val {
if !b.Valid {
return fmt.Errorf("index %d of time array contains a null value", i)
}
(*a)[i] = b.Time
}
return nil
}
return fmt.Errorf("invalid value for TimeArray: %v", v)
}
//goland:noinspection GoMixedReceiverTypes
func (a TimeArray) GormDataType() string {
return "ARRAY<TIMESTAMP>"
}
//goland:noinspection GoMixedReceiverTypes
func (a TimeArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<TIMESTAMP>"
}
// NullJSONArray is a named type for storing JSON arrays in Spanner.
// We must use a named type for this to implement the driver.Valuer interface.
// This is required, because gorm otherwise translates arrays/slices to
// literals in the form `(item1, item2, ..., itemN)`.
// ARRAY<JSON> is by default mapped to []spanner.NullJSON in the Spanner
// database/sql driver. This is because Spanner always allows arrays to contain
// null elements, even if the column itself is defined as NOT NULL.
type NullJSONArray []spanner.NullJSON
func (a NullJSONArray) Value() (driver.Value, error) {
return []spanner.NullJSON(a), nil
}
func (a NullJSONArray) GormDataType() string {
return "ARRAY<JSON>"
}
func (a NullJSONArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string {
return "ARRAY<JSON>"
}