-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
309 lines (258 loc) · 8.18 KB
/
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
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
package tag
import (
"fmt"
"reflect"
"strconv"
"github.com/go-zoox/tag/attribute"
"github.com/go-zoox/tag/datasource"
)
// Tag is a struct tag parser and decoder
type Tag struct {
Name string
DataSource datasource.DataSource
}
// New creates a new Tag
func New(name string, dataSource datasource.DataSource) *Tag {
return &Tag{
Name: name,
DataSource: dataSource,
}
}
// Decode decodes the given struct pointer from data source.
func (t *Tag) Decode(ptr interface{}) error {
return t.decodeR(ptr, "")
}
func (t *Tag) decodeR(ptr interface{}, keyPathParent string) error {
tagName, dataSource := t.Name, t.DataSource
rt := reflect.TypeOf(ptr).Elem()
rv := reflect.ValueOf(ptr).Elem()
// example:
// redis.host
// config.redis.host
for i := 0; i < rt.NumField(); i++ {
rtt := rt.Field(i)
rvv := rv.Field(i)
attribute := attribute.New(rtt.Name, rtt.Type.String(), keyPathParent, rtt.Tag.Get(tagName))
if err := attribute.SetValue(dataSource.Get(attribute.GetDataSourceKeyPath(), attribute.GetDataSourceKey())); err != nil {
return err
}
if err := t.setValue(rtt.Type, rvv, attribute); err != nil {
return err
}
}
return nil
}
func (t *Tag) setValue(rt reflect.Type, rv reflect.Value, attribute *attribute.Attribute) error {
value := attribute.GetValue()
// if data source value is nil, should not set the value
if value == nil {
return nil
// // @TODO if value is nil, create a new instance of the type
// // this action cause the struct data can be setted in recursively
// if attribute.Value == nil {
// attribute.Value = reflect.New(rt).Elem().Interface()
// }
// value = attribute.GetValue()
}
switch rv.Kind() {
case reflect.String:
rv.SetString(value.(string))
case reflect.Bool:
rv.SetBool(value.(bool))
case reflect.Int64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
if err := t.setValueInt(rv, value); err != nil {
return fmt.Errorf("setValueInt error at key %s, expect type(%s) (detail: %s)", attribute.GetDataSourceKeyPath(), rv.Kind(), err)
}
case reflect.Float64, reflect.Float32:
if err := t.setValueFloat(rv, value); err != nil {
return fmt.Errorf("setValueFloat error at key %s, expect type(%s) (detail: %s)", attribute.GetDataSourceKeyPath(), rv.Kind(), err)
}
case reflect.Struct:
if err := t.decodeR(rv.Addr().Interface(), attribute.GetDataSourceKeyPath()); err != nil {
return fmt.Errorf("struct decode error at key %s, expect type(%s) (detail: %s)", attribute.GetDataSourceKeyPath(), rv.Kind(), err)
}
case reflect.Slice:
if err := t.setValueSlice(rt, rv, value, attribute); err != nil {
return err
}
case reflect.Map:
if err := t.setValueMap(rt, rv, value, attribute); err != nil {
return err
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if err := t.setValueInt(rv, value); err != nil {
return fmt.Errorf("setValueInt error at key %s, expect type(%s) (detail: %s)", attribute.GetDataSourceKeyPath(), rv.Kind(), err)
}
default:
return fmt.Errorf("type(%s) is not supported at %s, fatal err", rv.Kind(), attribute.GetDataSourceKeyPath())
}
return nil
}
func (t *Tag) setValueInt(rv reflect.Value, value any) error {
switch v := value.(type) {
case int64:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(v)
}
case int:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(int64(v))
}
case float32:
rv.SetFloat(float64(v))
case float64:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(int64(v))
}
case int8:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(int64(v))
}
case int16:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(int64(v))
}
case int32:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(int64(v))
}
case uint:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(int64(v))
}
case uint8:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(int64(v))
}
case uint16:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(int64(v))
}
case uint32:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(int64(v))
}
case uint64:
switch rv.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
rv.SetUint(uint64(v))
default:
rv.SetInt(int64(v))
}
default:
return fmt.Errorf("setValueInt unknown value(%v) type: %s", value, reflect.TypeOf(value).Kind())
}
return nil
}
func (t *Tag) setValueFloat(rv reflect.Value, value any) error {
switch v := value.(type) {
case float64:
rv.SetFloat(v)
case float32:
rv.SetFloat(float64(v))
default:
return fmt.Errorf("setValueFloat unknown value type: %s", reflect.TypeOf(value).Kind())
}
return nil
}
func (t *Tag) setValueSlice(rt reflect.Type, rv reflect.Value, value any, attribute *attribute.Attribute) error {
s := reflect.ValueOf(value)
for index := 0; index < s.Len(); index++ {
switch v := s.Index(index).Interface().(type) {
case string, bool, int64, float64:
rv.Set(reflect.Append(rv, reflect.ValueOf(v)))
case float32:
rv.Set(reflect.Append(rv, reflect.ValueOf(float64(v))))
case int:
rv.Set(reflect.Append(rv, reflect.ValueOf(int64(v))))
case int32:
rv.Set(reflect.Append(rv, reflect.ValueOf(int64(v))))
case int16:
rv.Set(reflect.Append(rv, reflect.ValueOf(int64(v))))
case int8:
rv.Set(reflect.Append(rv, reflect.ValueOf(int64(v))))
case uint:
rv.Set(reflect.Append(rv, reflect.ValueOf(int64(v))))
case uint64:
rv.Set(reflect.Append(rv, reflect.ValueOf(int64(v))))
case uint32:
rv.Set(reflect.Append(rv, reflect.ValueOf(int64(v))))
case uint16:
rv.Set(reflect.Append(rv, reflect.ValueOf(int64(v))))
case uint8:
rv.Set(reflect.Append(rv, reflect.ValueOf(int64(v))))
case uintptr:
rv.Set(reflect.Append(rv, reflect.ValueOf(int64(v))))
default:
value := reflect.New(rt.Elem())
if err := t.decodeR(reflect.Value(value).Interface(), attribute.GetDataSourceKeyPath()+"."+strconv.Itoa(index)); err != nil {
return fmt.Errorf("%s is not slice(%s)", attribute.DataKey, err.Error())
}
// j, _ := json.MarshalIndent(value.Elem().Interface(), "", " ")
// fmt.Println("value:", string(j))
rv.Set(reflect.Append(rv, value.Elem()))
}
}
return nil
}
func (t *Tag) setValueMap(rt reflect.Type, rv reflect.Value, value any, attribute *attribute.Attribute) error {
// support map[string]any
// rv.Set(reflect.ValueOf(value))
// @TODO support map[string]T
// such as map[string]string
// https://stackoverflow.com/questions/7850140/how-do-you-create-a-new-instance-of-a-struct-from-its-type-at-run-time-in-go
newMap := reflect.MakeMap(rt)
values := reflect.ValueOf(value)
for _, k := range values.MapKeys() {
v := values.MapIndex(k)
// fmt.Println("vvv:", k, v, attribute.GetKey()+"."+k.String(), rt.Elem() == v.Elem().Type(), reflect.TypeOf(v.Elem().Interface()))
// nil
if v.IsNil() {
newMap.SetMapIndex(k, v.Elem())
continue
}
// same type
if rt.Elem() == v.Elem().Type() {
newMap.SetMapIndex(k, v.Elem())
continue
}
// map => struct
value := reflect.New(rt.Elem())
if err := t.decodeR(value.Interface(), attribute.GetDataSourceKeyPath()+"."+k.String()); err != nil {
return fmt.Errorf("%s is not map(%s)", attribute.DataKey, err.Error())
}
newMap.SetMapIndex(k, value.Elem())
}
rv.Set(newMap)
return nil
}