forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin_array.go
68 lines (61 loc) · 1.49 KB
/
in_array.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
package optimizer
import (
"reflect"
. "github.com/expr-lang/expr/ast"
)
type inArray struct{}
func (*inArray) Visit(node *Node) {
switch n := (*node).(type) {
case *BinaryNode:
if n.Operator == "in" {
if array, ok := n.Right.(*ArrayNode); ok {
if len(array.Nodes) > 0 {
t := n.Left.Type()
if t == nil || t.Kind() != reflect.Int {
// This optimization can be only performed if left side is int type,
// as runtime.in func uses reflect.Map.MapIndex and keys of map must,
// be same as checked value type.
goto string
}
for _, a := range array.Nodes {
if _, ok := a.(*IntegerNode); !ok {
goto string
}
}
{
value := make(map[int]struct{})
for _, a := range array.Nodes {
value[a.(*IntegerNode).Value] = struct{}{}
}
m := &ConstantNode{Value: value}
m.SetType(reflect.TypeOf(value))
patchCopyType(node, &BinaryNode{
Operator: n.Operator,
Left: n.Left,
Right: m,
})
}
string:
for _, a := range array.Nodes {
if _, ok := a.(*StringNode); !ok {
return
}
}
{
value := make(map[string]struct{})
for _, a := range array.Nodes {
value[a.(*StringNode).Value] = struct{}{}
}
m := &ConstantNode{Value: value}
m.SetType(reflect.TypeOf(value))
patchCopyType(node, &BinaryNode{
Operator: n.Operator,
Left: n.Left,
Right: m,
})
}
}
}
}
}
}