Skip to content

Commit de1f16b

Browse files
authored
[CHANGE] Tags are now case-sensitive (#225)
* [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.
1 parent 2d9ece2 commit de1f16b

File tree

3 files changed

+132
-30
lines changed

3 files changed

+132
-30
lines changed

v2/operator_claims_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,9 @@ func TestTags(t *testing.T) {
457457
if len(oc2.GenericFields.Tags) != 3 {
458458
t.Fatal("expected 3 tags")
459459
}
460-
for _, v := range oc.GenericFields.Tags {
461-
AssertFalse(v == "TWO", t)
462-
}
463460

464461
AssertTrue(oc.GenericFields.Tags.Contains("one"), t)
465-
AssertTrue(oc.GenericFields.Tags.Contains("two"), t)
462+
AssertTrue(oc.GenericFields.Tags.Contains("TWO"), t)
466463
AssertTrue(oc.GenericFields.Tags.Contains("three"), t)
467464
}
468465

v2/types.go

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 The NATS Authors
2+
* Copyright 2018-2024 The NATS Authors
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -427,20 +427,38 @@ type TagList []string
427427

428428
// Contains returns true if the list contains the tags
429429
func (u *TagList) Contains(p string) bool {
430-
p = strings.ToLower(strings.TrimSpace(p))
431-
for _, t := range *u {
432-
if t == p {
433-
return true
430+
return u.find(p) != -1
431+
}
432+
433+
func (u *TagList) Equals(other *TagList) bool {
434+
if len(*u) != len(*other) {
435+
return false
436+
}
437+
for _, v := range *u {
438+
if other.find(v) == -1 {
439+
return false
434440
}
435441
}
436-
return false
442+
return true
443+
}
444+
445+
func (u *TagList) find(p string) int {
446+
for idx, t := range *u {
447+
if p == t {
448+
return idx
449+
}
450+
}
451+
return -1
437452
}
438453

439454
// Add appends 1 or more tags to a list
440455
func (u *TagList) Add(p ...string) {
441456
for _, v := range p {
442-
v = strings.ToLower(strings.TrimSpace(v))
443-
if !u.Contains(v) && v != "" {
457+
v = strings.TrimSpace(v)
458+
if v == "" {
459+
continue
460+
}
461+
if !u.Contains(v) {
444462
*u = append(*u, v)
445463
}
446464
}
@@ -449,29 +467,47 @@ func (u *TagList) Add(p ...string) {
449467
// Remove removes 1 or more tags from a list
450468
func (u *TagList) Remove(p ...string) {
451469
for _, v := range p {
452-
v = strings.ToLower(strings.TrimSpace(v))
453-
for i, t := range *u {
454-
if t == v {
455-
a := *u
456-
*u = append(a[:i], a[i+1:]...)
457-
break
458-
}
470+
v = strings.TrimSpace(v)
471+
idx := u.find(v)
472+
if idx != -1 {
473+
a := *u
474+
*u = append(a[:idx], a[idx+1:]...)
459475
}
460476
}
461477
}
462478

463-
type CIDRList TagList
479+
type CIDRList []string
464480

465481
func (c *CIDRList) Contains(p string) bool {
466-
return (*TagList)(c).Contains(p)
482+
p = strings.ToLower(strings.TrimSpace(p))
483+
for _, t := range *c {
484+
if t == p {
485+
return true
486+
}
487+
}
488+
return false
467489
}
468490

469491
func (c *CIDRList) Add(p ...string) {
470-
(*TagList)(c).Add(p...)
492+
for _, v := range p {
493+
v = strings.ToLower(strings.TrimSpace(v))
494+
if !c.Contains(v) && v != "" {
495+
*c = append(*c, v)
496+
}
497+
}
471498
}
472499

473500
func (c *CIDRList) Remove(p ...string) {
474-
(*TagList)(c).Remove(p...)
501+
for _, v := range p {
502+
v = strings.ToLower(strings.TrimSpace(v))
503+
for i, t := range *c {
504+
if t == v {
505+
a := *c
506+
*c = append(a[:i], a[i+1:]...)
507+
break
508+
}
509+
}
510+
}
475511
}
476512

477513
func (c *CIDRList) Set(values string) {

v2/types_test.go

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 The NATS Authors
2+
* Copyright 2018-2024 The NATS Authors
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -117,19 +117,18 @@ func TestTagList(t *testing.T) {
117117
tags.Add("one")
118118

119119
AssertEquals(true, tags.Contains("one"), t)
120-
AssertEquals(true, tags.Contains("ONE"), t)
120+
AssertEquals(false, tags.Contains("ONE"), t)
121121
AssertEquals("one", tags[0], t)
122122

123123
tags.Add("TWO")
124124

125-
AssertEquals(true, tags.Contains("two"), t)
125+
AssertEquals(false, tags.Contains("two"), t)
126126
AssertEquals(true, tags.Contains("TWO"), t)
127-
AssertEquals("two", tags[1], t)
127+
AssertEquals("TWO", tags[1], t)
128128

129129
tags.Remove("ONE")
130-
AssertEquals("two", tags[0], t)
131-
AssertEquals(false, tags.Contains("one"), t)
132-
AssertEquals(false, tags.Contains("ONE"), t)
130+
AssertEquals("one", tags[0], t)
131+
AssertEquals(true, tags.Contains("TWO"), t)
133132
}
134133

135134
func TestStringList(t *testing.T) {
@@ -427,3 +426,73 @@ func TestInvalidInfo(t *testing.T) {
427426
}
428427
}
429428
}
429+
430+
func TestTagList_CasePreservingContains(t *testing.T) {
431+
type test struct {
432+
v string
433+
a TagList
434+
ok bool
435+
}
436+
437+
tests := []test{
438+
{v: "A", a: TagList{}, ok: false},
439+
{v: "A", a: TagList{"A"}, ok: true},
440+
{v: "a", a: TagList{"A"}, ok: false},
441+
{v: "a", a: TagList{"a:hello"}, ok: false},
442+
{v: "a:a", a: TagList{"a:c"}, ok: false},
443+
}
444+
445+
for idx, test := range tests {
446+
found := test.a.Contains(test.v)
447+
if !found && test.ok {
448+
t.Errorf("[%d] expected to contain %q", idx, test.v)
449+
}
450+
}
451+
}
452+
453+
func TestTagList_Add(t *testing.T) {
454+
type test struct {
455+
v string
456+
a TagList
457+
shouldBe TagList
458+
}
459+
460+
tests := []test{
461+
{v: "A", a: TagList{}, shouldBe: TagList{"A"}},
462+
{v: "A", a: TagList{"A"}, shouldBe: TagList{"A"}},
463+
{v: "a", a: TagList{"A"}, shouldBe: TagList{"A", "a"}},
464+
{v: "a", a: TagList{"a:hello"}, shouldBe: TagList{"a", "a:hello"}},
465+
{v: "a:Hello", a: TagList{"a:hello"}, shouldBe: TagList{"a:hello", "a:Hello"}},
466+
{v: "a:a", a: TagList{"a:c"}, shouldBe: TagList{"a:a", "a:c"}},
467+
}
468+
469+
for idx, test := range tests {
470+
test.a.Add(test.v)
471+
if !test.a.Equals(&test.shouldBe) {
472+
t.Errorf("[%d] expected lists to be equal: %v", idx, test.a)
473+
}
474+
}
475+
}
476+
477+
func TestTagList_Delete(t *testing.T) {
478+
type test struct {
479+
v string
480+
a TagList
481+
shouldBe TagList
482+
}
483+
484+
tests := []test{
485+
{v: "A", a: TagList{}, shouldBe: TagList{}},
486+
{v: "A", a: TagList{"A"}, shouldBe: TagList{}},
487+
{v: "a", a: TagList{"A"}, shouldBe: TagList{"A"}},
488+
{v: "a:Hello", a: TagList{"a:hello"}, shouldBe: TagList{"a:hello"}},
489+
{v: "a:a", a: TagList{"a:A"}, shouldBe: TagList{"a:A"}},
490+
}
491+
492+
for idx, test := range tests {
493+
test.a.Remove(test.v)
494+
if !test.a.Equals(&test.shouldBe) {
495+
t.Errorf("[%d] expected lists to be equal: %v", idx, test.a)
496+
}
497+
}
498+
}

0 commit comments

Comments
 (0)