Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
23d638b9
Commit
23d638b9
authored
1 year ago
by
Danis Kirasirov
Browse files
Options
Downloads
Patches
Plain Diff
Реализована функция len(arr) для <, >, ==
parent
05fa795f
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
pkg/expr/mongo.go
+39
-1
39 additions, 1 deletion
pkg/expr/mongo.go
pkg/expr/mongo_test.go
+5
-0
5 additions, 0 deletions
pkg/expr/mongo_test.go
with
44 additions
and
1 deletion
pkg/expr/mongo.go
+
39
−
1
View file @
23d638b9
...
...
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/expr-lang/expr"
...
...
@@ -232,6 +233,17 @@ func (c *compiler) identifier(node ast.Node) string {
func
(
c
*
compiler
)
BinaryNode
(
node
*
ast
.
BinaryNode
)
interface
{}
{
switch
node
.
Operator
{
case
"=="
:
op
:=
c
.
eval
(
node
.
Right
)
lenNode
,
ok
:=
node
.
Left
.
(
*
ast
.
BuiltinNode
)
if
ok
&&
lenNode
.
Name
==
"len"
&&
len
(
lenNode
.
Arguments
)
==
1
{
// Оптимизация для случая len(arr) == 0, т.к. $size не использует индекс
if
op
==
0
{
return
bson
.
M
{
c
.
identifier
(
lenNode
.
Arguments
[
0
])
:
bson
.
M
{
"$eq"
:
bson
.
A
{}}}
}
return
bson
.
M
{
c
.
identifier
(
lenNode
.
Arguments
[
0
])
:
bson
.
M
{
"$size"
:
op
}}
}
return
bson
.
M
{
c
.
identifier
(
node
.
Left
)
:
c
.
eval
(
node
.
Right
)}
case
"!="
:
...
...
@@ -250,10 +262,36 @@ func (c *compiler) BinaryNode(node *ast.BinaryNode) interface{} {
return
bson
.
M
{
c
.
identifier
(
node
.
Left
)
:
bson
.
M
{
"$nin"
:
c
.
eval
(
node
.
Right
)}}
case
"<"
:
op
:=
c
.
eval
(
node
.
Right
)
lenNode
,
ok
:=
node
.
Left
.
(
*
ast
.
BuiltinNode
)
if
ok
&&
lenNode
.
Name
==
"len"
&&
len
(
lenNode
.
Arguments
)
==
1
{
length
,
ok
:=
op
.
(
int
)
if
!
ok
{
panic
(
"len must be compared with an integer"
)
}
return
bson
.
M
{
c
.
identifier
(
lenNode
.
Arguments
[
0
])
+
"."
+
strconv
.
Itoa
(
length
)
:
bson
.
M
{
"$exists"
:
false
}}
}
return
bson
.
M
{
c
.
identifier
(
node
.
Left
)
:
bson
.
M
{
"$lt"
:
c
.
eval
(
node
.
Right
)}}
case
">"
:
return
bson
.
M
{
c
.
identifier
(
node
.
Left
)
:
bson
.
M
{
"$gt"
:
c
.
eval
(
node
.
Right
)}}
op
:=
c
.
eval
(
node
.
Right
)
lenNode
,
ok
:=
node
.
Left
.
(
*
ast
.
BuiltinNode
)
if
ok
&&
lenNode
.
Name
==
"len"
&&
len
(
lenNode
.
Arguments
)
==
1
{
length
,
ok
:=
op
.
(
int
)
if
!
ok
{
panic
(
"len must be compared with an integer"
)
}
// Оптимизация для случая len(arr) > 0
if
length
==
0
{
return
bson
.
M
{
c
.
identifier
(
lenNode
.
Arguments
[
0
])
:
bson
.
M
{
"$exists"
:
true
,
"$type"
:
"array"
,
"$ne"
:
bson
.
A
{}}}
}
return
bson
.
M
{
c
.
identifier
(
lenNode
.
Arguments
[
0
])
+
"."
+
strconv
.
Itoa
(
length
)
:
bson
.
M
{
"$exists"
:
true
}}
}
return
bson
.
M
{
c
.
identifier
(
node
.
Left
)
:
bson
.
M
{
"$gt"
:
op
}}
case
"<="
:
return
bson
.
M
{
c
.
identifier
(
node
.
Left
)
:
bson
.
M
{
"$lte"
:
c
.
eval
(
node
.
Right
)}}
...
...
This diff is collapsed.
Click to expand it.
pkg/expr/mongo_test.go
+
5
−
0
View file @
23d638b9
...
...
@@ -31,6 +31,11 @@ func TestConvertToMongo(t *testing.T) {
{
"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
},
{
"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
},
{
"len equal"
,
"len(s) == 1"
,
nil
,
bson
.
M
{
"s"
:
bson
.
M
{
"$size"
:
1
}},
false
},
{
"len equal empty"
,
"len(s) == 0"
,
nil
,
bson
.
M
{
"s"
:
bson
.
M
{
"$eq"
:
bson
.
A
{}}},
false
},
{
"len gt"
,
"len(s) > 1"
,
nil
,
bson
.
M
{
"s.1"
:
bson
.
M
{
"$exists"
:
true
}},
false
},
{
"len gt 0"
,
"len(s) > 0"
,
nil
,
bson
.
M
{
"s"
:
bson
.
M
{
"$exists"
:
true
,
"$type"
:
"array"
,
"$ne"
:
bson
.
A
{}}},
false
},
{
"len lt"
,
"len(s) < 1"
,
nil
,
bson
.
M
{
"s.1"
:
bson
.
M
{
"$exists"
:
false
}},
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#3"
,
"s[test] > 3"
,
nil
,
bson
.
M
{
"s.test"
:
bson
.
M
{
"$gt"
:
3
}},
false
},
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment