Skip to content

Commit

Permalink
wasm: Update planner to support with keyword
Browse files Browse the repository at this point in the history
This change updates the planner to support the with keyword in
Rego. When the planner encounters a with keyword it plans the
statement value and then temporarily replaces the local referred to in
the statement's target. While the statement is executing the plan sees
the replaced value. Once the statement finishes and execution
continues to the next statement, the local is restored.

This change modifies how rules are planned to handle 'with' statements
that apply to the 'data' document. Previously all rules were planned
up-front in one-shot. This worked because the set of virtual documents
visible to any given expression was static and would not change during
planning. The 'with' keyword changes this because 'with' statements
can be applied to the 'data' document that change set of virtual
documents visible to the expression. To deal with this the planner has
been updated to plan rules on-the-fly depth-first when references to
virtual documents are encountered. On top of this, the planner will
re-plan rules when 'with' statements against the 'data' document are
encountered. This approach was taken because while it increases the
size of the generated plan it keeps evaluation relatively simple: we
don't have to propagate context through the call stack to determine
whether a with modifier is in-place before executing call statements.

The other part of the planner implementation that was modified is the
trie that stores rules. The planned functions have been moved out of
the trie and are simply stored on the policy plan/result and the
mapping from virtual document path to function name has been moved
into a separate structure (funcstack). This change was made simplify
the data structures and improve maintainability. This change can be
revisited in the future if needed without any impact on generated
plans.

Fixes #1116

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
  • Loading branch information
tsandall committed Nov 6, 2019
1 parent d8a0031 commit 5f2b72e
Show file tree
Hide file tree
Showing 7 changed files with 659 additions and 179 deletions.
2 changes: 1 addition & 1 deletion internal/ir/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type (
// Funcs represents a collection of planned functions to include in the
// policy.
Funcs struct {
Funcs map[string]*Func
Funcs []*Func
}

// Func represents a named plan (function) that can be invoked. Functions
Expand Down
11 changes: 2 additions & 9 deletions internal/ir/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package ir

import "sort"

// Visitor defines the interface for visiting IR nodes.
type Visitor interface {
Before(x interface{})
Expand Down Expand Up @@ -54,13 +52,8 @@ func (w *walkerImpl) walk(x interface{}) {
w.walk(s)
}
case *Funcs:
keys := make([]string, 0, len(x.Funcs))
for k := range x.Funcs {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
w.walk(x.Funcs[k])
for _, fn := range x.Funcs {
w.walk(fn)
}
case *Func:
for _, b := range x.Blocks {
Expand Down
96 changes: 0 additions & 96 deletions internal/planner/functrie.go

This file was deleted.

Loading

0 comments on commit 5f2b72e

Please sign in to comment.