From 75225f65b5d237fd83fe3292b92d43f621507f8d Mon Sep 17 00:00:00 2001 From: Anton Sattarov <dirty.mew@gmail.com> Date: Thu, 12 Oct 2023 13:39:19 +0200 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5?= =?UTF-8?q?=D0=BA=D0=B0=20github.com/antonmedv/expr=20=D0=B4=D0=BE=20?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D0=B8=20v1.15.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/expr/expr.go | 61 ------------------------------------------- pkg/expr/expr_test.go | 54 -------------------------------------- 2 files changed, 115 deletions(-) delete mode 100644 pkg/expr/expr_test.go diff --git a/pkg/expr/expr.go b/pkg/expr/expr.go index 982157e9..9c788e71 100644 --- a/pkg/expr/expr.go +++ b/pkg/expr/expr.go @@ -1,8 +1,6 @@ package expr import ( - "github.com/antonmedv/expr" - "github.com/antonmedv/expr/ast" compiler2 "github.com/antonmedv/expr/compiler" "github.com/antonmedv/expr/parser" "github.com/antonmedv/expr/vm" @@ -11,14 +9,6 @@ import ( 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) { tree, err := parser.Parse(input) if err != nil { @@ -66,54 +56,3 @@ func EvalKV(ctx context.Context, input string, kv ...interface{}) (interface{}, 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 -} diff --git a/pkg/expr/expr_test.go b/pkg/expr/expr_test.go deleted file mode 100644 index d38e7e3f..00000000 --- a/pkg/expr/expr_test.go +++ /dev/null @@ -1,54 +0,0 @@ -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) - }) - } -} -- GitLab