-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_null.go
144 lines (116 loc) · 3.38 KB
/
engine_null.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
package expr
import (
"context"
"sync"
"github.com/google/cel-go/common/operators"
"github.com/ohler55/ojg/jp"
)
func newNullMatcher(concurrency int64) MatchingEngine {
return &nullLookup{
lock: &sync.RWMutex{},
paths: map[string]struct{}{},
null: map[string][]*StoredExpressionPart{},
not: map[string][]*StoredExpressionPart{},
concurrency: concurrency,
}
}
type nullLookup struct {
lock *sync.RWMutex
// paths stores all variable names as JSON paths used within the engine.
paths map[string]struct{}
null map[string][]*StoredExpressionPart
not map[string][]*StoredExpressionPart
concurrency int64
}
func (n *nullLookup) Type() EngineType {
return EngineTypeNullMatch
}
func (n *nullLookup) Match(ctx context.Context, data map[string]any) (matched []*StoredExpressionPart, err error) {
l := &sync.Mutex{}
matched = []*StoredExpressionPart{}
pool := newErrPool(errPoolOpts{concurrency: n.concurrency})
for item := range n.paths {
path := item
pool.Go(func() error {
x, err := jp.ParseString(path)
if err != nil {
return err
}
res := x.Get(data)
if len(res) == 0 {
// This isn't present, which matches null in our overloads. Set the
// value to nil.
res = []any{nil}
}
// This matches null, nil (as null), and any non-null items.
l.Lock()
// XXX: This engine hasn't been updated with denied items for !=. It needs consideration
// in how to handle these cases appropriately.
found := n.Search(ctx, path, res[0])
matched = append(matched, found...)
l.Unlock()
return nil
})
}
return matched, pool.Wait()
}
func (n *nullLookup) Search(ctx context.Context, variable string, input any) (matched []*StoredExpressionPart) {
if input == nil {
// The input data is null, so the only items that can match are equality
// comparisons to null.
all := n.null[variable]
return all
}
all := n.not[variable]
return all
}
func (n *nullLookup) Add(ctx context.Context, p ExpressionPart) error {
n.lock.Lock()
defer n.lock.Unlock()
varName := p.Predicate.Ident
n.paths[varName] = struct{}{}
// If we're comparing to null ("a" == null), we want the variable
// to be null and should place this in the `null` map.
//
// Any other comparison is a not-null comparison.
if p.Predicate.Operator == operators.Equals {
if _, ok := n.null[varName]; !ok {
n.null[varName] = []*StoredExpressionPart{p.ToStored()}
return nil
}
n.null[varName] = append(n.null[varName], p.ToStored())
return nil
}
if _, ok := n.not[varName]; !ok {
n.not[varName] = []*StoredExpressionPart{p.ToStored()}
return nil
}
n.not[varName] = append(n.not[varName], p.ToStored())
return nil
}
func (n *nullLookup) Remove(ctx context.Context, p ExpressionPart) error {
n.lock.Lock()
defer n.lock.Unlock()
coll, ok := n.not[p.Predicate.Ident]
if p.Predicate.Operator == operators.Equals {
coll, ok = n.null[p.Predicate.Ident]
}
if !ok {
// This could not exist as there's nothing mapping this variable for
// the given event name.
return ErrExpressionPartNotFound
}
// Remove the expression part from the leaf.
for i, eval := range coll {
if p.EqualsStored(eval) {
coll = append(coll[:i], coll[i+1:]...)
if p.Predicate.Operator == operators.Equals {
n.null[p.Predicate.Ident] = coll
} else {
n.not[p.Predicate.Ident] = coll
}
return nil
}
}
return ErrExpressionPartNotFound
}