Skip to content

Commit

Permalink
Added case-sensitive variants to Add, Remove, Contains to TagList
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Sep 16, 2024
1 parent 2d9ece2 commit b3c9c5b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
66 changes: 63 additions & 3 deletions v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions v2/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down

0 comments on commit b3c9c5b

Please sign in to comment.