/
reflector.go
726 lines (624 loc) · 16.4 KB
/
reflector.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
package sqlca
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/civet148/log"
"reflect"
"strconv"
"strings"
)
type ModelReflector struct {
value interface{} //value
engine *Engine // database engine
dict map[string]interface{} //dictionary of structure tag and value
}
type Fetcher struct {
count int //column count in db table
cols []string //column names in db table
types []*sql.ColumnType //column types in db table
arrValues [][]byte //value slice
mapValues map[string]string //value map
arrIndex int //fetch index
}
func newReflector(e *Engine, v interface{}) *ModelReflector {
return &ModelReflector{
value: v,
engine: e,
dict: make(map[string]interface{}),
}
}
// map[string]string to [][]byte
func mapToBytesSlice(m map[string]string) (arrays [][]byte) {
for _, v := range m {
arr := []byte(v)
arrays = append(arrays, arr)
}
return
}
// parse struct tag and value to map
func (s *ModelReflector) ToMap(tagNames ...string) map[string]interface{} {
typ := reflect.TypeOf(s.value)
val := reflect.ValueOf(s.value)
for {
if typ.Kind() != reflect.Ptr { // pointer type
break
}
typ = typ.Elem()
val = val.Elem()
}
kind := typ.Kind()
switch kind {
case reflect.Struct:
{
s.parseStructField(typ, val, tagNames...)
}
case reflect.Slice:
{
typ = val.Type().Elem()
val = reflect.New(typ).Elem()
s.parseStructField(typ, val, tagNames...)
}
case reflect.Map:
{
if v, ok := s.value.(*map[string]interface{}); ok {
s.dict = *v
break
}
if v, ok := s.value.(map[string]interface{}); ok {
s.dict = v
break
}
if v, ok := s.value.(*map[string]string); ok {
s.dict = s.convertMapString(*v)
break
}
if v, ok := s.value.(map[string]string); ok {
s.dict = s.convertMapString(v)
break
}
}
default:
log.Warnf("kind [%v] not support yet", typ.Kind())
}
return s.dict
}
func (s *ModelReflector) convertMapString(ms map[string]string) (mi map[string]interface{}) {
mi = make(map[string]interface{}, 10)
for k, v := range ms {
mi[k] = v
}
return
}
// get struct field's tag value
func (s *ModelReflector) getTag(sf reflect.StructField, tagName string) (strValue string, ignore bool) {
strValue = sf.Tag.Get(tagName)
if strValue == SQLCA_TAG_VALUE_IGNORE {
return "", true
}
return
}
// parse struct fields
func (s *ModelReflector) parseStructField(typ reflect.Type, val reflect.Value, tagNames ...string) {
kind := typ.Kind()
if kind == reflect.Struct {
NumField := val.NumField()
for i := 0; i < NumField; i++ {
typField := typ.Field(i)
valField := val.Field(i)
if typField.Type.Kind() == reflect.Ptr {
typField.Type = typField.Type.Elem()
valField = valField.Elem()
}
if !valField.IsValid() || !valField.CanInterface() {
continue
}
if typField.Type.Kind() == reflect.Struct {
if _, ok := valField.Interface().(driver.Valuer); ok {
s.parseValuer(typField, valField, tagNames...)
} else {
tagVal, ignore := s.getTag(typField, TAG_NAME_DB)
if ignore {
continue
}
if tagVal == "" {
s.parseStructField(typField.Type, valField, tagNames...) //recurse every field that type is a struct
} else {
data, err := json.Marshal(valField.Interface())
if err != nil {
fmt.Printf("[sqlca] marshal struct failed error [%s]\n", err.Error())
continue
}
s.dict[tagVal] = string(data)
}
}
} else {
s.setValueByField(typField, valField, tagNames...) // save field tag value and field value to map
}
}
}
}
//parse decimal
func (s *ModelReflector) parseValuer(field reflect.StructField, val reflect.Value, tagNames ...string) {
s.setValueByField(field, val, tagNames...)
}
//trim the field value's first and last blank character and save to map
func (s *ModelReflector) setValueByField(field reflect.StructField, val reflect.Value, tagNames ...string) {
if len(tagNames) == 0 {
log.Errorf("ModelReflector.setValueByField no tag to set value")
return
}
var tagVal string
for _, v := range tagNames {
if v == TAG_NAME_SQLCA {
continue
}
strTagValue, ignore := s.getTag(field, v)
//parse db、json、protobuf tag
tagVal = handleTagValue(v, strTagValue)
if ignore {
break
}
if tagVal != "" {
//log.Debugf("ModelReflector.setValueByField tag [%v] value [%+v]", tagVal, val.Interface())
if d, ok := val.Interface().(driver.Valuer); ok {
s.dict[tagVal], _ = d.Value()
} else {
s.dict[tagVal] = val.Interface()
}
break
}
}
for _, v := range tagNames {
if v == TAG_NAME_SQLCA { //parse sqlca tag
strTagValue, ignore := s.getTag(field, v)
if !ignore && strTagValue != "" {
vs := strings.Split(strTagValue, ",")
for _, vv := range vs {
if vv == SQLCA_TAG_VALUE_READ_ONLY { //column is read only
s.engine.readOnly = append(s.engine.readOnly, tagVal)
}
}
}
}
}
}
func (e *Engine) fetchRows(r *sql.Rows) (count int64, err error) {
for r.Next() {
var c int64
if e.getModelType() == ModelType_BaseType {
if c, err = e.fetchRow(r, e.model.([]interface{})...); err != nil {
log.Errorf("fetchRow error [%v]", err.Error())
return
}
} else {
if c, err = e.fetchRow(r, e.model); err != nil {
log.Errorf("fetchRow error [%v]", err.Error())
return
}
}
count += c
}
return
}
//fetch row to struct or slice, must call rows.Next() before call this function
func (e *Engine) fetchRow(rows *sql.Rows, args ...interface{}) (count int64, err error) {
fetcher, _ := e.getFetcher(rows)
for _, arg := range args {
typ := reflect.TypeOf(arg)
val := reflect.ValueOf(arg)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
}
switch typ.Kind() {
case reflect.Map:
{
err = e.fetchToMap(fetcher, arg)
count++
}
case reflect.Slice:
{
if val.IsNil() {
val.Set(reflect.MakeSlice(val.Type(), 0, 0)) //make slice for storage
}
for {
fetcher, _ := e.getFetcher(rows)
var elemVal reflect.Value
var elemTyp reflect.Type
elemTyp = val.Type().Elem()
if elemTyp.Kind() == reflect.Ptr {
elemVal = reflect.New(elemTyp.Elem())
} else {
elemVal = reflect.New(elemTyp).Elem()
}
if elemTyp.Kind() == reflect.Struct || elemTyp.Kind() == reflect.Ptr {
err = e.fetchToStruct(fetcher, elemTyp, elemVal) // assign to struct type variant
} else {
err = e.fetchToBaseType(fetcher, elemTyp, elemVal) // assign to base type variant
}
val.Set(reflect.Append(val, elemVal))
count++
if !rows.Next() {
break
}
}
}
case reflect.Struct:
{
err = e.fetchToStruct(fetcher, typ, val)
count++
}
default:
{
e.fetchToBaseType(fetcher, typ, val)
count++
}
}
}
return
}
func (e *Engine) getStructSliceKeyValues(excludeReadOnly bool) (keys []string, values [][]string) {
typ := reflect.TypeOf(e.model)
val := reflect.ValueOf(e.model)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
}
switch typ.Kind() {
case reflect.Slice:
{
elemTyp := val.Type().Elem()
for i := 0; i < val.Len(); i++ {
elemVal := val.Index(i)
if elemTyp.Kind() == reflect.Ptr {
elemTyp = elemTyp.Elem()
}
if elemVal.Kind() == reflect.Ptr {
elemVal = elemVal.Elem()
}
if elemTyp.Kind() == reflect.Struct {
var vs []string
keys, vs = e.getStructFieldValues(elemTyp, elemVal, excludeReadOnly)
values = append(values, vs)
}
}
}
default:
{
panic(fmt.Sprintf("expect struct got %v", typ.Name()))
}
}
return
}
func (e *Engine) getStructFieldValues(typ reflect.Type, val reflect.Value, excludeReadOnly bool) (keys, values []string) {
if typ.Kind() == reflect.Struct {
var isBool bool
NumField := val.NumField()
for i := 0; i < NumField; i++ {
typField := typ.Field(i)
valField := val.Field(i)
if typField.Type.Kind() == reflect.Ptr {
typField.Type = typField.Type.Elem()
valField = valField.Elem()
}
if !valField.IsValid() || !valField.CanInterface() {
//fmt.Printf("Filed [%s] tag(%s) is not valid \n", typField.Type.Name(), e.getTagValue(typField))
return
}
if typField.Type.Kind() == reflect.Bool {
isBool = true
}
strTagVal := e.getTagValue(typField)
strFieldVal := fmt.Sprintf("%v", valField)
if e.isPkValueNil() && strTagVal == e.GetPkName() {
continue
}
if excludeReadOnly {
if typField.Tag.Get(TAG_NAME_SQLCA) == SQLCA_TAG_VALUE_READ_ONLY {
continue
}
}
//convert bool value to int value
if isBool {
strFieldVal = convertBoolString(strFieldVal)
}
if strTagVal != "" && strTagVal != SQLCA_TAG_VALUE_IGNORE {
keys = append(keys, strTagVal)
values = append(values, strFieldVal)
//log.Debugf("filed tag name [%v] value [%v]", strTagVal, strFieldVal)
}
}
}
return
}
//fetch cache data to struct or slice or map
func (e *Engine) fetchCache(fetchers []*Fetcher, args ...interface{}) (count int64, err error) {
for i, fetcher := range fetchers {
for _, arg := range args {
typ := reflect.TypeOf(arg)
val := reflect.ValueOf(arg)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
}
switch typ.Kind() {
case reflect.Map:
{
err = e.fetchToMap(fetcher, arg)
count++
}
case reflect.Slice:
{
if val.IsNil() {
val.Set(reflect.MakeSlice(val.Type(), 0, 0)) //make slice for storage
}
for {
fetcher = fetchers[i]
elemTyp := val.Type().Elem()
elemVal := reflect.New(elemTyp).Elem()
if elemTyp.Kind() == reflect.Struct {
err = e.fetchToStruct(fetcher, elemTyp, elemVal) // assign to struct type variant
} else {
err = e.fetchToBaseType(fetcher, elemTyp, elemVal) // assign to base type variant
}
val.Set(reflect.Append(val, elemVal))
count++
if len(fetchers) == i+1 {
break
}
i++
}
}
case reflect.Struct:
{
err = e.fetchToStruct(fetcher, typ, val)
count++
}
default:
{
e.fetchToBaseType(fetcher, typ, val)
count++
}
}
}
}
return
}
func (e *Engine) getFetcher(rows *sql.Rows) (fetcher *Fetcher, err error) {
fetcher = &Fetcher{}
fetcher.cols, _ = rows.Columns()
fetcher.count = len(fetcher.cols)
fetcher.types, _ = rows.ColumnTypes()
fetcher.arrValues = make([][]byte, fetcher.count)
fetcher.mapValues = make(map[string]string)
scans := make([]interface{}, fetcher.count)
for i := range fetcher.arrValues {
scans[i] = &fetcher.arrValues[i]
}
if err = rows.Scan(scans...); err != nil {
return
}
for i, v := range fetcher.arrValues {
fetcher.mapValues[fetcher.cols[i]] = string(v)
}
return
}
//fetch row data to map
func (e *Engine) fetchToMap(fetcher *Fetcher, arg interface{}) (err error) {
typ := reflect.TypeOf(arg)
if typ.Kind() == reflect.Ptr {
for k, v := range fetcher.mapValues {
m := *arg.(*map[string]string) //just support map[string]string type
m[k] = v
}
}
return
}
//fetch row data to struct
func (e *Engine) fetchToStruct(fetcher *Fetcher, typ reflect.Type, val reflect.Value) (err error) {
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
}
if typ.Kind() == reflect.Struct {
NumField := val.NumField()
for i := 0; i < NumField; i++ {
typField := typ.Field(i)
valField := val.Field(i)
e.fetchToStructField(fetcher, typField.Type, typField, valField)
}
}
return
}
func (e *Engine) fetchToStructField(fetcher *Fetcher, typ reflect.Type, field reflect.StructField, val reflect.Value) {
// log.Debugf("typField name [%s] type [%s] valField can addr [%v]", field.Name, field.Type.Kind(), val.CanAddr())
switch typ.Kind() {
case reflect.Struct:
{
e.fetchToStructAny(fetcher, field, val)
}
case reflect.Slice:
if e.getTagValue(field) != "" {
_ = e.fetchToJsonObject(fetcher, field, val)
}
case reflect.Map: //ignore...
case reflect.Ptr:
{
typElem := field.Type.Elem()
if val.IsNil() {
valNew := reflect.New(typElem)
val.Set(valNew)
}
e.fetchToStructField(fetcher, typElem, field, val.Elem())
}
default:
{
_ = e.setValueByField(fetcher, field, val) //assign value to struct field
}
}
}
func (e *Engine) fetchToStructAny(fetcher *Fetcher, field reflect.StructField, val reflect.Value) {
if _, ok := val.Addr().Interface().(sql.Scanner); ok {
e.fetchToScanner(fetcher, field, val)
} else {
if e.getTagValue(field) != "" {
_ = e.fetchToJsonObject(fetcher, field, val)
} else {
_ = e.fetchToStruct(fetcher, field.Type, val)
}
}
}
//json string unmarshal to struct/slice
func (e *Engine) fetchToJsonObject(fetcher *Fetcher, field reflect.StructField, val reflect.Value) (err error) {
//优先给有db标签的成员变量赋值
strDbTagVal := e.getTagValue(field)
if strDbTagVal == SQLCA_TAG_VALUE_IGNORE {
return
}
if v, ok := fetcher.mapValues[strDbTagVal]; ok {
vp := val.Addr()
if strings.TrimSpace(v) != "" {
if err = json.Unmarshal([]byte(v), vp.Interface()); err != nil {
log.Errorf("json.Unmarshal [%s] error [%s]", v, err)
return
}
}
}
return
}
//fetch to struct object by customize scanner
func (e *Engine) fetchToScanner(fetcher *Fetcher, field reflect.StructField, val reflect.Value) {
//优先给有db标签的成员变量赋值
strDbTagVal := e.getTagValue(field)
if strDbTagVal == SQLCA_TAG_VALUE_IGNORE {
return
}
if v, ok := fetcher.mapValues[strDbTagVal]; ok {
vp := val.Addr()
d := vp.Interface().(sql.Scanner)
if v == "" {
return
}
if err := d.Scan(v); err != nil {
log.Errorf("scan '%v' to scanner [%+v] error [%+v]", v, vp.Interface(), err.Error())
}
}
}
func (e *Engine) fetchToBaseType(fetcher *Fetcher, typ reflect.Type, val reflect.Value) (err error) {
v := fetcher.arrValues[fetcher.arrIndex]
e.setValue(typ, val, string(v))
fetcher.arrIndex++
return
}
func handleTagValue(strTagName, strTagValue string) string {
if strTagValue == "" {
return ""
}
if strTagName == TAG_NAME_JSON {
vs := strings.Split(strTagValue, ",")
strTagValue = vs[0]
} else if strTagName == TAG_NAME_PROTOBUF {
//parse protobuf tag value
vs := strings.Split(strTagValue, ",")
for _, vv := range vs {
ss := strings.Split(vv, "=")
if len(ss) <= 1 {
//log.Warnf("protobuf tag value [%v] is not a invalid format", strTagValue)
continue
} else {
if ss[0] == PROTOBUF_VALUE_NAME {
strTagValue = ss[1]
return strTagValue
}
}
}
}
return strTagValue
}
func (e *Engine) getTagValue(sf reflect.StructField) (strValue string) {
for _, v := range e.dbTags { //support multiple tag
strValue = handleTagValue(v, sf.Tag.Get(v))
if strValue != "" {
return
}
}
return
}
//按结构体字段标签赋值
func (e *Engine) setValueByField(fetcher *Fetcher, field reflect.StructField, val reflect.Value) (err error) {
//优先给有db标签的成员变量赋值
strDbTagVal := e.getTagValue(field)
if strDbTagVal == SQLCA_TAG_VALUE_IGNORE {
return
}
if v, ok := fetcher.mapValues[strDbTagVal]; ok {
e.setValue(field.Type, val, v)
}
return
}
//将string存储的值赋值到变量
func (e *Engine) setValue(typ reflect.Type, val reflect.Value, v string) {
switch typ.Kind() {
case reflect.String:
val.SetString(v)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, _ := strconv.ParseInt(v, 10, 64)
val.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
i, _ := strconv.ParseUint(v, 10, 64)
val.SetUint(i)
case reflect.Float32, reflect.Float64:
i, _ := strconv.ParseFloat(v, 64)
val.SetFloat(i)
case reflect.Bool:
i, _ := strconv.ParseUint(v, 10, 64)
val.SetBool(true)
if i == 0 {
val.SetBool(false)
}
case reflect.Ptr:
typ = typ.Elem()
//val = val.Elem()
e.setValue(typ, val, v)
default:
panic(fmt.Sprintf("can't assign value [%v] to variant type [%v]\n", v, typ.Kind()))
return
}
}
func convertBool2Int(v interface{}) interface{} {
typ := reflect.TypeOf(v)
val := reflect.ValueOf(v)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
}
switch typ.Kind() {
case reflect.Bool:
{
if val.Interface() == false {
return 0
} else {
return 1
}
}
//case reflect.String:
// {
// if val.Interface() == "false" {
// return 0
// } else if val.Interface() == "true" {
// return 1
// }
// }
}
return v
}
func convertBoolString(strVal string) string {
if strVal == "false" {
return "0"
} else if strVal == "true" {
return "1"
}
return strVal
}