forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
165 lines (150 loc) · 3.92 KB
/
runtime.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
package expr
import (
"fmt"
"reflect"
)
func toNumber(n Node, val interface{}) float64 {
v, ok := cast(val)
if ok {
return v
}
panic(fmt.Sprintf("cannot convert %v (type %T) to type float64", n, val))
}
func cast(val interface{}) (float64, bool) {
v := reflect.ValueOf(val)
switch v.Kind() {
case reflect.Float32, reflect.Float64:
return v.Float(), true
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(v.Int()), true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(v.Uint()), true // TODO: Check if uint64 fits into float64.
}
return 0, false
}
func isNumber(val interface{}) bool {
return val != nil && reflect.TypeOf(val).Kind() == reflect.Float64
}
func canBeNumber(val interface{}) bool {
if val != nil {
return isNumberType(reflect.TypeOf(val))
}
return false
}
func equal(left, right interface{}) bool {
if isNumber(left) && canBeNumber(right) {
right, _ := cast(right)
return left == right
} else if canBeNumber(left) && isNumber(right) {
left, _ := cast(left)
return left == right
} else {
return reflect.DeepEqual(left, right)
}
}
func extract(val interface{}, i interface{}) (interface{}, bool) {
v := reflect.ValueOf(val)
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.String:
n, ok := cast(i)
if !ok {
break
}
value := v.Index(int(n))
if value.IsValid() && value.CanInterface() {
return value.Interface(), true
}
case reflect.Map:
value := v.MapIndex(reflect.ValueOf(i))
if value.IsValid() && value.CanInterface() {
return value.Interface(), true
}
case reflect.Struct:
value := v.FieldByName(reflect.ValueOf(i).String())
if value.IsValid() && value.CanInterface() {
return value.Interface(), true
}
case reflect.Ptr:
value := v.Elem()
if value.IsValid() && value.CanInterface() {
return extract(value.Interface(), i)
}
}
return nil, false
}
func getFunc(val interface{}, i interface{}) (interface{}, bool) {
v := reflect.ValueOf(val)
d := v
if v.Kind() == reflect.Ptr {
d = v.Elem()
}
switch d.Kind() {
case reflect.Map:
value := d.MapIndex(reflect.ValueOf(i))
if value.IsValid() && value.CanInterface() {
return value.Interface(), true
}
case reflect.Struct:
name := reflect.ValueOf(i).String()
method := v.MethodByName(name)
if method.IsValid() && method.CanInterface() {
return method.Interface(), true
}
// If struct has not method, maybe it has func field.
// To access this field we need dereferenced value.
value := d.FieldByName(name)
if value.IsValid() && value.CanInterface() {
return value.Interface(), true
}
}
return nil, false
}
func contains(needle interface{}, array interface{}) (bool, error) {
if array != nil {
v := reflect.ValueOf(array)
switch v.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < v.Len(); i++ {
value := v.Index(i)
if value.IsValid() && value.CanInterface() {
if equal(value.Interface(), needle) {
return true, nil
}
}
}
return false, nil
case reflect.Map:
n := reflect.ValueOf(needle)
if !n.IsValid() {
return false, fmt.Errorf("cannot use %T as index to %T", needle, array)
}
value := v.MapIndex(n)
if value.IsValid() {
return true, nil
}
return false, nil
case reflect.Struct:
n := reflect.ValueOf(needle)
if !n.IsValid() || n.Kind() != reflect.String {
return false, fmt.Errorf("cannot use %T as field name of %T", needle, array)
}
value := v.FieldByName(n.String())
if value.IsValid() {
return true, nil
}
return false, nil
case reflect.Ptr:
value := v.Elem()
if value.IsValid() && value.CanInterface() {
return contains(needle, value.Interface())
}
return false, nil
}
return false, fmt.Errorf("operator \"in\" not defined on %T", array)
}
return false, nil
}
func isNil(val interface{}) bool {
v := reflect.ValueOf(val)
return !v.IsValid() || v.IsNil()
}