MCPcopy
hub / github.com/expr-lang/expr

github.com/expr-lang/expr @v1.17.8 sqlite

repository ↗ · DeepWiki ↗ · release v1.17.8 ↗
2,570 symbols 12,033 edges 192 files 912 documented · 35%
README

Zx logo 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:

// Allow only admins and moderators to moderate comments.
user.Group in ["admin", "moderator"] || user.Id == comment.UserId
// Determine whether the request is in the permitted time window.
request.Time - resource.Age < duration("24h")
// Ensure all tweets are less than 240 characters.
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. go out, err := expr.Compile(`name + age`) // err: invalid operation + (mismatched types string and int) // | name + age // | .....^

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

go get github.com/expr-lang/expr

Documentation

Examples

Play Online

package main

import (
    "fmt"
    "github.com/expr-lang/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/expr-lang/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?

  • 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.
  • Alibaba uses Expr in a web framework for building recommendation services.
  • Argo integrates Expr into Argo Rollouts and Argo Workflows for Kubernetes.
  • Wish.com employs Expr in its decision-making rule engine for the Wish Assistant.
  • 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.
  • Naoma.AI uses Expr as a part of its call scoring engine.
  • GlassFlow.dev uses Expr to do realtime data transformation in ETL pipelines

Add your company too

License

MIT

Extension points exported contracts — how you extend this code

TestingT (Interface)
go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=assert -template=assertion [5 …
internal/testify/assert/assertions.go
AnyValuer (Interface)
A AnyValuer provides a generic function for a custom type to return standard go values. It allows for returning a `nil` [5 …
patcher/value/value.go
Type (Interface)
Type is a type that can be used to represent a value. [4 implementers]
types/types.go
Visitor (Interface)
(no doc) [28 implementers]
ast/visitor.go
Checker (Interface)
(no doc) [3 implementers]
conf/config.go
Foo (Interface)
(no doc) [3 implementers]
test/issues/688/issue_test.go
Option (FuncType)
Option for configuring config.
expr.go
StoreInterface (Interface)
(no doc) [2 implementers]
test/interface/interface_test.go

Core symbols most depended-on inside this repo

Sprintf
called by 612
internal/spew/config.go
NoError
called by 535
internal/testify/require/require.go
Compile
called by 285
expr.go
Errorf
called by 236
internal/testify/require/requirements.go
emit
called by 215
compiler/compiler.go
Run
called by 199
vm/vm.go
Equal
called by 178
internal/testify/assert/assertions.go
String
called by 175
ast/node.go

Shape

Function 1,343
Method 848
Struct 274
TypeAlias 51
Interface 36
FuncType 18

Languages

Go100%

Modules by API surface

internal/testify/assert/assertions_test.go148 symbols
internal/testify/require/require_forward.go144 symbols
internal/testify/require/require.go144 symbols
internal/testify/assert/assertion_forward.go144 symbols
expr_test.go121 symbols
internal/testify/assert/assertions.go101 symbols
internal/testify/assert/assertion_format.go72 symbols
internal/testify/require/requirements_test.go52 symbols
checker/nature/nature.go51 symbols
compiler/compiler.go49 symbols
test/mock/mock.go45 symbols
patcher/value/value.go41 symbols

Dependencies from manifests, versioned

github.com/bettercap/readlinev0.0.0-2021022815155 · 1×
github.com/chzyer/testv1.0.0 · 1×
github.com/gdamore/encodingv1.0.0 · 1×
github.com/lucasb-eyer/go-colorfulv1.2.0 · 1×
github.com/mattn/go-runewidthv0.0.15 · 1×
github.com/rivo/tviewv0.0.0-2023081411000 · 1×
github.com/rivo/unisegv0.4.4 · 1×
golang.org/x/sysv0.11.0 · 1×
golang.org/x/termv0.11.0 · 1×

For agents

$ claude mcp add expr \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact