Skip to content
Expr is an engine that can evaluate expressions.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml Test on more go versions Oct 21, 2018
LICENSE
README.md Update README.md Dec 2, 2018
UPGRADE.md Create UPGRADE.md Aug 21, 2018
bench_test.go
doc.go Update docs Oct 12, 2018
doc_test.go Use Env instead of With Oct 9, 2018
error.go
eval.go Delete toBool and toText Oct 19, 2018
eval_test.go
lexer.go Fix unquoting strings Sep 18, 2018
lexer_test.go Fix unquoting strings Sep 18, 2018
node.go Remove Type alias Oct 22, 2018
parser.go
parser_test.go
print.go dev(pretty_print): remove reduntant parenthesis Aug 14, 2018
print_test.go Refactor test with print of triple plus Aug 14, 2018
runtime.go
type.go
type_test.go

README.md

Expr Build Status Go Report Card Code Coverage Sparkline

Expr is an engine that can evaluate expressions.

The purpose of the package is to allow users to use expressions inside configuration for more complex logic. It is a perfect candidate for the foundation of a business rule engine. The idea is to let configure things in a dynamic way without recompile of a program:

# Get the special price if
user.Group in ["good_customers", "collaborator"]

# Promote article to the homepage when
len(article.Comments) > 100 and article.Category not in ["misc"]

# Send an alert when
product.Stock < 15

Inspired by

Features

  • Works with any valid Go object (structs, maps, etc)
  • Static and dynamic typing (example)
    code := "groups[0].Title + user.Age"
    p, err := expr.Parse(code, expr.Define("groups", []Group{}), expr.Define("user", User{}))
    // err: invalid operation: groups[0].Name + user.Age (mismatched types string and int)
  • User-friendly error messages
    unclosed "("
    (boo + bar]
    ----------^
    
  • Reasonable set of basic operators
  • Fast (faster otto and goja, see bench)

Install

go get -u github.com/antonmedv/expr

Documentation

  • See GoDoc for developer documentation,
  • See The Expression Syntax page to learn the syntax of the Expr expressions.

Examples

Executing arbitrary expressions.

env := map[string]interface{}{
    "foo": 1,
    "bar": struct{Value int}{1},
}

out, err := expr.Eval("foo + bar.Value", env)

Static type checker with struct as environment.

type env struct {
	Foo int
	Bar bar
}

type bar struct {
	Value int
}

p, err := expr.Parse("Foo + Bar.Value", expr.Env(env{}))

out, err := expr.Run(p, env{1, bar{2}})

Using env's methods as functions inside expressions.

type env struct {
	Name string
}

func (e env) Title() string {
	return strings.Title(e.Name)
}


p, err := expr.Parse("'Hello ' ~ Title()", expr.Env(env{}))

out, err := expr.Run(p, env{"world"})

Using embedded structs to construct env.

type env struct {
	helpers
	Name string
}

type helpers struct{}

func (h helpers) Title(s string) string {
	return strings.Title(s)
}


p, err := expr.Parse("'Hello ' ~ Title(Name)", expr.Env(env{}))

out, err := expr.Run(p, env{"world"})

License

MIT

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.