forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_value.go
223 lines (199 loc) · 4.8 KB
/
decode_value.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
package pg
import (
"database/sql"
"encoding/json"
"reflect"
"strconv"
"time"
"gopkg.in/pg.v3/pgutil"
)
var (
timePtrType = reflect.TypeOf((*time.Time)(nil))
timeType = timePtrType.Elem()
)
type valueDecoder func(reflect.Value, []byte) error
var valueDecoders []valueDecoder
func init() {
valueDecoders = []valueDecoder{
reflect.Bool: decodeBoolValue,
reflect.Int: decodeIntValue,
reflect.Int8: decodeIntValue,
reflect.Int16: decodeIntValue,
reflect.Int32: decodeIntValue,
reflect.Int64: decodeIntValue,
reflect.Uint: decodeUintValue,
reflect.Uint8: decodeUintValue,
reflect.Uint16: decodeUintValue,
reflect.Uint32: decodeUintValue,
reflect.Uint64: decodeUintValue,
reflect.Uintptr: nil,
reflect.Float32: decodeFloatValue,
reflect.Float64: decodeFloatValue,
reflect.Complex64: nil,
reflect.Complex128: nil,
reflect.Array: nil,
reflect.Chan: nil,
reflect.Func: nil,
reflect.Interface: decodeInterfaceValue,
reflect.Map: decodeMapValue,
reflect.Ptr: decodePtrValue,
reflect.Slice: decodeSliceValue,
reflect.String: decodeStringValue,
reflect.Struct: decodeStructValue,
reflect.UnsafePointer: nil,
}
}
func DecodeValue(v reflect.Value, b []byte) error {
if !v.IsValid() {
return errorf("pg: Decode(nil)")
}
if b == nil {
return decodeNullValue(v)
}
decoder := getDecoder(v.Type())
if decoder != nil {
return decoder(v, b)
}
if v.Kind() == reflect.Interface {
return errorf("pg: Decode(nil)")
}
return errorf("pg: Decode(unsupported %s)", v.Type())
}
func decodeBoolValue(v reflect.Value, b []byte) error {
if !v.CanSet() {
return errorf("pg: Decode(nonsettable %s)", v.Type())
}
v.SetBool(len(b) == 1 && b[0] == 't')
return nil
}
func decodeIntValue(v reflect.Value, b []byte) error {
n, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
v.SetInt(n)
return nil
}
func decodeUintValue(v reflect.Value, b []byte) error {
n, err := strconv.ParseUint(string(b), 10, 64)
if err != nil {
return err
}
v.SetUint(n)
return nil
}
func decodeFloatValue(v reflect.Value, b []byte) error {
n, err := strconv.ParseFloat(string(b), 64)
if err != nil {
return err
}
v.SetFloat(n)
return nil
}
func decodeStringValue(v reflect.Value, b []byte) error {
v.SetString(string(b))
return nil
}
func decodeStructValue(v reflect.Value, b []byte) error {
return json.Unmarshal(b, v.Addr().Interface())
}
func decodeTimeValue(v reflect.Value, b []byte) error {
tm, err := pgutil.ParseTime(b)
if err != nil {
return err
}
v.Set(reflect.ValueOf(tm))
return nil
}
func decodePtrValue(v reflect.Value, b []byte) error {
if v.IsNil() {
if !v.CanSet() {
return errorf("pg: Decode(nonsettable %s)", v.Type())
}
vv := reflect.New(v.Type().Elem())
v.Set(vv)
}
return DecodeValue(v.Elem(), b)
}
func decodeSliceValue(v reflect.Value, b []byte) error {
elemType := v.Type().Elem()
switch elemType.Kind() {
case reflect.Uint8:
bs, err := decodeBytes(b)
if err != nil {
return err
}
v.SetBytes(bs)
return nil
case reflect.String:
slice, err := decodeStringSlice(b)
if err != nil {
return err
}
v.Set(reflect.ValueOf(slice))
return nil
case reflect.Int:
slice, err := decodeIntSlice(b)
if err != nil {
return err
}
v.Set(reflect.ValueOf(slice))
return nil
case reflect.Int64:
slice, err := decodeInt64Slice(b)
if err != nil {
return err
}
v.Set(reflect.ValueOf(slice))
return nil
case reflect.Float64:
slice, err := decodeFloat64Slice(b)
if err != nil {
return err
}
v.Set(reflect.ValueOf(slice))
return nil
}
return errorf("pg: Decode(unsupported %s)", v.Type())
}
func decodeInterfaceValue(v reflect.Value, b []byte) error {
if v.IsNil() {
return errorf("pg: Decode(nil)")
}
return DecodeValue(v.Elem(), b)
}
func decodeMapValue(v reflect.Value, b []byte) error {
typ := v.Type()
if typ.Key().Kind() == reflect.String && typ.Elem().Kind() == reflect.String {
m, err := decodeStringStringMap(b)
if err != nil {
return err
}
v.Set(reflect.ValueOf(m))
return nil
}
return errorf("pg: Decode(unsupported %s)", v.Type())
}
func decodeNullValue(v reflect.Value) error {
kind := v.Kind()
if kind == reflect.Interface {
return decodeNullValue(v.Elem())
}
if v.CanSet() {
v.Set(reflect.Zero(v.Type()))
return nil
}
if kind == reflect.Ptr {
return decodeNullValue(v.Elem())
}
return nil
}
func decodeScannerValue(v reflect.Value, b []byte) error {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
return decodeScanner(v.Interface().(sql.Scanner), b)
}
func decodeScannerAddrValue(v reflect.Value, b []byte) error {
return decodeScanner(v.Addr().Interface().(sql.Scanner), b)
}