-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(filter): Add error handling for regex compilation #933
Merged
+32
−16
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,15 +129,19 @@ func ParseSeriesByTag(input string) ([]TagSpec, error) { | |
type MatchingHandler func(string, map[string]string) bool | ||
|
||
// CreateMatchingHandlerForPattern creates function for matching by tag list | ||
func CreateMatchingHandlerForPattern(tagSpecs []TagSpec, compatibility *Compatibility) (string, MatchingHandler) { | ||
func CreateMatchingHandlerForPattern(tagSpecs []TagSpec, compatibility *Compatibility) (string, MatchingHandler, error) { | ||
matchingHandlers := make([]MatchingHandler, 0) | ||
var nameTagValue string | ||
|
||
for _, tagSpec := range tagSpecs { | ||
if tagSpec.Name == "name" && tagSpec.Operator == EqualOperator { | ||
nameTagValue = tagSpec.Value | ||
} else { | ||
handler := createMatchingHandlerForOneTag(tagSpec, compatibility) | ||
handler, err := createMatchingHandlerForOneTag(tagSpec, compatibility) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
matchingHandlers = append(matchingHandlers, handler) | ||
} | ||
} | ||
|
@@ -151,10 +155,10 @@ func CreateMatchingHandlerForPattern(tagSpecs []TagSpec, compatibility *Compatib | |
return true | ||
} | ||
|
||
return nameTagValue, matchingHandler | ||
return nameTagValue, matchingHandler, nil | ||
} | ||
|
||
func createMatchingHandlerForOneTag(spec TagSpec, compatibility *Compatibility) MatchingHandler { | ||
func createMatchingHandlerForOneTag(spec TagSpec, compatibility *Compatibility) (MatchingHandler, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. На этой функции также тестов нет There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Аналогично комменту выше |
||
var matchingHandlerCondition func(string) bool | ||
allowMatchEmpty := false | ||
|
||
|
@@ -171,15 +175,21 @@ func createMatchingHandlerForOneTag(spec TagSpec, compatibility *Compatibility) | |
case MatchOperator: | ||
allowMatchEmpty = compatibility.AllowRegexMatchEmpty | ||
|
||
matchRegex := newMatchRegex(spec.Value, compatibility) | ||
matchRegex, err := newMatchRegex(spec.Value, compatibility) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
matchingHandlerCondition = func(value string) bool { | ||
return matchRegex.MatchString(value) | ||
} | ||
case NotMatchOperator: | ||
allowMatchEmpty = compatibility.AllowRegexMatchEmpty | ||
|
||
matchRegex := newMatchRegex(spec.Value, compatibility) | ||
matchRegex, err := newMatchRegex(spec.Value, compatibility) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
matchingHandlerCondition = func(value string) bool { | ||
return !matchRegex.MatchString(value) | ||
|
@@ -199,21 +209,19 @@ func createMatchingHandlerForOneTag(spec TagSpec, compatibility *Compatibility) | |
return matchingHandlerCondition(value) | ||
} | ||
return allowMatchEmpty && matchEmpty | ||
} | ||
}, nil | ||
} | ||
|
||
func newMatchRegex(value string, compatibility *Compatibility) *regexp.Regexp { | ||
var matchRegex *regexp.Regexp | ||
|
||
func newMatchRegex(value string, compatibility *Compatibility) (*regexp.Regexp, error) { | ||
if value == "*" { | ||
value = ".*" | ||
} | ||
|
||
if compatibility.AllowRegexLooseStartMatch { | ||
matchRegex = regexp.MustCompile(value) | ||
} else { | ||
matchRegex = regexp.MustCompile("^" + value) | ||
if !compatibility.AllowRegexLooseStartMatch { | ||
value = "^" + value | ||
} | ||
|
||
return matchRegex | ||
matchRegex, err := regexp.Compile(value) | ||
|
||
return matchRegex, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Может стоит добавить тесты на эту функцию? Я просто посмотрел, там на часть функций из файла
series_by_tag.go
есть тесты, а на другую часть нет, не обязательно прям в этом пр добавлять, но в целом они бы не помешали, я думаюThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Надо подумать, нужны ли тесты на эти функции или хватит тестов на самую выскокоуровневую. Тестируем-то логический модуль, а не каждую функцию по-отдельности. Но в этом ПР прям не хочется ещё тестов писать
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да, в этом пр давайте оставим пока так, это пока просто мысли на будущее :)