datalog

package
v2.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWorldRunLimitMaxFacts      = errors.New("datalog: world runtime limit: too many facts")
	ErrWorldRunLimitMaxIterations = errors.New("datalog: world runtime limit: too many iterations")
	ErrWorldRunLimitTimeout       = errors.New("datalog: world runtime limit: timeout")
)
View Source
var (
	ErrExprDivByZero = errors.New("datalog: Div by zero")
	ErrInt64Overflow = errors.New("datalog: expression overflowed int64")
)
View Source
var DEFAULT_SYMBOLS = [...]string{
	"read",
	"write",
	"resource",
	"operation",
	"right",
	"time",
	"role",
	"owner",
	"tenant",
	"namespace",
	"user",
	"team",
	"service",
	"admin",
	"email",
	"group",
	"member",
	"ip_address",
	"client",
	"client_ip",
	"domain",
	"path",
	"version",
	"cluster",
	"node",
	"hostname",
	"nonce",
	"query",
}
View Source
var OFFSET = 1024

Functions

This section is empty.

Types

type Add

type Add struct{}

Add performs the addition of left + right and returns the result. It requires left and right to be Integer.

func (Add) Eval

func (Add) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Add) Type

func (Add) Type() BinaryOpType

type And

type And struct{}

And performs a logical AND between left and right and returns a Bool. It requires left and right to be Bool.

func (And) Eval

func (And) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (And) Type

func (And) Type() BinaryOpType

type BinaryOp

type BinaryOp struct {
	BinaryOpFunc
}

func (BinaryOp) Print

func (op BinaryOp) Print(left, right string) string

func (BinaryOp) Type

func (BinaryOp) Type() OpType

type BinaryOpFunc

type BinaryOpFunc interface {
	Type() BinaryOpType
	Eval(left, right Term, symbols *SymbolTable) (Term, error)
}

type BinaryOpType

type BinaryOpType byte
const (
	BinaryLessThan BinaryOpType = iota
	BinaryLessOrEqual
	BinaryGreaterThan
	BinaryGreaterOrEqual
	BinaryEqual
	BinaryContains
	BinaryPrefix
	BinarySuffix
	BinaryRegex
	BinaryAdd
	BinarySub
	BinaryMul
	BinaryDiv
	BinaryAnd
	BinaryOr
	BinaryIntersection
	BinaryUnion
)

type Bool

type Bool bool

func (Bool) Equal

func (b Bool) Equal(t Term) bool

func (Bool) String

func (b Bool) String() string

func (Bool) Type

func (Bool) Type() TermType

type Bytes

type Bytes []byte

func (Bytes) Equal

func (b Bytes) Equal(t Term) bool

func (Bytes) String

func (b Bytes) String() string

func (Bytes) Type

func (Bytes) Type() TermType

type Check

type Check struct {
	Queries []Rule
}

type Contains

type Contains struct{}

Contains returns true when the right value exists in the left Set. The right value must be an Integer, Bytes, String or Symbol. The left value must be a Set, containing elements of right type.

func (Contains) Eval

func (Contains) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Contains) Type

func (Contains) Type() BinaryOpType

type Date

type Date uint64

func (Date) Equal

func (d Date) Equal(t Term) bool

func (Date) String

func (d Date) String() string

func (Date) Type

func (Date) Type() TermType

type Div

type Div struct{}

Div performs the division of left / right and returns the result. It requires left and right to be Integer.

func (Div) Eval

func (Div) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Div) Type

func (Div) Type() BinaryOpType

type Equal

type Equal struct{}

Equal returns true when left and right are equal. It requires left and right to have the same concrete type and only accepts Integer, Bytes or String.

func (Equal) Eval

func (Equal) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Equal) Type

func (Equal) Type() BinaryOpType

type Expression

type Expression []Op

func (*Expression) Evaluate

func (e *Expression) Evaluate(values map[Variable]*Term, symbols *SymbolTable) (Term, error)

func (*Expression) Print

func (e *Expression) Print(symbols *SymbolTable) string

type Fact

type Fact struct {
	Predicate
}

type FactSet

type FactSet []Fact

func (*FactSet) Equal

func (s *FactSet) Equal(x *FactSet) bool

func (*FactSet) Insert

func (s *FactSet) Insert(f Fact) bool

func (*FactSet) InsertAll

func (s *FactSet) InsertAll(facts []Fact)

type GreaterOrEqual

type GreaterOrEqual struct{}

GreaterOrEqual returns true when left is greater than right. It requires left and right to have the same concrete type and only accepts Integer and Date.

func (GreaterOrEqual) Eval

func (GreaterOrEqual) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (GreaterOrEqual) Type

type GreaterThan

type GreaterThan struct{}

GreaterThan returns true when left is greater than right. It requires left and right to have the same concrete type and only accepts Integer.

func (GreaterThan) Eval

func (GreaterThan) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (GreaterThan) Type

func (GreaterThan) Type() BinaryOpType

type Integer

type Integer int64

func (Integer) Equal

func (i Integer) Equal(t Term) bool

func (Integer) String

func (i Integer) String() string

func (Integer) Type

func (Integer) Type() TermType

type Intersection

type Intersection struct{}

Intersection returns the intersection of two sets

func (Intersection) Eval

func (Intersection) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Intersection) Type

func (Intersection) Type() BinaryOpType

type InvalidRuleError

type InvalidRuleError struct {
	Rule            Rule
	MissingVariable Variable
}

func (InvalidRuleError) Error

func (e InvalidRuleError) Error() string

type Length

type Length struct{}

Length returns the length of a value. It accepts String, Bytes and Set

func (Length) Eval

func (Length) Eval(value Term, symbols *SymbolTable) (Term, error)

func (Length) Type

func (Length) Type() UnaryOpType

type LessOrEqual

type LessOrEqual struct{}

LessOrEqual returns true when left is less or equal than right. It requires left and right to have the same concrete type and only accepts Integer and Date.

func (LessOrEqual) Eval

func (LessOrEqual) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (LessOrEqual) Type

func (LessOrEqual) Type() BinaryOpType

type LessThan

type LessThan struct{}

LessThan returns true when left is less than right. It requires left and right to have the same concrete type and only accepts Integer.

func (LessThan) Eval

func (LessThan) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (LessThan) Type

func (LessThan) Type() BinaryOpType

type MatchedVariables

type MatchedVariables map[Variable]*Term

func (MatchedVariables) Clone

func (MatchedVariables) Complete

func (m MatchedVariables) Complete() map[Variable]*Term

func (MatchedVariables) Insert

func (m MatchedVariables) Insert(k Variable, v Term) bool

type Mul

type Mul struct{}

Mul performs the multiplication of left * right and returns the result. It requires left and right to be Integer.

func (Mul) Eval

func (Mul) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Mul) Type

func (Mul) Type() BinaryOpType

type Negate

type Negate struct{}

Negate returns the negation of a value. It only accepts a Bool value.

func (Negate) Eval

func (Negate) Eval(value Term, symbols *SymbolTable) (Term, error)

func (Negate) Type

func (Negate) Type() UnaryOpType

type Op

type Op interface {
	Type() OpType
}

type OpType

type OpType byte
const (
	OpTypeValue OpType = iota
	OpTypeUnary
	OpTypeBinary
)

type Or

type Or struct{}

Or performs a logical OR between left and right and returns a Bool. It requires left and right to be Bool.

func (Or) Eval

func (Or) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Or) Type

func (Or) Type() BinaryOpType

type Parens

type Parens struct{}

Parens allows expression priority and grouping (like parenthesis in math operations) it is a no-op, but is used to print back the expressions properly, putting their value inside parenthesis.

func (Parens) Eval

func (Parens) Eval(value Term, symbols *SymbolTable) (Term, error)

func (Parens) Type

func (Parens) Type() UnaryOpType

type Predicate

type Predicate struct {
	Name  String
	Terms []Term
}

func (Predicate) Clone

func (p Predicate) Clone() Predicate

func (Predicate) Equal

func (p Predicate) Equal(p2 Predicate) bool

func (Predicate) Match

func (p Predicate) Match(p2 Predicate) bool

type Prefix

type Prefix struct{}

Prefix returns true when the left string starts with the right string. left and right must be String.

func (Prefix) Eval

func (Prefix) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Prefix) Type

func (Prefix) Type() BinaryOpType

type Regex

type Regex struct{}

Regex returns true when the right string is a regexp and left matches against it. left and right must be String.

func (Regex) Eval

func (Regex) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Regex) Type

func (Regex) Type() BinaryOpType

type Rule

type Rule struct {
	Head        Predicate
	Body        []Predicate
	Expressions []Expression
}

func (Rule) Apply

func (r Rule) Apply(facts *FactSet, newFacts *FactSet, syms *SymbolTable) error

type Set

type Set []Term

func (Set) Equal

func (s Set) Equal(t Term) bool

func (Set) Intersect

func (s Set) Intersect(t Set) Set

func (Set) String

func (s Set) String() string

func (Set) Type

func (Set) Type() TermType

func (Set) Union

func (s Set) Union(t Set) Set

type String

type String uint64

func (String) Equal

func (s String) Equal(t Term) bool

func (String) String

func (s String) String() string

func (String) Type

func (String) Type() TermType

type Sub

type Sub struct{}

Sub performs the substraction of left - right and returns the result. It requires left and right to be Integer.

func (Sub) Eval

func (Sub) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Sub) Type

func (Sub) Type() BinaryOpType

type Suffix

type Suffix struct{}

Suffix returns true when the left string ends with the right string. left and right must be String.

func (Suffix) Eval

func (Suffix) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Suffix) Type

func (Suffix) Type() BinaryOpType

type SymbolDebugger

type SymbolDebugger struct {
	*SymbolTable
}

func (SymbolDebugger) Check

func (d SymbolDebugger) Check(c Check) string

func (SymbolDebugger) CheckQuery

func (d SymbolDebugger) CheckQuery(r Rule) string

func (SymbolDebugger) Expression

func (d SymbolDebugger) Expression(e Expression) string

func (SymbolDebugger) FactSet

func (d SymbolDebugger) FactSet(s *FactSet) string

func (SymbolDebugger) Predicate

func (d SymbolDebugger) Predicate(p Predicate) string

func (SymbolDebugger) Rule

func (d SymbolDebugger) Rule(r Rule) string

func (SymbolDebugger) World

func (d SymbolDebugger) World(w *World) string

type SymbolTable

type SymbolTable []string

func (*SymbolTable) Clone

func (t *SymbolTable) Clone() *SymbolTable

func (*SymbolTable) Extend

func (t *SymbolTable) Extend(other *SymbolTable)

Extend insert symbols from the given SymbolTable in the receiving one excluding any Symbols already existing

func (*SymbolTable) Index

func (t *SymbolTable) Index(s string) uint64

func (*SymbolTable) Insert

func (t *SymbolTable) Insert(s string) String

func (*SymbolTable) IsDisjoint

func (t *SymbolTable) IsDisjoint(other *SymbolTable) bool

IsDisjoint returns true if receiver has no elements in common with other. This is equivalent to checking for an empty intersection.

func (*SymbolTable) Len

func (t *SymbolTable) Len() int

func (*SymbolTable) SplitOff

func (t *SymbolTable) SplitOff(at int) *SymbolTable

SplitOff returns a newly allocated slice containing the elements in the range [at, len). After the call, the receiver will be left containing the elements [0, at) with its previous capacity unchanged.

func (*SymbolTable) Str

func (t *SymbolTable) Str(sym String) string

func (*SymbolTable) Sym

func (t *SymbolTable) Sym(s string) Term

func (*SymbolTable) Var

func (t *SymbolTable) Var(v Variable) string

type Term

type Term interface {
	Type() TermType
	Equal(Term) bool
	String() string
}

type TermType

type TermType byte
const (
	TermTypeVariable TermType = iota
	TermTypeInteger
	TermTypeString
	TermTypeDate
	TermTypeBytes
	TermTypeBool
	TermTypeSet
)

type UnaryOp

type UnaryOp struct {
	UnaryOpFunc
}

func (UnaryOp) Print

func (op UnaryOp) Print(value string) string

func (UnaryOp) Type

func (UnaryOp) Type() OpType

type UnaryOpFunc

type UnaryOpFunc interface {
	Type() UnaryOpType
	Eval(value Term, symbols *SymbolTable) (Term, error)
}

type UnaryOpType

type UnaryOpType byte
const (
	UnaryNegate UnaryOpType = iota
	UnaryParens
	UnaryLength
)

type Union

type Union struct{}

Intersection returns the intersection of two sets

func (Union) Eval

func (Union) Eval(left Term, right Term, symbols *SymbolTable) (Term, error)

func (Union) Type

func (Union) Type() BinaryOpType

type Value

type Value struct {
	ID Term
}

func (Value) Type

func (v Value) Type() OpType

type Variable

type Variable uint32

func (Variable) Equal

func (v Variable) Equal(t Term) bool

func (Variable) String

func (v Variable) String() string

func (Variable) Type

func (Variable) Type() TermType

type World

type World struct {
	// contains filtered or unexported fields
}

func NewWorld

func NewWorld(opts ...WorldOption) *World

func (*World) AddFact

func (w *World) AddFact(f Fact)

func (*World) AddRule

func (w *World) AddRule(r Rule)

func (*World) Clone

func (w *World) Clone() *World

func (*World) Facts

func (w *World) Facts() *FactSet

func (*World) Query

func (w *World) Query(pred Predicate) *FactSet

func (*World) QueryRule

func (w *World) QueryRule(rule Rule, syms *SymbolTable) *FactSet

func (*World) ResetRules

func (w *World) ResetRules()

func (*World) Rules

func (w *World) Rules() []Rule

func (*World) Run

func (w *World) Run(syms *SymbolTable) error

type WorldOption

type WorldOption func(w *World)

func WithMaxDuration

func WithMaxDuration(maxDuration time.Duration) WorldOption

func WithMaxFacts

func WithMaxFacts(maxFacts int) WorldOption

func WithMaxIterations

func WithMaxIterations(maxIterations int) WorldOption

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL