-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_mapper.go
360 lines (342 loc) · 8.14 KB
/
model_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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package model
import (
"database/sql"
"errors"
"reflect"
"time"
)
type fdhandler func(fd *fieldDescriptor) bool
type ModelMapper struct {
model interface{}
value reflect.Value
pk string
fds map[string]*fieldDescriptor
field2col map[string]string
}
func NewModelMapper(model interface{}) *ModelMapper {
mm := new(ModelMapper)
mm.model = model
mm.fds = make(map[string]*fieldDescriptor)
mm.value = mm.modelValue(mm.model)
mm.field2col = make(map[string]string)
types := reflect.TypeOf(mm.model).Elem()
length := types.NumField()
for i := 0; i < length; i++ {
field := types.Field(i)
td := field.Tag.Get("db")
if td == "" {
continue
}
fd := newFd(field.Name, td)
mm.field2col[fd.fieldname] = fd.colname
mm.fds[fd.colname] = fd
if fd.ispk {
mm.pk = fd.colname
}
}
return mm
}
func (mm *ModelMapper) has(colname string) bool {
_, ok := mm.fds[colname]
return ok
}
func (mm *ModelMapper) each(handle fdhandler) {
for _, fd := range mm.fds {
if !handle(fd) {
break
}
}
}
func (mm *ModelMapper) fd(colname string) (fd *fieldDescriptor, ok bool) {
fd, ok = mm.fds[colname]
return
}
func (mm *ModelMapper) cols(columns []string) (result []interface{}, err error) {
pointers := make([]interface{}, len(columns))
var fd *fieldDescriptor
var ok bool
var field interface{}
for i, colname := range columns {
if fd, ok = mm.fd(colname); !ok {
err = &Error{ERR_COL_UNDEFINED, errors.New("col " + colname + " undefined")}
return
}
field = mm.value.FieldByName(fd.fieldname).Interface()
if converter, ok := mm.model.(ValueConverter); ok {
field = converter.DBValue(colname, field)
}
if fd.nullable {
switch field.(type) {
case string:
pointers[i] = new(sql.NullString)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
pointers[i] = new(sql.NullInt64)
case float32, float64:
pointers[i] = new(sql.NullFloat64)
case time.Time:
pointers[i] = new(NullTime)
case bool:
pointers[i] = new(sql.NullBool)
case sql.NullString:
pointers[i] = new(sql.NullString)
case sql.NullBool:
pointers[i] = new(sql.NullBool)
case sql.NullFloat64:
pointers[i] = new(sql.NullBool)
case sql.NullInt64:
pointers[i] = new(sql.NullInt64)
case NullTime:
pointers[i] = new(NullTime)
default:
err = &Error{
ERR_UNKNOWN_COLTYPE,
errors.New("unknown type of field " + colname),
}
}
} else {
switch field.(type) {
case string:
var value string
pointers[i] = &value
case int:
var value int
pointers[i] = &value
case int8:
var value int8
pointers[i] = &value
case int16:
var value int16
pointers[i] = &value
case int32:
var value int32
pointers[i] = &value
case int64:
var value int64
pointers[i] = &value
case uint:
var value uint
pointers[i] = &value
case uint8:
var value uint8
pointers[i] = &value
case uint16:
var value uint16
pointers[i] = &value
case uint32:
var value uint32
pointers[i] = &value
case uint64:
var value uint64
pointers[i] = &value
case float32:
var value float32
pointers[i] = &value
case float64:
var value float64
pointers[i] = &value
case time.Time:
pointers[i] = new(time.Time)
case bool:
var value bool
pointers[i] = &value
default:
err = &Error{
ERR_UNKNOWN_COLTYPE,
errors.New("unknown type of field " + colname),
}
}
}
}
result = pointers
return
}
func (mm *ModelMapper) pack(columns []string, cols []interface{}, key string) (model interface{}, id interface{}, err error) {
var fd *fieldDescriptor
var ok bool
var converter ValueConverter
v := reflect.New(reflect.ValueOf(mm.model).Elem().Type()).Elem()
for i, colname := range columns {
if fd, ok = mm.fd(colname); !ok {
err = &Error{ERR_COL_UNDEFINED, errors.New("col " + colname + " undefined")}
return
}
field := v.FieldByName(fd.fieldname)
col := reflect.ValueOf(cols[i]).Elem().Interface()
if converter, ok = mm.model.(ValueConverter); ok {
if val, catched := converter.Value(colname, col); catched {
field.Set(val)
if colname == key {
id = val.Interface()
}
continue
}
}
var value reflect.Value
if fd.nullable {
switch field.Interface().(type) {
case int:
t := col.(sql.NullInt64)
if t.Valid {
value = reflect.ValueOf(int(t.Int64))
} else {
value = reflect.ValueOf(0)
}
case int8:
t := col.(sql.NullInt64)
if t.Valid {
value = reflect.ValueOf(int8(t.Int64))
} else {
value = reflect.ValueOf(0)
}
case int16:
t := col.(sql.NullInt64)
if t.Valid {
value = reflect.ValueOf(int16(t.Int64))
} else {
value = reflect.ValueOf(0)
}
case int32:
t := col.(sql.NullInt64)
if t.Valid {
value = reflect.ValueOf(int32(t.Int64))
} else {
value = reflect.ValueOf(0)
}
case int64:
t := col.(sql.NullInt64)
if t.Valid {
value = reflect.ValueOf(int64(t.Int64))
} else {
value = reflect.ValueOf(0)
}
case uint:
t := col.(sql.NullInt64)
if t.Valid {
value = reflect.ValueOf(int64(t.Int64))
} else {
value = reflect.ValueOf(0)
}
case uint8:
t := col.(sql.NullInt64)
if t.Valid {
value = reflect.ValueOf(uint8(t.Int64))
} else {
value = reflect.ValueOf(0)
}
case uint16:
t := col.(sql.NullInt64)
if t.Valid {
value = reflect.ValueOf(uint16(t.Int64))
} else {
value = reflect.ValueOf(0)
}
case uint32:
t := col.(sql.NullInt64)
if t.Valid {
value = reflect.ValueOf(uint32(t.Int64))
} else {
value = reflect.ValueOf(0)
}
case uint64:
t := col.(sql.NullInt64)
if t.Valid {
value = reflect.ValueOf(uint64(t.Int64))
} else {
value = reflect.ValueOf(0)
}
case float32:
t := col.(sql.NullFloat64)
if t.Valid {
value = reflect.ValueOf(float32(t.Float64))
} else {
value = reflect.ValueOf(0.0)
}
case float64:
t := col.(sql.NullFloat64)
if t.Valid {
value = reflect.ValueOf(float64(t.Float64))
} else {
value = reflect.ValueOf(0.0)
}
case bool:
t := col.(sql.NullBool)
if t.Valid {
value = reflect.ValueOf(t.Bool)
} else {
value = reflect.ValueOf(false)
}
case string:
t := col.(sql.NullString)
if t.Valid {
value = reflect.ValueOf(t.String)
} else {
value = reflect.ValueOf("")
}
case time.Time:
t := col.(NullTime)
if t.Valid {
value = reflect.ValueOf(t.Time)
}
default:
err = &Error{
ERR_UNKNOWN_COLTYPE,
errors.New("unknown type of field " + colname),
}
}
} else {
value = reflect.ValueOf(col)
}
if value.IsValid() {
field.Set(value)
}
if colname == mm.model.(Model).PK() {
id = value.Interface()
}
}
model = New(v.Addr().Interface())
model.(Model).SetFresh(false)
return
}
func (mm *ModelMapper) extract(model interface{}) (result map[string]interface{}) {
result = make(map[string]interface{})
values := mm.modelValue(model)
for _, fd := range mm.fds {
var value interface{}
value = values.FieldByName(fd.fieldname).Interface()
if converter, ok := model.(ValueConverter); ok {
result[fd.colname] = converter.DBValue(fd.colname, value)
continue
}
switch value.(type) {
case time.Time:
if value.(time.Time).IsZero() {
result[fd.colname] = nil
} else {
result[fd.colname] = value
}
default:
result[fd.colname] = value
}
}
return
}
func (mm *ModelMapper) colValue(model interface{}, colname string) (result interface{}, err error) {
values := mm.modelValue(model)
if fd, ok := mm.fd(colname); ok {
result = values.FieldByName(fd.fieldname).Interface()
} else {
err = &Error{ERR_COL_UNDEFINED, errors.New("col " + colname + " undefined")}
}
return
}
func (mm *ModelMapper) fieldValue(model interface{}, fieldname string) (result interface{}, err error) {
colname := mm.field2col[fieldname]
return mm.colValue(model, colname)
}
func (mm *ModelMapper) modelValue(model interface{}) reflect.Value {
value := reflect.ValueOf(model)
for value.Kind() == reflect.Ptr {
value = value.Elem()
}
return value
}