Documentation ¶
Overview ¶
Package ast defines the AST for simple language AST can be created either by parser or by hand, it's up to implementer to decide how to create AST This package provides few examples of AST creation mostly by parsing JSON - ast_sugar.go - ast_human_friendly.go - ast_description_of_best_result Much more advance parser is also possible, but it's not implemented here
Index ¶
- func PtrBool(b bool) *bool
- func PtrFloat(f float64) *float64
- type Accessor
- type And
- type BoostRuleOneOf
- type BoostWhenFieldRuleOneOf
- type ConstBoost
- type DescriptionOfBestResult
- type Eq
- type EqTo
- type Field
- type FieldRuleOneOf
- type FiledBoostRule
- type FiledRule
- type GrThan
- type Gt
- type HumanFriendlyRules
- type InterpretValueAST
- type IntrprateOperatorAST
- func (e *IntrprateOperatorAST) Eval(ast Operator, data MapAny) bool
- func (e *IntrprateOperatorAST) Value(v Value, data MapAny) (value interface{}, found bool)
- func (e *IntrprateOperatorAST) VisitAnd(v *And) any
- func (e *IntrprateOperatorAST) VisitEq(v *Eq) any
- func (e *IntrprateOperatorAST) VisitGt(v *Gt) any
- func (e *IntrprateOperatorAST) VisitNot(v *Not) any
- func (e *IntrprateOperatorAST) VisitOr(v *Or) any
- type Lit
- type MapAny
- type Not
- type Or
- type OrFields
- type RuleOneOf
- type ScoreCalculationFromDescriptionOfBestResult
- type ScoreCalculationFromHumanFriendlyRules
- type TranslateSyntaxASTtoOperatorAST
- type ValueBoost
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Accessor ¶
type Accessor struct{ Path []string }
Value represents how value can be represented in AST Lit - literal value like int or string Accessor represents field access like "a.b.c" Accessor assumes that interpreter will provide data to return value that exists under such path
type And ¶
type And struct{ List []Operator }
Operator represents binary operation like - Eq - == - Gt - > - And - && - Or - || - Not - !
type BoostRuleOneOf ¶
type BoostRuleOneOf struct { ConstBoost *ConstBoost `json:"boost,omitempty"` ValueBoost *ValueBoost `json:"valueBoost,omitempty"` }
type BoostWhenFieldRuleOneOf ¶
type BoostWhenFieldRuleOneOf struct { Boost *float64 `json:"boost,omitempty"` // multiply score by value in field MultiplyUsingFieldValue *bool `json:"multiply,omitempty"` // There can be more operations supported When FieldRuleOneOf `json:"when"` }
type ConstBoost ¶
type DescriptionOfBestResult ¶
type DescriptionOfBestResult struct { AtLeastOneOf []BoostWhenFieldRuleOneOf `json:"atLeastOneOf"` MustMatchOneOf []FieldRuleOneOf `json:"mustMatchOneOf"` }
type Eq ¶
type Eq struct{ L, R Value }
Operator represents binary operation like - Eq - == - Gt - > - And - && - Or - || - Not - !
type FieldRuleOneOf ¶
type FieldRuleOneOf struct { Field Field `json:"field,omitempty"` Eq interface{} `json:"eq,omitempty"` Gt interface{} `json:"gt,omitempty"` Or *FieldRuleOneOf `json:"or,omitempty"` And *FieldRuleOneOf `json:"and,omitempty"` Not *FieldRuleOneOf `json:"not,omitempty"` }
func (FieldRuleOneOf) ToOperation ¶
func (a FieldRuleOneOf) ToOperation() Operator
type FiledBoostRule ¶
type FiledBoostRule map[Field]BoostRuleOneOf
type Gt ¶
type Gt struct{ L, R Value }
Operator represents binary operation like - Eq - == - Gt - > - And - && - Or - || - Not - !
type HumanFriendlyRules ¶
type HumanFriendlyRules struct { // OneOf implies OR for list of rules - AtLeastOneOf // Lack of it implies AND for list of rules - MustMatch AtLeastOneOf []FiledBoostRule `json:"atLeastOneOf"` MustMatch []FiledRule `json:"mustMatch"` }
func (HumanFriendlyRules) MustMatchToOperation ¶
func (ast HumanFriendlyRules) MustMatchToOperation() Operator
type InterpretValueAST ¶
type InterpretValueAST struct {
V MapAny
}
func (*InterpretValueAST) VisitAccessor ¶
func (e *InterpretValueAST) VisitAccessor(v *Accessor) any
func (*InterpretValueAST) VisitLit ¶
func (e *InterpretValueAST) VisitLit(v *Lit) any
type IntrprateOperatorAST ¶
type IntrprateOperatorAST struct {
// contains filtered or unexported fields
}
func NewInterpreter ¶
func NewInterpreter() *IntrprateOperatorAST
func (*IntrprateOperatorAST) Eval ¶
func (e *IntrprateOperatorAST) Eval(ast Operator, data MapAny) bool
func (*IntrprateOperatorAST) Value ¶
func (e *IntrprateOperatorAST) Value(v Value, data MapAny) (value interface{}, found bool)
func (*IntrprateOperatorAST) VisitAnd ¶
func (e *IntrprateOperatorAST) VisitAnd(v *And) any
func (*IntrprateOperatorAST) VisitEq ¶
func (e *IntrprateOperatorAST) VisitEq(v *Eq) any
func (*IntrprateOperatorAST) VisitGt ¶
func (e *IntrprateOperatorAST) VisitGt(v *Gt) any
func (*IntrprateOperatorAST) VisitNot ¶
func (e *IntrprateOperatorAST) VisitNot(v *Not) any
func (*IntrprateOperatorAST) VisitOr ¶
func (e *IntrprateOperatorAST) VisitOr(v *Or) any
type Lit ¶
type Lit struct{ Value any }
Value represents how value can be represented in AST Lit - literal value like int or string Accessor represents field access like "a.b.c" Accessor assumes that interpreter will provide data to return value that exists under such path
type Not ¶
type Not struct{ Operator }
Operator represents binary operation like - Eq - == - Gt - > - And - && - Or - || - Not - !
type Or ¶
type Or struct{ List []Operator }
Operator represents binary operation like - Eq - == - Gt - > - And - && - Or - || - Not - !
type RuleOneOf ¶
type RuleOneOf struct { Eq interface{} `json:"eq,omitempty"` Gt interface{} `json:"gt,omitempty"` }
type ScoreCalculationFromDescriptionOfBestResult ¶
type ScoreCalculationFromDescriptionOfBestResult struct {
// contains filtered or unexported fields
}
func NewScoreCalculatorFromDescriptionOfBestResult ¶
func NewScoreCalculatorFromDescriptionOfBestResult() *ScoreCalculationFromDescriptionOfBestResult
func (*ScoreCalculationFromDescriptionOfBestResult) Calculate ¶
func (s *ScoreCalculationFromDescriptionOfBestResult) Calculate(ast DescriptionOfBestResult, data MapAny) (score float64)
Calculate is based on rule that - must match - then allow should match calculation, otherwise score is 0 - should - takes and adds score from all should rules - result is final score for given data record
type ScoreCalculationFromHumanFriendlyRules ¶
type ScoreCalculationFromHumanFriendlyRules struct {
// contains filtered or unexported fields
}
func NewScoreCalculatorFromHumanFriendlyRules ¶
func NewScoreCalculatorFromHumanFriendlyRules() *ScoreCalculationFromHumanFriendlyRules
func (*ScoreCalculationFromHumanFriendlyRules) Calculate ¶
func (s *ScoreCalculationFromHumanFriendlyRules) Calculate(ast HumanFriendlyRules, data MapAny) (score float64)
Calculate is based on rule that - must match - then allow should match calculation, otherwise score is 0 - should - takes and adds score from all should rules - result is final score for given data record
type TranslateSyntaxASTtoOperatorAST ¶
type TranslateSyntaxASTtoOperatorAST struct {
// contains filtered or unexported fields
}
func (*TranslateSyntaxASTtoOperatorAST) VisitEqTo ¶
func (a *TranslateSyntaxASTtoOperatorAST) VisitEqTo(v *EqTo) any
func (*TranslateSyntaxASTtoOperatorAST) VisitGrThan ¶
func (a *TranslateSyntaxASTtoOperatorAST) VisitGrThan(v *GrThan) any
func (*TranslateSyntaxASTtoOperatorAST) VisitOrFields ¶
func (a *TranslateSyntaxASTtoOperatorAST) VisitOrFields(v *OrFields) any
type ValueBoost ¶
type ValueBoost struct {
RuleOneOf
}