kfl

package
v0.8.59 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const LATEST = "latest"
View Source
const REDACTED = "[REDACTED]"

Variables

This section is empty.

Functions

func AddMacro

func AddMacro(macro string, expanded string) map[string]string

AddMacro takes macro and its corresponding expanded version as arguments. It stores the macro in a global map.

func Apply

func Apply(b []byte, query string) (truth bool, record string, err error)

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

func ExpandMacros(query string) (string, error)

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)

func Validate

func Validate(query string) (err error)

Validate tries to parse the given query and checks if there are any syntax errors or not.

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 Logical

type Logical struct {
	Equality *Equality `@@`
	Op       string    `[ @( "and" | "or" )`
	Next     *Logical  `  @@ ]`
}

type Parameter

type Parameter struct {
	Tag        *string     `[ @Ident ":" ]`
	Expression *Expression `@@`
	JsonPath   *jp.Expr
	TimeSet    bool
	Time       time.Time
}

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

type Propagate struct {
	Path  string
	Limit uint64
}

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 `[ "." @@ ]`
}

type Unary

type Unary struct {
	Op      string   `  ( @( "!" | "-" )`
	Unary   *Unary   `    @@ )`
	Primary *Primary `| @@`
}

Jump to

Keyboard shortcuts

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