exp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2015 License: MIT Imports: 4 Imported by: 0

README

Binary Expression Tree

Package exp implements a binary expression tree which can be used to evaluate arbitrary binary expressions. You can use this package to build your own expressions however a few expressions are provided out of the box.

Installation

$ go get github.com/alexkappa/exp

Usage

conjunction := And(True, True, True)
disjunction := Or(True, False)
negation := Not(False)

complex := Or(And(conjunction, disjunction), negation)

fmt.Printf("%t\n", complex.Eval(p)) // true

Documentation

API documentation is available at godoc.

Documentation

Overview

Package exp implements a binary expression tree which can be used to evaluate arbitrary binary expressions. You can use this package to build your own expressions however a few expressions are provided out of the box.

Example
conjunction := And(True, True, True)
disjunction := Or(True, False)
negation := Not(False)

complex := Or(And(conjunction, disjunction), negation)

fmt.Printf("%t\n", complex.Eval(nil))
Output:

true
Example (URL)
writer := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "http://example.com?event=signup&date=2014-10-03&basket_value=199.90&referrer=https://google.com", nil)

handler := func(w http.ResponseWriter, r *http.Request) {
	exp := And(
		Match("event", "signup"),
		Before("date", time.Now()),
		GreaterThan("basket_value", 99.99),
		Contains("referrer", "google.com"))

	if exp.Eval(r.URL.Query()) {
		fmt.Fprintf(w, "Expression evaluates")
	}
}

handler(writer, request)

fmt.Println(writer.Body.String())
Output:

Expression evaluates

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// True is an expression that always evaluates to true.
	True = Bool(true)
	// False is an expression that always evaluates to false.
	False = Bool(false)
)

Functions

func DateFormat

func DateFormat(f string) string

DateFormat changes the date format used to parse dates and returnes the previous format in case you need to revert back in the future.

Types

type Bool

type Bool bool

Bool is a wrapper for the native bool type which provides an Eval function so that it satisfies the Exp interface.

func (Bool) Eval

func (b Bool) Eval(p Params) bool

Eval will always return the boolean value of b and disregard p.

func (Bool) String

func (b Bool) String() string

type Exp

type Exp interface {
	Eval(Params) bool
}

The Exp interface represents a tree node. There are several implementations of the interface in this package, but one may define custom Exp's as long as they implement the Eval function.

func After

func After(key string, date time.Time) Exp

After is an expression that evaluates to true if date is a time after the evaluated date. The value is parsed to a time.Time before comparing.

func And

func And(t ...Exp) Exp

And evaluates to true if all t's are true.

func Before

func Before(key string, date time.Time) Exp

Before evaluates to true if date is before the date pointed to by key. The value is parsed to a time.Time before comparing. In case of a parse error false is returned.

func Contains

func Contains(key, substr string) Exp

Contains is an expression that evaluates to true if substr is within the value pointed to by key.

func ContainsAny

func ContainsAny(key, chars string) Exp

ContainsAny evaluates to true if any Unicode code points in chars are within the value pointed to by key.

func ContainsRune

func ContainsRune(key string, r rune) Exp

ContainsRune evaluates to true if the Unicode code point r is within the value pointed to by key.

func Count

func Count(key, sep string, count int) Exp

Count evaluates to true if the number of non-overlapping instances of sep in the value pointed to by key is equal to count.

func Day

func Day(key string, day int) Exp

Day is an expression that evaluates to true if the date pointed to by key is on the specified day.

func Eq

func Eq(k string, v float64) Exp

Eq is an alias for Equal.

func Equal

func Equal(k string, v float64) Exp

Equal evaluates to true if the value pointed to by key is equal in value to v. The value pointed to by k is parsed into a float64 before comparing. If a parse error occurs false is returned.

func EqualFold

func EqualFold(key, s string) Exp

EqualFold evaluates to true if the value pointed to by key and s, interpreted as UTF-8 strings, are equal under Unicode case-folding.

func GreaterOrEqual

func GreaterOrEqual(k string, v float64) Exp

GreaterOrEqual is a shorthand for Or(Gt(k, v), Eq(k, v)).

func GreaterThan

func GreaterThan(k string, v float64) Exp

GreaterThan evaluates to true if the value pointed to by key is greater in value than v. The value is parsed as float before performing the comparison.

func Gt

func Gt(k string, v float64) Exp

Gt is an alias for GreaterThan.

func Gte

func Gte(k string, v float64) Exp

Gte is an alias for GreaterOrEqual.

func Len

func Len(key string, length int) Exp

Len evalates to true if the length of the string pointed to by key is equal to length.

func LessOrEqual

func LessOrEqual(k string, v float64) Exp

LessOrEqual is a shorthand for Lt(Gt(k, v), Eq(k, v)).

func LessThan

func LessThan(k string, v float64) Exp

LessThan evaluates to true if the value pointed to by key is less in value than v. The value is parsed as float before performing the comparison.

func Lt

func Lt(k string, v float64) Exp

Lt is an alias for LessThan.

func Lte

func Lte(k string, v float64) Exp

Lte is an alias for LessOrEqual.

func Match

func Match(key, str string) Exp

Match is an expression that evaluates to true if str is equal to the value pointed to by key.

func MatchAny

func MatchAny(key string, strs ...string) Exp

MatchAny is an expression that evaluates to true if any of the strs are equal to the value pointed to by key.

func Month

func Month(key string, month time.Month) Exp

Month is an expression that evaluates to true if the date pointed to by key is on the specified month.

func Neq

func Neq(k string, v float64) Exp

Neq is an alias for NotEqual.

func Not

func Not(t Exp) Exp

Not evaluates to the opposite of t.

func NotEqual

func NotEqual(k string, v float64) Exp

NotEqual is a shorthand for Not(Eq(k, v)).

func On

func On(key string, date time.Time) Exp

On evaluates to true if date is equal to the date pointed to by key. The value is parsed to a time.Time before comparing. In case of a parse error false is returned.

func Or

func Or(t ...Exp) Exp

Or evaluates to true if any t's are true.

func Weekday

func Weekday(key string, weekday time.Weekday) Exp

Weekday is an expression that evaluates to true if the date pointed to by key is on the specified weekday.

func Year

func Year(key string, year int) Exp

Year is an expression that evaluates to true if the date pointed to by key is on the specified year.

type Map

type Map map[string]string

Map is a simple implementation of Params using a map of strings.

func (Map) Get

func (m Map) Get(key string) string

Get returns the value pointed to by key.

type Params

type Params interface {
	Get(string) string
}

Params defines the interface needed by Exp in order to be able to validate conditions. An example implementation of this interface would be https://golang.org/pkg/net/url/#Values.

Jump to

Keyboard shortcuts

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