2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-18 15:25:31 +00:00
cointop/vendor/github.com/antonmedv/expr
2021-09-04 13:31:08 -07:00
..
ast Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
checker Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
compiler Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
conf Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
file Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
optimizer Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
parser Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
vm Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
.gitignore Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
.travis.yml Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
expr.go Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
LICENSE Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00
README.md Add vendor/github.com/antonmedv 2021-08-31 07:58:56 +10:00

Expr

Build Status 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)
}

Contributing

Expr consist of a few packages for parsing source code to AST, type checking AST, compiling to bytecode and VM for running bytecode program.

Also expr provides powerful tool exe for debugging. It has interactive terminal debugger for our bytecode virtual machine.

debugger

Who is using Expr?

  • Aviasales Aviasales are actively using Expr for different parts of the search engine.
  • Argo Argo Rollouts - Progressive Delivery for Kubernetes.
  • Argo Argo Workflows - The workflow engine for KubernetesOverview.
  • CrowdSec Crowdsec - A security automation tool.
  • Mystery Minds uses Expr to allow easy yet powerful customization of its matching algorithm.
  • qiniu qiniu cloud use Expr in trade systems.

Add your company too

License

MIT