Skip to content

Commit

Permalink
[CHANGE] Tags are now case-sensitive (#225)
Browse files Browse the repository at this point in the history
* [CHANGE] Tags (--tag and --rm-tag) are now case-sensitive. Previous versions of the library lowercased all tags. Now tags such as "A" and "a" are different.
  • Loading branch information
aricart authored Sep 17, 2024
1 parent 2d9ece2 commit de1f16b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 30 deletions.
5 changes: 1 addition & 4 deletions v2/operator_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,9 @@ func TestTags(t *testing.T) {
if len(oc2.GenericFields.Tags) != 3 {
t.Fatal("expected 3 tags")
}
for _, v := range oc.GenericFields.Tags {
AssertFalse(v == "TWO", t)
}

AssertTrue(oc.GenericFields.Tags.Contains("one"), t)
AssertTrue(oc.GenericFields.Tags.Contains("two"), t)
AssertTrue(oc.GenericFields.Tags.Contains("TWO"), t)
AssertTrue(oc.GenericFields.Tags.Contains("three"), t)
}

Expand Down
74 changes: 55 additions & 19 deletions v2/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 The NATS Authors
* Copyright 2018-2024 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -427,20 +427,38 @@ type TagList []string

// Contains returns true if the list contains the tags
func (u *TagList) Contains(p string) bool {
p = strings.ToLower(strings.TrimSpace(p))
for _, t := range *u {
if t == p {
return true
return u.find(p) != -1
}

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 false
return true
}

func (u *TagList) find(p string) int {
for idx, t := range *u {
if p == t {
return idx
}
}
return -1
}

// Add appends 1 or more tags to a list
func (u *TagList) Add(p ...string) {
for _, v := range p {
v = strings.ToLower(strings.TrimSpace(v))
if !u.Contains(v) && v != "" {
v = strings.TrimSpace(v)
if v == "" {
continue
}
if !u.Contains(v) {
*u = append(*u, v)
}
}
Expand All @@ -449,29 +467,47 @@ func (u *TagList) Add(p ...string) {
// Remove removes 1 or more tags from a list
func (u *TagList) Remove(p ...string) {
for _, v := range p {
v = strings.ToLower(strings.TrimSpace(v))
for i, t := range *u {
if t == v {
a := *u
*u = append(a[:i], a[i+1:]...)
break
}
v = strings.TrimSpace(v)
idx := u.find(v)
if idx != -1 {
a := *u
*u = append(a[:idx], a[idx+1:]...)
}
}
}

type CIDRList TagList
type CIDRList []string

func (c *CIDRList) Contains(p string) bool {
return (*TagList)(c).Contains(p)
p = strings.ToLower(strings.TrimSpace(p))
for _, t := range *c {
if t == p {
return true
}
}
return false
}

func (c *CIDRList) Add(p ...string) {
(*TagList)(c).Add(p...)
for _, v := range p {
v = strings.ToLower(strings.TrimSpace(v))
if !c.Contains(v) && v != "" {
*c = append(*c, v)
}
}
}

func (c *CIDRList) Remove(p ...string) {
(*TagList)(c).Remove(p...)
for _, v := range p {
v = strings.ToLower(strings.TrimSpace(v))
for i, t := range *c {
if t == v {
a := *c
*c = append(a[:i], a[i+1:]...)
break
}
}
}
}

func (c *CIDRList) Set(values string) {
Expand Down
83 changes: 76 additions & 7 deletions v2/types_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 The NATS Authors
* Copyright 2018-2024 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -117,19 +117,18 @@ func TestTagList(t *testing.T) {
tags.Add("one")

AssertEquals(true, tags.Contains("one"), t)
AssertEquals(true, tags.Contains("ONE"), t)
AssertEquals(false, tags.Contains("ONE"), t)
AssertEquals("one", tags[0], t)

tags.Add("TWO")

AssertEquals(true, tags.Contains("two"), t)
AssertEquals(false, tags.Contains("two"), t)
AssertEquals(true, tags.Contains("TWO"), t)
AssertEquals("two", tags[1], t)
AssertEquals("TWO", tags[1], t)

tags.Remove("ONE")
AssertEquals("two", tags[0], t)
AssertEquals(false, tags.Contains("one"), t)
AssertEquals(false, tags.Contains("ONE"), t)
AssertEquals("one", tags[0], t)
AssertEquals(true, tags.Contains("TWO"), t)
}

func TestStringList(t *testing.T) {
Expand Down Expand Up @@ -427,3 +426,73 @@ func TestInvalidInfo(t *testing.T) {
}
}
}

func TestTagList_CasePreservingContains(t *testing.T) {
type test struct {
v string
a TagList
ok bool
}

tests := []test{
{v: "A", a: TagList{}, ok: false},
{v: "A", a: TagList{"A"}, ok: true},
{v: "a", a: TagList{"A"}, ok: false},
{v: "a", a: TagList{"a:hello"}, ok: false},
{v: "a:a", a: TagList{"a:c"}, ok: false},
}

for idx, test := range tests {
found := test.a.Contains(test.v)
if !found && test.ok {
t.Errorf("[%d] expected to contain %q", idx, test.v)
}
}
}

func TestTagList_Add(t *testing.T) {
type test struct {
v string
a TagList
shouldBe TagList
}

tests := []test{
{v: "A", a: TagList{}, shouldBe: TagList{"A"}},
{v: "A", a: TagList{"A"}, shouldBe: TagList{"A"}},
{v: "a", a: TagList{"A"}, shouldBe: TagList{"A", "a"}},
{v: "a", a: TagList{"a:hello"}, shouldBe: TagList{"a", "a:hello"}},
{v: "a:Hello", a: TagList{"a:hello"}, shouldBe: TagList{"a:hello", "a:Hello"}},
{v: "a:a", a: TagList{"a:c"}, shouldBe: TagList{"a:a", "a:c"}},
}

for idx, test := range tests {
test.a.Add(test.v)
if !test.a.Equals(&test.shouldBe) {
t.Errorf("[%d] expected lists to be equal: %v", idx, test.a)
}
}
}

func TestTagList_Delete(t *testing.T) {
type test struct {
v string
a TagList
shouldBe TagList
}

tests := []test{
{v: "A", a: TagList{}, shouldBe: TagList{}},
{v: "A", a: TagList{"A"}, shouldBe: TagList{}},
{v: "a", a: TagList{"A"}, shouldBe: TagList{"A"}},
{v: "a:Hello", a: TagList{"a:hello"}, shouldBe: TagList{"a:hello"}},
{v: "a:a", a: TagList{"a:A"}, shouldBe: TagList{"a:A"}},
}

for idx, test := range tests {
test.a.Remove(test.v)
if !test.a.Equals(&test.shouldBe) {
t.Errorf("[%d] expected lists to be equal: %v", idx, test.a)
}
}
}

0 comments on commit de1f16b

Please sign in to comment.