diff --git a/pkg/expr/mongo.go b/pkg/expr/mongo.go index 95f4b6beb7a99a279db932cb62b8fcf2186e6a23..989454178f640144791a9e6fd84d39317fd1e283 100644 --- a/pkg/expr/mongo.go +++ b/pkg/expr/mongo.go @@ -57,21 +57,11 @@ func convertToMongo(ctx context.Context, tree *parser.Tree, env map[string]inter } c := &compiler{tree: tree, env: env, config: config, identifierRenameFn: identifierRenameFn} - v := c.compile(tree.Node) - switch e := v.(type) { - case bson.M: - b = e - case string: - b = bson.M{ - "$or": bson.A{ - bson.M{"_id": e}, - bson.M{"$text": bson.M{"$search": e}}, - }, - } - default: - err = fmt.Errorf("invalid expression") + v, ok := c.compile(tree.Node).(bson.M) + if !ok || v == nil { + return nil, fmt.Errorf("invalid expression") } - return + return v, nil } type compiler struct { diff --git a/pkg/expr/mongo_test.go b/pkg/expr/mongo_test.go index 9fe237e393f0280a7f2f12534c1b214bbbc21c43..75ec627c124cc0a24768d3cec30a4eac0ae34c15 100644 --- a/pkg/expr/mongo_test.go +++ b/pkg/expr/mongo_test.go @@ -50,11 +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}, {"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}, - {"text search or id", "id", nil, bson.M{"$or": bson.A{bson.M{"_id": "id"}, bson.M{"$text": bson.M{"$search": "id"}}}}, false}, + {"text search or id", "id", nil, nil, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotB, err := ConvertToMongo(ctx, tt.eval, tt.env, nil) + if tt.wantErr { + require.Error(t, err) + return + } require.NoError(t, err) assert.Equal(t, tt.wantB, gotB) })