Documentation ¶
Index ¶
- Constants
- func AddMacro(macro string, expanded string) map[string]string
- func Apply(b []byte, query string) (truth bool, record string, err error)
- func Eval(expr *Expression, json string) (truth bool, newJson string, err error)
- func ExpandMacros(query string) (string, error)
- func PrepareQuery(query string) (expr *Expression, prop Propagate, err error)
- func Validate(query string) (err error)
- type CallExpression
- type Comparison
- type Equality
- type Expression
- type Logical
- type Parameter
- type Primary
- type Propagate
- type SelectExpression
- type Unary
Constants ¶
const LATEST = "latest"
const REDACTED = "[REDACTED]"
Variables ¶
This section is empty.
Functions ¶
func AddMacro ¶
AddMacro takes macro and its corresponding expanded version as arguments. It stores the macro in a global map.
func Apply ¶
Apply does these steps: - Expand the macros in given query. - Parse the query. - Precompute the values in the query. - Evaluate the expression. and returns: - Truthiness of the record for the given query. - The new record that's altered by the query. - Error if any step ends with an error, otherwise nil.
func Eval ¶
func Eval(expr *Expression, json string) (truth bool, newJson string, err error)
Eval evaluatues boolean truthiness of given JSON against the query that's provided in the form of an AST (Expression). It's the method that implements the querying functionality in the database.
It's a lazy and a generic implementation to provide a medium for flexible filtering on an arbitrary JSON structure.
Calling Precompute() on Expression before calling Eval() improves performance.
func ExpandMacros ¶
ExpandMacro expands the macros in a given query, if there are any. It uses a lookahead regular expression to ignore the occurences of the macro inside the string literals.
func PrepareQuery ¶
func PrepareQuery(query string) (expr *Expression, prop Propagate, err error)
Types ¶
type CallExpression ¶
type CallExpression struct { Identifier *string `@Ident ( @("." "*" | ".") @Ident? )*` Parameters []*Parameter `[ "(" (@@ ("," @@)*)? ")" ]` SelectExpression *SelectExpression `[ @@ ]` }
type Comparison ¶
type Comparison struct { Unary *Unary `@@` Op string `[ @( ">" "=" | ">" | "<" "=" | "<" )` Next *Comparison ` @@ ]` }
type Equality ¶
type Equality struct { Comparison *Comparison `@@` Op string `[ @( "!" "=" | "=" "=" )` Next *Equality ` @@ ]` }
type Expression ¶
type Expression struct {
Logical *Logical `@@`
}
func Parse ¶
func Parse(text string) (expr *Expression, err error)
Parse parses the query (filtering syntax) into tree stucture defined as Expression. Tags defines the grammar rules and tokens. Expression is the Abstract Syntax Tree (AST) of this query language.
type Primary ¶
type Primary struct { Number *float64 ` @Float | @Int` String *string `| @(String|Char|RawString)` Regex *string `| "r" @(String|Char|RawString)` Bool *bool `| ( @"true" | "false" )` Nil bool `| @"nil"` CallExpression *CallExpression `| @@` SubExpression *Expression `| "(" @@ ")" ` JsonPath *jp.Expr Regexp *regexp.Regexp Helper *string }
type Propagate ¶
func Precompute ¶
func Precompute(expr *Expression) (prop Propagate, err error)
Precompute does compile-time evaluations on parsed query (AST/Expression) to prevent unnecessary computations in Eval() method. Modifies the fields of only the Primary struct.
type SelectExpression ¶
type SelectExpression struct { Index *int `[ "[" @Int "]" ]` Key *string `[ "[" @(String|Char|RawString|"*") "]" ]` RecursiveDescent *string `[ "." "." @Ident ]` Expression *Expression `[ "." @@ ]` }