Skip to content
Snippets Groups Projects
Commit cdfe0757 authored by Pavel Antonov's avatar Pavel Antonov :asterisk:
Browse files

Исправлен метод библиотеки expr - если выражение является строкой возвращается ошибка

Close #PRXS-1608
parents f5ce5cef 57fdf790
No related branches found
No related tags found
No related merge requests found
...@@ -57,16 +57,11 @@ func convertToMongo(ctx context.Context, tree *parser.Tree, env map[string]inter ...@@ -57,16 +57,11 @@ func convertToMongo(ctx context.Context, tree *parser.Tree, env map[string]inter
} }
c := &compiler{tree: tree, env: env, config: config, identifierRenameFn: identifierRenameFn} c := &compiler{tree: tree, env: env, config: config, identifierRenameFn: identifierRenameFn}
v := c.compile(tree.Node) v, ok := c.compile(tree.Node).(bson.M)
switch e := v.(type) { if !ok || v == nil {
case bson.M: return nil, fmt.Errorf("invalid expression")
b = e
case string:
b = bson.M{"$text": bson.M{"$search": e}}
default:
err = fmt.Errorf("invalid expression")
} }
return return v, nil
} }
type compiler struct { type compiler struct {
......
...@@ -50,10 +50,15 @@ func TestConvertToMongo(t *testing.T) { ...@@ -50,10 +50,15 @@ func TestConvertToMongo(t *testing.T) {
{"time", fmt.Sprintf("d > Time.Time('%s')", now.Format(time.RFC3339)), nil, bson.M{"d": bson.M{"$gt": tm}}, false}, {"time", fmt.Sprintf("d > Time.Time('%s')", now.Format(time.RFC3339)), nil, bson.M{"d": bson.M{"$gt": tm}}, false},
{"in", "In(s, [1,2,3])", nil, bson.M{"s": bson.M{"$in": []interface{}{1, 2, 3}}}, false}, {"in", "In(s, [1,2,3])", nil, bson.M{"s": bson.M{"$in": []interface{}{1, 2, 3}}}, false},
{"in", "In(s, 1)", nil, bson.M{"s": bson.M{"$in": []interface{}{1}}}, false}, {"in", "In(s, 1)", nil, bson.M{"s": bson.M{"$in": []interface{}{1}}}, false},
{"text search or id", "id", nil, nil, true},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
gotB, err := ConvertToMongo(ctx, tt.eval, tt.env, nil) gotB, err := ConvertToMongo(ctx, tt.eval, tt.env, nil)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, tt.wantB, gotB) assert.Equal(t, tt.wantB, gotB)
}) })
...@@ -72,7 +77,7 @@ func BenchmarkConvertToMongo(b *testing.B) { ...@@ -72,7 +77,7 @@ func BenchmarkConvertToMongo(b *testing.B) {
//fmt.Println(len(exp)) //fmt.Println(len(exp))
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
ConvertToMongo(ctx, exp, nil, nil, expr.Patch(&testVisitor{})) _, _ = ConvertToMongo(ctx, exp, nil, nil, expr.Patch(&testVisitor{}))
} }
} }
......
...@@ -54,6 +54,7 @@ type PreSaver interface { ...@@ -54,6 +54,7 @@ type PreSaver interface {
type Filter struct { type Filter struct {
ID []string ID []string
Data []*filter.Filter Data []*filter.Filter
// DEPRECATED Use Q instead
Search string // Поиск, одновременно поддерживается только один запрос Search string // Поиск, одновременно поддерживается только один запрос
Q []string Q []string
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment