Skip to content
Snippets Groups Projects
Commit 05fa795f authored by Danis Kirasirov's avatar Danis Kirasirov
Browse files

Реализована функция exists

parent d0f74a1a
No related branches found
No related tags found
No related merge requests found
...@@ -39,6 +39,7 @@ func TestIsExpression(t *testing.T) { ...@@ -39,6 +39,7 @@ func TestIsExpression(t *testing.T) {
{"time", fmt.Sprintf("d > Time.Time('%s')", now.Format(time.RFC3339)), true}, {"time", fmt.Sprintf("d > Time.Time('%s')", now.Format(time.RFC3339)), true},
{"in", "In(s, [1,2,3])", true}, {"in", "In(s, [1,2,3])", true},
{"in", "In(s, 1)", true}, {"in", "In(s, 1)", true},
{"exists", "exists(s)", true},
{"text search or id", "id", false}, {"text search or id", "id", false},
{"numbers", "3", false}, {"numbers", "3", false},
} }
......
...@@ -404,6 +404,15 @@ func (c *compiler) CallNode(node *ast.CallNode) interface{} { ...@@ -404,6 +404,15 @@ func (c *compiler) CallNode(node *ast.CallNode) interface{} {
} }
return bson.M{fields: bson.M{"$in": array}} return bson.M{fields: bson.M{"$in": array}}
case "exists":
field := c.identifier(node.Arguments[0])
if field == "" {
panic("incorrect argument, empty field name")
}
return bson.M{"$or": bson.A{
bson.M{field: bson.M{"$exists": true, "$type": "array"}},
bson.M{field: bson.M{"$ne": nil}},
}}
case "icontains": case "icontains":
v := c.identifier(node.Arguments[0]) v := c.identifier(node.Arguments[0])
......
...@@ -30,6 +30,7 @@ func TestConvertToMongo(t *testing.T) { ...@@ -30,6 +30,7 @@ func TestConvertToMongo(t *testing.T) {
{"equal", "s == 3", nil, bson.M{"s": 3}, false}, {"equal", "s == 3", nil, bson.M{"s": 3}, false},
{"in array", "s in [1,2,3]", nil, bson.M{"s": bson.M{"$in": []interface{}{1, 2, 3}}}, false}, {"in array", "s in [1,2,3]", nil, bson.M{"s": bson.M{"$in": []interface{}{1, 2, 3}}}, false},
{"not in array", "s not in [1,2,3]", nil, bson.M{"s": bson.M{"$nin": []interface{}{1, 2, 3}}}, false}, {"not in array", "s not in [1,2,3]", nil, bson.M{"s": bson.M{"$nin": []interface{}{1, 2, 3}}}, false},
{"exists", "exists(s)", nil, bson.M{"$or": bson.A{bson.M{"s": bson.M{"$exists": true, "$type": "array"}}, bson.M{"s": bson.M{"$ne": nil}}}}, false},
{"field#1", "s.test > 3", nil, bson.M{"s.test": bson.M{"$gt": 3}}, false}, {"field#1", "s.test > 3", nil, bson.M{"s.test": bson.M{"$gt": 3}}, false},
{"field#2", "s['test'] > 3", nil, bson.M{"s.test": bson.M{"$gt": 3}}, false}, {"field#2", "s['test'] > 3", nil, bson.M{"s.test": bson.M{"$gt": 3}}, false},
{"field#3", "s[test] > 3", nil, bson.M{"s.test": bson.M{"$gt": 3}}, false}, {"field#3", "s[test] > 3", nil, bson.M{"s.test": bson.M{"$gt": 3}}, false},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment