MCPcopy Index your code
hub / github.com/expr-lang/expr / Visit

Method Visit

optimizer/in_array.go:11–68  ·  view source on GitHub ↗
(node *Node)

Source from the content-addressed store, hash-verified

9type inArray struct{}
10
11func (*inArray) Visit(node *Node) {
12 switch n := (*node).(type) {
13 case *BinaryNode:
14 if n.Operator == "in" {
15 if array, ok := n.Right.(*ArrayNode); ok {
16 if len(array.Nodes) > 0 {
17 t := n.Left.Type()
18 if t == nil || t.Kind() != reflect.Int {
19 // This optimization can be only performed if left side is int type,
20 // as runtime.in func uses reflect.Map.MapIndex and keys of map must,
21 // be same as checked value type.
22 goto string
23 }
24
25 for _, a := range array.Nodes {
26 if _, ok := a.(*IntegerNode); !ok {
27 goto string
28 }
29 }
30 {
31 value := make(map[int]struct{})
32 for _, a := range array.Nodes {
33 value[a.(*IntegerNode).Value] = struct{}{}
34 }
35 m := &ConstantNode{Value: value}
36 m.SetType(reflect.TypeOf(value))
37 patchCopyType(node, &BinaryNode{
38 Operator: n.Operator,
39 Left: n.Left,
40 Right: m,
41 })
42 }
43
44 string:
45 for _, a := range array.Nodes {
46 if _, ok := a.(*StringNode); !ok {
47 return
48 }
49 }
50 {
51 value := make(map[string]struct{})
52 for _, a := range array.Nodes {
53 value[a.(*StringNode).Value] = struct{}{}
54 }
55 m := &ConstantNode{Value: value}
56 m.SetType(reflect.TypeOf(value))
57 patchCopyType(node, &BinaryNode{
58 Operator: n.Operator,
59 Left: n.Left,
60 Right: m,
61 })
62 }
63
64 }
65 }
66 }
67 }
68}

Callers

nothing calls this directly

Calls 3

patchCopyTypeFunction · 0.85
TypeMethod · 0.65
SetTypeMethod · 0.65

Tested by

no test coverage detected