-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package tengo_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/d5/tengo/v2" | ||
"github.com/d5/tengo/v2/require" | ||
) | ||
|
||
func TestEval(t *testing.T) { | ||
eval := func( | ||
expr string, | ||
params map[string]interface{}, | ||
expected interface{}, | ||
) { | ||
ctx := context.Background() | ||
actual, err := tengo.Eval(ctx, expr, params) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, actual) | ||
} | ||
|
||
eval(`undefined`, nil, nil) | ||
eval(`1`, nil, int64(1)) | ||
eval(`19 + 23`, nil, int64(42)) | ||
eval(`"foo bar"`, nil, "foo bar") | ||
eval(`[1, 2, 3][1]`, nil, int64(2)) | ||
|
||
eval( | ||
`5 + p`, | ||
map[string]interface{}{ | ||
"p": 7, | ||
}, | ||
int64(12), | ||
) | ||
eval( | ||
`"seven is " + p`, | ||
map[string]interface{}{ | ||
"p": 7, | ||
}, | ||
"seven is 7", | ||
) | ||
eval( | ||
`"" + a + b`, | ||
map[string]interface{}{ | ||
"a": 7, | ||
"b": " is seven", | ||
}, | ||
"7 is seven", | ||
) | ||
|
||
eval( | ||
`a ? "success" : "fail"`, | ||
map[string]interface{}{ | ||
"a": 1, | ||
}, | ||
"success", | ||
) | ||
} |