-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.go
110 lines (97 loc) · 2.49 KB
/
scan.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
package sqldb
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"reflect"
"time"
)
// ScanDriverValue scans a driver.Value into destPtr.
func ScanDriverValue(destPtr any, value driver.Value) error {
if destPtr == nil {
return errors.New("can't scan nil destPtr")
}
if destScanner, ok := destPtr.(sql.Scanner); ok {
return destScanner.Scan(value)
}
dest := reflect.ValueOf(destPtr)
if dest.Kind() != reflect.Ptr {
return fmt.Errorf("can't scan non-pointer %s", dest.Type())
}
dest = dest.Elem()
// destPtr is a pointer to interface{} type
if dest.Kind() == reflect.Interface {
if value != nil {
dest.Set(reflect.ValueOf(value)) // Assign any
} else {
dest.SetZero() // Set nil
}
return nil
}
switch src := value.(type) {
case int64:
switch dest.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
dest.SetInt(src)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
dest.SetUint(uint64(src))
return nil
case reflect.Float32, reflect.Float64:
dest.SetFloat(float64(src))
return nil
}
case float64:
switch dest.Kind() {
case reflect.Float32, reflect.Float64:
dest.SetFloat(src)
return nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
dest.SetInt(int64(src))
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
dest.SetUint(uint64(src))
return nil
}
case bool:
if dest.Kind() == reflect.Bool {
reflect.ValueOf(destPtr).SetBool(src)
return nil
}
case []byte:
switch {
case dest.Kind() == reflect.String:
dest.SetString(string(src))
return nil
case dest.Kind() == reflect.Slice && dest.Type().Elem().Kind() == reflect.Uint8:
dest.SetBytes(append([]byte(nil), src...)) // Make copy because src will be invalid after call
return nil
}
case string:
switch {
case dest.Kind() == reflect.String:
dest.SetString(src)
return nil
case dest.Kind() == reflect.Slice && dest.Type().Elem().Kind() == reflect.Uint8:
dest.SetBytes([]byte(src))
return nil
}
case time.Time:
if s := reflect.ValueOf(value); s.Type().AssignableTo(dest.Type()) {
dest.Set(s)
return nil
}
case nil:
if d, ok := destPtr.(interface{ SetNull() }); ok {
d.SetNull()
return nil
}
switch dest.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Map:
dest.SetZero()
return nil
}
}
return fmt.Errorf("can't scan %#v as %T", value, destPtr)
}