forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eq.go
318 lines (275 loc) · 7.21 KB
/
eq.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2016 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import (
"fmt"
"github.com/open-policy-agent/opa/ast"
)
func evalEq(t *Topdown, expr *ast.Expr, iter Iterator) error {
operands := expr.Terms.([]*ast.Term)
a := operands[1].Value
b := operands[2].Value
undo, err := evalEqUnify(t, a, b, nil, iter)
t.Unbind(undo)
return err
}
func evalEqGround(t *Topdown, a ast.Value, b ast.Value, iter Iterator) error {
a, err := ResolveRefs(a, t)
if err != nil {
return err
}
b, err = ResolveRefs(b, t)
if err != nil {
return err
}
if ast.Compare(a, b) == 0 {
return iter(t)
}
return nil
}
// evalEqUnify is the top level of the unification implementation.
//
// When evaluating equality expressions, OPA tries to unify variables
// with values or other variables in the expression.
//
// The simplest case for unification is an expression of the form "<var> = ???".
// In this case, the variable is unified/bound to the other side the expression
// and evaluation continues to the next expression.
//
// In cases involving composites, OPA tries to unify elements in the same position
// of collections. For example, given an expression "[1,2,3] = [1,x,y]", OPA will
// unify variables x and y with the numbers 2 and 3. This process happens recursively,
// such that unification can happen on deeply embedded values.
//
// In cases involving references, OPA assumes that the references are ground at this stage.
// As a result, references are just special cases of the normal scalar/composite unification.
func evalEqUnify(t *Topdown, a ast.Value, b ast.Value, prev *Undo, iter Iterator) (*Undo, error) {
// Plug bindings into both terms because this will be called recursively and there may be
// new bindings that have been made as part of unification.
a = PlugValue(a, t.Binding)
b = PlugValue(b, t.Binding)
switch a := a.(type) {
case ast.Var:
return evalEqUnifyVar(t, a, b, prev, iter)
case ast.Object:
return evalEqUnifyObject(t, a, b, prev, iter)
case ast.Array:
return evalEqUnifyArray(t, a, b, prev, iter)
case *ast.Set:
return evalEqUnifySet(t, a, b, prev, iter)
default:
switch b := b.(type) {
case ast.Var:
return evalEqUnifyVar(t, b, a, prev, iter)
case ast.Array:
return evalEqUnifyArray(t, b, a, prev, iter)
case ast.Object:
return evalEqUnifyObject(t, b, a, prev, iter)
case *ast.Set:
return evalEqUnifySet(t, b, a, prev, iter)
default:
return prev, evalEqGround(t, a, b, iter)
}
}
}
func evalEqUnifyArray(t *Topdown, a ast.Array, b ast.Value, prev *Undo, iter Iterator) (*Undo, error) {
switch b := b.(type) {
case ast.Var:
return evalEqUnifyVar(t, b, a, prev, iter)
case ast.Ref:
return evalEqUnifyArrayRef(t, a, b, prev, iter)
case ast.Array:
return evalEqUnifyArrays(t, a, b, prev, iter)
default:
return prev, nil
}
}
func evalEqUnifyArrayRef(t *Topdown, a ast.Array, b ast.Ref, prev *Undo, iter Iterator) (*Undo, error) {
r, err := t.Resolve(b)
if err != nil {
return prev, err
}
slice, ok := r.([]interface{})
if !ok {
return prev, nil
}
if len(a) != len(slice) {
return prev, nil
}
for i := range a {
var tmp *Topdown
child := make(ast.Ref, len(b), len(b)+1)
copy(child, b)
child = append(child, ast.IntNumberTerm(i))
p, err := evalEqUnify(t, a[i].Value, child, prev, func(t *Topdown) error {
tmp = t
return nil
})
prev = p
if err != nil {
return nil, err
}
if tmp == nil {
return nil, nil
}
t = tmp
}
return prev, iter(t)
}
func evalEqUnifyArrays(t *Topdown, a ast.Array, b ast.Array, prev *Undo, iter Iterator) (*Undo, error) {
aLen := len(a)
bLen := len(b)
if aLen != bLen {
return nil, nil
}
for i := 0; i < aLen; i++ {
ai := a[i].Value
bi := b[i].Value
var tmp *Topdown
p, err := evalEqUnify(t, ai, bi, prev, func(t *Topdown) error {
tmp = t
return nil
})
prev = p
if err != nil {
return nil, err
}
if tmp == nil {
return nil, nil
}
t = tmp
}
return prev, iter(t)
}
// evalEqUnifyObject attempts to unify the object "a" with some other value "b".
// TODO(tsandal): unification of object keys (or unordered sets in general) is not
// supported because it would be too expensive. We may revisit this in the future.
func evalEqUnifyObject(t *Topdown, a ast.Object, b ast.Value, prev *Undo, iter Iterator) (*Undo, error) {
switch b := b.(type) {
case ast.Var:
return evalEqUnifyVar(t, b, a, prev, iter)
case ast.Ref:
return evalEqUnifyObjectRef(t, a, b, prev, iter)
case ast.Object:
return evalEqUnifyObjects(t, a, b, prev, iter)
default:
return nil, nil
}
}
func evalEqUnifyObjectRef(t *Topdown, a ast.Object, b ast.Ref, prev *Undo, iter Iterator) (*Undo, error) {
r, err := t.Resolve(b)
if err != nil {
return prev, err
}
for i := range a {
if !a[i][0].IsGround() {
return prev, fmt.Errorf("illegal variable object key: %v", a[i][0])
}
}
obj, ok := r.(map[string]interface{})
if !ok {
return prev, nil
}
if len(obj) != len(a) {
return prev, nil
}
for i := range a {
// TODO(tsandall): support non-string keys in storage.
k, ok := a[i][0].Value.(ast.String)
if !ok {
return prev, fmt.Errorf("illegal object key type %T: %v", a[i][0], a[i][0])
}
_, ok = obj[string(k)]
if !ok {
return nil, nil
}
child := make(ast.Ref, len(b), len(b)+1)
copy(child, b)
child = append(child, a[i][0])
var tmp *Topdown
p, err := evalEqUnify(t, a[i][1].Value, child, prev, func(t *Topdown) error {
tmp = t
return nil
})
prev = p
if err != nil {
return nil, err
}
if tmp == nil {
return nil, nil
}
t = tmp
}
return prev, iter(t)
}
func evalEqUnifyObjects(t *Topdown, a ast.Object, b ast.Object, prev *Undo, iter Iterator) (*Undo, error) {
if len(a) != len(b) {
return nil, nil
}
for i := range a {
if !a[i][0].IsGround() {
return prev, fmt.Errorf("illegal variable object key: %v", a[i][0])
}
if !b[i][0].IsGround() {
return prev, fmt.Errorf("illegal variable object key: %v", b[i][0])
}
}
for i := range a {
var tmp *Topdown
for j := range b {
if b[j][0].Equal(a[i][0]) {
p, err := evalEqUnify(t, a[i][1].Value, b[j][1].Value, prev, func(t *Topdown) error {
tmp = t
return nil
})
prev = p
if err != nil {
return nil, err
}
if tmp == nil {
break
}
}
}
if tmp == nil {
return nil, nil
}
t = tmp
}
return prev, iter(t)
}
func evalEqUnifySet(t *Topdown, a *ast.Set, b ast.Value, prev *Undo, iter Iterator) (*Undo, error) {
switch b := b.(type) {
case *ast.Set:
return evalEqSets(t, a, b, prev, iter)
case ast.Var:
return evalEqUnifyVar(t, b, a, prev, iter)
default:
return prev, nil
}
}
func evalEqSets(t *Topdown, a *ast.Set, b *ast.Set, prev *Undo, iter Iterator) (*Undo, error) {
x, err := ResolveRefs(a, t)
if err != nil {
return prev, err
}
a = x.(*ast.Set)
y, err := ResolveRefs(b, t)
if err != nil {
return prev, err
}
b = y.(*ast.Set)
if a.Equal(b) {
return prev, iter(t)
}
return prev, nil
}
func evalEqUnifyVar(t *Topdown, a ast.Var, b ast.Value, prev *Undo, iter Iterator) (*Undo, error) {
undo := t.Bind(a, b, prev)
err := iter(t)
return undo, err
}
func init() {
RegisterBuiltinFunc(ast.Equality.Name, evalEq)
}