Skip to content

Commit

Permalink
Add StrListSubsetGlob and RemoveGlobs funcs to strutil
Browse files Browse the repository at this point in the history
  • Loading branch information
nvx committed Sep 15, 2021
1 parent 8b83eb3 commit 1ec973e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
23 changes: 23 additions & 0 deletions strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func StrListSubset(super, sub []string) bool {
return true
}

// StrListSubsetGlob checks if a given list is a subset of
// another set, allowing for globs.
func StrListSubsetGlob(super, sub []string) bool {
for _, item := range sub {
if !StrListContainsGlob(super, item) {
return false
}
}
return true
}

// ParseDedupAndSortStrings parses a comma separated list of strings
// into a slice of strings. The return slice will be sorted and will
// not contain duplicate or empty items.
Expand Down Expand Up @@ -287,6 +298,18 @@ func RemoveEmpty(items []string) []string {
return itemsSlice
}

// RemoveGlobs removes any elements containing globs from a slice of strings.
func RemoveGlobs(items []string) []string {
ret := make([]string, 0, len(items))
for _, item := range items {
// glob.GLOB is "*"; ignore items containing that
if !strings.Contains(item, glob.GLOB) {
ret = append(ret, item)
}
}
return ret
}

// EquivalentSlices checks whether the given string sets are equivalent, as in,
// they contain the same values.
func EquivalentSlices(a, b []string) bool {
Expand Down
59 changes: 59 additions & 0 deletions strutil/strutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@ func TestStrutil_ListSubset(t *testing.T) {
}
}

func TestStrutil_ListSubsetGlob(t *testing.T) {
parent := []string{
"dev",
"ops*",
"root/*",
"*-dev",
"_*_",
}
if StrListSubsetGlob(parent, []string{"tubez", "dev", "root/admin"}) {
t.Fatalf("Bad")
}
if StrListSubsetGlob(parent, []string{"devops", "ops-dev"}) {
t.Fatalf("Bad")
}
if StrListSubsetGlob(nil, parent) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, []string{"root/test", "dev", "_test_"}) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, []string{"ops_test", "ops", "devops-dev"}) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, []string{"ops"}) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, []string{"test-dev"}) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, []string{"_test_"}) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, nil) {
t.Fatalf("Bad")
}
}

func TestStrutil_ParseKeyValues(t *testing.T) {
actual := make(map[string]string)
expected := map[string]string{
Expand Down Expand Up @@ -469,6 +506,28 @@ func TestStrUtil_RemoveDuplicatesStable(t *testing.T) {
}
}

func TestStrUtil_RemoveGlobs(t *testing.T) {
type tCase struct {
input []string
expect []string
}

tCases := []tCase{
tCase{[]string{}, []string{}},
tCase{[]string{"hello"}, []string{"hello"}},
tCase{[]string{"h*i"}, []string{}},
tCase{[]string{"one", "two*", "*three", "f*our", "five"}, []string{"one", "five"}},
}

for _, tc := range tCases {
actual := RemoveGlobs(tc.input)

if !reflect.DeepEqual(actual, tc.expect) {
t.Fatalf("Bad testcase %#v, expected %v, got %v", tc, tc.expect, actual)
}
}
}

func TestStrUtil_ParseStringSlice(t *testing.T) {
type tCase struct {
input string
Expand Down

0 comments on commit 1ec973e

Please sign in to comment.