expression

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2021 License: Apache-2.0 Imports: 0 Imported by: 0

README

Expression Builder

Every database has its own query language, so this library provides in intermediate format that should be easy to convert into whatever specific language you need to use.

The expression library only represents the structure of the logical expression, and does not include implementations for any data sources. Those should be implemented in each individual data source adapter library.


// build single predicate expressions
criteria := expression.New("_id", "=", 42)

// use chaining for logical constructs
criteria := expression.New("_id", "=", 42).And("deleteDate", "=", 0)
criteria := expression.New("name", "=", "John").Or("name", "=", "Sarah")

// Also supports complex and/or logic

criteria := expression.Or(
    expression.New("_id", "=", 42).And("deleteDate", "=", 0),
    expression.New("_id", "=", 42).And("name", "=", "Sarah"),
)

// Constants define standard expected operators
data.OperatorEqual          = "="
data.OperatorNotEqual       = "!="
data.OperatorLessThan       = "<"
data.OperatorLessOrEqual    = "<="
data.OperatorGreaterThan    = ">"
data.OperatorGreaterOrEqual = ">="

Interfaces

This is accomplished with three very similar data types that all implement the same Expression interface.

Predicate represents a single predicate/comparison. Using .And() and .Or() will return the corresponding AndExpression or OrExpression object

AndExpression represents multiple predicates, all chained together with AND logic. Only supports the .And() method for additional predicates

OrExpression represents multiple predicates, all chained together with OR logic. Only supports the .Or() method for additional predicates.

Manually Walking the Logic Tree

Each of the three interfaces above implements a .Match() function that can be used by external programs to see if a dataset matches this expression. You must pass in a MatcherFunc that accepts a predicate and returns TRUE if that predicate matches the dataaset. AndExpression and OrExpression objects will call this function repeatedly for each element in their logic tree, and return a final boolean value for the entire logic structure.

Pull Requests Welcome

This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! 📚

Documentation

Index

Constants

View Source
const OperatorBeginsWith = "BEGINS"

OperatorBeginsWith represents a "begins with" comparison , when used in Predicates and Criteria. It is only valid for string values.

View Source
const OperatorContains = "CONTAINS"

OperatorContains represents a "contains" comparison , when used in Predicates and Criteria. It is only valid for string values.

View Source
const OperatorEndsWith = "ENDS"

OperatorEndsWith represents a "ends with" comparison , when used in Predicates and Criteria. It is only valid for string values.

View Source
const OperatorEqual = "="

OperatorEqual represents an "equals" comparison, when used in Predicates and Criteria

View Source
const OperatorGreaterOrEqual = ">="

OperatorGreaterOrEqual represents an "greater or equal" comparison, when used in Predicates and Criteria

View Source
const OperatorGreaterThan = ">"

OperatorGreaterThan represents an "greater than" comparison, when used in Predicates and Criteria

View Source
const OperatorLessOrEqual = "<="

OperatorLessOrEqual represents an "less or equal" comparison, when used in Predicates and Criteria

View Source
const OperatorLessThan = "<"

OperatorLessThan represents a "less than" comparison, when used in Predicates and Criteria

View Source
const OperatorNotEqual = "!="

OperatorNotEqual represents a "not equals" comparison, when used in Predicates and Criteria

Variables

This section is empty.

Functions

This section is empty.

Types

type AndExpression

type AndExpression []Expression

AndExpression combines a series of sub-expressions using AND logic

func All added in v0.3.5

func All() AndExpression

All a syntactic sugar alias for And(), so that expressions that query all values in a dataset read nicely.

func And

func And(expressions ...Expression) AndExpression

And combines one or more expression parameters into an AndExpression

func (AndExpression) Add added in v0.8.0

func (andExpression AndExpression) Add(exp Expression) AndExpression

Add appends a new expression into this compound expression

func (AndExpression) And

func (andExpression AndExpression) And(name string, operator string, value interface{}) AndExpression

And allows an additional predicate into this AndExpression

func (AndExpression) AndEqual added in v0.9.5

func (andExpression AndExpression) AndEqual(name string, value interface{}) AndExpression

func (AndExpression) AndGreaterOrEqual added in v0.9.5

func (andExpression AndExpression) AndGreaterOrEqual(name string, value interface{}) AndExpression

func (AndExpression) AndGreaterThan added in v0.9.5

func (andExpression AndExpression) AndGreaterThan(name string, value interface{}) AndExpression

func (AndExpression) AndLessOrEqual added in v0.9.5

func (andExpression AndExpression) AndLessOrEqual(name string, value interface{}) AndExpression

func (AndExpression) AndLessThan added in v0.9.5

func (andExpression AndExpression) AndLessThan(name string, value interface{}) AndExpression

func (AndExpression) AndNotEqual added in v0.9.5

func (andExpression AndExpression) AndNotEqual(name string, value interface{}) AndExpression

func (AndExpression) Match

func (andExpression AndExpression) Match(fn MatcherFunc) bool

Match implements the Expression interface. It loops through all sub-expressions and returns TRUE if all of them match

type Expression

type Expression interface {
	Match(MatcherFunc) bool
}

Expression is an interface that is implemented by Predicates, AndExpressions, and OrExpressions. It enables any of these items to be embedded into the criteria of a data.Query

type MatcherFunc

type MatcherFunc func(Predicate) bool

MatcherFunc is a function signature that is passed in to the .Match() functions of every Expression. It allows the caller to handle the actual matching independently of their underlying data, while the Expression objects handle the program flow.

type OrExpression

type OrExpression []Expression

OrExpression compares a series of sub-expressions, using the OR logic

func Or

func Or(expressions ...Expression) OrExpression

Or combines one or more expression parameters into an OrExpression

func (OrExpression) Add added in v0.8.0

func (orExpression OrExpression) Add(exp Expression) OrExpression

Add appends a new expression into this compound expression

func (OrExpression) Match

func (orExpression OrExpression) Match(fn MatcherFunc) bool

Match implements the Expression interface. It loops through all sub-expressions and returns TRUE if any of them match

func (OrExpression) Or

func (orExpression OrExpression) Or(name string, operator string, value interface{}) OrExpression

Or appends an additional predicate into the OrExpression

type Predicate

type Predicate struct {
	Field    string
	Operator string
	Value    interface{}
}

Predicate represents a single true/false comparison

func BeginsWith added in v0.9.5

func BeginsWith(field string, value interface{}) Predicate

BeginsWith creates a new Predicate using an "Greater Or Equal" comparison

func Contains added in v0.9.5

func Contains(field string, value interface{}) Predicate

Contains creates a new Predicate using an "Greater Or Equal" comparison

func EndsWith added in v0.9.5

func EndsWith(field string, value interface{}) Predicate

EndsWith creates a new Predicate using an "Greater Or Equal" comparison

func Equal added in v0.9.3

func Equal(field string, value interface{}) Predicate

Equal creates a new Predicate using an "Equals" comparison

func GreaterOrEqual added in v0.9.3

func GreaterOrEqual(field string, value interface{}) Predicate

GreaterOrEqual creates a new Predicate using an "Greater Or Equal" comparison

func GreaterThan added in v0.9.3

func GreaterThan(field string, value interface{}) Predicate

GreaterThan creates a new Predicate using an "Greater Than" comparison

func LessOrEqual added in v0.9.3

func LessOrEqual(field string, value interface{}) Predicate

LessOrEqual creates a new Predicate using an "Less Or Equal" comparison

func LessThan added in v0.9.3

func LessThan(field string, value interface{}) Predicate

LessThan creates a new Predicate using an "Less Than" comparison

func New

func New(field string, operator string, value interface{}) Predicate

New returns a fully populated Predicate

func NotEqual added in v0.9.3

func NotEqual(field string, value interface{}) Predicate

NotEqual creates a new Predicate using an "Not Equals" comparison

func (Predicate) And

func (predicate Predicate) And(field string, operator string, value interface{}) AndExpression

And combines this predicate with another one (created from the arguments) into an AndExpression

func (Predicate) AndEqual added in v0.10.0

func (predicate Predicate) AndEqual(name string, value interface{}) AndExpression

AndEqual combines this predicate with another one (created from the arguments) into an AndExpression

func (Predicate) AndGreaterOrEqual added in v0.10.0

func (predicate Predicate) AndGreaterOrEqual(name string, value interface{}) AndExpression

AndGreaterOrEqual combines this predicate with another one (created from the arguments) into an AndExpression

func (Predicate) AndGreaterThan added in v0.10.0

func (predicate Predicate) AndGreaterThan(name string, value interface{}) AndExpression

AndGreaterThan combines this predicate with another one (created from the arguments) into an AndExpression

func (Predicate) AndLessOrEqual added in v0.10.0

func (predicate Predicate) AndLessOrEqual(name string, value interface{}) AndExpression

AndLessOrEqual combines this predicate with another one (created from the arguments) into an AndExpression

func (Predicate) AndLessThan added in v0.10.0

func (predicate Predicate) AndLessThan(name string, value interface{}) AndExpression

AndLessThan combines this predicate with another one (created from the arguments) into an AndExpression

func (Predicate) AndNotEqual added in v0.10.0

func (predicate Predicate) AndNotEqual(name string, value interface{}) AndExpression

AndNotEqual combines this predicate with another one (created from the arguments) into an AndExpression

func (Predicate) Match

func (predicate Predicate) Match(fn MatcherFunc) bool

Match implements the Expression interface. It uses a MatcherFunc to determine if this predicate matches an arbitrary dataset.

func (Predicate) Or

func (predicate Predicate) Or(field string, operator string, value interface{}) OrExpression

Or combines this predicate with another one (created from the arguments) into an OrExpression

Jump to

Keyboard shortcuts

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