diff --git a/pkg/util/set.go b/pkg/util/set.go index 2b0db6002c5..98d57cd30b8 100644 --- a/pkg/util/set.go +++ b/pkg/util/set.go @@ -21,6 +21,13 @@ type empty struct{} // A set of strings, implemented via map[string]struct{} for minimal memory consumption. type StringSet map[string]empty +// NewStringSet creates a StringSet from a list of values. +func NewStringSet(items ...string) StringSet { + ss := StringSet{} + ss.Insert(items...) + return ss +} + // Insert adds items to the set. func (s StringSet) Insert(items ...string) { for _, item := range items { diff --git a/pkg/util/set_test.go b/pkg/util/set_test.go index cc4323ac802..2e48ed09196 100644 --- a/pkg/util/set_test.go +++ b/pkg/util/set_test.go @@ -22,7 +22,13 @@ import ( func TestStringSet(t *testing.T) { s := StringSet{} + if len(s) != 0 { + t.Errorf("Expected len=0: %d", len(s)) + } s.Insert("a", "b") + if len(s) != 2 { + t.Errorf("Expected len=2: %d", len(s)) + } s.Insert("c") if s.Has("d") { t.Errorf("Unexpected contents: %#v", s) @@ -35,3 +41,13 @@ func TestStringSet(t *testing.T) { t.Errorf("Unexpected contents: %#v", s) } } + +func TestNewStringSet(t *testing.T) { + s := NewStringSet("a", "b", "c") + if len(s) != 3 { + t.Errorf("Expected len=3: %d", len(s)) + } + if !s.Has("a") || !s.Has("b") || !s.Has("c") { + t.Errorf("Unexpected contents: %#v", s) + } +}