Documentation
¶
Index ¶
- Variables
- func DumpRulesToYAML(rules []RuleSpec) (string, error)
- func ExportToDOT(writer io.Writer, root *node)
- func FormatDebugIsnf(isn, message string, rest ...any) string
- func FormatIsnf(message string, rest ...any) string
- type ActionFn
- type ActionParams
- type ActionSpec
- type Actions
- type Compiler
- type ConditionSpec
- type DataInput
- type EIRBuilder
- type EIRPredicate
- type EQBuilder
- type EQPredicate
- type FailureSpec
- type Filterable
- type GTBuilder
- type GTEBuilder
- type GTEPredicate
- type GTPredicate
- type IIRBuilder
- type IIRPredicate
- type LTBuilder
- type LTEBuilder
- type LTEPredicate
- type LTPredicate
- type MetaPredicate
- func (meta *MetaPredicate) Debug(isn, token string) string
- func (meta *MetaPredicate) EvalExclusiveRange(input Filterable) bool
- func (meta *MetaPredicate) EvalInclusiveRange(input Filterable) bool
- func (meta *MetaPredicate) EvalStringMember(input Filterable, insens bool) bool
- func (meta *MetaPredicate) GetFloatValueFromInput(input Filterable) (float64, bool)
- func (meta *MetaPredicate) GetFloatValues(input Filterable) (float64, float64, bool)
- func (meta *MetaPredicate) GetPredicateFloatArray() ([]float64, bool)
- func (meta *MetaPredicate) GetPredicateStringArray() ([]string, bool)
- func (meta *MetaPredicate) GetStringValues(input Filterable) (string, string, bool)
- func (meta *MetaPredicate) String(token string) string
- type NEQBuilder
- type NEQPredicate
- type NOOPPredicate
- type Predicate
- type PredicateBuilder
- type PredicateDict
- type PredicateFn
- type REIMBuilder
- type REIMPredicate
- type RESMBuilder
- type RESMPredicate
- type RuleSpec
- type SIEQBuilder
- type SIEQPredicate
- type SIMBuilder
- type SIMPredicate
- type SINEQBuilder
- type SINEQPredicate
- type SSEQBuilder
- type SSEQPredicate
- type SSMBuilder
- type SSMPredicate
- type SSNEQBuilder
- type SSNEQPredicate
Constants ¶
This section is empty.
Variables ¶
var ( ErrExpectedParams = errors.Base("expected parameters to be given") ErrExpectedString = errors.Base("expected a string value") ErrMissingParam = errors.Base("parameter missing") ErrUnknownBuiltin = errors.Base("unknown builtin function") )
var ( ErrUnknownOperator = errors.Base("unknown operator") ErrRuleCompileFailed = errors.Base("rule compilation failed") )
Functions ¶
func DumpRulesToYAML ¶
Dump a slice of rule specifications to YAML format.
func ExportToDOT ¶
Generate a Graphviz visualisation of the DAG starting from the given node.
Types ¶
type ActionFn ¶
type ActionFn func(context.Context, Filterable)
Action function callback type.
An action callback is a function that takes a single argument containing the key/value pair map and returns no value.
type ActionParams ¶
Action parameters type.
A map of key/value pairs that is passed to the action handler.
type ActionSpec ¶
type ActionSpec struct {
// Parameters.
Params ActionParams `json:"params,omitempty" yaml:"params,omitempty"`
// Action name.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// Function to perform.
Perform string `json:"perform,omitempty" yaml:"perform,omitempty"`
}
Action specification.
type Actions ¶
type Actions interface {
// Build the given builtin functions.
Builder(string, ActionParams) (ActionFn, error)
}
Action builder interface.
The action builder provides a means of compiling JSON or YAML actions into explicit function objects
The resulting action is a function that takes `context.Context` and `Filterable` arguments and then performs some sort of user-defined action.
There are two default builtins provided for you:
`log`: Log the contents of the parameters to a logger. `mutate`: Change value(s) in the parameters.
To use the `log` builtin, you must provide a `logger.Logger` instance in the context used with the DAG. For this, you can see `logger.SetLogger`.
type Compiler ¶
type Compiler interface {
CompileAction(ActionSpec) (ActionFn, error)
CompileFailure(FailureSpec) (ActionFn, error)
Compile([]RuleSpec) []error
Evaluate(Filterable)
Export(io.Writer)
}
func NewCompiler ¶
Return a new DAG compiler.
func NewCompilerWithPredicates ¶
func NewCompilerWithPredicates( ctx context.Context, builder Actions, predicates PredicateDict, ) Compiler
Return a new DAG compiler with custom predicates.
type ConditionSpec ¶
type ConditionSpec struct {
// Value to check.
Value any `json:"value" yaml:"value"`
// Attribute to check.
Attribute string `json:"attribute" yaml:"attribute"`
// Predicate operator.
Operator string `json:"operator" yaml:"operator"`
}
Condition specification.
type DataInput ¶
type DataInput struct {
// contains filtered or unexported fields
}
Data Input.
This structure holds the data against which we wish to filter.
It is passed to functions such as `Compiler.Evaluate` as the input.
func NewDataInputFromMap ¶
Create a new `DataInput` object with a copy of the provided input map.
type EIRBuilder ¶
type EIRBuilder struct{}
func (*EIRBuilder) Token ¶
func (bld *EIRBuilder) Token() string
type EIRPredicate ¶
type EIRPredicate struct {
MetaPredicate
}
EIR - Exclusive In Range predicate.
Returne true if the input value is in the filter range inclusive.
func (*EIRPredicate) Debug ¶
func (pred *EIRPredicate) Debug() string
func (*EIRPredicate) Eval ¶
func (pred *EIRPredicate) Eval(_ context.Context, input Filterable) bool
func (*EIRPredicate) Instruction ¶
func (pred *EIRPredicate) Instruction() string
func (*EIRPredicate) String ¶
func (pred *EIRPredicate) String() string
func (*EIRPredicate) Token ¶
func (pred *EIRPredicate) Token() string
type EQPredicate ¶
type EQPredicate struct {
MetaPredicate
}
EQ - Numeric equality predicate.
Returns true if the input value matches the filter value.
func (*EQPredicate) Debug ¶
func (pred *EQPredicate) Debug() string
func (*EQPredicate) Eval ¶
func (pred *EQPredicate) Eval(_ context.Context, input Filterable) bool
func (*EQPredicate) Instruction ¶
func (pred *EQPredicate) Instruction() string
func (*EQPredicate) String ¶
func (pred *EQPredicate) String() string
func (*EQPredicate) Token ¶
func (pred *EQPredicate) Token() string
type FailureSpec ¶
type FailureSpec struct {
// Parameters.
Params ActionParams `json:"params,omitempty" yaml:"params,omitempty"`
// Action name.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// Function to perform.
Perform string `json:"perform,omitempty" yaml:"perform,omitempty"`
}
Failure action specification.
type Filterable ¶
type Filterable interface {
// Get the given key from the filterable entity.
//
// If the key exists, it is returned along with `true`.
//
// If the key does not exist, `false` returned.
Get(string) (any, bool)
// Set the given key to the given value.
//
// The graph engine should not add new entries, so if an attempt is
// made to do so, then `false` is returned and nothing happens.
Set(string, any) bool
// Get a list of keys from the filterable entity.
Keys() []string
// Return a string representation.
String() string
}
Filterable interface.
This interface allows objects to be used with the direct acyclig graph as input.
A 'filterable' entity provides a means of getting at its field contents so the DAG can look them up.
A decision was made to avoid `reflect` as the DAG might be in a hot path where reflection adds too big a performance hit.
type GTEBuilder ¶
type GTEBuilder struct{}
func (*GTEBuilder) Token ¶
func (bld *GTEBuilder) Token() string
type GTEPredicate ¶
type GTEPredicate struct {
MetaPredicate
}
GTE - Numeric Greater-Than-or-Equal-To predicate.
Returns true if the input value is greater than or equal to the filter value.
func (*GTEPredicate) Debug ¶
func (pred *GTEPredicate) Debug() string
func (*GTEPredicate) Eval ¶
func (pred *GTEPredicate) Eval(_ context.Context, input Filterable) bool
func (*GTEPredicate) Instruction ¶
func (pred *GTEPredicate) Instruction() string
func (*GTEPredicate) String ¶
func (pred *GTEPredicate) String() string
func (*GTEPredicate) Token ¶
func (pred *GTEPredicate) Token() string
type GTPredicate ¶
type GTPredicate struct {
MetaPredicate
}
GT - Numeric Greater-Than predicate.
Returns true of the input value is greater than the filter value.
func (*GTPredicate) Debug ¶
func (pred *GTPredicate) Debug() string
func (*GTPredicate) Eval ¶
func (pred *GTPredicate) Eval(_ context.Context, input Filterable) bool
func (*GTPredicate) Instruction ¶
func (pred *GTPredicate) Instruction() string
func (*GTPredicate) String ¶
func (pred *GTPredicate) String() string
func (*GTPredicate) Token ¶
func (pred *GTPredicate) Token() string
type IIRBuilder ¶
type IIRBuilder struct{}
func (*IIRBuilder) Token ¶
func (bld *IIRBuilder) Token() string
type IIRPredicate ¶
type IIRPredicate struct {
MetaPredicate
}
IIR - Inclusive In Range predicate.
Returns true if the input value is in the range defined in the filter inclusive.
func (*IIRPredicate) Debug ¶
func (pred *IIRPredicate) Debug() string
func (*IIRPredicate) Eval ¶
func (pred *IIRPredicate) Eval(_ context.Context, input Filterable) bool
func (*IIRPredicate) Instruction ¶
func (pred *IIRPredicate) Instruction() string
func (*IIRPredicate) String ¶
func (pred *IIRPredicate) String() string
func (*IIRPredicate) Token ¶
func (pred *IIRPredicate) Token() string
type LTEBuilder ¶
type LTEBuilder struct{}
func (*LTEBuilder) Token ¶
func (bld *LTEBuilder) Token() string
type LTEPredicate ¶
type LTEPredicate struct {
MetaPredicate
}
LTE - Numeric Less-Than-or-Equal-To predicate.
Returns true if the input value is lesser than or equal to the filter value.
func (*LTEPredicate) Debug ¶
func (pred *LTEPredicate) Debug() string
func (*LTEPredicate) Eval ¶
func (pred *LTEPredicate) Eval(_ context.Context, input Filterable) bool
func (*LTEPredicate) Instruction ¶
func (pred *LTEPredicate) Instruction() string
func (*LTEPredicate) String ¶
func (pred *LTEPredicate) String() string
func (*LTEPredicate) Token ¶
func (pred *LTEPredicate) Token() string
type LTPredicate ¶
type LTPredicate struct {
MetaPredicate
}
LT - Numeric Less-Than predicate.
Returns true if the input value is lesser than the filter value.
func (*LTPredicate) Debug ¶
func (pred *LTPredicate) Debug() string
func (*LTPredicate) Eval ¶
func (pred *LTPredicate) Eval(_ context.Context, input Filterable) bool
func (*LTPredicate) Instruction ¶
func (pred *LTPredicate) Instruction() string
func (*LTPredicate) String ¶
func (pred *LTPredicate) String() string
func (*LTPredicate) Token ¶
func (pred *LTPredicate) Token() string
type MetaPredicate ¶
type MetaPredicate struct {
// contains filtered or unexported fields
}
A `meta` predicate used by all predicates.
The meta preducate presents common fields and methods so as to avoid duplicate code.
func (*MetaPredicate) Debug ¶
func (meta *MetaPredicate) Debug(isn, token string) string
func (*MetaPredicate) EvalExclusiveRange ¶
func (meta *MetaPredicate) EvalExclusiveRange(input Filterable) bool
Does the predicate's input value fall within the exclusive range defined in the predicate's filter value?
func (*MetaPredicate) EvalInclusiveRange ¶
func (meta *MetaPredicate) EvalInclusiveRange(input Filterable) bool
Does the predicate's input value fall within the inclusive range defined in the predicate's filter value?
func (*MetaPredicate) EvalStringMember ¶
func (meta *MetaPredicate) EvalStringMember(input Filterable, insens bool) bool
Is the predicate's input value a member of the array of strings in the predicate's filter value?
func (*MetaPredicate) GetFloatValueFromInput ¶
func (meta *MetaPredicate) GetFloatValueFromInput(input Filterable) (float64, bool)
Return the predicate's input value as a 64-bit float.
This will return the value for the key on which the predicate operates.
func (*MetaPredicate) GetFloatValues ¶
func (meta *MetaPredicate) GetFloatValues(input Filterable) (float64, float64, bool)
Return both the predicate's input value and filter value as a 64-bit float.
func (*MetaPredicate) GetPredicateFloatArray ¶
func (meta *MetaPredicate) GetPredicateFloatArray() ([]float64, bool)
Return the predicate's filter value as an array of 64-bit floats.
func (*MetaPredicate) GetPredicateStringArray ¶
func (meta *MetaPredicate) GetPredicateStringArray() ([]string, bool)
Return the predicate's filter value as an array of strings.
func (*MetaPredicate) GetStringValues ¶
func (meta *MetaPredicate) GetStringValues(input Filterable) (string, string, bool)
Return both the predicate's input value and filter value as a string.
func (*MetaPredicate) String ¶
func (meta *MetaPredicate) String(token string) string
type NEQBuilder ¶
type NEQBuilder struct{}
func (*NEQBuilder) Token ¶
func (bld *NEQBuilder) Token() string
type NEQPredicate ¶
type NEQPredicate struct {
MetaPredicate
}
NEQ - Numeric Inequality predicate.
Returns true if the input value is not equal to the filter value.
func (*NEQPredicate) Debug ¶
func (pred *NEQPredicate) Debug() string
func (*NEQPredicate) Eval ¶
func (pred *NEQPredicate) Eval(_ context.Context, input Filterable) bool
func (*NEQPredicate) Instruction ¶
func (pred *NEQPredicate) Instruction() string
func (*NEQPredicate) String ¶
func (pred *NEQPredicate) String() string
func (*NEQPredicate) Token ¶
func (pred *NEQPredicate) Token() string
type NOOPPredicate ¶
type NOOPPredicate struct{}
NOOP - No operation.
This predicate is used internally to represent the root node.
It always returns true.
func (*NOOPPredicate) Debug ¶
func (pred *NOOPPredicate) Debug() string
func (*NOOPPredicate) Eval ¶
func (pred *NOOPPredicate) Eval(_ context.Context, _ Filterable) bool
func (*NOOPPredicate) Instruction ¶
func (pred *NOOPPredicate) Instruction() string
func (*NOOPPredicate) String ¶
func (pred *NOOPPredicate) String() string
func (*NOOPPredicate) Token ¶
func (pred *NOOPPredicate) Token() string
type Predicate ¶
type Predicate interface {
// Evaluate the predicate against the given `Filterable` object.
//
// Returns the result of the predicate.
Eval(context.Context, Filterable) bool
// Return the string representation of the predicate.
String() string
// Return the instruction name for the predicate.
//
// This isn't used in the current version of the directed acyclic
// graph, but the theory is that this could be used in a tokeniser
// or as opcode.
//
// The value this returns must be unique.
Instruction() string
// Return the token name for the predicate.
//
// This is the string value used in the action specification.
Token() string
// Return a string representation for debugging.
Debug() string
}
Predicate interface.
All predicates must adhere to this interface.
type PredicateBuilder ¶
type PredicateBuilder interface {
// Return the token name for the predicate.
//
// This isn't used in the current version of the directed acyclic
// graph, but the theory is that this could be used in a tokeniser
// or as opcode.
//
// The value this returns must be unique.
Token() string
// Build a new predicate.
//
// This will create a predicate that operates on the given field
// and data.
Build(field string, data any, lgr logger.Logger, dbg bool) (Predicate, error)
}
Predicate builder interface.
All predicate builders must adhere to this interface.
type PredicateDict ¶
type PredicateDict map[string]PredicateBuilder
Dictionary of available predicate builders.
func BuildPredicateDict ¶
func BuildPredicateDict() PredicateDict
Build the predicate dictionary for the directed acyclic graph filter.
type PredicateFn ¶
Predicate function type.
A predicate is a function that answers a yes-or-no question. In other words: any expression that can boil down to a boolean.
type REIMBuilder ¶
type REIMBuilder struct{}
func (*REIMBuilder) Token ¶
func (bld *REIMBuilder) Token() string
type REIMPredicate ¶
type REIMPredicate struct {
MetaPredicate
// contains filtered or unexported fields
}
REIM - Regular Expression (Insensitive) Match predicate.
Returns true if the regular expression in the filter matches against the input value.
The regular expression will be compiled with a prefix denoting that it does not care about case.
func (*REIMPredicate) Debug ¶
func (pred *REIMPredicate) Debug() string
func (*REIMPredicate) Eval ¶
func (pred *REIMPredicate) Eval(_ context.Context, input Filterable) bool
func (*REIMPredicate) Instruction ¶
func (pred *REIMPredicate) Instruction() string
func (*REIMPredicate) String ¶
func (pred *REIMPredicate) String() string
func (*REIMPredicate) Token ¶
func (pred *REIMPredicate) Token() string
type RESMBuilder ¶
type RESMBuilder struct{}
func (*RESMBuilder) Token ¶
func (bld *RESMBuilder) Token() string
type RESMPredicate ¶
type RESMPredicate struct {
MetaPredicate
// contains filtered or unexported fields
}
RESM - Regular Expression (Sensitive) Match predicate.
Returns true if the regular expression in the filter value matches against the input value.
The regular expression will not be forced into being case-insensitive.
func (*RESMPredicate) Debug ¶
func (pred *RESMPredicate) Debug() string
func (*RESMPredicate) Eval ¶
func (pred *RESMPredicate) Eval(_ context.Context, input Filterable) bool
func (*RESMPredicate) Instruction ¶
func (pred *RESMPredicate) Instruction() string
func (*RESMPredicate) String ¶
func (pred *RESMPredicate) String() string
func (*RESMPredicate) Token ¶
func (pred *RESMPredicate) Token() string
type RuleSpec ¶
type RuleSpec struct {
// Action to evaluate.
Action ActionSpec `json:"action" yaml:"action"`
// Action to evaluate on failure.
Failure FailureSpec `json:"failure" yaml:"failure"`
// Rule name.
Name string `json:"name" yaml:"name"`
// List of conditions.
Conditions []ConditionSpec `json:"conditions" yaml:"conditions"`
}
Filter rule specification.
func ParseFromJSON ¶
Parse a rule specification from a string containing JSON.
func ParseFromYAML ¶
Parse a rule specification from a string containing YAML.
func (*RuleSpec) DumpToJSON ¶
Dump the rule specification to JSON format.
func (*RuleSpec) DumpToYAML ¶
Dump the rule specification to YAML format.
type SIEQBuilder ¶
type SIEQBuilder struct{}
func (*SIEQBuilder) Token ¶
func (bld *SIEQBuilder) Token() string
type SIEQPredicate ¶
type SIEQPredicate struct {
MetaPredicate
}
SIEG - String (Insensitive) Equality predicate.
Returns true if the filter value matches the input value.
This predicate does not care about case.
func (*SIEQPredicate) Debug ¶
func (pred *SIEQPredicate) Debug() string
func (*SIEQPredicate) Eval ¶
func (pred *SIEQPredicate) Eval(_ context.Context, input Filterable) bool
func (*SIEQPredicate) Instruction ¶
func (pred *SIEQPredicate) Instruction() string
func (*SIEQPredicate) String ¶
func (pred *SIEQPredicate) String() string
func (*SIEQPredicate) Token ¶
func (pred *SIEQPredicate) Token() string
type SIMBuilder ¶
type SIMBuilder struct{}
func (*SIMBuilder) Token ¶
func (bld *SIMBuilder) Token() string
type SIMPredicate ¶
type SIMPredicate struct {
MetaPredicate
}
SIM - String (Insensitive) Member predicate.
Returns true if the input value is a member of the string array in the filter value.
func (*SIMPredicate) Debug ¶
func (pred *SIMPredicate) Debug() string
func (*SIMPredicate) Eval ¶
func (pred *SIMPredicate) Eval(_ context.Context, input Filterable) bool
func (*SIMPredicate) Instruction ¶
func (pred *SIMPredicate) Instruction() string
func (*SIMPredicate) String ¶
func (pred *SIMPredicate) String() string
func (*SIMPredicate) Token ¶
func (pred *SIMPredicate) Token() string
type SINEQBuilder ¶
type SINEQBuilder struct{}
func (*SINEQBuilder) Token ¶
func (bld *SINEQBuilder) Token() string
type SINEQPredicate ¶
type SINEQPredicate struct {
MetaPredicate
}
SINEQ - String (Insensitive) Inequality predicate.
Returns true if the input string is not the same as the filter string.
Case is not taken into account.
func (*SINEQPredicate) Debug ¶
func (pred *SINEQPredicate) Debug() string
func (*SINEQPredicate) Eval ¶
func (pred *SINEQPredicate) Eval(_ context.Context, input Filterable) bool
func (*SINEQPredicate) Instruction ¶
func (pred *SINEQPredicate) Instruction() string
func (*SINEQPredicate) String ¶
func (pred *SINEQPredicate) String() string
func (*SINEQPredicate) Token ¶
func (pred *SINEQPredicate) Token() string
type SSEQBuilder ¶
type SSEQBuilder struct{}
func (*SSEQBuilder) Token ¶
func (bld *SSEQBuilder) Token() string
type SSEQPredicate ¶
type SSEQPredicate struct {
MetaPredicate
}
SSEQ - String (Sensitive) Equality predicate.
Returns true if the input value is the same as the filter value.
func (*SSEQPredicate) Debug ¶
func (pred *SSEQPredicate) Debug() string
func (*SSEQPredicate) Eval ¶
func (pred *SSEQPredicate) Eval(_ context.Context, input Filterable) bool
func (*SSEQPredicate) Instruction ¶
func (pred *SSEQPredicate) Instruction() string
func (*SSEQPredicate) String ¶
func (pred *SSEQPredicate) String() string
func (*SSEQPredicate) Token ¶
func (pred *SSEQPredicate) Token() string
type SSMBuilder ¶
type SSMBuilder struct{}
func (*SSMBuilder) Token ¶
func (bld *SSMBuilder) Token() string
type SSMPredicate ¶
type SSMPredicate struct {
MetaPredicate
}
SSM - String (Sensitive) Member predicate.
Returns true if the input value is a member of the string array in the filter value.
func (*SSMPredicate) Debug ¶
func (pred *SSMPredicate) Debug() string
func (*SSMPredicate) Eval ¶
func (pred *SSMPredicate) Eval(_ context.Context, input Filterable) bool
func (*SSMPredicate) Instruction ¶
func (pred *SSMPredicate) Instruction() string
func (*SSMPredicate) String ¶
func (pred *SSMPredicate) String() string
func (*SSMPredicate) Token ¶
func (pred *SSMPredicate) Token() string
type SSNEQBuilder ¶
type SSNEQBuilder struct{}
func (*SSNEQBuilder) Token ¶
func (bld *SSNEQBuilder) Token() string
type SSNEQPredicate ¶
type SSNEQPredicate struct {
MetaPredicate
}
SSNEQ - String (Sensitive) Inequality predicate.
Returns true if the input value is different to the filter value.
func (*SSNEQPredicate) Debug ¶
func (pred *SSNEQPredicate) Debug() string
func (*SSNEQPredicate) Eval ¶
func (pred *SSNEQPredicate) Eval(_ context.Context, input Filterable) bool
func (*SSNEQPredicate) Instruction ¶
func (pred *SSNEQPredicate) Instruction() string
func (*SSNEQPredicate) String ¶
func (pred *SSNEQPredicate) String() string
func (*SSNEQPredicate) Token ¶
func (pred *SSNEQPredicate) Token() string
Source Files
¶
- actions.go
- compiler.go
- datainput.go
- graphviz.go
- node.go
- predicate.go
- predicate_eir.go
- predicate_eq.go
- predicate_gt.go
- predicate_gte.go
- predicate_iir.go
- predicate_lt.go
- predicate_lte.go
- predicate_neq.go
- predicate_noop.go
- predicate_reim.go
- predicate_resm.go
- predicate_sieq.go
- predicate_sim.go
- predicate_sineq.go
- predicate_sseq.go
- predicate_ssm.go
- predicate_ssneq.go
- types.go
- utils.go