forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindings.go
163 lines (139 loc) · 2.76 KB
/
bindings.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
// Copyright 2017 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"
"github.com/open-policy-agent/opa/util"
)
type undo struct {
k *ast.Term
u *bindings
next *undo
}
func (u *undo) Undo() {
if u == nil {
// Allow call on zero value of Undo for ease-of-use.
return
}
if u.u == nil {
// Call on empty unifier undos a no-op unify operation.
return
}
u.u.delete(u.k)
u.next.Undo()
}
type bindings struct {
values *util.HashMap
}
func newBindings() *bindings {
eq := func(a, b util.T) bool {
v1, ok1 := a.(*ast.Term)
if ok1 {
v2 := b.(*ast.Term)
return v1.Equal(v2)
}
uv1 := a.(*value)
uv2 := b.(*value)
return uv1.equal(uv2)
}
hash := func(x util.T) int {
v := x.(*ast.Term)
return v.Hash()
}
values := util.NewHashMap(eq, hash)
return &bindings{values}
}
func (u *bindings) Iter(iter func(*ast.Term, *ast.Term) error) error {
var err error
u.values.Iter(func(k, v util.T) bool {
if err != nil {
return true
}
term := k.(*ast.Term)
err = iter(term, u.Plug(term))
return false
})
return err
}
func (u *bindings) Plug(a *ast.Term) *ast.Term {
switch v := a.Value.(type) {
case ast.Var:
b, next := u.apply(a)
if a != b || u != next {
return next.Plug(b)
}
return b
case ast.Array:
cpy := *a
arr := make(ast.Array, len(v))
for i := 0; i < len(arr); i++ {
arr[i] = u.Plug(v[i])
}
cpy.Value = arr
return &cpy
case ast.Object:
cpy := *a
obj := make(ast.Object, len(v))
for i := 0; i < len(obj); i++ {
obj[i] = ast.Item(u.Plug(v[i][0]), u.Plug(v[i][1]))
}
cpy.Value = obj
return &cpy
case *ast.Set:
cpy := *a
cpy.Value, _ = v.Map(func(x *ast.Term) (*ast.Term, error) {
return u.Plug(x), nil
})
return &cpy
}
return a
}
func (u *bindings) String() string {
if u == nil {
return "{}"
}
return u.values.String()
}
func (u *bindings) bind(a *ast.Term, b *ast.Term, other *bindings) *undo {
// fmt.Println("bind:", a, b)
u.values.Put(a, value{
u: other,
v: b,
})
return &undo{a, u, nil}
}
func (u *bindings) apply(a *ast.Term) (*ast.Term, *bindings) {
val, ok := u.get(a)
if !ok {
return a, u
}
return val.u.apply(val.v)
}
func (u *bindings) delete(v *ast.Term) {
u.values.Delete(v)
}
func (u *bindings) get(v *ast.Term) (value, bool) {
if u == nil {
return value{}, false
}
r, ok := u.values.Get(v)
if !ok {
return value{}, false
}
return r.(value), true
}
type value struct {
u *bindings
v *ast.Term
}
func (v value) String() string {
return fmt.Sprintf("<%v, %p>", v.v, v.u)
}
func (v value) equal(other *value) bool {
if v.u == other.u {
return v.v.Equal(other.v)
}
return false
}