Skip to content
Snippets Groups Projects
Commit 75225f65 authored by Anton Sattarov's avatar Anton Sattarov
Browse files

Обновлена библиотека github.com/antonmedv/expr до версии v1.15.3

parent e3ec4f29
No related branches found
No related tags found
No related merge requests found
package expr package expr
import ( import (
"github.com/antonmedv/expr"
"github.com/antonmedv/expr/ast"
compiler2 "github.com/antonmedv/expr/compiler" compiler2 "github.com/antonmedv/expr/compiler"
"github.com/antonmedv/expr/parser" "github.com/antonmedv/expr/parser"
"github.com/antonmedv/expr/vm" "github.com/antonmedv/expr/vm"
...@@ -11,14 +9,6 @@ import ( ...@@ -11,14 +9,6 @@ import (
const EnvContextKey = "$context" const EnvContextKey = "$context"
func init() {
RegisterOption(
Extend("search", Search),
Extend("near", Near),
Extend("within", WithIn),
)
}
func Eval(ctx context.Context, input string, env map[string]interface{}) (interface{}, error) { func Eval(ctx context.Context, input string, env map[string]interface{}) (interface{}, error) {
tree, err := parser.Parse(input) tree, err := parser.Parse(input)
if err != nil { if err != nil {
...@@ -66,54 +56,3 @@ func EvalKV(ctx context.Context, input string, kv ...interface{}) (interface{}, ...@@ -66,54 +56,3 @@ func EvalKV(ctx context.Context, input string, kv ...interface{}) (interface{},
return Eval(ctx, input, m) return Eval(ctx, input, m)
} }
func IsValidExpression(ctx context.Context, input string) bool {
if input == "" {
return true
}
tree, err := parser.Parse(input)
if err != nil {
return false
}
env := GetEnv(ctx)
if env == nil {
env = make(map[string]interface{})
}
env[EnvContextKey] = ctx
cfg := GetDefaultConfig(env)
env, _ = cfg.Env.(map[string]interface{})
if len(cfg.Visitors) >= 0 {
for _, v := range cfg.Visitors {
ast.Walk(&tree.Node, v)
}
}
program, err := compiler2.Compile(tree, cfg)
if err != nil {
return false
}
if output, err := expr.Run(program, env); err != nil || output == nil {
return false
}
return true
}
func Search(v interface{}) bool {
_, ok := v.(string)
return ok
}
func Near(v1, v2, v3 interface{}) bool {
return false
}
func WithIn(v1, v2, v3 interface{}) bool {
return false
}
package expr
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestIsValidExpression(t *testing.T) {
now := time.Now()
dt, _ := time.Parse("2006-01-02", "2021-08-31")
tm, _ := time.Parse(time.RFC3339, now.Format(time.RFC3339))
ctx := WithEnv(context.Background(), map[string]interface{}{"i": 1, "a": 10.0, "d": dt, "t": tm, "value": "value"})
tests := []struct {
name string
eval string
want bool
}{
{"equal", "i == 3", true},
{"in array", "i in [1,2,3]", true},
{"contains", "value contains 'some'", true},
{"contains with . + () $ {} ^", "value contains 'something with . + () $ {} ^'", true},
{"startsWith", "value startsWith 'some'", true},
{"startsWith . + () $ {} ^", "value startsWith '. + () $ {} ^'", true},
{"endsWith", "value endsWith 'some'", true},
{"endsWith . + () $ {} ^", "value endsWith '. + () $ {} ^'", true},
{"icontains", "icontains(value, 'some')", true},
{"icontains with . + () $ {} ^", "icontains (value, 'something with . + () $ {} ^')", true},
{"istartsWith", "istartsWith(value, 'Some')", true},
{"istartsWith . + () $ {} ^ . + () $ {} ^", "istartsWith(value, '. + () $ {} ^')", true},
{"iendsWith", "iendsWith(value, 'some')", true},
{"iendsWith . + () $ {} ^", "iendsWith(value,'. + () $ {} ^')", true},
{"or", "i == 2 || i > 10", true},
{"search", "search('some') || i > 10", true},
{"vars:or", "i == a + 2 || i > a + 10", true},
{"near", "near(a, [55.5, 37.5], 1000)", true},
{"within", "within(a, 'box', [[54.54, 36.36], [55.55, 37.37]])", true},
{"time", "d > Time.Date('2021-08-31')", true},
{"time", fmt.Sprintf("d > Time.Time('%s')", now.Format(time.RFC3339)), true},
{"in", "In(s, [1,2,3])", true},
{"in", "In(s, 1)", true},
{"text search or id", "id", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsValidExpression(ctx, tt.eval)
assert.Equal(t, tt.want, got)
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment