Code Monkey home page Code Monkey logo

expr's Introduction

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

expr's People

Contributors

antonmedv avatar szyhf avatar aksentyev avatar nimdraugsael avatar rakoth avatar niaow avatar lmika avatar

Watchers

James Cloos avatar Hamit Burak Emre avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.