项目作者: antonmedv

项目描述 :
Expression evaluation engine for Go: fast, non-Turing complete, dynamic typing, static typing
高级语言: Go
项目地址: git://github.com/antonmedv/expr.git
创建时间: 2018-07-14T15:57:34Z
项目社区:https://github.com/antonmedv/expr

开源协议:MIT License

下载


Zx logo Expr

[!IMPORTANT]
The repository github.com/antonmedv/expr moved to github.com/expr-lang/expr.

test
Go Report Card
Fuzzing Status
GoDoc

Expr is a Go-centric expression language designed to deliver dynamic configurations with unparalleled accuracy, safety, and speed.
Expr combines simple syntax with powerful features for ease of use:

  1. // Allow only admins and moderators to moderate comments.
  2. user.Group in ["admin", "moderator"] || user.Id == comment.UserId
  1. // Determine whether the request is in the permitted time window.
  2. request.Time - resource.Age < duration("24h")
  1. // Ensure all tweets are less than 240 characters.
  2. all(tweets, len(.Content) <= 240)

Features

Expr is a safe, fast, and intuitive expression evaluator optimized for the Go language.
Here are its standout features:

Safety and Isolation

  • Memory-Safe: Expr is designed with a focus on safety, ensuring that programs do not access unrelated memory or introduce memory vulnerabilities.
  • Side-Effect-Free: Expressions evaluated in Expr only compute outputs from their inputs, ensuring no side-effects that can change state or produce unintended results.
  • Always Terminating: Expr is designed to prevent infinite loops, ensuring that every program will conclude in a reasonable amount of time.

Go Integration

  • Seamless with Go: Integrate Expr into your Go projects without the need to redefine types.

Static Typing

  • Ensures type correctness and prevents runtime type errors.
    1. out, err := expr.Compile(`name + age`)
    2. // err: invalid operation + (mismatched types string and int)
    3. // | name + age
    4. // | .....^

User-Friendly

  • Provides user-friendly error messages to assist with debugging and development.

Flexibility and Utility

  • Rich Operators: Offers a reasonable set of basic operators for a variety of applications.
  • Built-in Functions: Functions like all, none, any, one, filter, and map are provided out-of-the-box.

Performance

  • Optimized for Speed: Expr stands out in its performance, utilizing an optimizing compiler and a bytecode virtual machine. Check out these benchmarks for more details.

Install

  1. go get github.com/expr-lang/expr

Documentation

Examples

Play Online

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/expr-lang/expr"
  5. )
  6. func main() {
  7. env := map[string]interface{}{
  8. "greet": "Hello, %v!",
  9. "names": []string{"world", "you"},
  10. "sprintf": fmt.Sprintf,
  11. }
  12. code := `sprintf(greet, names[0])`
  13. program, err := expr.Compile(code, expr.Env(env))
  14. if err != nil {
  15. panic(err)
  16. }
  17. output, err := expr.Run(program, env)
  18. if err != nil {
  19. panic(err)
  20. }
  21. fmt.Println(output)
  22. }

Play Online

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/expr-lang/expr"
  5. )
  6. type Tweet struct {
  7. Len int
  8. }
  9. type Env struct {
  10. Tweets []Tweet
  11. }
  12. func main() {
  13. code := `all(Tweets, {.Len <= 240})`
  14. program, err := expr.Compile(code, expr.Env(Env{}))
  15. if err != nil {
  16. panic(err)
  17. }
  18. env := Env{
  19. Tweets: []Tweet{{42}, {98}, {69}},
  20. }
  21. output, err := expr.Run(program, env)
  22. if err != nil {
  23. panic(err)
  24. }
  25. fmt.Println(output)
  26. }

Who uses Expr?

  • Google uses Expr as one of its expression languages on the Google Cloud Platform.
  • Uber uses Expr to allow customization of its Uber Eats marketplace.
  • GoDaddy employs Expr for the customization of its GoDaddy Pro product.
  • ByteDance incorporates Expr into its internal business rule engine.
  • Aviasales utilizes Expr as a business rule engine for its flight search engine.
  • Wish.com employs Expr in its decision-making rule engine for the Wish Assistant.
  • Naoma.AI uses Expr as a part of its call scoring engine.
  • Argo integrates Expr into Argo Rollouts and Argo Workflows for Kubernetes.
  • OpenTelemetry integrates Expr into the OpenTelemetry Collector.
  • Philips Labs employs Expr in Tabia, a tool designed to collect insights on their code bases.
  • CrowdSec incorporates Expr into its security automation tool.
  • CoreDNS uses Expr in CoreDNS, which is a DNS server.
  • qiniu implements Expr in its trade systems.
  • Junglee Games uses Expr for its in-house marketing retention tool, Project Audience.
  • Faceit uses Expr to enhance customization of its eSports matchmaking algorithm.
  • Chaos Mesh incorporates Expr into Chaos Mesh, a cloud-native Chaos Engineering platform.
  • Visually.io employs Expr as a business rule engine for its personalization targeting algorithm.
  • Akvorado utilizes Expr to classify exporters and interfaces in network flows.
  • keda.sh uses Expr to allow customization of its Kubernetes-based event-driven autoscaling.
  • Span Digital uses Expr in its Knowledge Management products.
  • Xiaohongshu combining yaml with Expr for dynamically policies delivery.
  • Melrōse uses Expr to implement its music programming language.
  • Tork integrates Expr into its workflow execution.
  • Critical Moments uses Expr for its mobile realtime conditional targeting system.
  • WoodpeckerCI uses Expr for filtering workflows/steps.
  • FastSchema - A BaaS leveraging Expr for its customizable and dynamic Access Control system.
  • WunderGraph Cosmo - GraphQL Federeration Router uses Expr to customize Middleware behaviour
  • SOLO uses Expr interally to allow dynamic code execution with custom defined functions.

Add your company too

License

MIT