Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added case-sensitive variants to Add, Remove, Contains to TagList #227

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading