forked from eaigner/jet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapper.go
241 lines (221 loc) · 5.94 KB
/
mapper.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
package jet
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
type mapper struct {
conv ColumnConverter
}
func (m *mapper) unpack(keys []string, values []interface{}, out interface{}) error {
val := reflect.ValueOf(out)
if val.Kind() != reflect.Ptr {
return fmt.Errorf("cannot unpack result to non-pointer (%s)", val.Type().String())
}
return m.unpackValue(keys, values, val)
}
func isNil(val interface{}) bool {
if val == nil {
return true
}
if reflect.ValueOf(val).IsZero() {
return true
}
if reflect.ValueOf(val).Kind() == reflect.Ptr {
if reflect.ValueOf(val).Elem().Kind() == reflect.Struct || reflect.ValueOf(val).Elem().Kind() == reflect.Interface {
return reflect.ValueOf(val).Elem().IsNil()
}
}
return false
}
func (m *mapper) unpackValue(keys []string, values []interface{}, out reflect.Value) error {
switch out.Interface().(type) {
case ComplexValue:
if isNil(values[0]) {
if out.IsZero() {
return nil
}
if out.CanSet() {
out.Set(reflect.Zero(out.Type()))
return nil
}
}
if out.IsNil() {
out.Set(reflect.New(out.Type().Elem()))
}
plain := reflect.Indirect(reflect.ValueOf(values[0]))
return out.Interface().(ComplexValue).Decode(plain.Interface())
}
if out.CanAddr() {
switch out.Addr().Interface().(type) {
case ComplexValue:
return m.unpackValue(keys, values, out.Addr())
}
}
switch out.Kind() {
case reflect.Ptr:
if out.IsNil() {
out.Set(reflect.New(out.Type().Elem()))
}
return m.unpackValue(keys, values, reflect.Indirect(out))
case reflect.Slice:
if keys == nil {
return m.unpackSimple(nil, values, out)
} else {
return m.unpackSlice(keys, values, out)
}
case reflect.Struct:
return m.unpackStruct(keys, values, out)
case reflect.Map:
if keys == nil {
return m.unpackSimple(nil, values, out)
} else {
return m.unpackMap(keys, values, out)
}
default:
return m.unpackSimple(nil, values, out)
}
return fmt.Errorf("cannot unpack result to %T (%s)", out, out.Kind())
}
func (m *mapper) unpackSlice(keys []string, values []interface{}, out reflect.Value) error {
elemTyp := reflect.Indirect(reflect.New(out.Type().Elem()))
m.unpackValue(keys, values, elemTyp)
out.Set(reflect.Append(out, elemTyp))
return nil
}
func (m *mapper) unpackStruct(keys []string, values []interface{}, out reflect.Value) error {
// If no keys are passed in it's probably a struct field requiring
// a simple unpack like a time.Time struct field.
if len(keys) == 0 {
return m.unpackSimple(nil, values, out)
}
for i, k := range keys {
var convKey string
if m.conv == nil {
convKey = strings.ToUpper(k[:1]) + k[1:]
} else if m.conv != nil {
convKey = m.conv.ColumnToFieldName(k)
}
field := out.FieldByName(convKey)
// If the field is not found it can mean that we don't want it or that
// we have special case like UserID, UUID, userUUID
// So fix the name and try again
if !field.IsValid() {
convKey = strings.Replace(convKey, "Uuid", "UUID", -1)
convKey = strings.Replace(convKey, "Id", "ID", -1)
convKey = strings.Replace(convKey, "Ip", "IP", -1)
convKey = strings.Replace(convKey, "Url", "URL", -1)
field = out.FieldByName(convKey)
}
if field.IsValid() {
m.unpackValue(nil, values[i:i+1], field)
}
}
return nil
}
func (m *mapper) unpackMap(keys []string, values []interface{}, out reflect.Value) error {
if out.IsNil() {
out.Set(reflect.MakeMap(out.Type()))
}
for i, k := range keys {
elemTyp := reflect.Indirect(reflect.New(out.Type().Elem()))
m.unpackValue(nil, values[i:i+1], elemTyp)
out.SetMapIndex(reflect.ValueOf(k), elemTyp)
}
return nil
}
func (m *mapper) unpackSimple(keys []string, values []interface{}, out reflect.Value) error {
if !out.IsValid() {
panic("cannot unpack to zero value")
}
if len(values) != 1 {
panic("cannot unpack to simple value, invalid values input")
}
setValue(reflect.Indirect(reflect.ValueOf(values[0])), out)
return nil
}
func convertAndSet(f interface{}, to reflect.Value) {
from := reflect.ValueOf(f)
if from.IsValid() {
to.Set(from.Convert(to.Type()))
} else {
to.Set(reflect.Zero(to.Type()))
}
}
func setValue(from, to reflect.Value) {
switch t := from.Interface().(type) {
case []uint8:
setValueFromBytes(t, to)
case int, int8, int16, int32, int64:
setValueFromInt(reflect.ValueOf(t).Int(), to)
case uint, uint8, uint16, uint32, uint64:
setValueFromUint(reflect.ValueOf(t).Uint(), to)
case float32, float64:
setValueFromFloat(reflect.ValueOf(t).Float(), to)
case time.Time:
setValueFromTime(t, to)
default:
convertAndSet(t, to)
}
}
func setValueFromBytes(t []uint8, to reflect.Value) {
switch to.Interface().(type) {
case bool:
n, _ := strconv.ParseInt(string(t), 10, 32)
convertAndSet(bool(n == 1), to)
case int, int8, int16, int32, int64:
n, _ := strconv.ParseInt(string(t), 10, 64)
convertAndSet(n, to)
case uint, uint8, uint16, uint32, uint64:
n, _ := strconv.ParseUint(string(t), 10, 64)
convertAndSet(n, to)
case float32:
n, _ := strconv.ParseFloat(string(t), 32)
convertAndSet(n, to)
case float64:
n, _ := strconv.ParseFloat(string(t), 64)
convertAndSet(n, to)
case string:
to.SetString(string(t))
case map[string]interface{}:
to.Set(reflect.ValueOf(parseHstoreColumn(string(t))))
default:
convertAndSet(t, to)
}
}
func setValueFromFloat(f float64, to reflect.Value) {
switch to.Interface().(type) {
case bool:
convertAndSet(bool(f == 1), to)
default:
convertAndSet(f, to)
}
}
func setValueFromInt(i int64, to reflect.Value) {
switch to.Interface().(type) {
case bool:
convertAndSet(bool(i == 1), to)
default:
convertAndSet(i, to)
}
}
func setValueFromUint(i uint64, to reflect.Value) {
switch to.Interface().(type) {
case bool:
convertAndSet(bool(i == 1), to)
default:
convertAndSet(i, to)
}
}
func setValueFromTime(t time.Time, to reflect.Value) {
switch to.Interface().(type) {
case int64:
convertAndSet(int64(t.Unix()), to)
case uint64:
convertAndSet(uint64(t.Unix()), to)
default:
convertAndSet(t, to)
}
}