diff --git a/v2/types.go b/v2/types.go index d5814db..560f217 100644 --- a/v2/types.go +++ b/v2/types.go @@ -425,7 +425,29 @@ func (u *StringList) Remove(p ...string) { // All tag list methods lower case the strings in the arguments type TagList []string -// Contains returns true if the list contains the tags +// Equals return true if both TagLists have the same values (this is case-sensitive) +func (u *TagList) Equals(other *TagList) bool { + if len(*u) != len(*other) { + return false + } + for _, v := range *u { + if other.find(v) == -1 { + return false + } + } + return true +} + +func (u *TagList) find(p string) int { + for idx, t := range *u { + if p == t { + return idx + } + } + return -1 +} + +// Contains assumes tags are all lower-cased - use ContainsCaseSensitive func (u *TagList) Contains(p string) bool { p = strings.ToLower(strings.TrimSpace(p)) for _, t := range *u { @@ -436,7 +458,17 @@ func (u *TagList) Contains(p string) bool { return false } -// Add appends 1 or more tags to a list +func (u *TagList) ContainsCaseSensitive(p string) bool { + p = strings.TrimSpace(p) + for _, t := range *u { + if t == p { + return true + } + } + return false +} + +// Add assumes tags are all lower-cased - use AddCaseSensitive func (u *TagList) Add(p ...string) { for _, v := range p { v = strings.ToLower(strings.TrimSpace(v)) @@ -446,7 +478,16 @@ func (u *TagList) Add(p ...string) { } } -// Remove removes 1 or more tags from a list +func (u *TagList) AddCaseSensitive(p ...string) { + for _, v := range p { + v = strings.TrimSpace(v) + if !u.ContainsCaseSensitive(v) && v != "" { + *u = append(*u, v) + } + } +} + +// Remove assumes tags are all lower-cased - use RemoveCaseSensitive func (u *TagList) Remove(p ...string) { for _, v := range p { v = strings.ToLower(strings.TrimSpace(v)) @@ -460,6 +501,25 @@ func (u *TagList) Remove(p ...string) { } } +func (u *TagList) RemoveCaseSensitive(p ...string) error { + for _, v := range p { + found := false + v = strings.TrimSpace(v) + for i, t := range *u { + if t == v { + a := *u + *u = append(a[:i], a[i+1:]...) + found = true + break + } + } + if !found { + return fmt.Errorf("tag %q not found", v) + } + } + return nil +} + type CIDRList TagList func (c *CIDRList) Contains(p string) bool { diff --git a/v2/types_test.go b/v2/types_test.go index 34d1fa4..e147b42 100644 --- a/v2/types_test.go +++ b/v2/types_test.go @@ -132,6 +132,22 @@ func TestTagList(t *testing.T) { AssertEquals(false, tags.Contains("ONE"), t) } +func TestCaseSensitiveTagList(t *testing.T) { + tags := TagList{} + tags.AddCaseSensitive("One") + + AssertEquals(false, tags.ContainsCaseSensitive("one"), t) + AssertEquals(true, tags.ContainsCaseSensitive("One"), t) + AssertEquals("One", tags[0], t) + + tags.AddCaseSensitive("TWO") + err := tags.RemoveCaseSensitive("two") + if err == nil { + t.Fatal("expected to not find two") + } + AssertNoError(tags.RemoveCaseSensitive("TWO"), t) +} + func TestStringList(t *testing.T) { slist := StringList{}