Skip to content

Commit f9c7776

Browse files
authored
Retract case-insensitive tag functionality (#233)
* Revert "[CHANGE] Tags are now case-sensitive (#225)" This reverts commit de1f16b. * retract 2.7.0 Signed-off-by: Alberto Ricart <[email protected]> --------- Signed-off-by: Alberto Ricart <[email protected]>
1 parent 18a60d6 commit f9c7776

File tree

4 files changed

+38
-145
lines changed

4 files changed

+38
-145
lines changed

v2/go.mod

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
module github.com/nats-io/jwt/v2
22

3-
go 1.18
3+
go 1.22
44

55
require github.com/nats-io/nkeys v0.4.7
66

7+
retract (
8+
v2.7.1 // contains retractions only
9+
v2.7.0 // includes case insensitive changes to tags that break jetstream placement
10+
)
11+
712
require (
813
golang.org/x/crypto v0.19.0 // indirect
914
golang.org/x/sys v0.17.0 // indirect

v2/operator_claims_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,12 @@ 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+
}
460463

461464
AssertTrue(oc.GenericFields.Tags.Contains("one"), t)
462-
AssertTrue(oc.GenericFields.Tags.Contains("TWO"), t)
465+
AssertTrue(oc.GenericFields.Tags.Contains("two"), t)
463466
AssertTrue(oc.GenericFields.Tags.Contains("three"), t)
464467
}
465468

v2/types.go

+20-59
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2024 The NATS Authors
2+
* Copyright 2018-2019 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,90 +427,51 @@ type TagList []string
427427

428428
// Contains returns true if the list contains the tags
429429
func (u *TagList) Contains(p string) bool {
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
440-
}
441-
}
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
430+
p = strings.ToLower(strings.TrimSpace(p))
431+
for _, t := range *u {
432+
if t == p {
433+
return true
449434
}
450435
}
451-
return -1
436+
return false
452437
}
453438

454439
// Add appends 1 or more tags to a list
455440
func (u *TagList) Add(p ...string) {
456441
for _, v := range p {
457-
v = strings.TrimSpace(v)
458-
if v == "" {
459-
continue
460-
}
461-
if !u.Contains(v) {
442+
v = strings.ToLower(strings.TrimSpace(v))
443+
if !u.Contains(v) && v != "" {
462444
*u = append(*u, v)
463445
}
464446
}
465447
}
466448

467449
// Remove removes 1 or more tags from a list
468-
func (u *TagList) Remove(p ...string) error {
450+
func (u *TagList) Remove(p ...string) {
469451
for _, v := range p {
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:]...)
475-
} else {
476-
return fmt.Errorf("unable to remove tag: %q - not found", v)
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+
}
477459
}
478460
}
479-
return nil
480461
}
481462

482-
type CIDRList []string
463+
type CIDRList TagList
483464

484465
func (c *CIDRList) Contains(p string) bool {
485-
p = strings.ToLower(strings.TrimSpace(p))
486-
for _, t := range *c {
487-
if t == p {
488-
return true
489-
}
490-
}
491-
return false
466+
return (*TagList)(c).Contains(p)
492467
}
493468

494469
func (c *CIDRList) Add(p ...string) {
495-
for _, v := range p {
496-
v = strings.ToLower(strings.TrimSpace(v))
497-
if !c.Contains(v) && v != "" {
498-
*c = append(*c, v)
499-
}
500-
}
470+
(*TagList)(c).Add(p...)
501471
}
502472

503473
func (c *CIDRList) Remove(p ...string) {
504-
for _, v := range p {
505-
v = strings.ToLower(strings.TrimSpace(v))
506-
for i, t := range *c {
507-
if t == v {
508-
a := *c
509-
*c = append(a[:i], a[i+1:]...)
510-
break
511-
}
512-
}
513-
}
474+
(*TagList)(c).Remove(p...)
514475
}
515476

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

v2/types_test.go

+8-84
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2024 The NATS Authors
2+
* Copyright 2018 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,21 +117,19 @@ func TestTagList(t *testing.T) {
117117
tags.Add("one")
118118

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

123123
tags.Add("TWO")
124124

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

129-
err := tags.Remove("ONE")
130-
if err == nil {
131-
t.Fatal("removing tag that doesn't exist should have failed")
132-
}
133-
AssertEquals("one", tags[0], t)
134-
AssertEquals(true, tags.Contains("TWO"), t)
129+
tags.Remove("ONE")
130+
AssertEquals("two", tags[0], t)
131+
AssertEquals(false, tags.Contains("one"), t)
132+
AssertEquals(false, tags.Contains("ONE"), t)
135133
}
136134

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

0 commit comments

Comments
 (0)