scan.go
6.1 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
package urlstruct
import (
"database/sql"
"encoding"
"fmt"
"reflect"
"strconv"
"time"
)
var (
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
timeType = reflect.TypeOf((*time.Time)(nil)).Elem()
durationType = reflect.TypeOf((*time.Duration)(nil)).Elem()
nullBoolType = reflect.TypeOf((*sql.NullBool)(nil)).Elem()
nullInt64Type = reflect.TypeOf((*sql.NullInt64)(nil)).Elem()
nullFloat64Type = reflect.TypeOf((*sql.NullFloat64)(nil)).Elem()
nullStringType = reflect.TypeOf((*sql.NullString)(nil)).Elem()
mapStringStringType = reflect.TypeOf((*map[string]string)(nil)).Elem()
)
type scannerFunc func(v reflect.Value, values []string) error
func scanner(typ reflect.Type) scannerFunc {
if typ == timeType {
return scanTime
}
if typ.Implements(textUnmarshalerType) {
return scanTextUnmarshaler
}
if reflect.PtrTo(typ).Implements(textUnmarshalerType) {
return scanTextUnmarshalerAddr
}
switch typ {
case durationType:
return scanDuration
case nullBoolType:
return scanNullBool
case nullInt64Type:
return scanNullInt64
case nullFloat64Type:
return scanNullFloat64
case nullStringType:
return scanNullString
case mapStringStringType:
return scanMapStringString
}
switch typ.Kind() {
case reflect.Bool:
return scanBool
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return scanInt64
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return scanUint64
case reflect.Float32:
return scanFloat32
case reflect.Float64:
return scanFloat64
case reflect.String:
return scanString
}
return nil
}
func sliceScanner(typ reflect.Type) scannerFunc {
switch typ.Elem().Kind() {
case reflect.Int:
return scanIntSlice
case reflect.Int32:
return scanInt32Slice
case reflect.Int64:
return scanInt64Slice
case reflect.String:
return scanStringSlice
}
return nil
}
func scanTextUnmarshaler(v reflect.Value, values []string) error {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
u := v.Interface().(encoding.TextUnmarshaler)
return u.UnmarshalText([]byte(values[0]))
}
func scanTextUnmarshalerAddr(v reflect.Value, values []string) error {
if !v.CanAddr() {
return fmt.Errorf("pg: Scan(nonsettable %s)", v.Type())
}
u := v.Addr().Interface().(encoding.TextUnmarshaler)
return u.UnmarshalText([]byte(values[0]))
}
func scanBool(v reflect.Value, values []string) error {
f, err := strconv.ParseBool(values[0])
if err != nil {
return err
}
v.SetBool(f)
return nil
}
func scanInt64(v reflect.Value, values []string) error {
n, err := strconv.ParseInt(values[0], 10, 64)
if err != nil {
return err
}
v.SetInt(n)
return nil
}
func scanUint64(v reflect.Value, values []string) error {
n, err := strconv.ParseUint(values[0], 10, 64)
if err != nil {
return err
}
v.SetUint(n)
return nil
}
func scanFloat32(v reflect.Value, values []string) error {
return scanFloat(v, values, 32)
}
func scanFloat64(v reflect.Value, values []string) error {
return scanFloat(v, values, 64)
}
func scanFloat(v reflect.Value, values []string, bits int) error {
n, err := strconv.ParseFloat(values[0], bits)
if err != nil {
return err
}
v.SetFloat(n)
return nil
}
func scanString(v reflect.Value, values []string) error {
v.SetString(values[0])
return nil
}
func scanTime(v reflect.Value, values []string) error {
tm, err := parseTime(values[0])
if err != nil {
return err
}
v.Set(reflect.ValueOf(tm))
return nil
}
func parseTime(s string) (time.Time, error) {
n, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return time.Unix(n, 0), nil
}
return time.Parse(time.RFC3339Nano, s)
}
func scanDuration(v reflect.Value, values []string) error {
dur, err := time.ParseDuration(values[0])
if err != nil {
return err
}
v.SetInt(int64(dur))
return nil
}
func scanNullBool(v reflect.Value, values []string) error {
value := sql.NullBool{
Valid: true,
}
s := values[0]
if s == "" {
v.Set(reflect.ValueOf(value))
return nil
}
f, err := strconv.ParseBool(s)
if err != nil {
return err
}
value.Bool = f
v.Set(reflect.ValueOf(value))
return nil
}
func scanNullInt64(v reflect.Value, values []string) error {
value := sql.NullInt64{
Valid: true,
}
s := values[0]
if s == "" {
v.Set(reflect.ValueOf(value))
return nil
}
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
value.Int64 = n
v.Set(reflect.ValueOf(value))
return nil
}
func scanNullFloat64(v reflect.Value, values []string) error {
value := sql.NullFloat64{
Valid: true,
}
s := values[0]
if s == "" {
v.Set(reflect.ValueOf(value))
return nil
}
n, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
value.Float64 = n
v.Set(reflect.ValueOf(value))
return nil
}
func scanNullString(v reflect.Value, values []string) error {
value := sql.NullString{
Valid: true,
}
s := values[0]
if s == "" {
v.Set(reflect.ValueOf(value))
return nil
}
value.String = s
v.Set(reflect.ValueOf(value))
return nil
}
func scanMapStringString(v reflect.Value, values []string) error {
if len(values)%2 != 0 {
return nil
}
m := make(map[string]string)
for i := 0; i < len(values); i += 2 {
m[values[i]] = values[i+1]
}
v.Set(reflect.ValueOf(m))
return nil
}
func scanIntSlice(v reflect.Value, values []string) error {
nn := make([]int, 0, len(values))
for _, s := range values {
n, err := strconv.Atoi(s)
if err != nil {
return err
}
nn = append(nn, n)
}
v.Set(reflect.ValueOf(nn))
return nil
}
func scanInt32Slice(v reflect.Value, values []string) error {
nn := make([]int32, 0, len(values))
for _, s := range values {
n, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return err
}
nn = append(nn, int32(n))
}
v.Set(reflect.ValueOf(nn))
return nil
}
func scanInt64Slice(v reflect.Value, values []string) error {
nn := make([]int64, 0, len(values))
for _, s := range values {
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
nn = append(nn, n)
}
v.Set(reflect.ValueOf(nn))
return nil
}
func scanStringSlice(v reflect.Value, values []string) error {
v.Set(reflect.ValueOf(values))
return nil
}