Merge branch 'regexp2' of github.com:thetuxkeeper/grafana-zabbix into thetuxkeeper-regexp2
This commit is contained in:
@@ -148,7 +148,7 @@ func filterItemsByTag(items []*Item, filter string) ([]*Item, error) {
|
||||
}
|
||||
for _, t := range tags {
|
||||
if re != nil {
|
||||
if re.MatchString(t) {
|
||||
if match, err := re.MatchString(t); match && err != nil {
|
||||
filteredItems = append(filteredItems, i)
|
||||
break
|
||||
}
|
||||
@@ -173,7 +173,7 @@ func filterItemsByQuery(items []*Item, filter string) ([]*Item, error) {
|
||||
for _, i := range items {
|
||||
name := i.Name
|
||||
if re != nil {
|
||||
if re.MatchString(name) {
|
||||
if match, err := re.MatchString(name); match && err != nil {
|
||||
filteredItems = append(filteredItems, i)
|
||||
}
|
||||
} else if name == filter {
|
||||
@@ -212,7 +212,7 @@ func filterAppsByQuery(items []Application, filter string) ([]Application, error
|
||||
for _, i := range items {
|
||||
name := i.Name
|
||||
if re != nil {
|
||||
if re.MatchString(name) {
|
||||
if match, err := re.MatchString(name); match && err != nil {
|
||||
filteredItems = append(filteredItems, i)
|
||||
}
|
||||
} else if name == filter {
|
||||
@@ -251,7 +251,7 @@ func filterHostsByQuery(items []Host, filter string) ([]Host, error) {
|
||||
for _, i := range items {
|
||||
name := i.Name
|
||||
if re != nil {
|
||||
if re.MatchString(name) {
|
||||
if match, err := re.MatchString(name); match && err != nil {
|
||||
filteredItems = append(filteredItems, i)
|
||||
}
|
||||
} else if name == filter {
|
||||
@@ -282,7 +282,7 @@ func filterGroupsByQuery(items []Group, filter string) ([]Group, error) {
|
||||
for _, i := range items {
|
||||
name := i.Name
|
||||
if re != nil {
|
||||
if re.MatchString(name) {
|
||||
if match, err := re.MatchString(name); match && err != nil {
|
||||
filteredItems = append(filteredItems, i)
|
||||
}
|
||||
} else if name == filter {
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/dlclark/regexp2"
|
||||
)
|
||||
|
||||
func (item *Item) ExpandItemName() string {
|
||||
@@ -63,9 +65,10 @@ func splitKeyParams(paramStr string) []string {
|
||||
return params
|
||||
}
|
||||
|
||||
func parseFilter(filter string) (*regexp.Regexp, error) {
|
||||
regex := regexp.MustCompile(`^/(.+)/([imsU]*)$`)
|
||||
flagRE := regexp.MustCompile("[imsU]+")
|
||||
func parseFilter(filter string) (*regexp2.Regexp, error) {
|
||||
vaildREModifiers := "imncsxrde"
|
||||
regex := regexp.MustCompile(`^/(.+)/(\w*)$`)
|
||||
flagRE := regexp.MustCompile("[" + vaildREModifiers + "]+")
|
||||
|
||||
matches := regex.FindStringSubmatch(filter)
|
||||
if len(matches) <= 1 {
|
||||
@@ -77,12 +80,12 @@ func parseFilter(filter string) (*regexp.Regexp, error) {
|
||||
if flagRE.MatchString(matches[2]) {
|
||||
pattern += "(?" + matches[2] + ")"
|
||||
} else {
|
||||
return nil, fmt.Errorf("error parsing regexp: unsupported flags `%s` (expected [imsU])", matches[2])
|
||||
return nil, fmt.Errorf("error parsing regexp: unsupported flags `%s` (expected [%s])", matches[2], vaildREModifiers)
|
||||
}
|
||||
}
|
||||
pattern += matches[1]
|
||||
|
||||
return regexp.Compile(pattern)
|
||||
return regexp2.Compile(pattern, regexp2.RE2)
|
||||
}
|
||||
|
||||
func itemTagToString(tag ItemTag) string {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package zabbix
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/dlclark/regexp2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -61,26 +63,43 @@ func TestParseFilter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
filter string
|
||||
want *regexp2.Regexp
|
||||
expectNoError bool
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
name: "Simple regexp",
|
||||
filter: "/.*/",
|
||||
want: regexp2.MustCompile(".*", regexp2.RE2),
|
||||
expectNoError: true,
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
name: "Not a regex",
|
||||
filter: "/var/lib/mysql: Total space",
|
||||
want: nil,
|
||||
expectNoError: true,
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
name: "Regexp with modifier",
|
||||
filter: "/.*/i",
|
||||
want: regexp2.MustCompile("(?i).*", regexp2.RE2),
|
||||
expectNoError: true,
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
name: "Regexp with unsupported modifier",
|
||||
filter: "/.*/1",
|
||||
want: nil,
|
||||
expectNoError: false,
|
||||
expectedError: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := parseFilter(tt.filter)
|
||||
got, err := parseFilter(tt.filter)
|
||||
if tt.expectNoError {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
@@ -88,6 +107,9 @@ func TestParseFilter(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
assert.EqualError(t, err, tt.expectedError)
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("parseFilter() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user