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

fix ConvertToMongo

parent 397d7405
No related branches found
No related tags found
No related merge requests found
......@@ -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}},
},
v, ok := c.compile(tree.Node).(bson.M)
if !ok || v == nil {
return nil, fmt.Errorf("invalid expression")
}
default:
err = fmt.Errorf("invalid expression")
}
return
return v, nil
}
type compiler struct {
......
......@@ -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)
})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment