Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrorUnknownDecimalOperator is returned when a decimal comparison operator is unrecognized. ErrorUnknownDecimalOperator = errors.New("unknown decimal operator") // ErrorTypeMismatch is returned when two values fail a strict type check. ErrorTypeMismatch = errors.New("compare type mismatch") // ErrorUnknownStringOperator is returned when a string-based operator (co, sw, ew, in, pr) is unknown. ErrorUnknownStringOperator = errors.New("unknown string operator") // ErrorNoExpression is returned when the expression tree is nil or missing. ErrorNoExpression = errors.New("there is no expression") // ErrorInvalidValue is returned for invalid numeric/string parse attempts. ErrorInvalidValue = errors.New("invalid value") // ErrorUnknownType is returned for unknown or unsupported type annotations. ErrorUnknownType = errors.New("invalid value") // ErrorInvalidFunctionCall is returned when a function call parse is incomplete or malformed. ErrorInvalidFunctionCall = errors.New("invalid function call") // ErrorInvalidExpression is returned when the parse tree visitor hits an unexpected node. ErrorInvalidExpression = errors.New("invalid expression") // ErrorInvalidOperator is returned for an unknown or unsupported operator in a comparison. ErrorInvalidOperator = errors.New("invalid operator") // ErrorSyntaxError is used for general syntax errors in the input query. ErrorSyntaxError = errors.New("syntax error") )
Error variables used throughout the package:
Functions ¶
This section is empty.
Types ¶
type ArgumentType ¶
type ArgumentType int
ArgumentType indicates how to interpret a Parameter or FunctionArgument's value (string, int, decimal, etc.).
const ( // ArgTypeUnknown means the type could not be determined or does not match known annotations. ArgTypeUnknown ArgumentType = iota // ArgTypeString indicates a string type. ArgTypeString // ArgTypeInteger indicates a standard int type (no specific bit width). ArgTypeInteger // ArgTypeUnsignedInteger indicates a standard uint type (no specific bit width). ArgTypeUnsignedInteger // ArgTypeFloat64 indicates a float64 type. ArgTypeFloat64 // ArgTypeBoolean indicates a bool type. ArgTypeBoolean // ArgTypeNull indicates a nil or null value. ArgTypeNull // ArgTypeList indicates some bracketed list syntax, e.g. [1,2,3]. ArgTypeList // ArgTypeFloat32 indicates a float32 type. ArgTypeFloat32 // ArgTypeInteger32 indicates a 32-bit integer type (int32). ArgTypeInteger32 // ArgTypeInteger64 indicates a 64-bit integer type (int64). ArgTypeInteger64 // ArgTypeUnsignedInteger64 indicates a 64-bit unsigned integer type (uint64). ArgTypeUnsignedInteger64 // ArgTypeUnsignedInteger32 indicates a 32-bit unsigned integer type (uint32). ArgTypeUnsignedInteger32 // ArgTypeDecimal indicates a decimal.Decimal type (from shopspring/decimal). ArgTypeDecimal )
func (ArgumentType) String ¶
func (at ArgumentType) String() string
String returns the string representation of the ArgumentType (for debugging).
type Config ¶
type Config struct {
DebugMode bool
}
Config controls optional ParseQuery() behaviors.
Fields:
- DebugMode: if true, evaluation debug lines are printed to stdout
type Evaluation ¶
Evaluation couples a parsed Parameter with an actual value for runtime evaluation. The Evaluate() method will iterate over these pairs to resolve the final query result.
type FunctionArgument ¶
type FunctionArgument struct { ArgumentType ArgumentType // The determined type (ArgTypeString, ArgTypeInteger, etc.) Value any // The actual parsed value }
FunctionArgument represents a single argument in a function call, e.g. get_author("abc", 123).
type InputType ¶
type InputType int
InputType indicates if a Parameter is for a function call (FunctionCall) or a direct attribute expression (Expression).
type Parameter ¶
type Parameter struct { Name string InputType InputType FunctionArguments []FunctionArgument Expression ArgumentType // contains filtered or unexported fields }
Parameter holds all information needed to represent a single condition or function invocation within a query. For example, "age gt 18" or "get_author("Book") eq "Walt Whitman".
Fields:
- id: unique ID for internal evaluation references
- Name: the attribute name or function name
- InputType: either Expression or FunctionCall
- FunctionArguments: slice of arguments if it's a function call
- strictTypeCheck: indicates if type annotations must be strictly enforced
- Expression: if InputType == Expression, describes the type annotation discovered
- operator: the SCIM-like operator (eq, gt, etc.)
- compareValue: the RHS value for the comparison (number, string, decimal, etc.)
type Rule ¶
type Rule struct { Params []Parameter // contains filtered or unexported fields }
Rule represents the final compiled query. After parsing, a Rule contains:
- Params: all discovered parameters
- exprTree: the root of the expression tree for logical ops
- debugMode: flag enabling debug prints during evaluation
func ParseQuery ¶
ParseQuery takes a SCIM-like query (e.g. `age gt 30 and (lang eq "en" or lang eq "fr")`) and compiles it into a Rule object. Optional config can enable DebugMode.
If parsing fails due to syntax errors or other issues, an error is returned. Otherwise, the returned Rule can be used for Evaluate().
Example ¶
Example usage test (just to show quick usage)
rule, err := ParseQuery(`(active eq true) and (score gt 50)`, nil) if err != nil { fmt.Println("Error:", err) return } // Supply actual param values: evals := []Evaluation{ { Param: rule.Params[0], // active Result: true, }, { Param: rule.Params[1], // score Result: 75, }, } ok, err := rule.Evaluate(evals) if err != nil { fmt.Println("Error:", err) return } fmt.Println(ok)
Output: true