Documentation
¶
Overview ¶
File: actions.go Implements the action
File: condition.go Represents one condition ex. a == b
File: constants.go Stores all the constant shared within packages
File: context.go Represents a Context which is used during rule evaluation
File: engine.go Rule engine to evaluate the rule for the given data
File: operator.go Defines all the supported operators
File: parser.go Implements the rule parser
File: parser_utils.go Common utility functions
File: rule.go Implement the rule interface
Index ¶
- Constants
- func EvaluateOperation(operand1 interface{}, operand2 interface{}, optor Operator) bool
- func StringToInterface(value string) interface{}
- type Action
- type Condition
- type ConditionType
- type Context
- type ContextValue
- type MalformedRuleError
- type Operator
- type Rule
- type RuleContext
- type RuleEngine
- type RuleParser
- type RuleType
- type ScalarCondition
- type ScalarRule
- type SyntaxError
- type Token
- type VectorCondition
- type VectorRule
Constants ¶
const ( // IndexKey ... IndexKey string = "_INDEX_KEY" // IndexCurrentValue .. IndexCurrentValue string = "_INDEX_CURRENT_VALUE" // StartIndexValue ... StartIndexValue string = "_START_INDEX_VALUE" // EndIndexValue ... EndIndexValue string = "_END_INDEX_VALUE" )
Variables ¶
This section is empty.
Functions ¶
func EvaluateOperation ¶
EvaluateOperation is used to evaluate supported operations TODO: To make this more generic based on reflect package
func StringToInterface ¶
func StringToInterface(value string) interface{}
StringToInterface takes a string and returns the actual type wrapped in interface
Types ¶
type Condition ¶
type Condition interface { GetOperator() Operator Evaluate(ctx Context) (interface{}, error) GetValue() interface{} // contains filtered or unexported methods }
Condition represents the interface for condition
type ConditionType ¶
type ConditionType int
ConditionType represents the condition type
const ( // ScalarConditionType represents a binary condition which is one dimensional (ex. a == b && c == d) ScalarConditionType ConditionType = 1 // VectorConditionType represents iterative condition (ex. for i=0:N (result = result && a[i])) VectorConditionType ConditionType = 2 )
type Context ¶
type Context interface { SetValue(key string, value ContextValue) GetValue(key string) ContextValue KeyExists(key string) bool }
Context represents the structure that is used to hold the evaluation context
type ContextValue ¶
type ContextValue interface{}
ContextValue represents the type of the value held inside context
type MalformedRuleError ¶
type MalformedRuleError struct { }
MalformedRuleError raised when the input data is malformed in some way
func (*MalformedRuleError) Error ¶
func (_rt *MalformedRuleError) Error() string
type Operator ¶
type Operator string
Operator represents the type of operator
const ( // AndOperator for representing logical AND (works for bool ONLY) AndOperator Operator = "&&" // OrOperator for representing logical OR (works on bool ONLY) OrOperator Operator = "||" // EqualOperator for representing logical == (works for int, string, bool. float) EqualOperator Operator = "==" // GreaterThanOrEqualOperator for representing logical >= (works for int, float) GreaterThanOrEqualOperator Operator = ">=" // GreaterOperator for representing logical >= (works for int, float) GreaterOperator Operator = ">" // LesserThanOrEqualOperator for representing logical <= (works for int, float) LesserThanOrEqualOperator Operator = "<=" // LesserOperator for representing logical <= (works for int, float) LesserOperator Operator = "<" // NilOperator for representing NIL operator NilOperator Operator = "NIL" )
type Rule ¶
type Rule interface { GetType() RuleType Evaluate(ctx Context) (interface{}, error) BuildContext(ipData []byte, ctx Context) error }
Rule represent the common interface for any rule
type RuleContext ¶
type RuleContext struct {
// contains filtered or unexported fields
}
RuleContext represents the core context used during rule evaluation
func (*RuleContext) GetValue ¶
func (_ctx *RuleContext) GetValue(key string) ContextValue
GetValue gets a value from the context
func (*RuleContext) KeyExists ¶
func (_ctx *RuleContext) KeyExists(key string) bool
KeyExists checks if key exists on the context
func (*RuleContext) SetValue ¶
func (_ctx *RuleContext) SetValue(key string, value ContextValue)
SetValue sets a value inside context
type RuleEngine ¶
type RuleEngine struct { }
RuleEngine represents a service to evaluate the rule
func NewRuleEngine ¶
func NewRuleEngine() *RuleEngine
NewRuleEngine returns a fresh rule engine instance
func (*RuleEngine) Evaluate ¶
func (_re *RuleEngine) Evaluate(fgRule Rule, jsonData []byte) (interface{}, error)
Evaluate evaluates a rule for the given rule and jsonData args:
fgRule: The rule to evaluate jsonData: The data to be used during evaluation
Return
bool: Evaluation result (i.e true/false) error: Any error during evaluation
type RuleParser ¶
type RuleParser struct {
// contains filtered or unexported fields
}
RuleParser service to parse the rule
func NewRuleParser ¶
func NewRuleParser(ip string) *RuleParser
NewRuleParser returns the fresh instance of RuleParser
func (*RuleParser) ParseRule ¶
func (_p *RuleParser) ParseRule() (Rule, error)
ParseRule main entry to parse the rule args:
s (string): The input rule to parse
returns Rule: Parsed rule error: Error any dound while parsing TODO: Handle all cases - For now simplest case a && b && c
type ScalarCondition ¶
type ScalarCondition struct { Type ConditionType `json:"type"` Operator Operator `json:"optor"` Operand1 Condition `json:"o1"` Operand2 Condition `json:"o2"` Value interface{} `json:"value"` HasArrayIndex bool `json:"has_array_index"` }
ScalarCondition Condition represents one evaluatable binary expression (ex: a == b) Format: { a == b && c == d ) }
func (*ScalarCondition) Evaluate ¶
func (_c *ScalarCondition) Evaluate(ctx Context) (interface{}, error)
Evaluate does the evaluation of the condition and returns the result
func (*ScalarCondition) GetOperand1 ¶
func (_c *ScalarCondition) GetOperand1() Condition
GetOperand1 returns the underlying operand-1
func (*ScalarCondition) GetOperand2 ¶
func (_c *ScalarCondition) GetOperand2() Condition
GetOperand2 returns the underlying operand-2
func (*ScalarCondition) GetOperator ¶
func (_c *ScalarCondition) GetOperator() Operator
GetOperator returns the underlying operator in the condition
func (*ScalarCondition) GetValue ¶
func (_c *ScalarCondition) GetValue() interface{}
GetValue returns data stored in the condition
type ScalarRule ¶
type ScalarRule struct { Type RuleType `json:"type"` If Condition `json:"if"` Then Action `json:"then"` StartIndex interface{} `json:"start_index"` IndexKey interface{} `json:"index_key"` }
ScalarRule represents a structure of the If rule (created by parsing the user provided rules)
func (*ScalarRule) BuildContext ¶
func (_fgr *ScalarRule) BuildContext(ipData []byte, ctx Context) error
BuildContext builds the context
func (*ScalarRule) Evaluate ¶
func (_fgr *ScalarRule) Evaluate(ctx Context) (interface{}, error)
Evaluate evalates the rule
func (*ScalarRule) GetType ¶
func (_fgr *ScalarRule) GetType() RuleType
GetType gets the type of rule
type SyntaxError ¶
SyntaxError raised when there is a syntax error
func (*SyntaxError) Error ¶
func (_rt *SyntaxError) Error() string
type Token ¶
type Token string
Token represents a valid token.
const ( // IfToken represents start of If rule IfToken Token = "IF:" // ThenToken represents start of Then block ThenToken Token = "THEN:" // OpenBraceToken represents start of block OpenBraceToken Token = "(" // CloseBraceToken represents close of block CloseBraceToken Token = ")" // CurlyOpenBraceToken represents start of a condition/action CurlyOpenBraceToken Token = "{" // CurlyCloseBraceToken represents close of block CurlyCloseBraceToken Token = "}" // ColonToken represents delimiter ColonToken Token = ":" // ForToken represents start if For rule ForToken Token = "FOR:" )
type VectorCondition ¶
type VectorCondition struct { Type ConditionType `json:"type"` Operator Operator `json:"optor"` SCondition Condition `json:"scalar_condition"` Value interface{} `json:"value"` StartIndex interface{} `json:"start_index"` EndIndex interface{} `json:"end_index"` IndexKey string `json:"index_key"` }
VectorCondition represents collection of Conditions joined by operator Format: { FOR i=initialValue:finalValue SCALAR_CONDITION }
func (*VectorCondition) Evaluate ¶
func (_c *VectorCondition) Evaluate(ctx Context) (interface{}, error)
Evaluate does the evaluation of the condition and returns the result
func (*VectorCondition) GetOperator ¶
func (_c *VectorCondition) GetOperator() Operator
GetOperator returns the underlying operator in the condition
func (*VectorCondition) GetValue ¶
func (_c *VectorCondition) GetValue() interface{}
GetValue returns data stored in the condition
type VectorRule ¶
type VectorRule struct { Type RuleType `json:"type"` SRule Rule `json:"scalar_rule"` StartIndex interface{} `json:"start_index"` EndIndex interface{} `json:"end_index"` IndexKey interface{} `json:"index_key"` }
VectorRule represents a collection of Simple rules
func (*VectorRule) BuildContext ¶
func (_fgr *VectorRule) BuildContext(ipData []byte, ctx Context) error
BuildContext builds the context
func (*VectorRule) Evaluate ¶
func (_fgr *VectorRule) Evaluate(ctx Context) (interface{}, error)
Evaluate evalates the rule
func (*VectorRule) GetType ¶
func (_fgr *VectorRule) GetType() RuleType
GetType gets the type of rule