forked from project-flogo/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
415 lines (341 loc) · 9.11 KB
/
path.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
package path
import (
"encoding/json"
"errors"
"fmt"
"github.com/VSBLTY-Inc/core/data/mapper/config"
"reflect"
"strconv"
"strings"
"unicode"
)
var skipMissing = config.IsMappingSkipMissingOn()
//todo consolidate and optimize code
func GetValue(value interface{}, path string) (interface{}, error) {
if path == "" {
return value, nil
}
var newVal interface{}
var err error
var newPath string
//To interface if it is an string
if val, ok := value.(string); ok {
var in interface{}
err = json.Unmarshal([]byte(val), &in)
if err != nil {
return nil, err
}
value = in
}
if strings.HasPrefix(path, ".") {
if objVal, ok := value.(map[string]interface{}); ok {
newVal, newPath, err = getSetObjValue(objVal, path, nil, false)
} else if paramsVal, ok := value.(map[string]string); ok {
newVal, newPath, err = getSetParamsValue(paramsVal, path, nil, false)
} else {
fieldName, npIdx := getObjectKey(path[1:])
newVal, err = getFieldValueByName(value, fieldName)
if err != nil {
return nil, err
}
newPath = path[npIdx:]
}
} else if hasMapKey(path) {
if objVal, ok := value.(map[string]interface{}); ok {
newVal, newPath, err = getSetMapValue(objVal, path, nil, false)
} else if paramsVal, ok := value.(map[string]string); ok {
newVal, newPath, err = getSetMapParamsValue(paramsVal, path, nil, false)
} else {
fieldName, npIdx := getMapKey(path[2:])
newVal, err = getFieldValueByName(value, fieldName)
if err != nil {
return nil, err
}
newPath = path[npIdx:]
}
} else if strings.HasPrefix(path, "[") {
newVal, newPath, err = getSetArrayValue(value, path, nil, false)
} else {
return nil, fmt.Errorf("unable to evaluate path: %s", path)
}
if err != nil {
return nil, err
}
return GetValue(newVal, newPath)
}
func getFieldValueByName(object interface{}, name string) (interface{}, error) {
if object == nil && skipMissing {
return nil, nil
}
val := reflect.ValueOf(object)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() == reflect.Struct {
field := val.FieldByName(NormalizeFieldName(name))
if field.IsValid() {
return field.Interface(), nil
}
typ := reflect.TypeOf(object)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
jsonTag := GetJsonTag(p)
if len(jsonTag) > 0 && name == jsonTag {
return val.FieldByName(typ.Field(i).Name).Interface(), nil
}
}
}
} else if val.Kind() == reflect.Map {
v := val.MapIndex(reflect.ValueOf(name))
return v.Interface(), nil
}
return nil, fmt.Errorf("unable to evaluate path: %s", name)
}
func GetJsonTag(t reflect.StructField) string {
if jsonTag := t.Tag.Get("json"); jsonTag != "" && jsonTag != "-" {
var commaIdx int
if commaIdx = strings.Index(jsonTag, ","); commaIdx < 0 {
commaIdx = len(jsonTag)
}
return jsonTag[:commaIdx]
}
return ""
}
func NormalizeFieldName(name string) string {
symbols := []rune(name)
symbols[0] = unicode.ToUpper(symbols[0])
return string(symbols)
}
func SetValue(attrValue interface{}, path string, value interface{}) error {
if path == "" || attrValue == nil {
return nil
}
var newVal interface{}
var err error
var newPath string
if strings.HasPrefix(path, ".") {
if objVal, ok := attrValue.(map[string]interface{}); ok {
newVal, newPath, err = getSetObjValue(objVal, path, value, true)
} else if paramsVal, ok := attrValue.(map[string]string); ok {
newVal, newPath, err = getSetParamsValue(paramsVal, path, value, true)
} else {
return fmt.Errorf("unable to evaluate path: %s", path)
}
} else if hasMapKey(path) {
if objVal, ok := attrValue.(map[string]interface{}); ok {
newVal, newPath, err = getSetMapValue(objVal, path, value, true)
} else if paramsVal, ok := attrValue.(map[string]string); ok {
newVal, newPath, err = getSetMapParamsValue(paramsVal, path, value, true)
} else {
return fmt.Errorf("unable to evaluate path: %s", path)
}
} else if strings.HasPrefix(path, "[") {
newVal, newPath, err = getSetArrayValue(attrValue, path, value, true)
} else {
return fmt.Errorf("unable to evaluate path: %s", path)
}
if err != nil {
return err
}
return SetValue(newVal, newPath, value)
}
func hasMapKey(path string) bool {
return strings.HasPrefix(path, `["`) || strings.HasPrefix(path, `['`)
}
func equalMapKey(val string) bool {
return val == `["` || val == `['`
}
func getObjectKey(s string) (string, int) {
i := 0
for i < len(s) {
if s[i] == '.' || s[i] == '[' {
return s[:i], i + 1
}
i += 1
}
return s, len(s) + 1
}
func getMapKey(s string) (string, int) {
i := 0
for i < len(s) {
if s[i] == '"' || s[i] == '\'' {
return s[:i], i + 4 // [" "]
}
i += 1
}
return s, len(s) + 1
}
func getSetArrayValue(obj interface{}, path string, value interface{}, set bool) (interface{}, string, error) {
arrValue, valid := obj.([]interface{})
if !valid {
//Try to convert to a array in case it is a array string
val, err := toArray(obj)
if err != nil {
return nil, path, errors.New("'" + path + "' not an array")
}
arrValue = val
}
closeIdx := strings.Index(path, "]")
if closeIdx == -1 {
return nil, path, errors.New("'" + path + "' not an array")
}
arrayIdx, err := strconv.Atoi(path[1:closeIdx])
if err != nil {
return nil, path, errors.New("Invalid array index: " + path[1:closeIdx])
}
if arrayIdx >= len(arrValue) {
return nil, path, errors.New("Array index '" + path + "' out of range.")
}
if set && closeIdx == len(path)-1 {
arrValue[arrayIdx] = value
return nil, "", nil
}
return arrValue[arrayIdx], path[closeIdx+1:], nil
}
func getSetObjValue(objValue map[string]interface{}, path string, value interface{}, set bool) (interface{}, string, error) {
key, npIdx := getObjectKey(path[1:])
if set && key == path[1:] {
//end of path so set the value
objValue[key] = value
return nil, "", nil
}
val, found := objValue[key]
if !found {
if path == "."+key {
return nil, "", nil
}
if skipMissing {
return nil, "", nil
}
return nil, "", errors.New("Invalid path '" + path + "'. path not found.")
}
return val, path[npIdx:], nil
}
func getSetParamsValue(params map[string]string, path string, value interface{}, set bool) (interface{}, string, error) {
key, _ := getObjectKey(path[1:])
if set && key == path[1:] {
//end of path so set the value
paramVal, err := toString(value)
if err != nil {
return nil, "", err
}
params[key] = paramVal
return nil, "", nil
}
val, found := params[key]
if !found {
return "", "", nil
}
return val, "", nil
}
func getSetMapValue(objValue map[string]interface{}, path string, value interface{}, set bool) (interface{}, string, error) {
key, npIdx := getMapKey(path[2:])
if set && (key+`"]` == path[2:] || key+`']` == path[2:]) {
//end of path so set the value
objValue[key] = value
return nil, "", nil
}
val, found := objValue[key]
if !found {
if path == "."+key {
return nil, "", nil
}
if skipMissing {
return nil, "", nil
}
return nil, "", errors.New("Invalid path '" + path + "'. path not found.")
}
return val, path[npIdx:], nil
}
func getSetMapParamsValue(params map[string]string, path string, value interface{}, set bool) (interface{}, string, error) {
key, _ := getMapKey(path[2:])
if set && key+`"]` == path[2:] {
//end of path so set the value
paramVal, err := toString(value)
if err != nil {
return nil, "", err
}
params[key] = paramVal
return nil, "", nil
}
val, found := params[key]
if !found {
return "", "", nil
}
return val, "", nil
}
func Deconstruct(fullPath string) (attrName string, path string, err error) {
idx := strings.IndexFunc(fullPath, isSep)
if idx == -1 {
return fullPath, "", nil
}
return fullPath[:idx], fullPath[idx:], nil
}
func isSep(r rune) bool {
return r == '.' || r == '['
}
func toString(val interface{}) (string, error) {
switch t := val.(type) {
case string:
return t, nil
case int:
return strconv.Itoa(t), nil
case int64:
return strconv.FormatInt(t, 10), nil
case float32:
return strconv.FormatFloat(float64(t), 'f', -1, 64), nil
case float64:
return strconv.FormatFloat(t, 'f', -1, 64), nil
case json.Number:
return t.String(), nil
case bool:
return strconv.FormatBool(t), nil
case nil:
return "", nil
case []byte:
return string(t), nil
default:
b, err := json.Marshal(t)
if err != nil {
return "", fmt.Errorf("unable to coerce %#v to string", t)
}
return string(b), nil
}
}
func toArray(val interface{}) ([]interface{}, error) {
switch t := val.(type) {
case []interface{}:
return t, nil
case []map[string]interface{}:
var a []interface{}
for _, v := range t {
a = append(a, v)
}
return a, nil
case string:
a := make([]interface{}, 0)
if t != "" {
err := json.Unmarshal([]byte(t), &a)
if err != nil {
a = append(a, t)
}
}
return a, nil
case nil:
return nil, nil
default:
s := reflect.ValueOf(val)
if s.Kind() == reflect.Slice {
a := make([]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
a[i] = s.Index(i).Interface()
}
return a, nil
}
return nil, fmt.Errorf("unable to coerce %#v to []interface{}", val)
}
}