Giter VIP home page Giter VIP logo

expr's Introduction

Expr

test Go Report Card GoDoc

expr logo

Expr package provides an engine that can compile and evaluate expressions. An expression is a one-liner that returns a value (mostly, but not limited to, booleans). It is designed for simplicity, speed and safety.

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

Features

  • Seamless integration with Go (no need to redefine types)
  • Static typing (example).
    out, err := expr.Compile(`name + age`)
    // err: invalid operation + (mismatched types string and int)
    // | name + age
    // | .....^
  • User-friendly error messages.
  • Reasonable set of basic operators.
  • Builtins all, none, any, one, filter, map.
    all(Tweets, {.Size <= 280})
  • Fast (benchmarks): uses bytecode virtual machine and optimizing compiler.

Install

go get github.com/antonmedv/expr

Documentation

Expr Code Editor

Expr Code Editor

Also, I have an embeddable code editor written in JavaScript which allows editing expressions with syntax highlighting and autocomplete based on your types declaration.

Learn more โ†’

Examples

Play Online

package main

import (
	"fmt"
	"github.com/antonmedv/expr"
)

func main() {
	env := map[string]interface{}{
		"greet":   "Hello, %v!",
		"names":   []string{"world", "you"},
		"sprintf": fmt.Sprintf,
	}

	code := `sprintf(greet, names[0])`

	program, err := expr.Compile(code, expr.Env(env))
	if err != nil {
		panic(err)
	}

	output, err := expr.Run(program, env)
	if err != nil {
		panic(err)
	}

	fmt.Println(output)
}

Play Online

package main

import (
	"fmt"
	"github.com/antonmedv/expr"
)

type Tweet struct {
	Len int
}

type Env struct {
	Tweets []Tweet
}

func main() {
	code := `all(Tweets, {.Len <= 240})`

	program, err := expr.Compile(code, expr.Env(Env{}))
	if err != nil {
		panic(err)
	}

	env := Env{
		Tweets: []Tweet{{42}, {98}, {69}},
	}
	output, err := expr.Run(program, env)
	if err != nil {
		panic(err)
	}

	fmt.Println(output)
}

Who uses Expr?

  • Aviasales uses Expr as a business rule engine for our flight search engine.
  • Wish.com uses Expr for decision-making rule engine in the Wish Assistant.
  • Argo uses Expr in Argo Rollouts and Argo Workflows for Kubernetes.
  • Crowdsec uses Expr in a security automation tool.
  • FACEIT uses Expr to allow customization of its eSports matchmaking algorithm.
  • qiniu uses Expr in trade systems.
  • Junglee Games uses Expr for an in house marketing retention tool Project Audience.
  • OpenTelemetry uses Expr in the OpenTelemetry Collector.
  • Philips Labs uses Expr in Tabia, a tool for collecting insights on the characteristics of our code bases.
  • CoreDNS uses Expr in CoreDNS, a DNS server.
  • Chaos Mesh uses Expr in Chaos Mesh, a cloud-native Chaos Engineering platform.
  • Milvus uses Expr in Milvus, an open-source vector database.
  • Visually.io uses Expr as a business rule engine for our personalization targeting algorithm.
  • Akvorado uses Expr to classify exporters and interfaces in network flows.

Add your company too

License

MIT

expr's People

Contributors

antonmedv avatar rakoth avatar szyhf avatar emicklei avatar aksentyev avatar shmsr avatar tufitko avatar ffenix113 avatar vincentbernat avatar deanm avatar davidklassen avatar niaow avatar lmika avatar mhr3 avatar phanirithvij avatar xhit avatar taras-zak avatar panthershark avatar krvladislav avatar 3timeslazy avatar blkperl avatar zachbadgett avatar cameronhatfield avatar imh966 avatar javasgl avatar uunnii avatar dentrax avatar fzhang-wish avatar rtpt-erikgeiser avatar viggin543 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.