Skip to content

Commit

Permalink
Merge pull request #3 from Kilemonn/allow-multiple-instances-of-unique
Browse files Browse the repository at this point in the history
Allow multiple unique constraint instances
  • Loading branch information
Kilemonn authored Dec 18, 2024
2 parents ccc44ee + 781865c commit 5b98a1b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
12 changes: 11 additions & 1 deletion condition/condition-action/condition_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ 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
UniqueConditionActionObj UniqueConditionAction = NewUniqueConditionAction()
uniqueActionsMap map[string]UniqueConditionAction = make(map[string]UniqueConditionAction)
)

func GetUniqueInstance(id string) UniqueConditionAction {
if entry, exists := uniqueActionsMap[id]; exists {
return entry
} else {
uniqueAction := NewUniqueConditionAction()
uniqueActionsMap[id] = uniqueAction
return uniqueAction
}
}

type ConditionAction interface {
CheckCondition(input string, args []string) bool
}
4 changes: 2 additions & 2 deletions condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ func getArguments(arg string, expectedArgsCount uint) (args []string, err error)
return
}

func (c Condition) ApplyCondition(input string) bool {
return c.Type.getConditionAction().CheckCondition(input, c.Args)
func (c Condition) ApplyCondition(id string, input string) bool {
return c.Type.getConditionAction(id).CheckCondition(input, c.Args)
}
22 changes: 14 additions & 8 deletions condition/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,24 @@ func TestNewCondition(t *testing.T) {
func TestApplyCondition(t *testing.T) {
var cases = []struct {
conditionString string
id string
expectError bool
successInputs []string
failInputs []string
}{
{"IsNumeric", false, []string{"-19", "40", "9999999999999"}, []string{"wow", "-", "!@!#$!"}},
{"IsBoolean", false, []string{"true", "false"}, []string{"yes", "-", "222159", "!@!#$!"}},
{"Unique", false, []string{"unique1", "unique2", "unique3"}, []string{"unique1", "unique2", "unique3"}},
{"HasPrefix(test-)", false, []string{"test-scenario1", "test-case1", "test-please"}, []string{"not-test-", "ttest-failed"}},
{"HasSuffix(-test)", false, []string{"some-test", "another-test"}, []string{"not-test-", "failing-tests"}},
{"IsNumeric", "is-numeric", false, []string{"-19", "40", "9999999999999"}, []string{"wow", "-", "!@!#$!"}},
{"IsBoolean", "is-boolean", false, []string{"true", "false"}, []string{"yes", "-", "222159", "!@!#$!"}},
{"Unique", "is-unique1", false, []string{"unique1", "unique2", "unique3"}, []string{"unique1", "unique2", "unique3"}},

// Duplicate the unique entry with the same ID to make sure that the same unique instance is used
{"Unique", "is-unique1", false, []string{"unique4", "unique5", "unique6"}, []string{"unique1", "unique2", "unique3", "unique4", "unique5", "unique6"}},
// Using a different ID should give us a different validator and not collide with the other unique instances
{"Unique", "is-unique2", false, []string{"unique1", "unique2", "unique3"}, []string{"unique1", "unique2", "unique3"}},
{"HasPrefix(test-)", "has-prefix", false, []string{"test-scenario1", "test-case1", "test-please"}, []string{"not-test-", "ttest-failed"}},
{"HasSuffix(-test)", "has-suffix", false, []string{"some-test", "another-test"}, []string{"not-test-", "failing-tests"}},

// Make sure an invalid constraint is forced to validate false to everything
{"SomethingInvalid(arg1, arg2)", true, []string{}, []string{"test test test", "1237532123", "true", "$!&@#($)"}},
{"SomethingInvalid(arg1, arg2)", "invalid", true, []string{}, []string{"test test test", "1237532123", "true", "$!&@#($)"}},
}

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

for _, successTest := range c.successInputs {
require.True(t, condition.ApplyCondition(successTest))
require.True(t, condition.ApplyCondition(c.id, successTest))
}
for _, failTest := range c.failInputs {
require.False(t, condition.ApplyCondition(failTest))
require.False(t, condition.ApplyCondition(c.id, failTest))
}
}
}
8 changes: 4 additions & 4 deletions condition/condition_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ func (c ConditionType) expectedArgsCount() uint {
return args[uint(c)]
}

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

func (c ConditionType) getConditionAction() condition_action.ConditionAction {
return c.conditionActions()[uint(c)]
func (c ConditionType) getConditionAction(id string) condition_action.ConditionAction {
return c.conditionActions(id)[uint(c)]
}
2 changes: 1 addition & 1 deletion validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ExecuteConstraintsAgainstProviders(providers []credential_provider.Credenti
if debugLog {
fmt.Printf("Credential [%s] matched pattern for constraint [%s], applying condition...\n", credentialName, constraint.Name)
}
if !constraint.Condition.ApplyCondition(credential) {
if !constraint.Condition.ApplyCondition(constraint.Name, credential) {
if debugLog {
fmt.Printf("Fail - Provider [%s], Constraint [%s], Credential [%s].\n", provider.Identifier.String(), constraint.Name, credentialName)
}
Expand Down

0 comments on commit 5b98a1b

Please sign in to comment.