-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect_array.go
113 lines (108 loc) · 2.1 KB
/
reflect_array.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
package api
import (
"github.com/mymmsc/gox/errors"
"reflect"
"strconv"
)
var (
mapTag map[reflect.Type]map[int]reflect.StructField = nil
)
func init() {
//fmt.Println("reflect_array init...")
mapTag = make(map[reflect.Type]map[int]reflect.StructField)
//fmt.Println("reflect_array init...OK")
}
func initTag(t reflect.Type) map[int]reflect.StructField {
ma, mok := mapTag[t]
if mok {
return ma
}
ma = nil
fieldNum := t.NumField()
for i := 0; i < fieldNum; i++ {
field := t.Field(i)
tag := field.Tag
if len(tag) > 0 {
tv, ok := tag.Lookup("array")
if ok {
index, err := strconv.Atoi(tv)
if err == nil {
if ma == nil {
ma = make(map[int]reflect.StructField)
mapTag[t] = ma
}
ma[index] = field
}
}
}
}
return ma
}
func Convert(data []string, v interface{}) error {
val := reflect.ValueOf(v)
//t := reflect.TypeOf(v)
//fieldNum := val.NumField()
//_ = fieldNum
obj := reflect.ValueOf(v)
t := val.Type()
if val.Kind() == reflect.Ptr {
t = t.Elem()
obj = obj.Elem()
}
ma := initTag(t)
if ma == nil {
return errors.New("can not Convert")
}
dl := len(data)
for i := 0; i < dl; i++ {
field, ok := ma[i]
if ok {
dv := data[i]
ov := obj.FieldByName(field.Name)
if ov.CanSet() {
var value interface{}
switch ov.Interface().(type) {
case string:
value = string(dv)
case int8:
t := ParseInt(dv)
value = int8(t)
case int16:
t := ParseInt(dv)
value = int16(t)
case int32:
t := ParseInt(dv)
value = int32(t)
case int64:
t := ParseInt(dv)
value = int64(t)
case uint8:
t := ParseUint(dv)
value = uint8(t)
case uint16:
t := ParseUint(dv)
value = uint16(t)
case uint32:
t := ParseUint(dv)
value = uint32(t)
case uint64:
t := ParseUint(dv)
value = t
case float32:
t := ParseFloat(dv)
value = float32(t)
case float64:
t := ParseFloat(dv)
value = t
case bool:
t, _ := strconv.ParseBool(dv)
value = t
default:
value = dv
}
ov.Set(reflect.ValueOf(value))
}
}
}
return nil
}