diff --git a/go.mod b/go.mod index b9e42e0014e2f82e8f42c24a8f1dd7096831cfd8..bf29c6a739b66772a81480974a716052f9599109 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.14 require ( github.com/coreos/go-systemd/v22 v22.3.2 - github.com/d5/tengo/v2 v2.7.0 + github.com/d5/tengo/v2 v2.8.0 github.com/go-kit/kit v0.10.0 // indirect github.com/golang-migrate/migrate/v4 v4.14.1 github.com/golang/protobuf v1.5.2 diff --git a/go.sum b/go.sum index 07067a72acb4b327e6a4d94cb81b4c1b6e1e6965..99e7af1f4b962f05a17323d65c5c7a8c11097567 100644 --- a/go.sum +++ b/go.sum @@ -102,6 +102,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do github.com/cznic/mathutil v0.0.0-20180504122225-ca4c9f2c1369/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM= github.com/d5/tengo/v2 v2.7.0 h1:oAGQ+gcas0/T0bdqvNxfKzjOJhpQRwceWIF5gldB3aM= github.com/d5/tengo/v2 v2.7.0/go.mod h1:XRGjEs5I9jYIKTxly6HCF8oiiilk5E/RYXOZ5b0DZC8= +github.com/d5/tengo/v2 v2.8.0 h1:w8iRRZgiKhgT+/FwjFKdKJIGHQ0/OzygQIlCwrRecC4= +github.com/d5/tengo/v2 v2.8.0/go.mod h1:XRGjEs5I9jYIKTxly6HCF8oiiilk5E/RYXOZ5b0DZC8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/vendor/github.com/d5/tengo/v2/README.md b/vendor/github.com/d5/tengo/v2/README.md index bee195999597cb3d8d4bd3dfdefa304d08398045..fd214cd96bd5a25e6acad617f8055a859f9d711e 100644 --- a/vendor/github.com/d5/tengo/v2/README.md +++ b/vendor/github.com/d5/tengo/v2/README.md @@ -4,7 +4,7 @@ # The Tengo Language -[](https://godoc.org/github.com/d5/tengo) +[](https://godoc.org/github.com/d5/tengo/v2)  [](https://goreportcard.com/report/github.com/d5/tengo) @@ -53,19 +53,19 @@ fmt.println(sum("", [1, 2, 3])) // "123" | | fib(35) | fibt(35) | Language (Type) | | :--- | ---: | ---: | :---: | -| [**Tengo**](https://github.com/d5/tengo) | `2,931ms` | `4ms` | Tengo (VM) | -| [go-lua](https://github.com/Shopify/go-lua) | `4,824ms` | `4ms` | Lua (VM) | -| [GopherLua](https://github.com/yuin/gopher-lua) | `5,365ms` | `4ms` | Lua (VM) | -| [goja](https://github.com/dop251/goja) | `5,533ms` | `5ms` | JavaScript (VM) | -| [starlark-go](https://github.com/google/starlark-go) | `11,495ms` | `5ms` | Starlark (Interpreter) | -| [Yaegi](https://github.com/containous/yaegi) | `15,645ms` | `12ms` | Yaegi (Interpreter) | -| [gpython](https://github.com/go-python/gpython) | `16,322ms` | `5ms` | Python (Interpreter) | -| [otto](https://github.com/robertkrimen/otto) | `73,093ms` | `10ms` | JavaScript (Interpreter) | -| [Anko](https://github.com/mattn/anko) | `79,809ms` | `8ms` | Anko (Interpreter) | +| [**Tengo**](https://github.com/d5/tengo) | `2,315ms` | `3ms` | Tengo (VM) | +| [go-lua](https://github.com/Shopify/go-lua) | `4,028ms` | `3ms` | Lua (VM) | +| [GopherLua](https://github.com/yuin/gopher-lua) | `4,409ms` | `3ms` | Lua (VM) | +| [goja](https://github.com/dop251/goja) | `5,194ms` | `4ms` | JavaScript (VM) | +| [starlark-go](https://github.com/google/starlark-go) | `6,954ms` | `3ms` | Starlark (Interpreter) | +| [gpython](https://github.com/go-python/gpython) | `11,324ms` | `4ms` | Python (Interpreter) | +| [Yaegi](https://github.com/containous/yaegi) | `11,715ms` | `10ms` | Yaegi (Interpreter) | +| [otto](https://github.com/robertkrimen/otto) | `48,539ms` | `6ms` | JavaScript (Interpreter) | +| [Anko](https://github.com/mattn/anko) | `52,821ms` | `6ms` | Anko (Interpreter) | | - | - | - | - | -| Go | `53ms` | `3ms` | Go (Native) | -| Lua | `1,612ms` | `3ms` | Lua (Native) | -| Python | `2,632ms` | `23ms` | Python 2 (Native) | +| Go | `47ms` | `2ms` | Go (Native) | +| Lua | `756ms` | `2ms` | Lua (Native) | +| Python | `1,907ms` | `14ms` | Python2 (Native) | _* [fib(35)](https://github.com/d5/tengobench/blob/master/code/fib.tengo): Fibonacci(35)_ @@ -93,21 +93,18 @@ import ( ) func main() { - // Tengo script code - src := ` -each := func(seq, fn) { + // create a new Script instance + script := tengo.NewScript([]byte( +`each := func(seq, fn) { for x in seq { fn(x) } } sum := 0 mul := 1 each([a, b, c, d], func(x) { - sum += x - mul *= x -})` - - // create a new Script instance - script := tengo.NewScript([]byte(src)) + sum += x + mul *= x +})`)) // set values _ = script.Add("a", 1) @@ -128,6 +125,19 @@ each([a, b, c, d], func(x) { } ``` +Or, if you need to evaluate a simple expression, you can use [Eval](https://pkg.go.dev/github.com/d5/tengo/v2#Eval) function instead: + + +```golang +res, err := tengo.Eval(ctx, + `input ? "success" : "fail"`, + map[string]interface{}{"input": 1}) +if err != nil { + panic(err) +} +fmt.Println(res) // "success" +``` + ## References - [Language Syntax](https://github.com/d5/tengo/blob/master/docs/tutorial.md) diff --git a/vendor/github.com/d5/tengo/v2/eval.go b/vendor/github.com/d5/tengo/v2/eval.go new file mode 100644 index 0000000000000000000000000000000000000000..9ce9b2a7332365f1761282e3a21246ddec0122ee --- /dev/null +++ b/vendor/github.com/d5/tengo/v2/eval.go @@ -0,0 +1,35 @@ +package tengo + +import ( + "context" + "fmt" + "strings" +) + +// Eval compiles and executes given expr with params, and returns an +// evaluated value. expr must be an expression. Otherwise it will fail to +// compile. Expression must not use or define variable "__res__" as it's +// reserved for the internal usage. +func Eval( + ctx context.Context, + expr string, + params map[string]interface{}, +) (interface{}, error) { + expr = strings.TrimSpace(expr) + if expr == "" { + return nil, fmt.Errorf("empty expression") + } + + script := NewScript([]byte(fmt.Sprintf("__res__ := (%s)", expr))) + for pk, pv := range params { + err := script.Add(pk, pv) + if err != nil { + return nil, fmt.Errorf("script add: %w", err) + } + } + compiled, err := script.RunContext(ctx) + if err != nil { + return nil, fmt.Errorf("script run: %w", err) + } + return compiled.Get("__res__").Value(), nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index d27039d7f77a5b02dee3149f3b6296eb4bf15b5d..6e321e560bf885534670883e1b81dd2ebe370469 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -5,7 +5,7 @@ github.com/cespare/xxhash/v2 # github.com/coreos/go-systemd/v22 v22.3.2 ## explicit github.com/coreos/go-systemd/v22/daemon -# github.com/d5/tengo/v2 v2.7.0 +# github.com/d5/tengo/v2 v2.8.0 ## explicit github.com/d5/tengo/v2 github.com/d5/tengo/v2/parser