package ast
import "github.com/open-policy-agent/opa/ast"
Index ¶
- Constants
- Variables
- func MustParseStatement(input string) interface{}
- func MustParseStatements(input string) []interface{}
- func ParseStatement(input string) (interface{}, error)
- func ParseStatements(input string) ([]interface{}, error)
- func RegisterBuiltin(b *Builtin)
- func Walk(v Visitor, x interface{})
- type Array
- func (arr Array) Equal(other Value) bool
- func (arr Array) Hash() int
- func (arr Array) IsGround() bool
- func (arr Array) Query(ref Ref, iter QueryIterator) error
- func (arr Array) String() string
- type Body
- func MustParseBody(input string) Body
- func ParseBody(input string) (Body, error)
- func (body Body) Contains(x *Expr) bool
- func (body Body) Equal(other Body) bool
- func (body Body) String() string
- func (body Body) Vars() VarSet
- type Boolean
- func (bol Boolean) Equal(other Value) bool
- func (bol Boolean) Hash() int
- func (bol Boolean) IsGround() bool
- func (bol Boolean) String() string
- type Builtin
- func (b *Builtin) GetPrintableName() string
- func (b *Builtin) Unifies(i int) bool
- func (b *Builtin) UnifiesRecursively(i int) bool
- type Compiler
- func NewCompiler() *Compiler
- func (c *Compiler) Compile(mods map[string]*Module)
- func (c *Compiler) Failed() bool
- func (c *Compiler) FlattenErrors() string
- type DocKind
- type Expr
- func NewBuiltinExpr(terms ...*Term) *Expr
- func (expr *Expr) Complement() *Expr
- func (expr *Expr) Equal(other *Expr) bool
- func (expr *Expr) IsEquality() bool
- func (expr *Expr) OutputVars() VarSet
- func (expr *Expr) String() string
- func (expr *Expr) UnmarshalJSON(bs []byte) error
- func (expr *Expr) Vars() VarSet
- type Import
- type Location
- type Module
- func MustParseModule(input string) *Module
- func ParseModule(input string) (*Module, error)
- func ParseModuleFile(filename string) (*Module, error)
- func (mod *Module) Equal(other *Module) bool
- type ModuleTreeNode
- type Null
- func (null Null) Equal(other Value) bool
- func (null Null) Hash() int
- func (null Null) IsGround() bool
- func (null Null) String() string
- type Number
- func (num Number) Equal(other Value) bool
- func (num Number) Hash() int
- func (num Number) IsGround() bool
- func (num Number) String() string
- type Object
- func (obj Object) Equal(other Value) bool
- func (obj Object) Hash() int
- func (obj Object) IsGround() bool
- func (obj Object) Query(ref Ref, iter QueryIterator) error
- func (obj Object) String() string
- type Package
- type QueryIterator
- type Ref
- func EmptyRef() Ref
- func MustParseRef(input string) Ref
- func ParseRef(input string) (Ref, error)
- func (ref Ref) Equal(other Value) bool
- func (ref Ref) Hash() int
- func (ref Ref) IsGround() bool
- func (ref Ref) String() string
- func (ref Ref) Underlying() ([]interface{}, error)
- type Rule
- func MustParseRule(input string) *Rule
- func ParseRule(input string) (*Rule, error)
- func (rule *Rule) DocKind() DocKind
- func (rule *Rule) Equal(other *Rule) bool
- func (rule *Rule) HeadVars() VarSet
- func (rule *Rule) String() string
- type String
- func (str String) Equal(other Value) bool
- func (str String) Hash() int
- func (str String) IsGround() bool
- func (str String) String() string
- type Term
- func ArrayTerm(a ...*Term) *Term
- func BooleanTerm(b bool) *Term
- func Item(key, value *Term) [2]*Term
- func MustParseTerm(input string) *Term
- func NullTerm() *Term
- func NumberTerm(n float64) *Term
- func ObjectTerm(o ...[2]*Term) *Term
- func ParseTerm(input string) (*Term, error)
- func RefTerm(r ...*Term) *Term
- func StringTerm(s string) *Term
- func VarTerm(v string) *Term
- func (term *Term) Equal(other *Term) bool
- func (term *Term) IsGround() bool
- func (term *Term) MarshalJSON() ([]byte, error)
- func (term *Term) String() string
- func (term *Term) UnmarshalJSON(bs []byte) error
- type Value
- type Var
- func (variable Var) Equal(other Value) bool
- func (variable Var) Hash() int
- func (variable Var) IsGround() bool
- func (variable Var) String() string
- type VarSet
- func NewVarSet(vs ...Var) VarSet
- func (s VarSet) Add(v Var)
- func (s VarSet) Contains(v Var) bool
- func (s VarSet) Copy() VarSet
- func (s VarSet) String() string
- func (s VarSet) Update(vs VarSet)
- type Visitor
Constants ¶
const ( // CompleteDoc represents a document that is completely defined by the rule. CompleteDoc = iota // PartialSetDoc represents a set document that is partially defined by the rule. PartialSetDoc = iota // PartialObjectDoc represents an object document that is partially defined by the rule. PartialObjectDoc = iota )
Variables ¶
BuiltinMap provides a convenient mapping of built-in names to built-in definitions.
var Builtins []*Builtin
Builtins is the registry of built-in functions supported by OPA. Call RegisterBuiltin to add a new built-in.
var DefaultBuiltins = [...]*Builtin{ Equality, GreaterThan, GreaterThanEq, LessThan, LessThanEq, NotEqual, }
DefaultBuiltins is the registry of built-in functions supported in OPA by default. When adding a new built-in function to OPA, update this list.
var DefaultRootDocument = VarTerm("data")
DefaultRootDocument is the default root document. All package directives inside source files are implicitly prefixed with the DefaultRootDocument value.
Equality represents the "=" operator.
GreaterThan represents the ">" comparison operator.
GreaterThanEq represents the ">=" comparison operator.
var Keywords = [...]string{ "package", "import", "not", }
Keywords is an array of reserved keywords in the language. These are reserved names that cannot be used for variables.
LessThan represents the "<" comparison operator.
LessThanEq represents the "<=" comparison operator.
NotEqual represents the "!=" comparison operator.
var ReservedVars = NewVarSet(DefaultRootDocument.Value.(Var))
ReservedVars is the set of reserved variable names.
Wildcard represents the wildcard variable as defined in the language.
var WildcardPrefix = "$"
WildcardPrefix is the special character that all wildcard variables are prefixed with when the statement they are contained in is parsed.
Functions ¶
func MustParseStatement ¶
func MustParseStatement(input string) interface{}
MustParseStatement returns exactly one statement. If an error occurs during parsing, panic.
func MustParseStatements ¶
func MustParseStatements(input string) []interface{}
MustParseStatements returns a slice of parsed statements. If an error occurs during parsing, panic.
func ParseStatement ¶
ParseStatement returns exactly one statement. A statement might be a term, expression, rule, etc. Regardless, this function expects *exactly* one statement. If multiple statements are parsed, an error is returned.
func ParseStatements ¶
ParseStatements returns a slice of parsed statements. This is the default return value from the parser.
func RegisterBuiltin ¶
func RegisterBuiltin(b *Builtin)
RegisterBuiltin adds a new built-in function to the registry.
func Walk ¶
func Walk(v Visitor, x interface{})
Walk iterates the AST by calling the Visit function on the Visitor v for x before recursing.
Types ¶
type Array ¶
type Array []*Term
Array represents an array as defined by the language. Arrays are similar to the same types as defined by JSON with the exception that they can contain Vars and References.
func (Array) Equal ¶
Equal returns true if the other Value is an Array and the elements of the other Array are equal to the elements of this Array. The elements are ordered.
func (Array) Hash ¶
Hash returns the hash code for the Value.
func (Array) IsGround ¶
IsGround returns true if all of the Array elements are ground.
func (Array) Query ¶
func (arr Array) Query(ref Ref, iter QueryIterator) error
Query invokes the iterator for each referenced value inside the array.
func (Array) String ¶
type Body ¶
type Body []*Expr
Body represents one or more expressios contained inside a rule.
func MustParseBody ¶
MustParseBody returns a parsed body. If an error occurs during parsing, panic.
func ParseBody ¶
ParseBody returns exactly one body. If multiple bodies are parsed, an error is returned.
func (Body) Contains ¶
Contains returns true if this body contains the given expression.
func (Body) Equal ¶
Equal returns true if this Body is equal to the other Body. Two bodies are equal if consist of equal, ordered expressions.
func (Body) String ¶
func (Body) Vars ¶
Vars returns map where keys represent all of the variables found in the body. The values of the map are ignored.
type Boolean ¶
type Boolean bool
Boolean represents a boolean value defined by JSON.
func (Boolean) Equal ¶
Equal returns true if the other Value is a Boolean and is equal.
func (Boolean) Hash ¶
Hash returns the hash code for the Value.
func (Boolean) IsGround ¶
IsGround always returns true.
func (Boolean) String ¶
type Builtin ¶
Builtin represents a built-in function supported by OPA. Every built-in function is uniquely identified by a name.
func (*Builtin) GetPrintableName ¶
GetPrintableName returns a printable name for the builtin. Some built-ins have names that are used for infix operators but when printing we want to use something a bit more readable, e.g., "gte(a,b)" instead of ">=(a,b)".
func (*Builtin) Unifies ¶
Unifies returns true if a term in the given position will unify non-recursively or recursively.
func (*Builtin) UnifiesRecursively ¶
UnifiesRecursively returns true if a term in the given position will unify recursively, i.e., variables embedded inside a collection type will unify.
type Compiler ¶
type Compiler struct { // Errors contains errors that occurred during the compilation process. // If there are one or more errors, the compilation process is considered // "failed". Errors []error // Modules contains the compiled modules. The compiled modules are the // output of the compilation process. If the compilation process failed, // there is no guarantee about the state of the modules. Modules map[string]*Module // Exports contains a mapping of package paths to variables. The variables // represent externally accessible symbols. For now the only type of // externally visible symbol is a rule. For example: // // package a.b.c // // import data.e.f // // p = true :- q[x] = 1 # "p" is an export // q[x] :- f.r[x], not f.m[x] # "q" is an export // // In this case, the mapping would be: // // { // a.b.c: [p, q] // } Exports *util.HashMap // Globals contains a mapping of modules to globally accessible variables // within each module. Each variable is mapped to the value which represents // the fully qualified name of the variable. For example: // // package a.b.c // // import data.e.f // import y as z // // p = true :- q[x] = 1 // q[x] :- f.r[x], not f.m[x] // // In this case, the mapping would be // // { // <modptr>: {q: data.a.b.c.q, f: data.e.f, p: data.a.b.c.p, z: y} // } Globals map[*Module]map[Var]Value // ModuleTree organizes the modules into a tree where each node is keyed // by an element in the module's package path. E.g., given modules containg // the following package directives: "a", "a.b", "a.c", and "a.b", the // resulting module tree would be: // // root // | // +--- data (no modules) // | // +--- a (1 module) // | // +--- b (2 modules) // | // +--- c (1 module) // ModuleTree *ModuleTreeNode // RuleGraph represents the rule dependencies. // An edge (u, v) is added to the graph if rule "u" depends on rule "v". // A rule depends on another rule if it refers to it. RuleGraph map[*Rule]map[*Rule]struct{} // contains filtered or unexported fields }
Compiler contains the state of a compilation process.
func NewCompiler ¶
func NewCompiler() *Compiler
NewCompiler returns a new empty compiler.
func (*Compiler) Compile ¶
Compile runs the compilation process on the input modules. The output of the compilation process can be obtained from the Errors or Modules attributes of the Compiler.
func (*Compiler) Failed ¶
Failed returns true if a compilation error has been encountered.
func (*Compiler) FlattenErrors ¶
FlattenErrors returns a single message that contains a flattened version of the compiler error messages. This must only be called when the compilation process has failed.
type DocKind ¶
type DocKind int
DocKind represents the collection of document types that can be produced by rules.
type Expr ¶
type Expr struct { Location *Location `json:"-"` Negated bool `json:",omitempty"` Terms interface{} }
Expr represents a single expression contained inside the body of a rule.
func NewBuiltinExpr ¶
NewBuiltinExpr creates a new Expr object with the supplied terms. The builtin operator must be the first term.
func (*Expr) Complement ¶
Complement returns a copy of this expression with the negation flag flipped.
func (*Expr) Equal ¶
Equal returns true if this Expr equals the other Expr. Two expressions are considered equal if both expressions are negated (or not), are built-ins (or not), and have the same ordered terms.
func (*Expr) IsEquality ¶
IsEquality returns true if this is an equality expression.
func (*Expr) OutputVars ¶
OutputVars returns the set of variables that would be bound by evaluating this expression in isolation.
func (*Expr) String ¶
func (*Expr) UnmarshalJSON ¶
UnmarshalJSON parses the byte array and stores the result in expr.
func (*Expr) Vars ¶
Vars returns a VarSet containing all of the variables in the expression.
type Import ¶
Import represents a dependency on a document outside of the policy namespace. Imports are optional.
func (*Import) Equal ¶
Equal returns true if this Import has the same path and alias as the other Import.
func (*Import) String ¶
type Location ¶
type Location struct { Text []byte // The original text fragment from the source. File string // The name of the source file (which may be empty). Row int // The line in the source. Col int // The column in the row. }
Location records a position in source code
func NewLocation ¶
NewLocation returns a new Location object.
type Module ¶
Module represents a collection of policies (defined by rules) within a namespace (defined by the package) and optional dependencies on external documents (defined by imports).
func MustParseModule ¶
MustParseModule returns a parsed module. If an error occurs during parsing, panic.
func ParseModule ¶
ParseModule returns a parsed Module object. For details on Module objects and their fields, see policy.go. Empty input will return nil, nil.
func ParseModuleFile ¶
ParseModuleFile returns a parsed Module object.
func (*Module) Equal ¶
Equal returns true if this Module equals the other Module. Two modules are equal if they contain the same package, ordered imports, and ordered rules.
type ModuleTreeNode ¶
type ModuleTreeNode struct { Key string Modules []*Module Children map[string]*ModuleTreeNode }
ModuleTreeNode represents a node in the module tree. The module tree is keyed by the package path.
func NewModuleTree ¶
func NewModuleTree(mods map[string]*Module) *ModuleTreeNode
NewModuleTree returns a new ModuleTreeNode that represents the root of the module tree populated with the given modules.
func (*ModuleTreeNode) Size ¶
func (n *ModuleTreeNode) Size() int
Size returns the number of modules in the tree.
type Null ¶
type Null struct{}
Null represents the null value defined by JSON.
func (Null) Equal ¶
Equal returns true if the other term Value is also Null.
func (Null) Hash ¶
Hash returns the hash code for the Value.
func (Null) IsGround ¶
IsGround always returns true.
func (Null) String ¶
type Number ¶
type Number float64
Number represents a numeric value as defined by JSON.
func (Number) Equal ¶
Equal returns true if the other Value is a Number and is equal.
func (Number) Hash ¶
Hash returns the hash code for the Value.
func (Number) IsGround ¶
IsGround always returns true.
func (Number) String ¶
type Object ¶
type Object [][2]*Term
Object represents an object as defined by the language. Objects are similar to the same types as defined by JSON with the exception that they can contain Vars and References.
func (Object) Equal ¶
Equal returns true if the other Value is an Object and the key/value pairs of the Other object are equal to the key/value pairs of this Object. The key/value pairs are ordered.
func (Object) Hash ¶
Hash returns the hash code for the Value.
func (Object) IsGround ¶
IsGround returns true if all of the Object key/value pairs are ground.
func (Object) Query ¶
func (obj Object) Query(ref Ref, iter QueryIterator) error
Query invokes the iterator for each referenced value inside the object.
func (Object) String ¶
type Package ¶
Package represents the namespace of the documents produced by rules inside the module.
func (*Package) Equal ¶
Equal returns true if this Package has the same path as the other Package.
func (*Package) String ¶
type QueryIterator ¶
QueryIterator defines the interface for querying AST documents with references.
type Ref ¶
type Ref []*Term
Ref represents a reference as defined by the language.
func EmptyRef ¶
func EmptyRef() Ref
EmptyRef returns a new, empty reference.
func MustParseRef ¶
MustParseRef returns a parsed reference. If an error occurs during parsing, panic.
func ParseRef ¶
ParseRef returns exactly one reference.
func (Ref) Equal ¶
Equal returns true if the other Value is a Ref and the elements of the other Ref are equal to the this Ref.
func (Ref) Hash ¶
Hash returns the hash code for the Value.
func (Ref) IsGround ¶
IsGround returns true if all of the parts of the Ref are ground.
func (Ref) String ¶
func (Ref) Underlying ¶
Underlying returns a slice of underlying Go values. If the slice is not ground, an error is returned.
type Rule ¶
type Rule struct { Location *Location `json:"-"` Name Var Key *Term `json:",omitempty"` Value *Term `json:",omitempty"` Body Body }
Rule represents a rule as defined in the language. Rules define the content of documents that represent policy decisions.
func MustParseRule ¶
MustParseRule returns a parsed rule. If an error occurs during parsing, panic.
func ParseRule ¶
ParseRule returns exactly one rule. If multiple rules are parsed, an error is returned.
func (*Rule) DocKind ¶
DocKind returns the type of document produced by this rule.
func (*Rule) Equal ¶
Equal returns true if this Rule has the same name, arguments, and body as the other Rule.
func (*Rule) HeadVars ¶
HeadVars returns map where keys represent all of the variables found in the head of the rule. The values of the map are ignored.
func (*Rule) String ¶
type String ¶
type String string
String represents a string value as defined by JSON.
func (String) Equal ¶
Equal returns true if the other Value is a String and is equal.
func (String) Hash ¶
Hash returns the hash code for the Value.
func (String) IsGround ¶
IsGround always returns true.
func (String) String ¶
type Term ¶
type Term struct { Value Value // the value of the Term as represented in Go Location *Location `json:"-"` // the location of the Term in the source }
Term is an argument to a function.
func ArrayTerm ¶
ArrayTerm creates a new Term with an Array value.
func BooleanTerm ¶
BooleanTerm creates a new Term with a Boolean value.
func Item ¶
Item is a helper for constructing an tuple containing two Terms representing a key/value pair in an Object.
func MustParseTerm ¶
MustParseTerm returns a parsed term. If an error occurs during parsing, panic.
func NullTerm ¶
func NullTerm() *Term
NullTerm creates a new Term with a Null value.
func NumberTerm ¶
NumberTerm creates a new Term with a Number value.
func ObjectTerm ¶
ObjectTerm creates a new Term with an Object value.
func ParseTerm ¶
ParseTerm returns exactly one term. If multiple terms are parsed, an error is returned.
func RefTerm ¶
RefTerm creates a new Term with a Ref value.
func StringTerm ¶
StringTerm creates a new Term with a String value.
func VarTerm ¶
VarTerm creates a new Term with a Variable value.
func (*Term) Equal ¶
Equal returns true if this term equals the other term. Equality is defined for each kind of term.
func (*Term) IsGround ¶
IsGround returns true if this terms' Value is ground.
func (*Term) MarshalJSON ¶
MarshalJSON returns the JSON encoding of the term. Specialized marshalling logic is required to include a type hint for Value.
func (*Term) String ¶
func (*Term) UnmarshalJSON ¶
UnmarshalJSON parses the byte array and stores the result in term. Specialized unmarshalling is required to handle Value.
type Value ¶
type Value interface { // Equal returns true if this value equals the other value. Equal(other Value) bool // IsGround returns true if this value is not a variable or contains no variables. IsGround() bool // String returns a human readable string representation of the value. String() string // Returns hash code of the value. Hash() int }
Value declares the common interface for all Term values. Every kind of Term value in the language is represented as a type that implements this interface:
- Null, Boolean, Number, String - Object, Array - Variables - References
type Var ¶
type Var string
Var represents a variable as defined by the language.
func (Var) Equal ¶
Equal returns true if the other Value is a Variable and has the same value (name).
func (Var) Hash ¶
Hash returns the hash code for the Value.
func (Var) IsGround ¶
IsGround always returns false.
func (Var) String ¶
type VarSet ¶
type VarSet map[Var]struct{}
VarSet represents a set of variables.
func NewVarSet ¶
NewVarSet returns a new VarSet containing the specified variables.
func (VarSet) Add ¶
Add updates the set to include the variable "v".
func (VarSet) Contains ¶
Contains returns true if the set contains the variable "v".
func (VarSet) Copy ¶
Copy returns a shallow copy of the VarSet.
func (VarSet) String ¶
func (VarSet) Update ¶
Update merges the other VarSet into this VarSet.
type Visitor ¶
type Visitor interface { Visit(v interface{}) (w Visitor) }
Visitor defines the interface for iterating AST elements. The Visit function can return a Visitor w which will be used to visit the children of the AST element v. If the Visit function returns nil, the children will not be visited.
Source Files ¶
builtins.go compile.go parser_ext.go policy.go term.go varset.go visit.go
- Version
- v0.1.0-rc2
- Published
- Jun 7, 2016
- Platform
- js/wasm
- Imports
- 9 packages
- Last checked
- 3 minutes ago –
Tools for package owners.