Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Expression Language

Expression Language provides an engine for parsing, evaluating, compiling, and serializing restricted expressions in Go. It is designed around explicit Go types, interfaces, error handling, and reusable function values.

The package supports variables, arrays and maps, property and method access, custom functions and providers, parsed-expression caching, AST inspection, null-safe access, null coalescing, and a broad set of arithmetic, logical, comparison, string, and collection operators. It is safe for concurrent evaluation after configuration and has no external dependencies.

Installation

The package requires Go 1.24 or newer.

go get github.com/lemric/expression-language-go

Quick Start

package main

import (
	"fmt"
	"log"

	expressionlanguage "github.com/lemric/expression-language-go"
)

func main() {
	language := expressionlanguage.New()

	value, err := language.Evaluate(
		expressionlanguage.Expr("price * quantity"),
		expressionlanguage.Variables{
			"price":    21,
			"quantity": 2,
		},
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(value) // 42
}

Evaluate parses and executes an expression immediately. Compile returns a reusable Program, while Parse returns a ParsedExpression whose syntax tree can be inspected, dumped, evaluated, compiled, or serialized.

program, err := language.Compile(
	expressionlanguage.Expr("subtotal * (1 - discount)"),
	expressionlanguage.Names{
		"subtotal": "subtotal",
		"discount": "discount",
	},
)
if err != nil {
	log.Fatal(err)
}

total, err := program(expressionlanguage.Variables{
	"subtotal": 125.0,
	"discount": 0.2,
})

Compilation produces a Go function rather than generated Go source. The returned program can be reused with different variable values.

Documentation

Testing

go test ./...
go test -race ./...
go vet ./...

Run the complete language benchmark with:

go test -run '^$' -bench '^BenchmarkLanguage$' -benchmem

License

This package is released under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages