diff --git a/pkg/expr/expr.go b/pkg/expr/expr.go index 982157e9bfe875a215b33e335e0db410a602c463..9c788e71e108e85b483ec71a8700b7fc3bb046be 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 d38e7e3fe020fc9933913ef64c5c2da4caacc492..0000000000000000000000000000000000000000 --- 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) - }) - } -}