Documentation ¶
Overview ¶
Package ir defines an intermediate representation (IR) for Rego.
The IR specifies an imperative execution model for Rego policies similar to a query plan in traditional databases.
Index ¶
- func Pretty(w io.Writer, x interface{}) error
- func Walk(vis Visitor, x interface{}) error
- type ArrayAppendStmt
- type AssignIntStmt
- type AssignVarOnceStmt
- type AssignVarStmt
- type Block
- type BlockStmt
- type Bool
- type BreakStmt
- type BuiltinFunc
- type CallDynamicStmt
- type CallStmt
- type DotStmt
- type EqualStmt
- type Func
- type Funcs
- type IsArrayStmt
- type IsDefinedStmt
- type IsObjectStmt
- type IsUndefinedStmt
- type LenStmt
- type Local
- type Location
- type MakeArrayStmt
- type MakeNullStmt
- type MakeNumberIntStmt
- type MakeNumberRefStmt
- type MakeObjectStmt
- type MakeSetStmt
- type NopStmt
- type NotEqualStmt
- type NotStmt
- type ObjectInsertOnceStmt
- type ObjectInsertStmt
- type ObjectMergeStmt
- type Operand
- type Plan
- type Plans
- type Policy
- type ResetLocalStmt
- type ResultSetAddStmt
- type ReturnLocalStmt
- type ScanStmt
- type SetAddStmt
- type Static
- type Stmt
- type StringConst
- type StringIndex
- type Val
- type Visitor
- type WithStmt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ArrayAppendStmt ¶ added in v0.10.3
ArrayAppendStmt represents a dynamic append operation of a value onto an array.
type AssignIntStmt ¶ added in v0.10.3
AssignIntStmt represents an assignment of an integer value to a local variable.
type AssignVarOnceStmt ¶ added in v0.11.0
type AssignVarOnceStmt struct { Source Operand `json:"source"` Target Local `json:"target"` Location }
AssignVarOnceStmt represents an assignment of one local variable to another. If the target is defined, execution aborts with a conflict error.
TODO(tsandall): is there a better name for this?
type AssignVarStmt ¶ added in v0.10.3
AssignVarStmt represents an assignment of one local variable to another.
type Block ¶
type Block struct {
Stmts []Stmt `json:"stmts"`
}
Block represents an ordered sequence of statements to execute. Blocks are executed until a return statement is encountered, a statement is undefined, or there are no more statements. If all statements are defined but no return statement is encountered, the block is undefined.
func (*Block) MarshalJSON ¶ added in v0.37.0
func (*Block) UnmarshalJSON ¶ added in v0.37.0
type BlockStmt ¶ added in v0.11.0
BlockStmt represents a nested block. Nested blocks and break statements can be used to short-circuit execution.
type BreakStmt ¶ added in v0.11.0
BreakStmt represents a jump out of the current block. The index specifies how many blocks to jump starting from zero (the current block). Execution will continue from the end of the block that is jumped to.
type BuiltinFunc ¶ added in v0.15.1
BuiltinFunc represents a built-in function that may be required by the policy.
type CallDynamicStmt ¶ added in v0.27.0
type CallDynamicStmt struct { Args []Local `json:"args"` Result Local `json:"result"` Path []Operand `json:"path"` Location }
CallDynamicStmt represents an indirect (data) function call. The result should be stored in the result local.
type CallStmt ¶ added in v0.11.0
type CallStmt struct { Func string `json:"func"` Args []Operand `json:"args"` Result Local `json:"result"` Location }
CallStmt represents a named function call. The result should be stored in the result local.
type DotStmt ¶
type DotStmt struct { Source Operand `json:"source"` Key Operand `json:"key"` Target Local `json:"target"` Location }
DotStmt represents a lookup operation on a value (e.g., array, object, etc.) The source of a DotStmt may be a scalar value in which case the statement will be undefined.
type Func ¶ added in v0.11.0
type Func struct { Name string `json:"name"` Params []Local `json:"params"` Return Local `json:"return"` Blocks []*Block `json:"blocks"` // TODO(tsandall): should this be a plan? Path []string `json:"path,omitempty"` // optional: if non-nil, include in data function tree }
Func represents a named plan (function) that can be invoked. Functions accept one or more parameters and return a value. By convention, the input document and data documents are always passed as the first and second arguments (respectively).
type Funcs ¶ added in v0.11.0
type Funcs struct {
Funcs []*Func `json:"funcs"`
}
Funcs represents a collection of planned functions to include in the policy.
type IsArrayStmt ¶ added in v0.10.3
IsArrayStmt represents a dynamic type check on a local variable.
type IsDefinedStmt ¶ added in v0.11.0
IsDefinedStmt represents a check of whether a local variable is defined.
type IsObjectStmt ¶ added in v0.10.3
IsObjectStmt represents a dynamic type check on a local variable.
type IsUndefinedStmt ¶ added in v0.11.0
IsUndefinedStmt represents a check of whether local variable is undefined.
type LenStmt ¶ added in v0.10.3
LenStmt represents a length() operation on a local variable. The result is stored in the target local variable.
type Local ¶
type Local int
Local represents a plan-scoped variable.
TODO(tsandall): should this be int32 for safety?
type Location ¶ added in v0.26.0
type Location struct { File int `json:"file"` // filename string constant index Col int `json:"col"` Row int `json:"row"` // contains filtered or unexported fields }
Location records the filen index, and the row and column inside that file that a statement can be connected to.
func (*Location) GetLocation ¶ added in v0.26.0
GetLocation returns a Stmt's Location.
func (*Location) SetLocation ¶ added in v0.26.0
SetLocation sets the Location for a given Stmt.
type MakeArrayStmt ¶ added in v0.10.3
type MakeArrayStmt struct { Capacity int32 `json:"capacity"` Target Local `json:"target"` Location }
MakeArrayStmt constructs a local variable that refers to an array value.
type MakeNullStmt ¶ added in v0.10.3
MakeNullStmt constructs a local variable that refers to a null value.
type MakeNumberIntStmt ¶
MakeNumberIntStmt constructs a local variable that refers to an integer value.
type MakeNumberRefStmt ¶ added in v0.15.1
MakeNumberRefStmt constructs a local variable that refers to a number stored as a string.
type MakeObjectStmt ¶ added in v0.10.3
MakeObjectStmt constructs a local variable that refers to an object value.
type MakeSetStmt ¶ added in v0.11.0
MakeSetStmt constructs a local variable that refers to a set value.
type NopStmt ¶ added in v0.26.0
type NopStmt struct {
Location
}
NopStmt adds a nop instruction. Useful during development and debugging only.
type NotEqualStmt ¶
NotEqualStmt represents a != check of two local variables.
type ObjectInsertOnceStmt ¶ added in v0.11.0
type ObjectInsertOnceStmt struct { Key Operand `json:"key"` Value Operand `json:"value"` Object Local `json:"object"` Location }
ObjectInsertOnceStmt represents a dynamic insert operation of a key/value pair into an object. If the key already exists and the value differs, execution aborts with a conflict error.
type ObjectInsertStmt ¶ added in v0.10.3
type ObjectInsertStmt struct { Key Operand `json:"key"` Value Operand `json:"value"` Object Local `json:"object"` Location }
ObjectInsertStmt represents a dynamic insert operation of a key/value pair into an object.
type ObjectMergeStmt ¶ added in v0.15.0
type ObjectMergeStmt struct { A Local `json:"a"` B Local `json:"b"` Target Local `json:"target"` Location }
ObjectMergeStmt performs a recursive merge of two object values. If either of the locals refer to non-object values this operation will abort with a conflict error. Overlapping object keys are merged recursively.
type Operand ¶ added in v0.37.0
type Operand struct {
Value Val `json:"value"`
}
Operand represents a value that a statement operates on.
func (*Operand) MarshalJSON ¶ added in v0.37.0
func (*Operand) UnmarshalJSON ¶ added in v0.37.0
type Plan ¶
Plan represents an ordered series of blocks to execute. Plan execution stops when a return statement is reached. Blocks are executed in-order.
type Plans ¶ added in v0.25.0
type Plans struct {
Plans []*Plan `json:"plans"`
}
Plans represents a collection of named query plans to expose in the policy.
type Policy ¶
type Policy struct { Static *Static `json:"static,omitempty"` Plans *Plans `json:"plans,omitempty"` Funcs *Funcs `json:"funcs,omitempty"` }
Policy represents a planned policy query.
type ResetLocalStmt ¶ added in v0.26.0
ResetLocalStmt resets a local variable to 0.
type ResultSetAddStmt ¶ added in v0.37.0
ResultSetAddStmt adds a value into the result set returned by the query plan.
type ReturnLocalStmt ¶ added in v0.11.0
ReturnLocalStmt represents a return statement that yields a local value.
type ScanStmt ¶ added in v0.10.3
type ScanStmt struct { Source Local `json:"source"` Key Local `json:"key"` Value Local `json:"value"` Block *Block `json:"block"` Location }
ScanStmt represents a linear scan over a composite value. The source may be a scalar in which case the block will never execute.
type SetAddStmt ¶ added in v0.11.0
SetAddStmt represents a dynamic add operation of an element into a set.
type Static ¶
type Static struct { Strings []*StringConst `json:"strings,omitempty"` BuiltinFuncs []*BuiltinFunc `json:"builtin_funcs,omitempty"` Files []*StringConst `json:"files,omitempty"` }
Static represents a static data segment that is indexed into by the policy.
type Stmt ¶
type Stmt interface {
// contains filtered or unexported methods
}
Stmt represents an operation (e.g., comparison, loop, dot, etc.) to execute.
type StringConst ¶
type StringConst struct {
Value string `json:"value"`
}
StringConst represents a string value.
type StringIndex ¶ added in v0.27.0
type StringIndex int
StringIndex represents the index into the plan's list of constant strings of a constant string.
func (StringIndex) String ¶ added in v0.28.0
func (s StringIndex) String() string
type Val ¶ added in v0.37.0
Val represents an abstract value that statements operate on. There are currently 3 types of values:
1. Local - a local variable that can refer to any type. 2. StringIndex - a string constant that refers to a compiled string. 3. Bool - a boolean constant.
type Visitor ¶
type Visitor interface { Before(x interface{}) Visit(x interface{}) (Visitor, error) After(x interface{}) }
Visitor defines the interface for visiting IR nodes.
type WithStmt ¶ added in v0.15.1
type WithStmt struct { Local Local `json:"local"` Path []int `json:"path"` Value Operand `json:"value"` Block *Block `json:"block"` Location }
WithStmt replaces the Local or a portion of the document referred to by the Local with the Value and executes the contained block. If the Path is non-empty, the Value is upserted into the Local. If the intermediate nodes in the Local referred to by the Path do not exist, they will be created. When the WithStmt finishes the Local is reset to it's original value.