Skip to content

Commit

Permalink
Add log when matches cannot parse the regex and make sure it fails fr…
Browse files Browse the repository at this point in the history
…om then onward.

Update test to confirm invalid regex behaviour.
  • Loading branch information
Kilemonn committed Dec 18, 2024
1 parent 3233712 commit 3ed0169
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion condition/condition-action/condition_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetMatchesInstance(id string, pattern string) MatchesConditionAction {
if entry, exists := matchesActionsMap[id]; exists {
return entry
} else {
matchesAction := NewMatchesConditionAction(pattern)
matchesAction := NewMatchesConditionAction(id, pattern)
matchesActionsMap[id] = matchesAction
return matchesAction
}
Expand Down
15 changes: 12 additions & 3 deletions condition/condition-action/matches_condition_action.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package condition_action

import "regexp"
import (
"fmt"
"regexp"
)

type MatchesConditionAction struct {
regex *regexp.Regexp
}

func NewMatchesConditionAction(pattern string) MatchesConditionAction {
regex := regexp.MustCompile(pattern)
func NewMatchesConditionAction(id string, pattern string) MatchesConditionAction {
regex, err := regexp.Compile(pattern)
if err != nil {
fmt.Printf("Failed to initialise [Matches] condition's pattern with ID [%s], pattern [%s] with error [%s]. All condition checks will fail.", id, pattern, err.Error())
}

return MatchesConditionAction{
regex: regex,
}
}

func (a MatchesConditionAction) CheckCondition(input string, args []string) bool {
if a.regex == nil {
return false
}
return a.regex.Match([]byte(input))
}
2 changes: 1 addition & 1 deletion condition/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestApplyCondition(t *testing.T) {
{"SomethingInvalid(arg1, arg2)", "invalid", true, []string{}, []string{"test test test", "1237532123", "true", "$!&@#($)"}},

{"Matches(\\d+)", "matches-regex", false, []string{"1234", "1", "testwith number 1"}, []string{"test", "no numbers++--"}},
{"Matches([)", "invalid-regex", false, []string{"test"}, []string{"?"}},
{"Matches([)", "invalid-regex", false, []string{}, []string{"everything should fail"}},
}

for _, c := range cases {
Expand Down

0 comments on commit 3ed0169

Please sign in to comment.