-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparams.go
More file actions
94 lines (84 loc) · 2.2 KB
/
params.go
File metadata and controls
94 lines (84 loc) · 2.2 KB
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
// Copyright (c) 2017, Paessler AG <support@paessler.com>
// All rights reserved.
package goeval
import (
"reflect"
)
func SelectValue(value any, key string) any {
if value == nil {
return nil
}
vv := reflect.ValueOf(value)
vvElem := resolvePotentialPointer(vv)
switch vvElem.Kind() {
case reflect.Map:
mapKey, ok := reflectConvertTo(vv.Type().Key().Kind(), key)
if !ok {
return nil
}
vvElem = vv.MapIndex(reflect.ValueOf(mapKey))
vvElem = resolvePotentialPointer(vvElem)
if vvElem.IsValid() {
return vvElem.Interface()
}
case reflect.Slice, reflect.Array, reflect.String:
if i := NewValue("", key).Int(); i >= 0 && vv.Len() > i {
vvElem = resolvePotentialPointer(vv.Index(i))
return vvElem.Interface()
}
case reflect.Struct:
field := vvElem.FieldByName(key)
if field.IsValid() {
return field.Interface()
}
method := vv.MethodByName(key)
if method.IsValid() {
return method.Interface()
}
default:
return nil
}
return nil
}
func resolvePotentialPointer(value reflect.Value) reflect.Value {
if value.Kind() == reflect.Ptr {
return value.Elem()
}
return value
}
func reflectConvertTo(k reflect.Kind, value any) (any, bool) {
switch k {
case reflect.String:
return NewValue("", value).String(), true
case reflect.Interface:
return value, true
case reflect.Int:
return NewValue("", value).Int(), true
case reflect.Float64:
return NewValue("", value).Float(), true
case reflect.Float32:
return float32(NewValue("", value).Float()), true
case reflect.Bool:
return NewValue("", value).Boolean(), true
case reflect.Int8:
return int8(NewValue("", value).Int()), true
case reflect.Int16:
return int16(NewValue("", value).Int()), true
case reflect.Int32:
return int32(NewValue("", value).Int()), true
case reflect.Int64:
return int64(NewValue("", value).Int()), true
case reflect.Uint:
return uint(NewValue("", value).Int()), true
case reflect.Uint8:
return uint8(NewValue("", value).Int()), true
case reflect.Uint16:
return uint16(NewValue("", value).Int()), true
case reflect.Uint32:
return uint32(NewValue("", value).Int()), true
case reflect.Uint64:
return uint64(NewValue("", value).Int()), true
default:
return nil, false
}
}