Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connectors: refactor filter code into a helper package
I hope I didn't miss any :D Signed-off-by: Stephan Renatus <srenatus@chef.io>
- Loading branch information
Showing
6 changed files
with
55 additions
and
66 deletions.
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Package groups contains helper functions related to groups | ||
package groups | ||
|
||
// Filter filters out any groups of given that are not in required. Thus it may | ||
// happen that the resulting slice is empty. | ||
func Filter(given, required []string) []string { | ||
groups := []string{} | ||
groupFilter := make(map[string]struct{}) | ||
for _, group := range required { | ||
groupFilter[group] = struct{}{} | ||
} | ||
for _, group := range given { | ||
if _, ok := groupFilter[group]; ok { | ||
groups = append(groups, group) | ||
} | ||
} | ||
return groups | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package groups_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/dexidp/dex/pkg/groups" | ||
) | ||
|
||
func TestFilter(t *testing.T) { | ||
cases := map[string]struct { | ||
given, required, expected []string | ||
}{ | ||
"nothing given": {given: []string{}, required: []string{"ops"}, expected: []string{}}, | ||
"exactly one match": {given: []string{"foo"}, required: []string{"foo"}, expected: []string{"foo"}}, | ||
"no group of the required ones": {given: []string{"foo", "bar"}, required: []string{"baz"}, expected: []string{}}, | ||
"subset matching": {given: []string{"foo", "bar", "baz"}, required: []string{"bar", "baz"}, expected: []string{"bar", "baz"}}, | ||
} | ||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
actual := groups.Filter(tc.given, tc.required) | ||
assert.ElementsMatch(t, tc.expected, actual) | ||
}) | ||
} | ||
} |