This repository has been archived by the owner on May 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
697 lines (609 loc) · 14 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
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
/**
* Date: 11/02/14
* Time: 7:17 PM
*/
package opal
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"reflect"
"strconv"
"time"
)
const (
PrimaryKey reflect.Kind = iota + reflect.UnsafePointer + 1
OpalTime
Embedded
DAO
)
var (
lockOpal bool
opal OpalMagic = opalMagic
)
// Borrowed from sql library
var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
// Allows a user to set their own id
// Can only be run once
// Initial Gem startup will run this to check
func SetMagic(pId *OpalMagic) {
if lockOpal {
return
}
lockOpal = true
if pId != nil {
opal = *pId
}
}
// ***************************************** Opal TYPE SYSTEM
// A simple type to return the global identifying opal
type OpalMagic int
// Interface Opal acts as a shoe horn for valid types in a model
type Opal interface {
opal() OpalMagic
Kind() reflect.Kind
}
// An embeddable struct with which to create your own types
type T struct{}
// T partially implements the opal.Opal interface
func (T) opal() OpalMagic {
return opal
}
// T partially implements the opal.Opal interface
func (T) Kind() reflect.Kind {
return Embedded
}
// ********************************************** SPECIAL TYPES
// Key is a special type of opal.Opal
type Key interface {
Opal
}
// ************************************************ STRING TYPE
// String represents an int64 that may be null.
// String implements the Scanner interface so
// It can be used as a scan destination, similar to sql.NullString.
type String struct {
Str *string
}
// String implements the opal.Opal interface
func (String) opal() OpalMagic {
return opal
}
// Convenience constructor for a String
func NewString(p string) String {
return String{&p}
}
// Convenience setting method
func (o *String) A(p string) {
o.Str = &p
}
// String implements the sql.Scanner interface.
func (o *String) Scan(pValue interface{}) error {
if pValue == nil {
return nil
}
o.Str = new(string)
switch s := pValue.(type) {
case string:
*o.Str = s
return nil
case *string:
o.Str = s
return nil
case []byte:
*o.Str = string(s)
return nil
}
return errors.New("Opal.String: invalid value to scan into String")
}
// String implements the driver Valuer interface.
func (o String) Value() (driver.Value, error) {
if o.Str == nil {
return nil, nil
}
return *o.Str, nil
}
// Returns the primitive type
func (o String) Kind() reflect.Kind {
return reflect.String
}
// Prints the value
func (o String) String() string {
if o.Str != nil {
return *o.Str
}
return fmt.Sprint(nil)
}
// ************************************************ Slice TYPE
// Slice implements the Scanner interface so
// It can be used as a scan destination, similar to sql.NullString.
type Slice struct {
Slice []byte
}
// Slice implements the opal.Opal interface
func (Slice) opal() OpalMagic {
return opal
}
// Convenience constructor for a Slice
func NewSlice(p []byte) Slice {
return Slice{p}
}
// Convenience setting method
func (o *Slice) A(p []byte) {
o.Slice = p
}
// Slice implements the sql.Scanner interface.
func (o *Slice) Scan(pValue interface{}) error {
if pValue == nil {
return nil
}
switch value := pValue.(type) {
case []byte:
o.Slice = make([]byte, len(value))
copy(o.Slice, value)
return nil
case *[]byte:
o.Slice = make([]byte, len(*value))
copy(o.Slice, *value)
return nil
}
// if value, ok := pValue.([]byte); ok {
// *o = make([]byte, len(value))
// copy(*o, value)
// return nil
// }
panic("Opal.String: invalid value to scan into String")
return nil
}
// Slice implements the driver Valuer interface.
func (o *Slice) Value() (driver.Value, error) {
return o.Slice, nil
}
// Returns the primitive type
func (o Slice) Kind() reflect.Kind {
return reflect.Slice
} // TODO reduce slice to itself to get rid of scan
// Prints the value
func (o Slice) String() string {
return string(o.Slice)
}
// ************************************************** INT64 TYPE
// Int64 represents an int64 that may be null.
// Int64 implements the Scanner interface so
// It can be used as a scan destination, similar to sql.NullString.
type Int64 struct {
Int64 *int64
}
type Type interface {
Scan(interface{}) error
Interface() interface{}
Value() (driver.Value, error)
String() string
}
// Int64 implements the opal.Opal interface
func (Int64) opal() OpalMagic {
return opal
}
// Convenience constructor for an Int64
func NewInt64(p int64) Int64 {
return Int64{&p}
}
// Convenience setting method
func (o *Int64) A(p int64) {
o.Int64 = &p
}
// Convenience setting method
func (o *Int64) Assign(p int64) {
o.A(p)
}
// Int64 implements the Scanner interface.
func (o *Int64) Scan(pValue interface{}) error {
if pValue == nil {
return nil
}
o.Int64 = new(int64)
switch value := pValue.(type) {
case int64:
*o.Int64 = value
return nil
case int:
*o.Int64 = int64(value)
return nil
case *int64:
v := int64(*value)
o.Int64 = &v
return nil
case *int:
v := int64(*value)
o.Int64 = &v
return nil
}
panic("Opal.Int64: invalid value to scan into Int64")
return nil
}
// Int64 implements the driver Valuer interface.
func (o Int64) Value() (driver.Value, error) {
return o.Interface(), nil
}
func (o Int64) Interface() interface{} {
if o.Int64 == nil {
return nil
}
return int64(*o.Int64)
}
// Returns the primitive value of Int64 if nil returns 0
// Same as Primitive()
func (o Int64) Primitive() int64 {
if v := o.Interface(); v != nil {
return v.(int64)
}
return 0 // TODO maybe return max instead
}
// Prints the value
func (o Int64) String() string {
return fmt.Sprint(o.Interface())
}
// Returns the primitive type
func (o Int64) Kind() reflect.Kind {
return reflect.Int64
} // TODO reduce slice to itself to get rid of scan
// Is an Int64 under the covers where its name flags its use
type AutoIncrement struct {
Int64
}
// Makes a new AutoIncrement
func NewAutoIncrement(p int64) *AutoIncrement {
return &AutoIncrement{NewInt64(p)}
}
// ************************************************ FLOAT64 TYPE
// Float64 represents an int64 that may be null.
// Float64 implements the Scanner interface so
// It can be used as a scan destination, similar to sql.NullString.
type Float64 struct {
Float64 *float64
}
// Float64 implements the opal.Opal interface
func (Float64) opal() OpalMagic {
return opal
}
// Convenience constructor for an Float64
func NewFloat64(p float64) Float64 {
return Float64{&p}
}
// Convenience setting method
func (o *Float64) A(p float64) {
o.Float64 = &p
}
// Float64 the Scanner interface.
func (o *Float64) Scan(pValue interface{}) error {
if pValue == nil {
return nil
}
o.Float64 = new(float64)
switch value := pValue.(type) {
case float64:
*o.Float64 = value
return nil
case *float64:
o.Float64 = value
return nil
}
panic("Opal.Float64: invalid value to scan into Float64")
return nil
}
// Float64 implements the driver Valuer interface.
func (o Float64) Value() (driver.Value, error) {
if o.Float64 == nil {
return nil, nil
}
return *o.Float64, nil
}
// Returns the primitive type
func (o Float64) Kind() reflect.Kind {
return reflect.Float64
}
// Prints the value
func (o Float64) String() string {
if o.Float64 != nil {
return fmt.Sprintf("%f", *o.Float64)
}
return fmt.Sprint(nil)
}
// ************************************************ Bool TYPE
// Bool represents an int64 that may be null.
// Bool implements the Scanner interface so
// It can be used as a scan destination, similar to sql.NullString.
type Bool struct {
Bool *bool
}
// Bool implements the opal.Opal interface
func (Bool) opal() OpalMagic {
return opal
}
// Convenience constructor for an Bool
func NewBool(p bool) Bool {
return Bool{&p}
}
// Convenience setting method
func (o *Bool) A(p bool) {
o.Bool = &p
}
// Bool implements the sql.Scanner interface.
func (o *Bool) Scan(pValue interface{}) error {
if pValue == nil {
return nil
}
o.Bool = new(bool)
switch value := pValue.(type) {
case bool:
*o.Bool = value
return nil
case *bool:
o.Bool = value
return nil
}
panic("Opal.Bool: invalid value to scan into")
return nil
}
// Bool implements the driver Valuer interface.
func (o Bool) Value() (driver.Value, error) {
if o.Bool == nil {
return nil, nil
}
return *o.Bool, nil
}
// Prints the value
func (o Bool) String() string {
if o.Bool != nil {
return fmt.Sprintf("%t", *o.Bool)
}
return fmt.Sprint(nil)
}
// Returns the primitive type
func (o Bool) Kind() reflect.Kind {
return reflect.Bool
} // TODO reduce slice to itself to get rid of scan
// ************************************************ Date TYPE
// Time represents an time.Time that may be null.
// Time implements the Scanner interface so
// It can be used as a scan destination, similar to sql.NullString.
type Time struct {
*time.Time
}
// Time implements the opal.Opal interface
func (Time) opal() OpalMagic {
return opal
}
// Convenience constructor for an Bool
func NewTime(p time.Time) Time {
return Time{&p}
}
// Convenience setting method
func (o *Time) A(p time.Time) {
o.Time = &p
}
// Time implements the sql.Scanner interface.
func (o *Time) Scan(pValue interface{}) error {
if pValue == nil {
return nil
}
o.Time = new(time.Time)
switch value := pValue.(type) {
case time.Time:
*o.Time = value
return nil
case *time.Time:
o.Time = value
return nil
}
// TODO better support for time as it can be string, bytes pointers...
panic("Opal.Time: invalid value to scan into Time")
return nil
}
// Time implements the driver Valuer interface.
func (o Time) Value() (driver.Value, error) {
if o.Time == nil {
return nil, nil
}
return *o.Time, nil
}
// Returns the primitive type
func (o Time) Kind() reflect.Kind {
return reflect.String
}
// Prints the value
func (o Time) String() string {
if o.Time != nil {
return o.Time.String()
}
return fmt.Sprint(nil)
}
// ************************************************
func ABool(p bool) *bool {
return &p
}
// convertAssign copies to dest the value in src, converting it if possible.
// An error is returned if the copy would result in loss of information.
// dest should be a pointer type.
func convertAssign(dest, src interface{}) error {
// Common cases, without reflect.
switch s := src.(type) {
case string:
switch d := dest.(type) {
case *string:
if d == nil {
return errNilPtr
}
*d = s
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = []byte(s)
return nil
}
case []byte:
switch d := dest.(type) {
case *string:
if d == nil {
return errNilPtr
}
*d = string(s)
return nil
case *interface{}:
if d == nil {
return errNilPtr
}
*d = cloneBytes(s)
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = cloneBytes(s)
return nil
case *sql.RawBytes:
if d == nil {
return errNilPtr
}
*d = s
return nil
}
case nil:
switch d := dest.(type) {
case *interface{}:
if d == nil {
return errNilPtr
}
*d = nil
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = nil
return nil
case *sql.RawBytes:
if d == nil {
return errNilPtr
}
*d = nil
return nil
}
}
var sv reflect.Value
switch d := dest.(type) {
case *string:
sv = reflect.ValueOf(src)
switch sv.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
*d = fmt.Sprintf("%v", src)
return nil
}
case *[]byte:
sv = reflect.ValueOf(src)
switch sv.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
*d = []byte(fmt.Sprintf("%v", src))
return nil
}
case *sql.RawBytes:
sv = reflect.ValueOf(src)
switch sv.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
*d = sql.RawBytes(fmt.Sprintf("%v", src))
return nil
}
case *bool:
bv, err := driver.Bool.ConvertValue(src)
if err == nil {
*d = bv.(bool)
}
return err
case *interface{}:
*d = src
return nil
}
if scanner, ok := dest.(sql.Scanner); ok {
return scanner.Scan(src)
}
dpv := reflect.ValueOf(dest)
if dpv.Kind() != reflect.Ptr {
return errors.New("destination not a pointer")
}
if dpv.IsNil() {
return errNilPtr
}
if !sv.IsValid() {
sv = reflect.ValueOf(src)
}
dv := reflect.Indirect(dpv)
if dv.Kind() == sv.Kind() {
dv.Set(sv)
return nil
}
switch dv.Kind() {
case reflect.Ptr:
if src == nil {
dv.Set(reflect.Zero(dv.Type()))
return nil
} else {
dv.Set(reflect.New(dv.Type().Elem()))
return convertAssign(dv.Interface(), src)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s := asString(src)
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
if err != nil {
return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
}
dv.SetInt(i64)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
s := asString(src)
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
if err != nil {
return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
}
dv.SetUint(u64)
return nil
case reflect.Float32, reflect.Float64:
s := asString(src)
f64, err := strconv.ParseFloat(s, dv.Type().Bits())
if err != nil {
return fmt.Errorf("converting string %q to a %s: %v", s, dv.Kind(), err)
}
dv.SetFloat(f64)
return nil
}
return fmt.Errorf("unsupported driver -> Scan pair: %T -> %T", src, dest)
}
func asString(src interface{}) string {
switch v := src.(type) {
case string:
return v
case []byte:
return string(v)
}
return fmt.Sprintf("%v", src)
}
func cloneBytes(b []byte) []byte {
if b == nil {
return nil
} else {
c := make([]byte, len(b))
copy(c, b)
return c
}
}