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

[CHANGE] Tags are now case-sensitive #225

Merged
merged 2 commits into from
Sep 17, 2024
Merged
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
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)
}
}
}
Loading