summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/d5/tengo/v2/eval.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2021-07-31 18:27:55 +0200
committerGitHub <noreply@github.com>2021-07-31 18:27:55 +0200
commit44f3e2557dad86129419ed098c29e67638519f4f (patch)
tree17b8ddba49057cd8bf39cdf2618b31c7f6bc8c6a /vendor/github.com/d5/tengo/v2/eval.go
parent1f365c716eae44b64dc5bdace5cb70441d7eb4c2 (diff)
downloadmatterbridge-msglm-44f3e2557dad86129419ed098c29e67638519f4f.tar.gz
matterbridge-msglm-44f3e2557dad86129419ed098c29e67638519f4f.tar.bz2
matterbridge-msglm-44f3e2557dad86129419ed098c29e67638519f4f.zip
Update vendor (#1560)
Diffstat (limited to 'vendor/github.com/d5/tengo/v2/eval.go')
-rw-r--r--vendor/github.com/d5/tengo/v2/eval.go35
1 files changed, 35 insertions, 0 deletions
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 00000000..9ce9b2a7
--- /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
+}