-
Notifications
You must be signed in to change notification settings - Fork 5
/
ini_tag.go
195 lines (161 loc) · 3.87 KB
/
ini_tag.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
package ini
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
func Unmarshal(data []byte, v interface{}) error {
if data == nil {
return nil
}
mp := New().Load(data).Marshal2Map()
fmt.Println("map:", mp)
bindTag("ini", v, mp)
return nil
}
// Bind binds the content of data into the struct s
func bindTag(tagName string, s interface{}, data map[string]interface{}) interface{} {
if s == nil {
return nil
}
t := reflect.TypeOf(s)
tk := t.Kind()
if tk != reflect.Ptr {
return nil
}
t = t.Elem()
tk = t.Kind()
if tk != reflect.Struct {
return nil
}
v := reflect.ValueOf(s).Elem()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
fi := parseField(tagName, f)
fv := v.FieldByName(fi.Name)
fk := fv.Kind()
ft := fv.Type()
if v, ok := data[fi.Alias]; ok {
vt := reflect.TypeOf(v)
vk := vt.Kind()
if !vt.AssignableTo(ft) {
change_fun := func(k reflect.Kind, v interface{}) (val reflect.Value, err error) {
err = errors.New("unsupper kind")
var str_v string
var ok bool
if str_v, ok = v.(string); !ok {
return
}
// str_v = v.(string)
if k == reflect.Int {
rv, er := strconv.Atoi(str_v)
if er == nil {
val = reflect.ValueOf(rv)
err = nil
}
} else if k == reflect.Int8 {
rv, er := strconv.ParseInt(str_v, 10, 8)
if er == nil {
val = reflect.ValueOf(rv)
err = nil
}
} else if k == reflect.Int16 {
rv, er := strconv.ParseInt(str_v, 10, 16)
if er == nil {
val = reflect.ValueOf(rv)
err = nil
}
} else if k == reflect.Int32 {
rv, er := strconv.ParseInt(str_v, 10, 32)
if er == nil {
val = reflect.ValueOf(rv)
err = nil
}
} else if k == reflect.Int64 {
rv, er := strconv.ParseInt(str_v, 10, 64)
if er == nil {
val = reflect.ValueOf(rv)
err = nil
}
} else if k == reflect.Float32 {
rv, er := strconv.ParseFloat(str_v, 32)
if er == nil {
val = reflect.ValueOf(float32(rv))
err = nil
}
} else if k == reflect.Float64 {
rv, er := strconv.ParseFloat(str_v, 64)
if er == nil {
val = reflect.ValueOf(rv)
err = nil
}
}
return val, err
}
val, err := change_fun(fk, v)
if err == nil {
fv.Set(val)
continue
}
} else {
fv.Set(reflect.ValueOf(v))
continue
}
if fk == reflect.Struct && vk == reflect.Map {
if fv.CanInterface() {
bindTag(tagName, fv.Addr().Interface(), v.(map[string]interface{}))
continue
}
}
}
}
return s
}
func bindSlice(tagName string, s reflect.Value, data []interface{}) {
// sk := s.Kind()
et := s.Type().Elem()
ek := et.Kind()
ret := reflect.MakeSlice(et, s.Len(), s.Cap())
vet := reflect.TypeOf(data).Elem()
if vet.AssignableTo(et) {
for i := 0; i < s.Len(); i++ {
ret.Index(i).Set(reflect.ValueOf(data[i]))
}
} else if ek == reflect.Struct {
for i := 0; i < s.Len(); i++ {
// v := Bind(tagName, ret.Index(i).Addr().Interface(), data[i].(map[string]interface{}))
v := bindTag(tagName, ret.Index(i).Addr().Interface(), data[i].(map[string]interface{}))
ret.Index(i).Set(reflect.ValueOf(v))
}
}
}
type fieldInfo struct {
Alias string
Name string
}
// ParseField parses [FieldInfo] for the given struct field [f] from struct tag with name [tagName]
func parseField(tagName string, f reflect.StructField) *fieldInfo {
var parts []string
alias := f.Name
tag, tagOk := f.Tag.Lookup(tagName)
if tagOk {
partsTemp := strings.Split(tag, ",")
parts = make([]string, 0, len(partsTemp))
for i := 0; i < len(partsTemp); i++ {
part := strings.TrimSpace(partsTemp[i])
if len(part) != 0 {
parts = append(parts, part)
}
}
}
if len(parts) != 0 {
alias = parts[0]
// TODO parse other tags
}
return &fieldInfo{
Alias: alias,
Name: f.Name,
}
}