GitHub - expr-lang/expr: Expression language and expression evaluation for Go (original) (raw)

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

Go Integration

Static Typing

User-Friendly

Flexibility and Utility

Performance

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?

Add your company too

License

MIT