Skip to content

Commit e78e77e

Browse files
committed
Update slice comparisons to use slices package
1 parent c691fd1 commit e78e77e

File tree

3 files changed

+31
-61
lines changed

3 files changed

+31
-61
lines changed

pkg/cloud/services/loadbalancer/loadbalancer.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package loadbalancer
1919
import (
2020
"errors"
2121
"fmt"
22+
"slices"
2223
"time"
2324

2425
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners"
@@ -274,11 +275,11 @@ func (s *Service) getOrUpdateAllowedCIDRS(openStackCluster *infrav1.OpenStackClu
274275
// Validate CIDRs and convert any given IP into a CIDR.
275276
allowedCIDRs = validateIPs(openStackCluster, allowedCIDRs)
276277

277-
// Remove duplicates.
278-
allowedCIDRs = capostrings.Unique(allowedCIDRs)
279-
listener.AllowedCIDRs = capostrings.Unique(listener.AllowedCIDRs)
278+
// Sort and remove duplicates
279+
allowedCIDRs = capostrings.Canonicalize(allowedCIDRs)
280+
listener.AllowedCIDRs = capostrings.Canonicalize(listener.AllowedCIDRs)
280281

281-
if !capostrings.CompareLists(allowedCIDRs, listener.AllowedCIDRs) {
282+
if !slices.Equal(allowedCIDRs, listener.AllowedCIDRs) {
282283
s.scope.Logger().Info("CIDRs do not match, updating listener", "expectedCIDRs", allowedCIDRs, "currentCIDRs", listener.AllowedCIDRs)
283284
listenerUpdateOpts := listeners.UpdateOpts{
284285
AllowedCIDRs: &allowedCIDRs,

pkg/utils/strings/strings.go

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,12 @@ limitations under the License.
1616

1717
package strings
1818

19-
func Unique(s []string) []string {
20-
inResult := make(map[string]bool)
21-
var result []string
22-
for _, str := range s {
23-
if _, ok := inResult[str]; !ok {
24-
inResult[str] = true
25-
result = append(result, str)
26-
}
27-
}
28-
return result
29-
}
19+
import (
20+
"cmp"
21+
"slices"
22+
)
3023

31-
func CompareLists(a, b []string) bool {
32-
if len(a) != len(b) {
33-
return false
34-
}
35-
for _, v := range a {
36-
eq := false
37-
for _, v2 := range b {
38-
if v == v2 {
39-
eq = true
40-
break
41-
}
42-
}
43-
if !eq {
44-
return false
45-
}
46-
}
47-
return true
24+
func Canonicalize[S ~[]E, E cmp.Ordered](s S) S {
25+
slices.Sort(s)
26+
return slices.Compact(s)
4827
}

pkg/utils/strings/strings_test.go

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,42 @@ limitations under the License.
1717
package strings
1818

1919
import (
20+
"slices"
2021
"testing"
2122
)
2223

23-
func TestCompareLists(t *testing.T) {
24+
func TestCanonicalize(t *testing.T) {
2425
tests := []struct {
25-
name string
26-
a []string
27-
b []string
28-
want bool
26+
name string
27+
value []string
28+
want []string
2929
}{
3030
{
31-
name: "Empty lists",
32-
a: []string{},
33-
b: []string{},
34-
want: true,
31+
name: "Empty list",
32+
value: []string{},
33+
want: []string{},
3534
},
3635
{
37-
name: "Equal lists",
38-
a: []string{"a", "b", "c"},
39-
b: []string{"a", "b", "c"},
40-
want: true,
36+
name: "Identity",
37+
value: []string{"a", "b", "c"},
38+
want: []string{"a", "b", "c"},
4139
},
4240
{
43-
name: "Different order",
44-
a: []string{"a", "b", "c"},
45-
b: []string{"c", "b", "a"},
46-
want: true,
41+
name: "Out of order",
42+
value: []string{"c", "b", "a"},
43+
want: []string{"a", "b", "c"},
4744
},
4845
{
49-
name: "Different elements",
50-
a: []string{"a", "b", "c"},
51-
b: []string{"d", "e", "f"},
52-
want: false,
53-
},
54-
{
55-
name: "Different lengths",
56-
a: []string{"a", "b", "c"},
57-
b: []string{"a", "b"},
58-
want: false,
46+
name: "Duplicate elements",
47+
value: []string{"c", "b", "a", "c"},
48+
want: []string{"a", "b", "c"},
5949
},
6050
}
6151

6252
for _, tt := range tests {
6353
t.Run(tt.name, func(t *testing.T) {
64-
got := CompareLists(tt.a, tt.b)
65-
if got != tt.want {
54+
got := Canonicalize(tt.value)
55+
if !slices.Equal(got, tt.want) {
6656
t.Errorf("CompareLists() = %v, want %v", got, tt.want)
6757
}
6858
})

0 commit comments

Comments
 (0)