Skip to content

Commit

Permalink
fix match data nil panic.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxin1248 committed Aug 31, 2022
1 parent 5dc19dd commit 4ba37c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions gparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import (

// Match 利用原生parser完成表达式与输入数据匹配任务
func Match(expr string, data map[string]interface{}) (bool, error) {
// 空表达式默认匹配成功
if expr == "" {
return true, nil
}
// 空数据默认匹配失败
if data == nil {
return false, nil
}
// 解析表达式
parseExpr, err := parser.ParseExpr(expr)
if err != nil {
Expand Down
16 changes: 14 additions & 2 deletions gparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestGoParser_Match(t *testing.T) {
want: false,
},
{
name: "test_case6",
name: "test_case7",
expr: "!(a == 1 && b == 2 && c == \"test\" && d == false)",
data: map[string]interface{}{
"a": 1,
Expand All @@ -84,7 +84,7 @@ func TestGoParser_Match(t *testing.T) {
want: true,
},
{
name: "test_case7",
name: "test_case8",
expr: "!(a == 1 && b == 2) || (c == \"test\" && d == false)",
data: map[string]interface{}{
"a": 1,
Expand All @@ -94,6 +94,18 @@ func TestGoParser_Match(t *testing.T) {
},
want: true,
},
{
name: "test_case9",
expr: "a == 1 && b == 2",
data: nil,
want: false,
},
{
name: "test_case10",
expr: "",
data: nil,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 4ba37c5

Please sign in to comment.