-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicprocessor.go
407 lines (384 loc) · 10.2 KB
/
dynamicprocessor.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
package dynamictags
import (
"errors"
"reflect"
"slices"
"strconv"
"strings"
)
// Base struct for dynamic tag processors.
type DynamicTagProcessor struct {
dictionary map[string]string
converters []TagConverterer
}
// Init dynamic processor
func (processor *DynamicTagProcessor) InitProcessor() {
processor.dictionary = make(map[string]string)
}
// Set dictionary.
// Parameters:
// - dict dictionary.
func (processor *DynamicTagProcessor) SetDictionary(dict map[string]string) {
processor.dictionary = dict
}
// Returns dictionary.
// Returns:
// - dictionary.
func (processor DynamicTagProcessor) GetDictionary() map[string]string {
return processor.dictionary
}
// Set dictionary value.
// Parameters:
// - key key
// - value value
func (processor *DynamicTagProcessor) SetDictionaryValue(key string, value string) {
processor.dictionary[key] = value
}
// Remove dictionary value.
// Parameters:
// - key key
func (processor *DynamicTagProcessor) RemoveDictionaryValue(key string) {
delete(processor.dictionary, key)
}
// Add tag converter
// Parameters:
// - converter tag converter.
func (processor *DynamicTagProcessor) AddTagConverter(converter TagConverterer) {
processor.converters = append(processor.converters, converter)
}
// Fill structure fields by tags
// Parameters:
// - data pointer to filled structure.
// - blackList black list of fields. This fields will be ignored during processing
// Path to fields should be in the json path format. For example ($.InternalStructure.Field1)
// Only simple paths is supported (without filters)
//
// Returns:
// - error in case of error or nil
//
// For example we have processor (EnvProcessor) which process envoironment
// variables and struct which is defined in follow way:
//
// type Data struct {
// EnvValue string `env:"${SERVER_NAME}_VALUE"`
// }
//
// During processing:
// - get value of 'SERVER_NAME' key from processor dictionary
// - if the value is not exists get 'SERVER_NAME' environment variable
// - if the environment variable is not defined use empty string
// - replace 'SERVER_NAME' by the value. For example if 'SERVER_NAME' is set
// to 'TEST' tag will be 'TEST_VALUE'
// - call function processor.GetSimpleValue with tag 'TEST_VALUE' which calulate
// 'EnvValue' field value. Default implementation just returns tag value. This
// behavouir can be changed in inhereted processor
func (processor DynamicTagProcessor) Process(data any, blackList []string) error {
v := reflect.ValueOf(data)
t := reflect.TypeOf(data)
if v.Kind() != reflect.Pointer || v.Elem().Kind() != reflect.Struct {
return errors.New("pointer to structure is expected")
}
tagpaths := make(map[string]string)
err := processor.processStructure(t, v, "$", tagpaths, blackList)
return err
}
func (processor DynamicTagProcessor) convertBool(val any) (bool, error) {
res, ok := val.(bool)
if ok {
return res, nil
}
return false, errors.New("unsupported type")
}
func (processor DynamicTagProcessor) convertFloat(val any) (float64, error) {
val32, ok := val.(float32)
if ok {
return float64(val32), nil
}
val64, ok := val.(float64)
if ok {
return val64, nil
}
return 0, errors.New("unsopported type")
}
func (processor DynamicTagProcessor) convertUInt(val any) (uint64, error) {
valInt, ok := val.(uint)
if ok {
return uint64(valInt), nil
}
val8, ok := val.(uint8)
if ok {
return uint64(val8), nil
}
val16, ok := val.(uint16)
if ok {
return uint64(val16), nil
}
val32, ok := val.(uint32)
if ok {
return uint64(val32), nil
}
val64, ok := val.(uint64)
if ok {
return val64, nil
}
return 0, errors.New("unsopported type")
}
func (processor DynamicTagProcessor) convertInt(val any) (int64, error) {
valInt, ok := val.(int)
if ok {
return int64(valInt), nil
}
val8, ok := val.(int8)
if ok {
return int64(val8), nil
}
val16, ok := val.(int16)
if ok {
return int64(val16), nil
}
val32, ok := val.(int32)
if ok {
return int64(val32), nil
}
val64, ok := val.(int64)
if ok {
return val64, nil
}
return 0, errors.New("unsopported type")
}
func (processor DynamicTagProcessor) getUIntInterfaceValue(val any) (uint64, error) {
valUInt, err := processor.convertUInt(val)
if err == nil {
return valUInt, nil
}
valInt, err := processor.convertInt(val)
if err == nil {
return uint64(valInt), nil
}
valFloat, err := processor.convertFloat(val)
if err == nil {
return uint64(valFloat), err
}
return 0, err
}
func (processor DynamicTagProcessor) getIntInterfaceValue(val any) (int64, error) {
valInt, err := processor.convertInt(val)
if err == nil {
return valInt, nil
}
valUint, err := processor.convertUInt(val)
if err == nil {
return int64(valUint), nil
}
valFloat, err := processor.convertFloat(val)
if err == nil {
return int64(valFloat), err
}
return 0, err
}
func (processor DynamicTagProcessor) getFloatInterfaceValue(val any) (float64, error) {
valFloat, err := processor.convertFloat(val)
if err == nil {
return valFloat, nil
}
valInt, err := processor.convertInt(val)
if err == nil {
return float64(valInt), nil
}
valUInt, err := processor.convertUInt(val)
if err == nil {
return float64(valUInt), err
}
return 0, err
}
func (processor DynamicTagProcessor) convertSliceInterface(src []interface{}) ([]string, error) {
res := make([]string, 0, len(src))
for _, srcData := range src {
str, ok := srcData.(string)
if !ok {
return res, errors.New("incompatible slice elements type")
}
res = append(res, str)
}
return res, nil
}
func (processor DynamicTagProcessor) convertSliceString(src string) ([]string, error) {
res := strings.Split(src, ",")
return res, nil
}
func (processor DynamicTagProcessor) setInterfaceSimpleValue(t reflect.StructField, v reflect.Value, val any, path string) error {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
valInt, err := processor.getIntInterfaceValue(val)
if err != nil {
return err
}
v.SetInt(valInt)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
valUInt, err := processor.getUIntInterfaceValue(val)
if err != nil {
return err
}
v.SetUint(valUInt)
case reflect.Float32, reflect.Float64:
valFloat, err := processor.getFloatInterfaceValue(val)
if err != nil {
return err
}
v.SetFloat(valFloat)
case reflect.Bool:
valBool, err := processor.convertBool(val)
if err != nil {
return err
}
v.SetBool(valBool)
case reflect.Slice:
slice, ok := val.([]interface{})
if ok {
sliceVal, err := processor.convertSliceInterface(slice)
if err != nil {
return err
}
v.Set(reflect.ValueOf(sliceVal))
}
}
return nil
}
func (processor DynamicTagProcessor) setStringSimpleValue(t reflect.StructField, v reflect.Value, val string, path string) error {
switch t.Type.Kind() {
case reflect.String:
v.SetString(val)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return err
}
if v.OverflowInt(n) {
return errors.New("int value overflow. Path: " + path)
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return err
}
if v.OverflowUint(n) {
return errors.New("uint value overflow. Path: " + path)
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(val, v.Type().Bits())
if err != nil {
return err
}
if v.OverflowFloat(n) {
return errors.New("float value overflow. Path: " + path)
}
v.SetFloat(n)
case reflect.Bool:
n, err := strconv.ParseBool(val)
if err != nil {
return err
}
v.SetBool(n)
case reflect.Slice:
sliceVal, err := processor.convertSliceString(val)
if err != nil {
return err
}
v.Set(reflect.ValueOf(sliceVal))
default:
return errors.New("unexpected key type")
}
return nil
}
func (processor DynamicTagProcessor) processSimpleType(t reflect.StructField, v reflect.Value, tagpaths map[string]string, path string) error {
for _, converter := range processor.converters {
tag := converter.GetTag()
tagVal := t.Tag.Get(tag)
if tagVal == "" {
continue
}
res, err := ProcessString(tagVal, processor.dictionary)
if err != nil {
return err
}
tagPath, ok := tagpaths[tag]
if !ok {
tagPath = path
}
val, isSet, err := converter.GetSimpleValue(res, t, v, tagPath)
if err != nil {
return err
}
if isSet {
strVal, ok := val.(string)
if ok {
err = processor.setStringSimpleValue(t, v, strVal, path)
} else {
err = processor.setInterfaceSimpleValue(t, v, val, path)
}
return err
}
}
return nil
}
func (processor DynamicTagProcessor) fillTagsPath(t reflect.StructField, tagPaths map[string]string) error {
for _, converter := range processor.converters {
tag := converter.GetTag()
tagVal := t.Tag.Get(tag)
tagVal, err := ProcessString(tagVal, processor.dictionary)
if err != nil {
return err
}
currTagPath := tagVal
if !strings.HasPrefix(tagVal, "$") {
if tagVal == "" {
tagVal = t.Name
}
var ok bool
currTagPath, ok = tagPaths[tag]
if !ok {
currTagPath = "$"
}
currTagPath = currTagPath + "." + tagVal
}
tagPaths[tag] = currTagPath
}
return nil
}
func (processor DynamicTagProcessor) processStructure(t reflect.Type, v reflect.Value, path string, tagpaths map[string]string, blackList []string) error {
var structValue reflect.Value
var structType reflect.Type
if v.Kind() == reflect.Pointer {
structValue = v.Elem()
structType = t.Elem()
} else {
structValue = v
structType = t
}
for i := 0; i < structValue.NumField(); i++ {
fieldValue := structValue.Field(i)
fieldType := structType.Field(i)
if !fieldValue.CanSet() {
continue
}
var err error = nil
currPath := path + "." + fieldType.Name
if blackList == nil || !slices.Contains(blackList, currPath) {
if fieldValue.Kind() == reflect.Struct {
err = processor.fillTagsPath(fieldType, tagpaths)
if err != nil {
return err
}
err = processor.processStructure(fieldType.Type, fieldValue, currPath, tagpaths, blackList)
} else {
err = processor.processSimpleType(fieldType, fieldValue, tagpaths, path)
}
}
if err != nil {
return err
}
}
return nil
}