-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathload.go
158 lines (132 loc) · 3.39 KB
/
load.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
package dbr
import (
"database/sql"
"fmt"
"reflect"
)
// Load loads any value from sql.Rows
func Load(rows *sql.Rows, value interface{}) (int, error) {
defer rows.Close()
column, err := rows.Columns()
if err != nil {
return 0, err
}
v := reflect.ValueOf(value)
if v.Kind() != reflect.Ptr || v.IsNil() {
return 0, ErrInvalidPointer
}
v = v.Elem()
isSlice := v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8
elem := v
elemType := elem.Type()
if isSlice {
elemType = elemType.Elem()
elem = reflect.New(elemType).Elem()
}
extractor, err := findExtractor(elemType)
if err != nil {
return 0, err
}
ptrs := extractor(column, elem)
count := 0
for rows.Next() {
if err = rows.Scan(ptrs...); err != nil {
return count, err
}
count++
if !isSlice {
break
}
if elemType.Kind() == reflect.Ptr {
elemCopy := reflect.New(elemType.Elem()).Elem()
elemCopy.Set(elem.Elem())
v.Set(reflect.Append(v, elemCopy.Addr()))
} else {
v.Set(reflect.Append(v, elem))
}
}
return count, rows.Err()
}
type dummyScanner struct{}
func (dummyScanner) Scan(interface{}) error {
return nil
}
type keyValueMap map[string]interface{}
type kvScanner struct {
column string
m keyValueMap
}
func (kv *kvScanner) Scan(v interface{}) error {
if b, ok := v.([]byte); ok {
tmp := make([]byte, len(b))
copy(tmp, b)
kv.m[kv.column] = tmp
} else {
// int64, float64, bool, string, time.Time, nil
kv.m[kv.column] = v
}
return nil
}
type pointersExtractor func(columns []string, value reflect.Value) []interface{}
var (
dummyDest sql.Scanner = dummyScanner{}
typeScanner = reflect.TypeOf((*sql.Scanner)(nil)).Elem()
typeKeyValueMap = reflect.TypeOf(keyValueMap(nil))
)
func getStructFieldsExtractor(t reflect.Type) pointersExtractor {
mapping := structMap(t)
return func(columns []string, value reflect.Value) []interface{} {
var ptr []interface{}
for _, key := range columns {
if index, ok := mapping[key]; ok {
ptr = append(ptr, value.FieldByIndex(index).Addr().Interface())
} else {
ptr = append(ptr, dummyDest)
}
}
return ptr
}
}
func getIndirectExtractor(extractor pointersExtractor) pointersExtractor {
return func(columns []string, value reflect.Value) []interface{} {
if value.IsNil() {
value.Set(reflect.New(value.Type().Elem()))
}
return extractor(columns, value.Elem())
}
}
func mapExtractor(columns []string, value reflect.Value) []interface{} {
if value.IsNil() {
value.Set(reflect.MakeMap(value.Type()))
}
m := value.Convert(typeKeyValueMap).Interface().(keyValueMap)
var ptr = make([]interface{}, 0, len(columns))
for _, c := range columns {
ptr = append(ptr, &kvScanner{column: c, m: m})
}
return ptr
}
func dummyExtractor(columns []string, value reflect.Value) []interface{} {
return []interface{}{value.Addr().Interface()}
}
func findExtractor(t reflect.Type) (pointersExtractor, error) {
if reflect.PtrTo(t).Implements(typeScanner) {
return dummyExtractor, nil
}
switch t.Kind() {
case reflect.Map:
if !t.ConvertibleTo(typeKeyValueMap) {
return nil, fmt.Errorf("expected %v, got %v", typeKeyValueMap, t)
}
return mapExtractor, nil
case reflect.Ptr:
inner, err := findExtractor(t.Elem())
if err != nil {
return nil, err
}
return getIndirectExtractor(inner), nil
case reflect.Struct:
return getStructFieldsExtractor(t), nil
}
return dummyExtractor, nil
}