dfl

package
Version: v0.0.0-...-8ad63dc Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2019 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package dfl provides the interfaces, embedded structs, and implementing code for parsing, compiling, and evaluating Dynamic Filter Language (DFL) expressions.

Usage

You can import dfl as a package into your own Go project or use the command line interface. A common architecture is to have a client application generate a DFL expression string and submit to a Go application using a rest interface.

import (
  "github.com/spatialcurrent/go-dfl/pkg/dfl"
)
root, err := dfl.Parse("<YOUR EXPRESSION>")
if err != nil {
  panic(err)
}
root = root.Compile()  // Flattens tree for performance if possible.
result := root.Evaluate(Context{<YOUR KEY:VALUE Context>})

DFL

DFL is another query or filter language like SQL, CQL, or ECQL. DFL aims to be easily understood by humans in a variety of contexts, such as in a url, in an editor, or in a python terminal. The principals are as follows:

1.  Easy to Read - The "@" in front of every attribute.
2.  Clean - Quotes are optional (unless required because of spaces in an element)
3.  Strict Execution Path - Use of parentheses is strongly encouraged o maximize performance over large datasets.
4.  Dynamically typed - Operators support multiple types and try to cast if possible.  Fails hard if not valid.
5.  Embeddable - Easily written in other languages, such as Python, Javascript, or Shell, without endless worry about escaping.

DFL aims to cover a wide variety of filters while keeping the language expressive and easy to read. DFL currently supports:

* Boolean: Not, And, Or
* Numeric: LessThan, LessThanOrEqual, Equal, NotEqual, GreaterThan, GreaterThanOrEqual, Add, Subtract
* String: Like, ILike, In
* Time: Before, After
* Array/Set: In
* Function: Function

Command Line Interface

See the github.com/go-dfl/cmd/dfl package for a command line tool for testing DFL expressions.

- https://godoc.org/github.com/spatialcurrent/go-dfl/dfl

Projects

go-dfl is used by the railgun and go-osm project.

- https://godoc.org/github.com/spatialcurrent/railgun/railgun
- https://godoc.org/github.com/spatialcurrent/go-osm/osm
- https://godoc.org/github.com/spatialcurrent/go-dfl/dfl

Examples

Below are some simple examples.

import (
  "github.com/spatialcurrent/go-dfl/pkg/dfl"
)
root, err := dfl.Parse("(@amenity in [restaurant, bar]) or (@craft in [brewery, distillery])")
if err != nil {
  panic(err)
}
root = root.Compile()
valid := root.Evaluate(Context{"amenity": "bar", "name": "John's Whiskey Bar"})

Index

Constants

This section is empty.

Variables

View Source
var DefaultFunctionMap = NewFuntionMapWithDefaults()
View Source
var DefaultQuotes = []string{"'", "\"", "`"}
View Source
var DefaultTab = "  "
View Source
var NoContext = map[string]interface{}{}
View Source
var NoVars = map[string]interface{}{}
View Source
var SqlArrayPrefix = "{"
View Source
var SqlArraySuffix = "}"
View Source
var SqlQuote = "'"
View Source
var Version = "0.0.11"

Functions

func AttachLeft

func AttachLeft(root Node, left Node) error

AttachLeft attaches the left Node as the left child node to the parent root Node.

func CompareNumbers

func CompareNumbers(a interface{}, b interface{}) (int, error)

CompareNumbers compares parameter a and parameter b. The parameters may be of type uint8, int, int64, or float64. If a > b, then returns 1. If a < b, then returns -1. If a == b, then return 0.

func CompareStrings

func CompareStrings(lvs string, rvs string) (bool, error)

CompareStrings compares parameter a and parameter b. The parameters must be of type string. Returns true if a like b.

func CompareTimes

func CompareTimes(a interface{}, b interface{}) (int, error)

CompareTimes compares parameter a and parameter b. The parameters may be of type string, time.Time, or *time.Time. If a is after b, then returns 1. If a is before b, then returns -1. If a is at the same time as b, then return 0.

func CreateGroups

func CreateGroups(depth int) interface{}

func DivideNumbers

func DivideNumbers(a interface{}, b interface{}) (interface{}, error)

DivideNumbers divides the first number by the second number and returns the results. The parameters can be an int, int64, or float64. The parameters will be cast as applicable. For example you can divide two integers with

total := DivideNumbers(1, 2)

or you could divide an int with a float64.

total := DivideNumbers(4, 3.2)

func EscapeString

func EscapeString(in string) string

func EvaluateArray

func EvaluateArray(n Node, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

EvaluateArray returns the array/slice value of a node given a context. If the result is not an array or slice, then returns an error.

func EvaluateBool

func EvaluateBool(n Node, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, bool, error)

EvaluateBool returns the boolean value of a node given a context. If the result is not a bool, then returns an error.

func EvaluateFloat64

func EvaluateFloat64(n Node, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, float64, error)

EvaluateFloat64 returns the float64 value of a node given a context. If the result is not a float64, then returns an error.

func EvaluateInt

func EvaluateInt(n Node, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, int, error)

EvaluateInt returns the int value of a node given a context. If the result is not an int, then returns an error.

func EvaluateMap

func EvaluateMap(n Node, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

EvaluateMap returns the map value of a node given a context. If the result is not a map, then returns an error.

func EvaluateString

func EvaluateString(n Node, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, string, error)

EvaluateString returns the string value of a node given a context. If the result is not a string, then returns an error.

func Extract

func Extract(path string, obj interface{}, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (interface{}, error)

Extract is a function to extract a value from an object. Extract supports a standard dot (.) and null-safe (?.) indexing. Extract also supports wildcard indexing using *. Extract also support array indexing, including [A], [A:B], [A:], and [:B].

func FormatList

func FormatList(values []string, delim string, pretty bool, tabs int) string

func FormatNodes

func FormatNodes(nodes []Node, quotes []string, pretty bool, tabs int) []string

FormatNodes formats an array of nodes to a string.

func FormatSql

func FormatSql(value interface{}, pretty bool, tabs int) string

FormatSQL formats an object as a SQL string

func MustParseCompileEvaluate

func MustParseCompileEvaluate(exp string, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{})

MustParseCompileEvaluate parses the expression, compiles the node, and evaluates on the given context. Panics if any error.

func ParseAndCompileExpressions

func ParseAndCompileExpressions(expressions map[string]string) (map[string]Node, error)

ParseAndCompileExpressions is a usability function to parse and compile multiple expressions.

func ParseCompileEvaluate

func ParseCompileEvaluate(exp string, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

ParseCompileEvaluate parses the expression, compiles the node, and evaluates on the given context.

func ParseCompileEvaluateInt

func ParseCompileEvaluateInt(exp string, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, int, error)

ParseCompileEvaluateInt parses the expression, compiles the node, evaluates on the given context, and returns a result of type int if valid, otherwise returns and error.

func ParseCompileEvaluateMap

func ParseCompileEvaluateMap(exp string, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

ParseCompileEvaluateMap parses the expression, compiles the node, evaluates on the given context, and returns a result of kind map if valid, otherwise returns and error.

func ParseCompileEvaluateString

func ParseCompileEvaluateString(exp string, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, string, error)

ParseCompileEvaluateString parses the expression, compiles the node, evaluates on the given context, and returns a result of kind string if valid, otherwise returns and error.

func ParseKeyValue

func ParseKeyValue(in string) (map[Node]Node, error)

ParseKeyValue parses a sequence of key value pairs

func RemoveComments

func RemoveComments(in string) string

RemoveComments removes comments from a multi-line dfl expression

func TryConvertArray

func TryConvertArray(a []interface{}) interface{}

TryConvertArray attempts to convert the []interface{} array into []int, []int64, []float64, or []string, if possible.

func TryConvertMap

func TryConvertMap(m interface{}) interface{}

TryConvertMap attempts to convert the interface{} map into a typed version

func TryConvertString

func TryConvertString(s string) interface{}

TryConvertString attempts to parse the string parameter s into an appropriate literal value of type string, bool, int, float64, or time.Time. The functions evaluates the following rules in order. It returns the first success. The rules are:

1. "null", "none", "" => ""
2. "true" => true (bool)
3. "false" => false (bool)
4. "0.234" => float64
5. 131238 => int
6. time.Parse(time.RFC3339Nano, s)
7. time.Parse(time.RFC3339, s)
8. time.Parse("2006-01-02", s)
9. If no rules pass without error, then just return the input value

For example:

TryConvertString("a") => "a" (string)
TryConvertString("true") => true (bool)
TryConvertString("123.31") => 123.31 (float64)
TryConvertString("4") => 4 (int)
TryConvertString("2018-05-01") => 2018-05-01T00:00:00Z (time.Time)

func TryFormatLiteral

func TryFormatLiteral(value interface{}, quotes []string, pretty bool, tabs int) string

func UnescapeString

func UnescapeString(in string) string

UnescapeString unescapes a string

- \\ => \
- \n => new line
- \r => carriage return
- \t => horizontal tab
- \s => space
- \u1234 => unicode value

func WithinRange

func WithinRange(value interface{}, start interface{}, end interface{}) (bool, error)

WithinRange returns true if value is in the range [start, end]

Types

type Add

type Add struct {
	*BinaryOperator
}

Add is a BinaryOperator that represents the addition of two nodes.

func (Add) Compile

func (a Add) Compile() Node

Compile returns a compiled version of this node. If the left and right values are both compiled as literals, then returns the compiled Literal with that value set. Otherwise returns a clone of this node.

func (Add) Dfl

func (a Add) Dfl(quotes []string, pretty bool, tabs int) string

Dfl returns the DFL representation of this node as a string

func (Add) Evaluate

func (a Add) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

Evaluate returns the value of this node given Context ctx, and an error if any.

func (Add) Map

func (a Add) Map() map[string]interface{}

Map returns a map representation of this node.

func (Add) Sql

func (a Add) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node.

type After

type After struct {
	*TemporalBinaryOperator // Extends the TemporalBinaryOperator struct
}

After is a TemporalBinaryOperator evaluating to true if the left value is after the right value. The left and right values must be string, time.Time, or *time.Time.

func (After) Compile

func (a After) Compile() Node

func (After) Dfl

func (a After) Dfl(quotes []string, pretty bool, tabs int) string

func (After) Evaluate

func (a After) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (After) Map

func (a After) Map() map[string]interface{}

func (After) Sql

func (a After) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type And

type And struct {
	*BinaryOperator
}

And is a BinaryOperator which represents the logical boolean AND operation of left and right values.

func (And) Compile

func (a And) Compile() Node

Compile returns a compiled version of this node. If the left value and right value are both compiled as Literals, then returns the logical boolean AND operation of the left and right value. Otherwise, returns a clone.

func (And) Dfl

func (a And) Dfl(quotes []string, pretty bool, tabs int) string

func (And) Evaluate

func (a And) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (And) Map

func (a And) Map() map[string]interface{}

func (And) Sql

func (a And) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Array

type Array struct {
	Nodes []Node
}

Array is a Node representing an array of values, which can be either a Literal or Attribute.

func (Array) Attributes

func (a Array) Attributes() []string

func (Array) Compile

func (a Array) Compile() Node

Compile returns a compiled version of this node. If all the values of an Set are literals, returns a single Literal with the corresponding array as its value. Otherwise returns the original node..

func (Array) Dfl

func (a Array) Dfl(quotes []string, pretty bool, tabs int) string

Dfl returns the DFL representation of this node as a string

func (Array) Evaluate

func (a Array) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Array) Len

func (a Array) Len() int

Len returns the length of the underlying array.

func (Array) Map

func (a Array) Map() map[string]interface{}

func (Array) Sql

func (a Array) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

func (Array) Variables

func (a Array) Variables() []string

type Assign

type Assign struct {
	*BinaryOperator
}

Assign is a BinaryOperator which sets the value of the right side to the attribute or variable defined by the left side.

func (Assign) Compile

func (a Assign) Compile() Node

Compile returns a compiled version of this node.

func (Assign) Dfl

func (a Assign) Dfl(quotes []string, pretty bool, tabs int) string

func (Assign) Evaluate

func (a Assign) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Assign) Map

func (a Assign) Map() map[string]interface{}

func (Assign) Sql

func (a Assign) Sql(pretty bool, tabs int) string

type AssignAdd

type AssignAdd struct {
	*BinaryOperator
}

AssignAdd is a BinaryOperator which sets the added value of the left side and right side to the attribute or variable defined by the left side.

func (AssignAdd) Compile

func (a AssignAdd) Compile() Node

Compile returns a compiled version of this node.

func (AssignAdd) Dfl

func (a AssignAdd) Dfl(quotes []string, pretty bool, tabs int) string

func (AssignAdd) Evaluate

func (a AssignAdd) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (AssignAdd) Map

func (a AssignAdd) Map() map[string]interface{}

func (AssignAdd) Sql

func (a AssignAdd) Sql(pretty bool, tabs int) string

type AssignMultiply

type AssignMultiply struct {
	*BinaryOperator
}

AssignMultiply is a BinaryOperator which sets the multiplied value of the left side and right side to the attribute or variable defined by the left side.

func (AssignMultiply) Compile

func (a AssignMultiply) Compile() Node

Compile returns a compiled version of this node.

func (AssignMultiply) Dfl

func (a AssignMultiply) Dfl(quotes []string, pretty bool, tabs int) string

func (AssignMultiply) Evaluate

func (a AssignMultiply) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (AssignMultiply) Map

func (a AssignMultiply) Map() map[string]interface{}

func (AssignMultiply) Sql

func (a AssignMultiply) Sql(pretty bool, tabs int) string

type AssignSubtract

type AssignSubtract struct {
	*BinaryOperator
}

AssignSubtract is a BinaryOperator which sets the value of the left side subtracted by the right side to the attribute or variable defined by the left side.

func (AssignSubtract) Compile

func (a AssignSubtract) Compile() Node

Compile returns a compiled version of this node.

func (AssignSubtract) Dfl

func (a AssignSubtract) Dfl(quotes []string, pretty bool, tabs int) string

func (AssignSubtract) Evaluate

func (a AssignSubtract) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (AssignSubtract) Map

func (a AssignSubtract) Map() map[string]interface{}

func (AssignSubtract) Sql

func (a AssignSubtract) Sql(pretty bool, tabs int) string

type Attribute

type Attribute struct {
	Name string
}

Attribute is a Node representing the value of an attribute in the context map. Attributes start with a "@" and follow with the name or full path into the object if multiple levels deep. For example, @a and @a.b.c.d. You can also use a null-safe operator, e.g., @a?.b?.c?.d

func (Attribute) Attributes

func (a Attribute) Attributes() []string

func (Attribute) Compile

func (a Attribute) Compile() Node

func (Attribute) Dfl

func (a Attribute) Dfl(quotes []string, pretty bool, tabs int) string

func (Attribute) Evaluate

func (a Attribute) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Attribute) Map

func (a Attribute) Map() map[string]interface{}

func (Attribute) Sql

func (a Attribute) Sql(pretty bool, tabs int) string

func (Attribute) Variables

func (a Attribute) Variables() []string

type Before

type Before struct {
	*TemporalBinaryOperator // Extends the TemporalBinaryOperator struct
}

Before is a TemporalBinaryOperator evaluating to true if the left value is before the right value. The left and right values must be string, time.Time, or *time.Time.

func (Before) Compile

func (b Before) Compile() Node

func (Before) Dfl

func (b Before) Dfl(quotes []string, pretty bool, tabs int) string

func (Before) Evaluate

func (b Before) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Before) Map

func (b Before) Map() map[string]interface{}

func (Before) Sql

func (b Before) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type BinaryOperator

type BinaryOperator struct {
	Left  Node
	Right Node
}

BinaryOperator is a DFL Node that represents the binary operator of a left value and right value. This struct functions as an embedded struct for many comparator operations.

func (BinaryOperator) Attributes

func (bo BinaryOperator) Attributes() []string

Attributes returns a slice of all attributes used in the evaluation of this node, including a children nodes. Attributes de-duplicates values from the left node and right node using a set.

func (BinaryOperator) Builder

func (bo BinaryOperator) Builder(operator string, quotes []string, tabs int) builder.Builder

func (BinaryOperator) Dfl

func (bo BinaryOperator) Dfl(operator string, quotes []string, pretty bool, tabs int) string

func (BinaryOperator) EvaluateLeftAndRight

func (bo BinaryOperator) EvaluateLeftAndRight(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, interface{}, error)

EvaluateLeftAndRight evaluates the value of the left node and right node given a context map (ctx) and function map (funcs). Returns a 3 value tuple of left value, right value, and error.

func (BinaryOperator) Map

func (bo BinaryOperator) Map(operator string, left Node, right Node) map[string]interface{}

func (BinaryOperator) Sql

func (bo BinaryOperator) Sql(operator string, pretty bool, tabs int) string

func (BinaryOperator) Variables

func (bo BinaryOperator) Variables() []string

Variables returns a slice of all variables used in the evaluation of this node, including a children nodes. Attributes de-duplicates values from the left node and right node using a set.

type Coalesce

type Coalesce struct {
	*BinaryOperator
}

Coalesce is a BinaryOperator which returns the left value if not null otherwise the right value.

func (Coalesce) Compile

func (c Coalesce) Compile() Node

Compile returns a compiled version of this node. If the left value is compiled as a Literal, then returns the left value. Otherwise, returns a clone.

func (Coalesce) Dfl

func (c Coalesce) Dfl(quotes []string, pretty bool, tabs int) string

func (Coalesce) Evaluate

func (c Coalesce) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Coalesce) Map

func (c Coalesce) Map() map[string]interface{}

func (Coalesce) Sql

func (c Coalesce) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Concat

type Concat struct {
	*MultiOperator
}

Concat concatenates the string representation of all the arguments

func (Concat) Compile

func (c Concat) Compile() Node

func (Concat) Dfl

func (c Concat) Dfl(quotes []string, pretty bool, tabs int) string

Dfl returns the DFL expression representation of this node.

func (Concat) Evaluate

func (c Concat) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Concat) Map

func (c Concat) Map() map[string]interface{}

func (Concat) Sql

func (c Concat) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

func (Concat) Suffix

func (c Concat) Suffix() string

Suffix returns the suffix of the result of evaluation, if the last argument is a Literal. If the last argument is not a literal, then returns an empty string.

type Context

type Context struct {
	Data map[string]interface{}
}

Context is a simple alias for a map[string]interface{} that is used for containing the context for evaluating a DFL Node. The values in a context are essentially the input parameters for a DFL expression and match up with the Attribute. The Context is built from the trailing command line arguments. For example the arguments from the following command line

./dfl -filter "(@amenity like bar) and (open > 0)" amenity=bar popularity=10 open=1

Would be interpreted as the following Context

ctx := Context{"amenity": "bar", "popularity": 10, "open": 1}

func (*Context) Get

func (c *Context) Get(key string) interface{}

func (*Context) Has

func (c *Context) Has(key string) bool

func (*Context) Len

func (c *Context) Len() int

func (*Context) Set

func (c *Context) Set(key string, value interface{})

func (*Context) Values

func (c *Context) Values() []interface{}

type Dictionary

type Dictionary struct {
	Nodes map[Node]Node
}

Dictionary is a Node representing a dictionary of key value pairs.

func NewDictionary

func NewDictionary(m map[string]interface{}) *Dictionary

func (Dictionary) Attributes

func (d Dictionary) Attributes() []string

func (Dictionary) Compile

func (d Dictionary) Compile() Node

Compile returns a compiled version of this node.

func (Dictionary) Dfl

func (d Dictionary) Dfl(quotes []string, pretty bool, tabs int) string

func (Dictionary) Evaluate

func (d Dictionary) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Dictionary) Len

func (d Dictionary) Len() int

Len returns the length of the underlying array.

func (Dictionary) Map

func (d Dictionary) Map() map[string]interface{}

func (Dictionary) Sql

func (d Dictionary) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

func (Dictionary) Variables

func (d Dictionary) Variables() []string

type Divide

type Divide struct {
	*NumericBinaryOperator
}

Divide is a NumericBinaryOperator that represents the mathematical division of two nodes.

func (Divide) Compile

func (d Divide) Compile() Node

Compile returns a compiled version of this node.

func (Divide) Dfl

func (d Divide) Dfl(quotes []string, pretty bool, tabs int) string

Dfl returns the DFL representation of this node as a string

func (Divide) Evaluate

func (d Divide) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

Evaluate evaluates this node given the variables "vars" and context "ctx" and returns the output, and an error if any.

func (Divide) Map

func (d Divide) Map() map[string]interface{}

Map returns a map representation of this node.

func (Divide) Sql

func (d Divide) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Equal

type Equal struct {
	*BinaryOperator
}

Equal is a BinaryOperator that evaluating to true if parameter a is equal to parameter b. The parameters may be of type int, int64, or float64.

func (Equal) Compile

func (e Equal) Compile() Node

func (Equal) Dfl

func (e Equal) Dfl(quotes []string, pretty bool, tabs int) string

func (Equal) Evaluate

func (e Equal) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Equal) Map

func (e Equal) Map() map[string]interface{}

func (Equal) Sql

func (e Equal) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type ErrParseTernary

type ErrParseTernary struct {
	Original  string // original
	Condition string // the condition
	True      string // the true value
	False     string // the false value
}

ErrParseTernary is an error when parsing ternary expressions.

func (ErrParseTernary) Error

func (e ErrParseTernary) Error() string

Error returns the error as a string.

type ErrorEvaluate

type ErrorEvaluate struct {
	Node   Node     // the name of the Function
	Quotes []string // the quotes to use
}

ErrorEvaluate is an error returned when an error occurs during evaluation of a Node.

func (ErrorEvaluate) Error

func (e ErrorEvaluate) Error() string

Error returns the error as a string.

type Function

type Function struct {
	*MultiOperator
	Name string `json:"name" bson:"name" yaml:"name" hcl:"name"` // name of the function
}

Function is a refrenced function in a DFL filter. The actual function in a given FunctionMap is derefernced by name.

func (Function) Compile

func (f Function) Compile() Node

func (Function) Dfl

func (f Function) Dfl(quotes []string, pretty bool, tabs int) string

func (Function) Evaluate

func (f Function) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Function) Map

func (f Function) Map() map[string]interface{}

func (Function) Sql

func (f Function) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type FunctionMap

type FunctionMap map[string]func(FunctionMap, map[string]interface{}, interface{}, []interface{}, []string) (interface{}, error)

FunctionMap is a map of functions by string that are reference by name in the Function Node.

func NewFuntionMapWithDefaults

func NewFuntionMapWithDefaults() FunctionMap

type GreaterThan

type GreaterThan struct {
	*NumericBinaryOperator
}

GreaterThan is a NumericBinaryOperator that evaluating to true if parameter a is greater than parameter b. The parameters may be of type int, int64, or float64.

func (GreaterThan) Compile

func (gt GreaterThan) Compile() Node

func (GreaterThan) Dfl

func (gt GreaterThan) Dfl(quotes []string, pretty bool, tabs int) string

func (GreaterThan) Evaluate

func (gt GreaterThan) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (GreaterThan) Map

func (gt GreaterThan) Map() map[string]interface{}

func (GreaterThan) Sql

func (gt GreaterThan) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type GreaterThanOrEqual

type GreaterThanOrEqual struct {
	*NumericBinaryOperator
}

GreaterThanOrEqual is a NumericBinaryOperator that evaluating to true if parameter a is greater than or equal to parameter b. The parameters may be of type int, int64, or float64.

func (GreaterThanOrEqual) Compile

func (gte GreaterThanOrEqual) Compile() Node

func (GreaterThanOrEqual) Dfl

func (gte GreaterThanOrEqual) Dfl(quotes []string, pretty bool, tabs int) string

func (GreaterThanOrEqual) Evaluate

func (gte GreaterThanOrEqual) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (GreaterThanOrEqual) Map

func (gte GreaterThanOrEqual) Map() map[string]interface{}

func (GreaterThanOrEqual) Sql

func (gte GreaterThanOrEqual) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type IIn

type IIn struct {
	*BinaryOperator
}

In is a BinaryOperator that evaluates to true if the left value is in the right value. Unlike "in", it is case insensitive. If the right value is an array/slice, then evaluated to true if the left value is in the array/slice. Otherwise, evaluates to true if the right string is contained by the left string.

func (IIn) Compile

func (i IIn) Compile() Node

func (IIn) Dfl

func (i IIn) Dfl(quotes []string, pretty bool, tabs int) string

func (IIn) Evaluate

func (i IIn) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (IIn) Map

func (i IIn) Map() map[string]interface{}

func (IIn) Sql

func (i IIn) Sql(pretty bool, tabs int) string

type ILike

type ILike struct {
	*BinaryOperator
}

func (ILike) Compile

func (i ILike) Compile() Node

func (ILike) Dfl

func (i ILike) Dfl(quotes []string, pretty bool, tabs int) string

func (ILike) Evaluate

func (i ILike) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (ILike) Map

func (i ILike) Map() map[string]interface{}

func (ILike) Sql

func (i ILike) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type In

type In struct {
	*BinaryOperator
}

In is a BinaryOperator that evaluates to true if the left value is in the right value. The left value is cast as a string using "fmt.Sprint(lv)". If the right value is an array/slice, then evaluated to true if the left value is in the array/slice. Otherwise, evaluates to true if the right string is contained by the left string.

func (In) Compile

func (i In) Compile() Node

func (In) Dfl

func (i In) Dfl(quotes []string, pretty bool, tabs int) string

func (In) Evaluate

func (i In) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (In) Map

func (i In) Map() map[string]interface{}

func (In) Sql

func (i In) Sql(pretty bool, tabs int) string

type Lengther

type Lengther interface {
	Len() int
}

type LessThan

type LessThan struct {
	*NumericBinaryOperator
}

LessThan is a NumericBinaryOperator that evaluating to true if parameter a is less than parameter b. The parameters may be of type int, int64, or float64.

func (LessThan) Compile

func (lt LessThan) Compile() Node

func (LessThan) Dfl

func (lt LessThan) Dfl(quotes []string, pretty bool, tabs int) string

Sql returns the DFL representation of this node as a string

func (LessThan) Evaluate

func (lt LessThan) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (LessThan) Map

func (lt LessThan) Map() map[string]interface{}

func (LessThan) Sql

func (lt LessThan) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type LessThanOrEqual

type LessThanOrEqual struct {
	*NumericBinaryOperator
}

LessThanOrEqual is a NumericBinaryOperator that evaluating to true if parameter a is less than or equal to parameter b. The parameters may be of type int, int64, or float64.

func (LessThanOrEqual) Compile

func (lte LessThanOrEqual) Compile() Node

func (LessThanOrEqual) Dfl

func (lte LessThanOrEqual) Dfl(quotes []string, pretty bool, tabs int) string

Sql returns the DFL representation of this node as a string

func (LessThanOrEqual) Evaluate

func (lte LessThanOrEqual) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (LessThanOrEqual) Map

func (lte LessThanOrEqual) Map() map[string]interface{}

func (LessThanOrEqual) Sql

func (lte LessThanOrEqual) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Like

type Like struct {
	*BinaryOperator
}

Like is a BinaryOperator that evaluates the SQL standard like expression. It is similar to the ILike BinaryOperator but is case sensitive. The parameters must be of type string. The right parameter may have "%" characters that are interpreted as (.*) in a regular expression test.

func (Like) Compile

func (l Like) Compile() Node

func (Like) Dfl

func (l Like) Dfl(quotes []string, pretty bool, tabs int) string

func (Like) Evaluate

func (l Like) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Like) Map

func (l Like) Map() map[string]interface{}

func (Like) Sql

func (l Like) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Literal

type Literal struct {
	Value interface{} // the field containing the actual value
}

Literal is a Node representing a literal/static value regardless of the context. The value may be of any type; however, it will likely a bool, int, or string. For example

Literal{Value: ""}
Literal{Value: 0.0}

func (Literal) Attributes

func (l Literal) Attributes() []string

func (Literal) Compile

func (l Literal) Compile() Node

func (Literal) Dfl

func (l Literal) Dfl(quotes []string, pretty bool, tabs int) string

func (Literal) Evaluate

func (l Literal) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Literal) Map

func (l Literal) Map() map[string]interface{}

func (Literal) Sql

func (l Literal) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

func (Literal) Variables

func (l Literal) Variables() []string

type MultiOperator

type MultiOperator struct {
	Arguments []Node `json:"arguments" bson:"arguments" yaml:"arguments" hcl:"arguments"` // list of function arguments
}

MultiOperator represents an operator with a dynamic list of arguments.

func (MultiOperator) Attributes

func (mo MultiOperator) Attributes() []string

func (MultiOperator) First

func (mo MultiOperator) First() Node

First returns the last argument for this operator, if exists.

func (MultiOperator) Last

func (mo MultiOperator) Last() Node

Last returns the last argument for this operator, if exists.

func (MultiOperator) Map

func (mo MultiOperator) Map(operator string) map[string]interface{}

func (MultiOperator) Variables

func (mo MultiOperator) Variables() []string

type Multiply

type Multiply struct {
	*NumericBinaryOperator
}

Multiply is a NumericBinaryOperator that represents the mathematical multiplication of two nodes.

func (Multiply) Compile

func (m Multiply) Compile() Node

Compile returns a compiled version of this node.

func (Multiply) Dfl

func (m Multiply) Dfl(quotes []string, pretty bool, tabs int) string

Dfl returns the DFL representation of this node as a string

func (Multiply) Evaluate

func (m Multiply) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

Evaluate evaluates this node given the variables "vars" and context "ctx" and returns the output, and an error if any.

func (Multiply) Map

func (m Multiply) Map() map[string]interface{}

Map returns a map representation of this node.

func (Multiply) Sql

func (m Multiply) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Node

type Node interface {
	Dfl(quotes []string, pretty bool, tabs int) string                                                                                      // returns the DFL expression representation of this node
	Sql(pretty bool, tabs int) string                                                                                                       // returns the SQL representation of this node
	Map() map[string]interface{}                                                                                                            // returns a map representing this node
	Compile() Node                                                                                                                          // compiles this node (and all children).
	Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) // evaluates the value of a node given a context
	Attributes() []string                                                                                                                   // returns a slice of all attributes used by this node (and all children nodes)
	Variables() []string                                                                                                                    // returns a slice of all variables used by this node (and all children nodes)
}

Node is the interface for representing the constructs of the Dyanmic Filter Language in an Abstract Syntax Tree. This interface is inherited by most structs in the dfl package.

func MustParseCompile

func MustParseCompile(expression string) Node

MustParseCompile parses the input expression and compiles the DFL node. Panics if any error.

func Parse

func Parse(in string) (Node, string, error)

Parse parses a DFL expression into an an Abstract Synatax Tree (AST). Parse returns the AST, remainder, and error if any.

func ParseArray

func ParseArray(in string, remainder string) (Node, string, error)

ParseArray parses an array of nodes. If parameter "in" is gramatically a child node, then return the parent node. DFL arrays can include Attribute or Literal Nodes. As all attribute references must start with an "@" character, parantheses are optional for literals except if a comma exists. Below are some example inputs

[bank, bureau_de_change, atm]
[1, 2, @target]
[Taco, Tacos, Burrito, Burritos, "Mexican Food", @example]

func ParseAttribute

func ParseAttribute(in string, remainder string) (Node, string, error)

ParseAttribute parses an Attribute Node from an input string If parameter "in" is gramatically a child node, then return the parent node. All attribute references must start with an "@" character and have no spaces. For example, @amenity, @shop, @population, etc. Given those rules the remainder, if any, if simply parsed from the input strings Examples:

node, err := ParseAttribute("@amenity in [bar, restaurant]")

func ParseCompile

func ParseCompile(expression string) (Node, error)

ParseCompile parses the input expression and compiles the DFL node.

func ParseFunction

func ParseFunction(in string, remainder string) (Node, string, error)

ParseFunction parses a function from in and attaches the remainder.

func ParseList

func ParseList(in string) ([]Node, error)

ParseList parses a list of values.

func ParseListOrKeyValue

func ParseListOrKeyValue(in string) (bool, []Node, map[Node]Node, error)

ParseList parses a list of values.

func ParseLiteral

func ParseLiteral(v interface{}, remainder string) (Node, string, error)

ParseLiteral wraps parameter v in a Literal Node and parses a remainder, if any. ParseLiteral does not additional parsing of parameter v. TryConvertString is used to parse an int, float64, or time from a string representation. If parameter "in" is gramatically a child node, then return the parent node. For example, @amenity, @shop, @population, etc. Given those rules the remainder, if any, if simply parsed from the input strings Examples:

node, err := ParseLiteral("brewery")

func ParseSetOrDictionary

func ParseSetOrDictionary(in string, remainder string) (Node, string, error)

ParseSetOrDictionary parses a Set or Dictionary Node and recursively any remainder. If parameter "in" is gramatically a child node, then return the parent node. DFL sets/dictionaries can include Attribute or Literal Nodes. As all attribute references must start with an "@" character, parentheses are optional for literals except if a comma exists. Below are some example inputs

{bank, bureau_de_change, atm}
{1, 2, @target}
{Taco, Tacos, Burrito, Burritos, "Mexican Food", @example}
{amenity: bank}

func ParseSub

func ParseSub(s string, remainder string) (Node, string, error)

ParseSub is used to parse a sub-expression and the remainder, if any. A sub-expression is usually enclosed by parantheses. The parantheses are removed before being passed to ParseSub. If parameter "in" is gramatically a child node, then return the parent node. For Example with an input string "(@cuisine like mexican) or (@name ilike %burrito%)",

node, err : ParseSub("@cuisine like mexican", "or (@name ilike %burrito%)")

func ParseVariable

func ParseVariable(in string, remainder string) (Node, string, error)

ParseVariable parses a Variable Node from an input string If parameter "in" is gramatically a child node, then return the parent node. All variable references must start with an "$" character and have no spaces. For example, $amenity, $shop, $population, etc. Given those rules the remainder, if any, if simply parsed from the input strings Examples:

node, err := ParseAttribute("$amenities := [bar, restaurant]")

type Not

type Not struct {
	*UnaryOperator
}

Not is a UnaryOperator that inverts the boolean value of the children Node.

func (Not) Compile

func (n Not) Compile() Node

Compile returns a compiled version of this node. If the the child Node is compiled as a boolean Literal, then returns an inverse Literal Node. Otherwise returns a clone of this node.

func (Not) Dfl

func (n Not) Dfl(quotes []string, pretty bool, tabs int) string

Dfl returns the DFL representation of this node (and its children nodes)

func (Not) Evaluate

func (n Not) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

Evaluate evaluates this node within a context and returns the bool result, and error if any.

func (Not) Map

func (n Not) Map() map[string]interface{}

Map returns a map representation of this node (and its children nodes)

func (Not) Sql

func (n Not) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type NotEqual

type NotEqual struct {
	*BinaryOperator
}

NotEqual is a BinaryOperator that evaluates to true if the left value is not equal to the right value. The values may be of type int, int64, or float64.

func (NotEqual) Compile

func (ne NotEqual) Compile() Node

func (NotEqual) Dfl

func (ne NotEqual) Dfl(quotes []string, pretty bool, tabs int) string

Dfl returns the DFL expression representation of the node as a string value. For example

"( @amenity  !=  shop )"

func (NotEqual) Evaluate

func (ne NotEqual) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (NotEqual) Map

func (ne NotEqual) Map() map[string]interface{}

func (NotEqual) Sql

func (ne NotEqual) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Null

type Null struct{}

Null is used as return value for Extract and DFL functions instead of returning nil pointers.

func (Null) Dfl

func (n Null) Dfl() string

func (Null) Sql

func (n Null) Sql() string

type NumericBinaryOperator

type NumericBinaryOperator struct {
	*BinaryOperator // Extends the BinaryOperator struct
}

NumericBinaryOperator is an abstract struct extending the BinaryOperator struct. NumericBinaryOperator is a convience struct that attaches to EvaluateAndCompare function that is used by structs implementing the Node interface.

func (NumericBinaryOperator) EvaluateAndCompare

func (nbo NumericBinaryOperator) EvaluateAndCompare(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, int, error)

EvaluateAndCompare returns the value of the node given the Context ctx, and error if any. If the left value and right value are mathematically equal, returns 0. If the left value is less than the right value, returns -1. if the left value is greater than the right value, returns 1.

type Or

type Or struct {
	*BinaryOperator
}

Or is a BinaryOperator which represents the logical boolean OR operation of left and right values.

func (Or) Compile

func (o Or) Compile() Node

Compile returns a compiled version of this node. If the left value and right value are both compiled as Literals, then returns the logical boolean AND operation of the left and right value. Otherwise, returns a clone.

func (Or) Dfl

func (o Or) Dfl(quotes []string, pretty bool, tabs int) string

func (Or) Evaluate

func (o Or) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Or) Map

func (o Or) Map() map[string]interface{}

func (Or) Sql

func (o Or) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Pipe

type Pipe struct {
	*BinaryOperator
}

Pipe is a BinaryOperator which represents the "|" pipe operation of left and right values.

func (Pipe) Compile

func (p Pipe) Compile() Node

Compile returns a compiled version of this node. If the left value and right value are both compiled as Literals, then returns the logical boolean AND operation of the left and right value. Otherwise, returns a clone.

func (Pipe) Dfl

func (p Pipe) Dfl(quotes []string, pretty bool, tabs int) string

func (Pipe) Evaluate

func (p Pipe) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Pipe) Last

func (p Pipe) Last() Node

func (Pipe) Map

func (p Pipe) Map() map[string]interface{}

func (Pipe) Sql

func (p Pipe) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Pipeline

type Pipeline struct {
	Nodes []Node
}

Pipeline is a Node representing a pipeline of nodes where the output of each node is used as the input for the next.

func (Pipeline) Attributes

func (p Pipeline) Attributes() []string

func (Pipeline) Compile

func (p Pipeline) Compile() Node

Compile returns a compiled version of this node. If all the values of an Set are literals, returns a single Literal with the corresponding array as its value. Otherwise returns the original node..

func (Pipeline) Dfl

func (p Pipeline) Dfl(quotes []string, pretty bool, tabs int) string

Dfl returns the DFL representation of this node as a string

func (Pipeline) Evaluate

func (p Pipeline) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Pipeline) Len

func (p Pipeline) Len() int

Len returns the length of the underlying array.

func (Pipeline) Map

func (p Pipeline) Map() map[string]interface{}

func (Pipeline) Sql

func (p Pipeline) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

func (Pipeline) Variables

func (p Pipeline) Variables() []string

type Print

type Print struct {
	*UnaryOperator
}

Print is a UnaryOperator that prints a value to stdout

func (Print) Compile

func (p Print) Compile() Node

Compile returns a compiled version of this node. If the the child Node is compiled as a boolean Literal, then returns an inverse Literal Node. Otherwise returns a clone of this node.

func (Print) Dfl

func (p Print) Dfl(quotes []string, pretty bool, tabs int) string

Dfl returns the DFL representation of this node (and its children nodes)

func (Print) Evaluate

func (p Print) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

Evaluate evaluates this node within a context and returns the bool result, and error if any.

func (Print) Map

func (p Print) Map() map[string]interface{}

Map returns a map representation of this node (and its children nodes)

func (Print) Sql

func (p Print) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Reader

type Reader interface {
	ReadAll() ([]byte, error)
	ReadRange(start int, end int) ([]byte, error)
}

type Set

type Set struct {
	Nodes []Node
}

Set is a Node representing a set of values, which can be either a Literal or Attribute.

func (Set) Attributes

func (a Set) Attributes() []string

func (Set) Compile

func (a Set) Compile() Node

Compile returns a compiled version of this node. If all the values of an Set are literals, returns a single Literal with the corresponding Set/slice as its value. Otherwise returns the original node..

func (Set) Dfl

func (s Set) Dfl(quotes []string, pretty bool, tabs int) string

func (Set) Evaluate

func (a Set) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Set) Len

func (s Set) Len() int

Len returns the length of the underlying array.

func (Set) Map

func (a Set) Map() map[string]interface{}

func (Set) Sql

func (s Set) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

func (Set) Variables

func (a Set) Variables() []string

type StringSet

type StringSet map[string]struct{}

StringSet is a logical set of string values using a map[string]struct{} backend. The use of a string -> empty struct backend provides a higher write performance versus a slice backend.

func NewStringSet

func NewStringSet() StringSet

NewStringSet returns a new StringSet.

func (StringSet) Add

func (set StringSet) Add(values ...string)

Add is a variadic function to add values to a set

- https://gobyexample.com/variadic-functions

func (StringSet) Contains

func (set StringSet) Contains(x string) bool

func (StringSet) Dfl

func (set StringSet) Dfl(quotes []string, pretty bool, tabs int) string

func (StringSet) Intersection

func (set StringSet) Intersection(values interface{}) StringSet

func (StringSet) Intersects

func (set StringSet) Intersects(values interface{}) bool

func (StringSet) Len

func (set StringSet) Len() int

func (StringSet) Slice

func (set StringSet) Slice(sorted bool) sort.StringSlice

Slice returns a slice representation of this set. If parameter sorted is true, then sorts the values using natural sort order.

func (StringSet) Union

func (set StringSet) Union(values interface{}) StringSet

type Subtract

type Subtract struct {
	*NumericBinaryOperator
}

func (Subtract) Compile

func (s Subtract) Compile() Node

func (Subtract) Dfl

func (s Subtract) Dfl(quotes []string, pretty bool, tabs int) string

func (Subtract) Evaluate

func (s Subtract) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Subtract) Map

func (s Subtract) Map() map[string]interface{}

func (Subtract) Sql

func (s Subtract) Sql(pretty bool, tabs int) string

type TemporalBinaryOperator

type TemporalBinaryOperator struct {
	*BinaryOperator // Extends the BinaryOperator struct
}

TemporalBinaryOperator is an abstract struct NumericBinaryOperator is a convience struct that attaches to EvaluateAndCompare function that is used by structs implementing the Node interface.

func (TemporalBinaryOperator) EvaluateAndCompare

func (tbo TemporalBinaryOperator) EvaluateAndCompare(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, int, error)

EvaluateAndCompare returns the value of the node given the Context ctx, and error if any. If the left value and right value are at the same time, returns 0. If the left value is before the right value, returns -1. if the left value is after the right value, returns 1.

type TernaryOperator

type TernaryOperator struct {
	Left  Node
	True  Node
	False Node
}

TernaryOperator is a DFL Node that represents the ternary operator of a condition, true value, and false value.

func (TernaryOperator) Attributes

func (to TernaryOperator) Attributes() []string

Attributes returns a slice of all attributes used in the evaluation of this node, including a children nodes. Attributes de-duplicates values from the condition, true, and false nodes using a set.

func (TernaryOperator) Compile

func (to TernaryOperator) Compile() Node

func (TernaryOperator) Dfl

func (to TernaryOperator) Dfl(quotes []string, pretty bool, tabs int) string

func (TernaryOperator) Evaluate

func (to TernaryOperator) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (TernaryOperator) Map

func (to TernaryOperator) Map() map[string]interface{}

func (TernaryOperator) Sql

func (to TernaryOperator) Sql(pretty bool, tabs int) string

func (TernaryOperator) Variables

func (to TernaryOperator) Variables() []string

Variables returns a slice of all variables used in the evaluation of this node, including a children nodes. Variables de-duplicates values from the condition, true, and false nodes using a set.

type TestCase

type TestCase struct {
	Expression string      // the DFL expression
	Context    interface{} // the Context to use for evaluation
	Result     interface{} // The result of the evaluation
}

TestCase is a struct containing the variables for a unit test of expression evaluation.

func NewTestCase

func NewTestCase(exp string, ctx interface{}, result interface{}) TestCase

NewTestCase returns a new TestCase

type UnaryOperator

type UnaryOperator struct {
	Node Node
}

UnaryOperator is an abstract Node the represents an operator with only 1 operand. THe only implementing struct is the "Not" struct.

func (UnaryOperator) Attributes

func (uo UnaryOperator) Attributes() []string

Attributes returns the context attributes used by the child node, if any.

func (UnaryOperator) Variables

func (uo UnaryOperator) Variables() []string

Variables returns the variables used by the child node, if any.

type Variable

type Variable struct {
	Name string
}

Variable is a Node representing the value of a temporary variable. Variables start with a "#" and follow with the name or full path into the object if multiple levels deep. For example, #a and #a.b.c.d. You can also use a null-safe operator, e.g., #a?.b?.c?.d

func (Variable) Attributes

func (v Variable) Attributes() []string

func (Variable) Compile

func (v Variable) Compile() Node

func (Variable) Dfl

func (v Variable) Dfl(quotes []string, pretty bool, tabs int) string

func (Variable) Evaluate

func (v Variable) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Variable) Map

func (v Variable) Map() map[string]interface{}

func (Variable) Sql

func (v Variable) Sql(pretty bool, tabs int) string

func (Variable) Variables

func (v Variable) Variables() []string

type Within

type Within struct {
	*BinaryOperator
}

Within is a BinaryOperator that represents that the left value is between

func (Within) Compile

func (w Within) Compile() Node

Compile returns a compiled version of this node. If the left and right values are both compiled as literals, then returns the compiled Literal with that value set. Otherwise returns a clone of this node.

func (Within) Dfl

func (w Within) Dfl(quotes []string, pretty bool, tabs int) string

Dfl returns the DFL representation of this node as a string

func (Within) Evaluate

func (w Within) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

Evaluate returns the value of this node given Context ctx, and an error if any.

func (Within) Map

func (w Within) Map() map[string]interface{}

Map returns a map representation of this node

func (Within) Sql

func (w Within) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string

type Xor

type Xor struct {
	*BinaryOperator // Extends the BinaryOperator struct
}

Xor is a BinaryOperator which represents the logical boolean XOR operation of left and right values.

- https://en.wikipedia.org/wiki/Exclusive_or

func (Xor) Compile

func (x Xor) Compile() Node

Compile returns a compiled version of this node. If the left value and right value are both compiled as Literals, then returns the logical boolean XOR operation of the left and right value. Otherwise, returns a clone.

func (Xor) Dfl

func (x Xor) Dfl(quotes []string, pretty bool, tabs int) string

func (Xor) Evaluate

func (x Xor) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error)

func (Xor) Map

func (x Xor) Map() map[string]interface{}

func (Xor) Sql

func (x Xor) Sql(pretty bool, tabs int) string

Sql returns the SQL representation of this node as a string Equivalent to (A and not B) or (not A and B)

Directories

Path Synopsis
Package builder is for building DFL expressions.
Package builder is for building DFL expressions.
Package syntax includes functions for parsing syntax.
Package syntax includes functions for parsing syntax.

Jump to

Keyboard shortcuts

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