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
d12a6faf
Commit
d12a6faf
authored
1 year ago
by
Anton Sattarov
Browse files
Options
Downloads
Patches
Plain Diff
Добавлена функция проверки на то что строка является выражением для библиотеки expr в SDK
parent
636b0906
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
perxis-proto
+1
-1
1 addition, 1 deletion
perxis-proto
pkg/expr/expr.go
+23
-0
23 additions, 0 deletions
pkg/expr/expr.go
pkg/expr/expr_test.go
+51
-0
51 additions, 0 deletions
pkg/expr/expr_test.go
with
75 additions
and
1 deletion
perxis-proto
@
81c96784
Compare
6e55b78e
...
81c96784
Subproject commit
6e55b78ebc551eb6f3ee9be1b1c7464749091e68
Subproject commit
81c967842f55811b459e455572703631712d7d86
This diff is collapsed.
Click to expand it.
pkg/expr/expr.go
+
23
−
0
View file @
d12a6faf
package
expr
package
expr
import
(
import
(
"regexp"
"strings"
"git.perx.ru/perxis/perxis-go/pkg/data"
compiler2
"github.com/antonmedv/expr/compiler"
compiler2
"github.com/antonmedv/expr/compiler"
"github.com/antonmedv/expr/parser"
"github.com/antonmedv/expr/parser"
"github.com/antonmedv/expr/vm"
"github.com/antonmedv/expr/vm"
"golang.org/x/net/context"
"golang.org/x/net/context"
)
)
var
(
additionalFunctions
=
[]
string
{
"contains"
,
"startsWith"
,
"endsWith"
,
"and"
,
"or"
,
"in"
,
"not"
}
isExpression
=
regexp
.
MustCompile
(
`[()}{<>=|&%*+\-\/\]\[\\]`
)
.
MatchString
)
const
EnvContextKey
=
"$context"
const
EnvContextKey
=
"$context"
func
Eval
(
ctx
context
.
Context
,
input
string
,
env
map
[
string
]
interface
{})
(
interface
{},
error
)
{
func
Eval
(
ctx
context
.
Context
,
input
string
,
env
map
[
string
]
interface
{})
(
interface
{},
error
)
{
...
@@ -56,3 +65,17 @@ func EvalKV(ctx context.Context, input string, kv ...interface{}) (interface{},
...
@@ -56,3 +65,17 @@ func EvalKV(ctx context.Context, input string, kv ...interface{}) (interface{},
return
Eval
(
ctx
,
input
,
m
)
return
Eval
(
ctx
,
input
,
m
)
}
}
func
IsExpression
(
input
string
)
bool
{
if
isExpression
(
input
)
{
return
true
}
for
_
,
s
:=
range
strings
.
Fields
(
input
)
{
if
data
.
Contains
(
s
,
additionalFunctions
)
{
return
true
}
}
return
false
}
This diff is collapsed.
Click to expand it.
pkg/expr/expr_test.go
0 → 100644
+
51
−
0
View file @
d12a6faf
package
expr
import
(
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func
TestIsExpression
(
t
*
testing
.
T
)
{
now
:=
time
.
Now
()
tests
:=
[]
struct
{
name
string
eval
string
want
bool
}{
{
"equal"
,
"i == 3"
,
true
},
{
"in array"
,
"i in [1,2,3]"
,
true
},
{
"contains"
,
"value contains 'some'"
,
true
},
{
"contains with . + () $ {} ^"
,
"value contains 'something with . + () $ {} ^'"
,
true
},
{
"startsWith"
,
"value startsWith 'some'"
,
true
},
{
"startsWith . + () $ {} ^"
,
"value startsWith '. + () $ {} ^'"
,
true
},
{
"endsWith"
,
"value endsWith 'some'"
,
true
},
{
"endsWith . + () $ {} ^"
,
"value endsWith '. + () $ {} ^'"
,
true
},
{
"icontains"
,
"icontains(value, 'some')"
,
true
},
{
"icontains with . + () $ {} ^"
,
"icontains (value, 'something with . + () $ {} ^')"
,
true
},
{
"istartsWith"
,
"istartsWith(value, 'Some')"
,
true
},
{
"istartsWith . + () $ {} ^ . + () $ {} ^"
,
"istartsWith(value, '. + () $ {} ^')"
,
true
},
{
"iendsWith"
,
"iendsWith(value, 'some')"
,
true
},
{
"iendsWith . + () $ {} ^"
,
"iendsWith(value,'. + () $ {} ^')"
,
true
},
{
"or"
,
"i == 2 || i > 10"
,
true
},
{
"search"
,
"search('some') || i > 10"
,
true
},
{
"vars:or"
,
"i == a + 2 || i > a + 10"
,
true
},
{
"near"
,
"near(a, [55.5, 37.5], 1000)"
,
true
},
{
"within"
,
"within(a, 'box', [[54.54, 36.36], [55.55, 37.37]])"
,
true
},
{
"time"
,
"d > Time.Date('2021-08-31')"
,
true
},
{
"time"
,
fmt
.
Sprintf
(
"d > Time.Time('%s')"
,
now
.
Format
(
time
.
RFC3339
)),
true
},
{
"in"
,
"In(s, [1,2,3])"
,
true
},
{
"in"
,
"In(s, 1)"
,
true
},
{
"text search or id"
,
"id"
,
false
},
{
"numbers"
,
"3"
,
false
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
got
:=
IsExpression
(
tt
.
eval
)
assert
.
Equal
(
t
,
tt
.
want
,
got
)
})
}
}
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