Skip to content

Commit

Permalink
Merge pull request #4 from Kilemonn/add-matches-condition
Browse files Browse the repository at this point in the history
Add matches condition
  • Loading branch information
Kilemonn authored Dec 18, 2024
2 parents 5b98a1b + 3ed0169 commit f75794f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
18 changes: 14 additions & 4 deletions condition/condition-action/condition_action.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package condition_action

var (
// TODO: Create a new instance for each different constraint definition that uses Unique
// Need to create and hold this variable, since it's state needs to be retained
uniqueActionsMap map[string]UniqueConditionAction = make(map[string]UniqueConditionAction)

matchesActionsMap map[string]MatchesConditionAction = make(map[string]MatchesConditionAction)
)

type ConditionAction interface {
CheckCondition(input string, args []string) bool
}

func GetUniqueInstance(id string) UniqueConditionAction {
if entry, exists := uniqueActionsMap[id]; exists {
return entry
Expand All @@ -16,6 +20,12 @@ func GetUniqueInstance(id string) UniqueConditionAction {
}
}

type ConditionAction interface {
CheckCondition(input string, args []string) bool
func GetMatchesInstance(id string, pattern string) MatchesConditionAction {
if entry, exists := matchesActionsMap[id]; exists {
return entry
} else {
matchesAction := NewMatchesConditionAction(id, pattern)
matchesActionsMap[id] = matchesAction
return matchesAction
}
}
28 changes: 28 additions & 0 deletions condition/condition-action/matches_condition_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package condition_action

import (
"fmt"
"regexp"
)

type MatchesConditionAction struct {
regex *regexp.Regexp
}

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))
}
8 changes: 7 additions & 1 deletion condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,11 @@ func getArguments(arg string, expectedArgsCount uint) (args []string, err error)
}

func (c Condition) ApplyCondition(id string, input string) bool {
return c.Type.getConditionAction(id).CheckCondition(input, c.Args)
// TODO: Make this nicer, we are only passing in this arg for the matches condition so that we only need to compile
// the regex once on initialisation
arg := ""
if len(c.Args) > 0 {
arg = c.Args[0]
}
return c.Type.getConditionAction(id, arg).CheckCondition(input, c.Args)
}
7 changes: 5 additions & 2 deletions condition/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func TestApplyCondition(t *testing.T) {

// Make sure an invalid constraint is forced to validate false to everything
{"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{}, []string{"everything should fail"}},
}

for _, c := range cases {
Expand All @@ -102,10 +105,10 @@ func TestApplyCondition(t *testing.T) {
}

for _, successTest := range c.successInputs {
require.True(t, condition.ApplyCondition(c.id, successTest))
require.True(t, condition.ApplyCondition(c.id, successTest), c.id+" - "+successTest)
}
for _, failTest := range c.failInputs {
require.False(t, condition.ApplyCondition(c.id, failTest))
require.False(t, condition.ApplyCondition(c.id, failTest), c.id+" - "+failTest)
}
}
}
12 changes: 7 additions & 5 deletions condition/condition_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
ConditionTypeHasSuffix ConditionType = iota
ConditionTypeIsNumeric ConditionType = iota
ConditionTypeIsBoolean ConditionType = iota
ConditionTypeMatches ConditionType = iota
)

func ConditionTypeFromString(input string) ConditionType {
Expand All @@ -28,29 +29,30 @@ func ConditionTypeFromString(input string) ConditionType {
}

func conditionTypeStrings() []string {
return []string{"invalid", "unique", "hasprefix", "hassuffix", "isnumeric", "isboolean"}
return []string{"invalid", "unique", "hasprefix", "hassuffix", "isnumeric", "isboolean", "matches"}
}

func (c ConditionType) IsValid() bool {
return c != ConditionTypeInvalid
}

func (c ConditionType) expectedArgsCount() uint {
args := []uint{0, 0, 1, 1, 0, 0}
args := []uint{0, 0, 1, 1, 0, 0, 1}
return args[uint(c)]
}

func (c ConditionType) conditionActions(id string) []condition_action.ConditionAction {
func (c ConditionType) conditionActions(id string, pattern string) []condition_action.ConditionAction {
return []condition_action.ConditionAction{
condition_action.InvalidConditionAction{},
condition_action.GetUniqueInstance(id),
condition_action.HasPrefixConditionAction{},
condition_action.HasSuffixConditionAction{},
condition_action.IsNumericConditionAction{},
condition_action.IsBooleanConditionAction{},
condition_action.GetMatchesInstance(id, pattern),
}
}

func (c ConditionType) getConditionAction(id string) condition_action.ConditionAction {
return c.conditionActions(id)[uint(c)]
func (c ConditionType) getConditionAction(id string, pattern string) condition_action.ConditionAction {
return c.conditionActions(id, pattern)[uint(c)]
}

0 comments on commit f75794f

Please sign in to comment.