https://github.com/rosbit/go-expr
make expr-lang be embedded easily in Golang
https://github.com/rosbit/go-expr
expr-lang extending golang
Last synced: 6 months ago
JSON representation
make expr-lang be embedded easily in Golang
- Host: GitHub
- URL: https://github.com/rosbit/go-expr
- Owner: rosbit
- Created: 2024-01-05T07:10:32.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-01-06T13:48:57.000Z (almost 2 years ago)
- Last Synced: 2025-03-05T11:16:39.434Z (10 months ago)
- Topics: expr-lang, extending, golang
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-expr, an extending of expr-lang
[Expr](https://github.com/expr-lang/expr) is a Go-centric expression language designed to deliver dynamic
configurations with unparalleled accuracy, safety, and speed
`go-expr` is a package extending the `Expr` and making it a **pragmatic embeddable** language.
With some helper functions provided by `go-expr`, calling Golang functions from `Expr`,
or calling `Expr` functions from Golang are both very simple. So, with the help of `go-expr`, expr-lang
can be looked as **an embeddable expr-lang**.
### Expr functions
All to be evaluated in `Expr` are expression strings. Function is an extending notion to wrapper `Expr` expressions.
We make use of `YAML` syntax to declare `Expr` functions. e.g., Function `add` is desclared like this:
```yaml
funcs:
add:
params: [A, B]
expr: A + B
other-funcs:
..
```
`params` are parameter variable names for function. `expr` is the body of function, is just the `Expr` expressions.
### Usage
The package is fully go-getable, so, just type
`go get github.com/rosbit/go-expr`
to install.
#### 1. Evaluate expressions
```go
package main
import (
"github.com/rosbit/go-expr"
"fmt"
)
func main() {
ctx := ex.New()
res, _ := ctx.Eval("1 + 2", nil)
fmt.Println("result is:", res)
}
```
#### 2. Go calls Expr function:
Suppose there's a Expr file named `a.yaml` like this:
```yaml
funcs:
add:
params: [A, B]
expr: A + B
```
one can call the Expr function `add()` in Go code like the following:
```go
package main
import (
"github.com/rosbit/go-expr"
"fmt"
)
var add func(int, int)int
func main() {
ctx := ex.New()
if err := ctx.LoadFile("a.yaml", nil); err != nil {
fmt.Printf("%v\n", err)
return
}
if err := ctx.BindFunc("add", &add); err != nil {
fmt.Printf("%v\n", err)
return
}
res := add(1, 2)
fmt.Println("result is:", res)
}
```
#### 3. Expr calls Go function
Expr calling Go function is also easy. In the Go code, make a Golang function
as Expr built-in function by registering golang functions. There's the example:
```go
package main
import "github.com/rosbit/go-expr"
// function to be called by Expr
func adder(a1 float64, a2 float64) float64 {
return a1 + a2
}
func main() {
ctx := ex.New()
if err := ctx.LoadFile("b.yaml", map[string]interface{}{
"adder": adder,
}); err != nil {
// error handler
return
}
res, _ := ctx.CallFunc("add", 10, 2)
fmt.Println("result is:", res)
}
```
In Expr code, one can call the registered function directly. There's the example `b.yaml`
```yaml
funcs:
add:
params: [A, B]
expr: adder(A, B) # the function "adder" is implmented in Go.
```
### Status
The package is not fully tested, so be careful.
### Contribution
Pull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes.
__Convention:__ fork the repository and make changes on your fork in a feature branch.