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
e44f39ce
"git@git.perx.ru:perxis/perxis-go.git" did not exist on "cbe7886e3d90f6616cc97a7279d609144ff07534"
Commit
e44f39ce
authored
1 year ago
by
Danis Kirasirov
Browse files
Options
Downloads
Patches
Plain Diff
Изменена функция try
parent
448d31e2
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
pkg/expr/mongo.go
+8
-14
8 additions, 14 deletions
pkg/expr/mongo.go
pkg/expr/mongo_test.go
+4
-4
4 additions, 4 deletions
pkg/expr/mongo_test.go
with
12 additions
and
18 deletions
pkg/expr/mongo.go
+
8
−
14
View file @
e44f39ce
...
@@ -713,7 +713,7 @@ func (c *compiler) handleLenNode(node *ast.BinaryNode) bson.M {
...
@@ -713,7 +713,7 @@ func (c *compiler) handleLenNode(node *ast.BinaryNode) bson.M {
}
}
}
}
// handleTryNode получает узел AST с двумя выражениями
-строками
и пытается выполнить первое
// handleTryNode получает узел AST с двумя выражениями и пытается выполнить первое
// в случае успеха - возвращает его
// в случае успеха - возвращает его
// в случае ошибки - пытается выполнить второе и вернуть его результат
// в случае ошибки - пытается выполнить второе и вернуть его результат
func
(
c
*
compiler
)
handleTryNode
(
node
*
ast
.
CallNode
)
(
result
interface
{})
{
func
(
c
*
compiler
)
handleTryNode
(
node
*
ast
.
CallNode
)
(
result
interface
{})
{
...
@@ -721,23 +721,17 @@ func (c *compiler) handleTryNode(node *ast.CallNode) (result interface{}) {
...
@@ -721,23 +721,17 @@ func (c *compiler) handleTryNode(node *ast.CallNode) (result interface{}) {
panic
(
"try() expects exactly 2 arguments"
)
panic
(
"try() expects exactly 2 arguments"
)
}
}
subcompiler
:=
&
compiler
{
env
:
c
.
env
,
config
:
c
.
config
,
identifierRenameFn
:
c
.
identifierRenameFn
}
subexprCompile
:=
func
(
subexpr
string
)
interface
{}
{
tree
,
err
:=
parser
.
Parse
(
subexpr
)
if
err
!=
nil
{
panic
(
err
)
}
subcompiler
.
tree
=
tree
return
subcompiler
.
compile
(
tree
.
Node
)
}
defer
func
()
{
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
if
r
:=
recover
();
r
!=
nil
{
result
=
subexprC
ompile
(
node
.
Arguments
[
1
]
.
(
*
ast
.
StringNode
)
.
Value
)
result
=
c
.
c
ompile
(
node
.
Arguments
[
1
])
}
}
}()
}()
result
=
subexprCompile
(
node
.
Arguments
[
0
]
.
(
*
ast
.
StringNode
)
.
Value
)
.
(
bson
.
M
)
tree
,
err
:=
parser
.
Parse
(
node
.
Arguments
[
0
]
.
(
*
ast
.
StringNode
)
.
Value
)
if
err
!=
nil
{
panic
(
err
)
}
subcompiler
:=
&
compiler
{
tree
:
tree
,
env
:
c
.
env
,
config
:
c
.
config
,
identifierRenameFn
:
c
.
identifierRenameFn
}
result
=
subcompiler
.
compile
(
tree
.
Node
)
.
(
bson
.
M
)
return
return
}
}
This diff is collapsed.
Click to expand it.
pkg/expr/mongo_test.go
+
4
−
4
View file @
e44f39ce
...
@@ -79,10 +79,10 @@ func TestConvertToMongo(t *testing.T) {
...
@@ -79,10 +79,10 @@ func TestConvertToMongo(t *testing.T) {
{
"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
},
{
"text search or id"
,
"id"
,
nil
,
nil
,
true
},
{
"struct env"
,
"db_item.id == env_item.id"
,
map
[
string
]
interface
{}{
"env_item"
:
&
testEnvStruct
{
ID
:
"id1"
}},
bson
.
M
{
"db_item.id"
:
"id1"
},
false
},
{
"struct env"
,
"db_item.id == env_item.id"
,
map
[
string
]
interface
{}{
"env_item"
:
&
testEnvStruct
{
ID
:
"id1"
}},
bson
.
M
{
"db_item.id"
:
"id1"
},
false
},
{
"try#1"
,
"try('s == 1',
'
s == 2
'
)"
,
nil
,
bson
.
M
{
"s"
:
1
},
false
},
{
"try#1"
,
"try('s == 1', s == 2)"
,
nil
,
bson
.
M
{
"s"
:
1
},
false
},
{
"try#2"
,
"try('some-slug',
'
search(
\\
'some
\\
') || _id contains
\\
'some-slug
\\
'
')"
,
nil
,
bson
.
M
{
"$or"
:
bson
.
A
{
bson
.
M
{
"$text"
:
bson
.
M
{
"$search"
:
"some"
}},
bson
.
M
{
"_id"
:
bson
.
M
{
"$regex"
:
"some-slug"
}}}},
false
},
{
"try#2"
,
"try('some-slug', search('some
-slug
') || _id contains 'some-slug')"
,
nil
,
bson
.
M
{
"$or"
:
bson
.
A
{
bson
.
M
{
"$text"
:
bson
.
M
{
"$search"
:
"some
-slug
"
}},
bson
.
M
{
"_id"
:
bson
.
M
{
"$regex"
:
"some-slug"
}}}},
false
},
{
"try#3"
,
"try(bad-expr, bad-expr)"
,
nil
,
nil
,
true
},
{
"try#3"
,
"try(
'
bad-expr
'
, bad-expr)"
,
nil
,
nil
,
true
},
{
"try#4"
,
"try('3',
'
s == 1
'
)"
,
nil
,
bson
.
M
{
"s"
:
1
},
false
},
{
"try#4"
,
"try('3', s == 1)"
,
nil
,
bson
.
M
{
"s"
:
1
},
false
},
}
}
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
...
...
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