github.com/expr-lang/expr@v1.16.9/README.md (about)

     1  <h1><a href="https://expr-lang.org"><img src="https://expr-lang.org/img/logo.png" alt="Zx logo" height="48"align="right"></a> Expr</h1>
     2  
     3  > [!IMPORTANT]
     4  > The repository [github.com/antonmedv/expr](https://github.com/antonmedv/expr) moved to [github.com/**expr-lang**/expr](https://github.com/expr-lang/expr).
     5  
     6  [![test](https://github.com/expr-lang/expr/actions/workflows/test.yml/badge.svg)](https://github.com/expr-lang/expr/actions/workflows/test.yml) 
     7  [![Go Report Card](https://goreportcard.com/badge/github.com/expr-lang/expr)](https://goreportcard.com/report/github.com/expr-lang/expr) 
     8  [![Fuzzing Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/expr.svg)](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:expr)
     9  [![GoDoc](https://godoc.org/github.com/expr-lang/expr?status.svg)](https://godoc.org/github.com/expr-lang/expr)
    10  
    11  **Expr** is a Go-centric expression language designed to deliver dynamic configurations with unparalleled accuracy, safety, and speed. 
    12  **Expr** combines simple [syntax](https://expr-lang.org/docs/language-definition) with powerful features for ease of use:
    13  
    14  ```js
    15  // Allow only admins and moderators to moderate comments.
    16  user.Group in ["admin", "moderator"] || user.Id == comment.UserId
    17  ```
    18  
    19  ```js
    20  // Determine whether the request is in the permitted time window.
    21  request.Time - resource.Age < duration("24h")
    22  ```
    23  
    24  ```js
    25  // Ensure all tweets are less than 240 characters.
    26  all(tweets, len(.Content) <= 240)
    27  ```
    28  
    29  ## Features
    30  
    31  **Expr** is a safe, fast, and intuitive expression evaluator optimized for the Go language. 
    32  Here are its standout features:
    33  
    34  ### Safety and Isolation
    35  * **Memory-Safe**: Expr is designed with a focus on safety, ensuring that programs do not access unrelated memory or introduce memory vulnerabilities.
    36  * **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.
    37  * **Always Terminating**: Expr is designed to prevent infinite loops, ensuring that every program will conclude in a reasonable amount of time.
    38  
    39  ### Go Integration
    40  * **Seamless with Go**: Integrate Expr into your Go projects without the need to redefine types.
    41  
    42  ### Static Typing
    43  * Ensures type correctness and prevents runtime type errors.
    44    ```go
    45    out, err := expr.Compile(`name + age`)
    46    // err: invalid operation + (mismatched types string and int)
    47    // | name + age
    48    // | .....^
    49    ```
    50  
    51  ### User-Friendly
    52  * Provides user-friendly error messages to assist with debugging and development.
    53  
    54  ### Flexibility and Utility
    55  * **Rich Operators**: Offers a reasonable set of basic operators for a variety of applications.
    56  * **Built-in Functions**: Functions like `all`, `none`, `any`, `one`, `filter`, and `map` are provided out-of-the-box.
    57  
    58  ### Performance
    59  * **Optimized for Speed**: Expr stands out in its performance, utilizing an optimizing compiler and a bytecode virtual machine. Check out these [benchmarks](https://github.com/antonmedv/golang-expression-evaluation-comparison#readme) for more details.
    60  
    61  ## Install
    62  
    63  ```
    64  go get github.com/expr-lang/expr
    65  ```
    66  
    67  ## Documentation
    68  
    69  * See [Getting Started](https://expr-lang.org/docs/Getting-Started) page for developer documentation.
    70  * See [Language Definition](https://expr-lang.org/docs/language-definition) page to learn the syntax.
    71  
    72  ## Examples
    73  
    74  [Play Online](https://go.dev/play/p/XCoNXEjm3TS)
    75  
    76  ```go
    77  package main
    78  
    79  import (
    80  	"fmt"
    81  	"github.com/expr-lang/expr"
    82  )
    83  
    84  func main() {
    85  	env := map[string]interface{}{
    86  		"greet":   "Hello, %v!",
    87  		"names":   []string{"world", "you"},
    88  		"sprintf": fmt.Sprintf,
    89  	}
    90  
    91  	code := `sprintf(greet, names[0])`
    92  
    93  	program, err := expr.Compile(code, expr.Env(env))
    94  	if err != nil {
    95  		panic(err)
    96  	}
    97  
    98  	output, err := expr.Run(program, env)
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  
   103  	fmt.Println(output)
   104  }
   105  ```
   106  
   107  [Play Online](https://go.dev/play/p/tz-ZneBfSuw)
   108  
   109  ```go
   110  package main
   111  
   112  import (
   113  	"fmt"
   114  	"github.com/expr-lang/expr"
   115  )
   116  
   117  type Tweet struct {
   118  	Len int
   119  }
   120  
   121  type Env struct {
   122  	Tweets []Tweet
   123  }
   124  
   125  func main() {
   126  	code := `all(Tweets, {.Len <= 240})`
   127  
   128  	program, err := expr.Compile(code, expr.Env(Env{}))
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  
   133  	env := Env{
   134  		Tweets: []Tweet{{42}, {98}, {69}},
   135  	}
   136  	output, err := expr.Run(program, env)
   137  	if err != nil {
   138  		panic(err)
   139  	}
   140  
   141  	fmt.Println(output)
   142  }
   143  ```
   144  
   145  ## Who uses Expr?
   146  
   147  * [Google](https://google.com) uses Expr as one of its expression languages on the [Google Cloud Platform](https://cloud.google.com).
   148  * [Uber](https://uber.com) uses Expr to allow customization of its Uber Eats marketplace.
   149  * [GoDaddy](https://godaddy.com) employs Expr for the customization of its GoDaddy Pro product.
   150  * [ByteDance](https://bytedance.com) incorporates Expr into its internal business rule engine.
   151  * [Aviasales](https://aviasales.ru) utilizes Expr as a business rule engine for its flight search engine.
   152  * [Wish.com](https://www.wish.com) employs Expr in its decision-making rule engine for the Wish Assistant.
   153  * [Argo](https://argoproj.github.io) integrates Expr into Argo Rollouts and Argo Workflows for Kubernetes.
   154  * [OpenTelemetry](https://opentelemetry.io) integrates Expr into the OpenTelemetry Collector.
   155  * [Philips Labs](https://github.com/philips-labs/tabia) employs Expr in Tabia, a tool designed to collect insights on their code bases.
   156  * [CrowdSec](https://crowdsec.net) incorporates Expr into its security automation tool.
   157  * [CoreDNS](https://coredns.io) uses Expr in CoreDNS, which is a DNS server.
   158  * [qiniu](https://www.qiniu.com) implements Expr in its trade systems.
   159  * [Junglee Games](https://www.jungleegames.com/) uses Expr for its in-house marketing retention tool, Project Audience.
   160  * [Faceit](https://www.faceit.com) uses Expr to enhance customization of its eSports matchmaking algorithm.
   161  * [Chaos Mesh](https://chaos-mesh.org) incorporates Expr into Chaos Mesh, a cloud-native Chaos Engineering platform.
   162  * [Visually.io](https://visually.io) employs Expr as a business rule engine for its personalization targeting algorithm.
   163  * [Akvorado](https://github.com/akvorado/akvorado) utilizes Expr to classify exporters and interfaces in network flows.
   164  * [keda.sh](https://keda.sh) uses Expr to allow customization of its Kubernetes-based event-driven autoscaling.
   165  * [Span Digital](https://spandigital.com/) uses Expr in it's Knowledge Management products.
   166  * [Xiaohongshu](https://www.xiaohongshu.com/) combining yaml with Expr for dynamically policies delivery.
   167  * [Melrōse](https://melrōse.org) uses Expr to implement its music programming language.
   168  
   169  [Add your company too](https://github.com/expr-lang/expr/edit/master/README.md)
   170  
   171  ## License
   172  
   173  [MIT](https://github.com/expr-lang/expr/blob/master/LICENSE)
   174  
   175  <p align="center"><img src="https://expr-lang.org/img/gopher-small.png" width="150" /></p>