ast

package
v1.22.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func PtrBool

func PtrBool(b bool) *bool

func PtrFloat

func PtrFloat(f float64) *float64

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 ConstBoost struct {
	Boost float64 `json:"boost"`
	RuleOneOf
}

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 EqTo

type EqTo struct{ V any }

type Field

type Field string

func (Field) ToPath

func (f Field) ToPath() []string

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 FiledRule

type FiledRule map[Field]RuleOneOf

type GrThan

type GrThan struct{ V any }

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 MapAny

type MapAny = map[string]any

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 OrFields

type OrFields struct{ M map[string]SyntaxSugar }

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

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

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
}

Jump to

Keyboard shortcuts

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