-
Notifications
You must be signed in to change notification settings - Fork 10
/
query.go
153 lines (132 loc) · 3.46 KB
/
query.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
package rego
import (
"context"
"fmt"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
"github.com/open-policy-agent/opa/topdown"
)
type Options struct {
Modules map[string]*ast.Module
Document map[string]interface{}
Capabilities *ast.Capabilities
}
type State struct {
compiler *ast.Compiler
store storage.Store
}
func NewState(options Options) (*State, error) {
compiler := ast.NewCompiler()
if options.Capabilities != nil {
compiler = compiler.WithCapabilities(options.Capabilities)
}
document := options.Document
if document == nil {
document = map[string]interface{}{}
}
compiler.Compile(options.Modules)
if len(compiler.Errors) > 0 {
return nil, compiler.Errors
}
return &State{
compiler: compiler,
store: inmem.NewFromObject(document),
}, nil
}
// Query options can be extended in an associative way using Add.
type Query struct {
Query string
Input ast.Value
Builtins map[string]*topdown.Builtin
StrictBuiltinErrors *bool // Defaults to true for us.
Tracers []topdown.QueryTracer
}
func (q Query) Add(other Query) Query {
if other.Query != "" {
q.Query = other.Query
}
if other.Input != nil {
q.Input = other.Input
}
if q.Builtins == nil && other.Builtins != nil {
q.Builtins = other.Builtins
} else if other.Builtins != nil {
builtins := map[string]*topdown.Builtin{}
for k, builtin := range q.Builtins {
q.Builtins[k] = builtin
}
for k, builtin := range other.Builtins {
q.Builtins[k] = builtin
}
q.Builtins = builtins
}
if other.StrictBuiltinErrors != nil {
q.StrictBuiltinErrors = other.StrictBuiltinErrors
}
if len(q.Tracers) == 0 && len(other.Tracers) > 0 {
q.Tracers = other.Tracers
} else if len(other.Tracers) > 0 {
tracers := []topdown.QueryTracer{}
tracers = append(tracers, q.Tracers...)
tracers = append(tracers, other.Tracers...)
q.Tracers = tracers
}
return q
}
func (s *State) Query(
ctx context.Context,
query Query,
process func(ast.Value) error,
) error {
parsed, err := ast.ParseBody(query.Query)
if err != nil {
return err
}
if len(parsed) > 1 {
return fmt.Errorf("query expects a single term but got: %s", query.Query)
}
captureVar := ast.Var("_capture")
err = capture(captureVar, parsed)
if err != nil {
return err
}
compiled, err := s.compiler.QueryCompiler().Compile(parsed)
if err != nil {
return err
}
txn, err := s.store.NewTransaction(ctx)
if err != nil {
return err
}
defer s.store.Abort(ctx, txn)
q := topdown.NewQuery(compiled).
WithCompiler(s.compiler).
WithStore(s.store).
WithTransaction(txn).
WithBuiltins(query.Builtins).
WithStrictBuiltinErrors(query.StrictBuiltinErrors == nil || *query.StrictBuiltinErrors).
WithInput(&ast.Term{Value: query.Input})
for _, tracer := range query.Tracers {
q = q.WithQueryTracer(tracer)
}
return q.Iter(ctx, func(qr topdown.QueryResult) error {
regoValue := qr[captureVar]
return process(regoValue.Value)
})
}
func capture(variable ast.Var, body ast.Body) error {
if len(body) != 1 {
return fmt.Errorf("query expects a single term but got: %s", body.String())
}
varTerm := &ast.Term{Value: variable}
expr := body[0]
switch terms := expr.Terms.(type) {
case *ast.Term:
expr.Terms = ast.Equality.Expr(terms, varTerm).Terms
case []*ast.Term:
// NOTE: we assume the call is unsaturated here.
expr.Terms = append(terms, varTerm)
}
return nil
}