-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.go
341 lines (313 loc) · 8.52 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
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
package exql
import (
"database/sql"
"errors"
"reflect"
"golang.org/x/xerrors"
)
// Error returned when record not found
var ErrRecordNotFound = errors.New("record not found")
// Deprecated: Use Finder It will be removed in next version.
type Mapper interface {
// Deprecated: Use Find or MapRow. It will be removed in next version.
Map(rows *sql.Rows, destPtr any) error
// Deprecated: Use FindContext or MapRows. It will be removed in next version.
MapMany(rows *sql.Rows, destSlicePtr any) error
}
type mapper struct{}
// Map reads data from single row and maps those columns into destination struct.
func (m *mapper) Map(rows *sql.Rows, destPtr any) error {
return MapRow(rows, destPtr)
}
// MapMany reads all data from rows and maps those columns for each destination struct.
func (m *mapper) MapMany(rows *sql.Rows, destSlicePtr any) error {
return MapRows(rows, destSlicePtr)
}
type ColumnSplitter func(i int) string
// SerialMapper is an interface for mapping a joined row into one or more destinations serially.
type SerialMapper interface {
// Map reads joined rows and maps columns for each destination serially.
// The second argument, pointerOfStruct, MUST BE a pointer of the struct.
//
// NOTE: DO NOT FORGET to close rows manually, as it WON'T do it automatically.
//
// Example:
//
// var user User
// var favorite UserFavorite
// defer rows.Close()
// err := m.Map(rows, &user, &favorite)
Map(rows *sql.Rows, pointersOfStruct ...any) error
}
type serialMapper struct {
splitter ColumnSplitter
}
func NewSerialMapper(s ColumnSplitter) SerialMapper {
return &serialMapper{splitter: s}
}
var errMapDestination = xerrors.Errorf("destination must be a pointer of struct")
// MapRow reads data from single row and maps those columns into destination struct.
// pointerOfStruct MUST BE a pointer of struct.
// It closes rows after mapping regardless error occurred.
//
// Example:
//
// var user User
// err := exql.MapRow(rows, &user)
func MapRow(row *sql.Rows, pointerOfStruct interface{}) error {
defer func() {
if row != nil {
row.Close()
}
}()
if pointerOfStruct == nil {
return errMapDestination
}
destValue := reflect.ValueOf(pointerOfStruct)
destType := destValue.Type()
if destType.Kind() != reflect.Ptr {
return errMapDestination
}
destValue = destValue.Elem()
if destValue.Kind() != reflect.Struct {
return errMapDestination
}
if row.Next() {
return mapRow(row, &destValue)
}
if err := row.Err(); err != nil {
return err
}
err := row.Close()
if err != nil {
return err
}
return ErrRecordNotFound
}
var errMapManyDestination = xerrors.Errorf("destination must be a pointer of slice of struct")
// MapRows reads all data from rows and maps those columns for each destination struct.
// pointerOfSliceOfStruct MUST BE a pointer of slice of pointer of struct.
// It closes rows after mapping regardless error occurred.
//
// Example:
//
// var users []*Users
// err := exql.MapRows(rows, &users)
func MapRows(rows *sql.Rows, structPtrOrSlicePtr interface{}) error {
defer func() {
if rows != nil {
rows.Close()
}
}()
if structPtrOrSlicePtr == nil {
return errMapManyDestination
}
destValue := reflect.ValueOf(structPtrOrSlicePtr)
destType := destValue.Type()
if destType.Kind() != reflect.Ptr {
return errMapManyDestination
}
destType = destType.Elem()
if destType.Kind() != reflect.Slice {
return errMapManyDestination
}
// []*Model -> *Model
sliceType := destType.Elem()
if sliceType.Kind() != reflect.Ptr {
return errMapManyDestination
}
// *Model -> Model
sliceType = sliceType.Elem()
cnt := 0
for rows.Next() {
// modelValue := SliceType{}
modelValue := reflect.New(sliceType).Elem()
if err := mapRow(rows, &modelValue); err != nil {
return err
}
// *dest = append(*dest, i)
destValue.Elem().Set(reflect.Append(destValue.Elem(), modelValue.Addr()))
cnt++
}
if err := rows.Err(); err != nil {
return err
}
err := rows.Close()
if err != nil {
return err
}
if cnt == 0 {
return ErrRecordNotFound
}
return nil
}
func mapRow(
row *sql.Rows,
dest *reflect.Value,
) error {
fields, err := aggregateFields(dest)
if err != nil {
return err
}
cols, err := row.ColumnTypes()
if err != nil {
return err
}
destVals := make([]interface{}, len(cols))
for j, col := range cols {
if fIndex, ok := fields[col.Name()]; ok {
f := dest.Field(fIndex)
destVals[j] = f.Addr().Interface()
} else {
ns := &noopScanner{}
destVals[j] = ns
}
}
return row.Scan(destVals...)
}
func aggregateFields(dest *reflect.Value) (map[string]int, error) {
// *Model || **Model
destType := dest.Type()
if dest.Kind() == reflect.Ptr {
destType = destType.Elem()
}
fields := make(map[string]int)
for i := 0; i < destType.NumField(); i++ {
f := destType.Field(i)
tag := f.Tag.Get("exql")
if tag != "" {
if f.Type.Kind() == reflect.Ptr {
return nil, xerrors.Errorf("struct field must not be a pointer: %s %s", f.Type.Name(), f.Type.Kind())
}
tags, err := ParseTags(tag)
if err != nil {
return nil, err
}
col := tags["column"]
fields[col] = i
}
}
return fields, nil
}
var errMapRowSerialDestination = xerrors.Errorf("destination must be either *(struct) or *((*struct)(nil))")
func (s *serialMapper) Map(rows *sql.Rows, dest ...interface{}) error {
var values []*reflect.Value
if len(dest) == 0 {
return xerrors.Errorf("empty dest list")
}
for _, model := range dest {
v := reflect.ValueOf(model)
if v.Kind() != reflect.Ptr {
return errMapRowSerialDestination
}
v = v.Elem()
if v.Kind() == reflect.Struct {
values = append(values, &v)
} else if v.Kind() != reflect.Ptr {
return errMapRowSerialDestination
} else if !v.IsNil() || v.Type().Elem().Kind() != reflect.Struct {
return errMapRowSerialDestination
} else {
values = append(values, &v)
}
}
return mapRowSerial(rows, values, s.splitter)
}
func mapRowSerial(
row *sql.Rows,
destList []*reflect.Value,
headColProvider ColumnSplitter,
) error {
// *Model || **Model
var destFields []map[string]int
destTypes := map[int]reflect.Type{}
for destIndex, dest := range destList {
fields, err := aggregateFields(dest)
if err != nil {
return err
}
destFields = append(destFields, fields)
destTypes[destIndex] = dest.Type() // Model || *Model
}
cols, err := row.ColumnTypes()
if err != nil {
return err
}
destVals := make([]interface{}, len(cols))
colIndex := 0
columnCounts := map[int]int{}
for destIndex, dest := range destList {
fields := destFields[destIndex]
headCol := cols[colIndex]
expectedHeadCol := headColProvider(destIndex)
if headCol.Name() != expectedHeadCol {
return xerrors.Errorf(
"head col mismatch: expected=%s, actual=%s",
expectedHeadCol, headCol.Name(),
)
}
start := colIndex
ns := &noopScanner{}
model := dest
if destTypes[destIndex].Kind() == reflect.Ptr {
m := reflect.New(destTypes[destIndex].Elem()).Elem() // Model
model = &m
}
for ; colIndex < len(cols); colIndex++ {
col := cols[colIndex]
if colIndex > start && destIndex < len(destList)-1 {
// Reach next column's head
if col.Name() == headColProvider(destIndex+1) {
columnCounts[destIndex] = colIndex - start
break
}
} else if destIndex == len(destList)-1 {
columnCounts[destIndex]++
}
if fIndex, ok := fields[col.Name()]; ok {
f := model.Field(fIndex)
if destTypes[destIndex].Kind() == reflect.Struct {
destVals[colIndex] = f.Addr().Interface() // *(Model.Field)
} else {
destVals[colIndex] = reflect.New(f.Addr().Type()).Interface() // **(Model.Field)
}
} else {
destVals[colIndex] = ns
}
}
}
if err := row.Scan(destVals...); err != nil {
return err
}
colIndex = 0
for destIndex, dest := range destList {
fields := destFields[destIndex]
if destTypes[destIndex].Kind() == reflect.Struct || reflect.ValueOf(destVals[colIndex]).Elem().IsNil() {
if destIndex < len(destList)-1 {
colIndex += columnCounts[destIndex]
}
continue
}
model := reflect.New(destTypes[destIndex].Elem()) // *Model
start := colIndex
for ; colIndex < start+columnCounts[destIndex]; colIndex++ {
col := cols[colIndex]
if fIndex, ok := fields[col.Name()]; ok {
f := model.Elem().Field(fIndex)
if t := reflect.ValueOf(destVals[colIndex]).Elem(); t.IsNil() {
f.Set(reflect.Zero(t.Type().Elem())) // To set (*null.Type)(nil) as null.Type{}
} else {
f.Set(reflect.ValueOf(destVals[colIndex]).Elem().Elem())
}
}
}
dest.Set(model) // dest = *Model
}
return nil
}
type noopScanner struct {
}
func (n *noopScanner) Scan(_ interface{}) error {
// noop
return nil
}