Skip to content

Commit 6eb0c69

Browse files
committed
chore: refactor containers to use generics
golangci-lint was not happy with the type-safety previously.
1 parent 0b3530e commit 6eb0c69

File tree

4 files changed

+42
-195
lines changed

4 files changed

+42
-195
lines changed

internal/legacy/container/set.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,25 @@ limitations under the License.
1717
package container
1818

1919
// Set is a basic set-like data structure.
20-
type Set map[Element]interface{}
20+
type Set[K comparable] map[K]struct{}
2121

22-
// Element is a singleton item that goes with Set.
23-
type Element interface{}
22+
// NewSet contructs a Set with the specified items.
23+
func NewSet[K comparable](items ...K) Set[K] {
24+
s := make(Set[K])
25+
for _, item := range items {
26+
s[item] = struct{}{}
27+
}
28+
return s
29+
}
30+
31+
// Insert inserts an item into the set.
32+
func (s Set[K]) Insert(item K) {
33+
s[item] = struct{}{}
34+
}
2435

2536
// Minus returns a new set, by subtracting everything in b from a.
26-
func (a Set) Minus(b Set) Set {
27-
c := make(Set)
37+
func (a Set[K]) Minus(b Set[K]) Set[K] {
38+
c := make(Set[K])
2839
for k, v := range a {
2940
c[k] = v
3041
}
@@ -35,8 +46,8 @@ func (a Set) Minus(b Set) Set {
3546
}
3647

3748
// Union takes two sets and returns their union in a new set.
38-
func (a Set) Union(b Set) Set {
39-
c := make(Set)
49+
func (a Set[K]) Union(b Set[K]) Set[K] {
50+
c := make(Set[K])
4051
for k, v := range a {
4152
c[k] = v
4253
}
@@ -48,8 +59,8 @@ func (a Set) Union(b Set) Set {
4859

4960
// Intersection takes two sets and returns elements common to both. Note that we
5061
// throw away information about the values of the elements in b.
51-
func (a Set) Intersection(b Set) Set {
52-
c := make(Set)
62+
func (a Set[K]) Intersection(b Set[K]) Set[K] {
63+
c := make(Set[K])
5364
for k, v := range a {
5465
if _, ok := b[k]; ok {
5566
c[k] = v

internal/legacy/dockerregistry/inventory_test.go

Lines changed: 17 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
cr "github.com/google/go-containerregistry/pkg/v1/types"
2828
"github.com/stretchr/testify/require"
2929

30+
"sigs.k8s.io/promo-tools/v4/internal/legacy/container"
3031
reg "sigs.k8s.io/promo-tools/v4/internal/legacy/dockerregistry"
3132
"sigs.k8s.io/promo-tools/v4/internal/legacy/dockerregistry/registry"
3233
"sigs.k8s.io/promo-tools/v4/internal/legacy/dockerregistry/schema"
@@ -1238,161 +1239,92 @@ func TestGetTokenKeyDomainRepoPath(t *testing.T) {
12381239
}
12391240
}
12401241

1241-
func TestSetManipulationsRegistryInventories(t *testing.T) {
1242-
tests := []struct {
1243-
name string
1244-
input1 registry.RegInvImage
1245-
input2 registry.RegInvImage
1246-
op func(a, b registry.RegInvImage) registry.RegInvImage
1247-
expectedOutput registry.RegInvImage
1248-
}{
1249-
{
1250-
"Set Minus",
1251-
registry.RegInvImage{
1252-
"foo": {
1253-
"sha256:abc": {"1.0", "latest"},
1254-
},
1255-
"bar": {
1256-
"sha256:def": {"0.9"},
1257-
},
1258-
},
1259-
registry.RegInvImage{
1260-
"foo": {
1261-
"sha256:abc": {"1.0", "latest"},
1262-
},
1263-
"bar": {
1264-
"sha256:def": {"0.9"},
1265-
},
1266-
},
1267-
registry.RegInvImage.Minus,
1268-
registry.RegInvImage{},
1269-
},
1270-
{
1271-
"Set Union",
1272-
registry.RegInvImage{
1273-
"foo": {
1274-
"sha256:abc": {"1.0", "latest"},
1275-
},
1276-
"bar": {
1277-
"sha256:def": {"0.9"},
1278-
},
1279-
},
1280-
registry.RegInvImage{
1281-
"apple": {
1282-
"sha256:abc": {"1.0", "latest"},
1283-
},
1284-
"banana": {
1285-
"sha256:def": {"0.9"},
1286-
},
1287-
},
1288-
registry.RegInvImage.Union,
1289-
registry.RegInvImage{
1290-
"foo": {
1291-
"sha256:abc": {"1.0", "latest"},
1292-
},
1293-
"bar": {
1294-
"sha256:def": {"0.9"},
1295-
},
1296-
"apple": {
1297-
"sha256:abc": {"1.0", "latest"},
1298-
},
1299-
"banana": {
1300-
"sha256:def": {"0.9"},
1301-
},
1302-
},
1303-
},
1304-
}
1305-
1306-
for _, test := range tests {
1307-
got := test.op(test.input1, test.input2)
1308-
expected := test.expectedOutput
1309-
require.Equal(t, expected, got)
1310-
}
1311-
}
1312-
13131242
func TestSetManipulationsTags(t *testing.T) {
13141243
tests := []struct {
13151244
name string
13161245
input1 registry.TagSlice
13171246
input2 registry.TagSlice
1318-
op func(a, b registry.TagSlice) registry.TagSet
1319-
expectedOutput registry.TagSet
1247+
op func(a, b registry.TagSlice) container.Set[image.Tag]
1248+
expectedOutput []string
13201249
}{
13211250
{
13221251
"Set Minus (both blank)",
13231252
registry.TagSlice{},
13241253
registry.TagSlice{},
13251254
registry.TagSlice.Minus,
1326-
registry.TagSet{},
1255+
[]string{},
13271256
},
13281257
{
13291258
"Set Minus (first blank)",
13301259
registry.TagSlice{},
13311260
registry.TagSlice{"a"},
13321261
registry.TagSlice.Minus,
1333-
registry.TagSet{},
1262+
[]string{},
13341263
},
13351264
{
13361265
"Set Minus (second blank)",
13371266
registry.TagSlice{"a", "b"},
13381267
registry.TagSlice{},
13391268
registry.TagSlice.Minus,
1340-
registry.TagSet{"a": nil, "b": nil},
1269+
[]string{"a", "b"},
13411270
},
13421271
{
13431272
"Set Minus",
13441273
registry.TagSlice{"a", "b"},
13451274
registry.TagSlice{"b"},
13461275
registry.TagSlice.Minus,
1347-
registry.TagSet{"a": nil},
1276+
[]string{"a"},
13481277
},
13491278
{
13501279
"Set Union (both blank)",
13511280
registry.TagSlice{},
13521281
registry.TagSlice{},
13531282
registry.TagSlice.Union,
1354-
registry.TagSet{},
1283+
[]string{},
13551284
},
13561285
{
13571286
"Set Union (first blank)",
13581287
registry.TagSlice{},
13591288
registry.TagSlice{"a"},
13601289
registry.TagSlice.Union,
1361-
registry.TagSet{"a": nil},
1290+
[]string{"a"},
13621291
},
13631292
{
13641293
"Set Union (second blank)",
13651294
registry.TagSlice{"a"},
13661295
registry.TagSlice{},
13671296
registry.TagSlice.Union,
1368-
registry.TagSet{"a": nil},
1297+
[]string{"a"},
13691298
},
13701299
{
13711300
"Set Union",
13721301
registry.TagSlice{"a", "c"},
13731302
registry.TagSlice{"b", "d"},
13741303
registry.TagSlice.Union,
1375-
registry.TagSet{"a": nil, "b": nil, "c": nil, "d": nil},
1304+
[]string{"a", "b", "c", "d"},
13761305
},
13771306
{
13781307
"Set Intersection (no intersection)",
13791308
registry.TagSlice{"a"},
13801309
registry.TagSlice{"b"},
13811310
registry.TagSlice.Intersection,
1382-
registry.TagSet{},
1311+
[]string{},
13831312
},
13841313
{
13851314
"Set Intersection (some intersection)",
13861315
registry.TagSlice{"a", "b"},
13871316
registry.TagSlice{"b", "c"},
13881317
registry.TagSlice.Intersection,
1389-
registry.TagSet{"b": nil},
1318+
[]string{"b"},
13901319
},
13911320
}
13921321

13931322
for _, test := range tests {
13941323
got := test.op(test.input1, test.input2)
1395-
expected := test.expectedOutput
1324+
expected := container.NewSet[image.Tag]()
1325+
for _, tag := range test.expectedOutput {
1326+
expected.Insert(image.Tag(tag))
1327+
}
13961328
require.Equal(t, expected, got)
13971329
}
13981330
}

internal/legacy/dockerregistry/registry/registry.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ type DigestTags map[image.Digest]TagSlice
5454
// TagSlice is a slice of Tags.
5555
type TagSlice []image.Tag
5656

57-
// TagSet is a set of Tags.
58-
type TagSet map[image.Tag]interface{}
59-
6057
// ToYAML displays a RegInvImage as YAML, but with the map items sorted
6158
// alphabetically.
6259
func (a *RegInvImage) ToYAML(o YamlMarshalingOpts) string {

internal/legacy/dockerregistry/registry/set.go

Lines changed: 5 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -24,59 +24,13 @@ import (
2424
// Various set manipulation operations. Some set operations are missing,
2525
// because, we don't use them.
2626

27-
// ToSet converts a RegInvImage to a Set.
28-
func (a RegInvImage) ToSet() container.Set {
29-
b := make(container.Set)
30-
for k, v := range a {
31-
b[k] = v
32-
}
33-
34-
return b
35-
}
36-
37-
func toRegistryInventory(a container.Set) RegInvImage {
38-
b := make(RegInvImage)
39-
for k, v := range a {
40-
// TODO: Why are we not checking errors here?
41-
//nolint:errcheck
42-
b[k.(image.Name)] = v.(DigestTags)
43-
}
44-
45-
return b
46-
}
47-
48-
// Minus is a set operation.
49-
// TODO: ST1016: methods on the same type should have the same receiver name.
50-
func (a RegInvImage) Minus(b RegInvImage) RegInvImage {
51-
aSet := a.ToSet()
52-
bSet := b.ToSet()
53-
cSet := aSet.Minus(bSet)
54-
55-
return toRegistryInventory(cSet)
56-
}
57-
58-
// Union is a set operation.
59-
func (a RegInvImage) Union(b RegInvImage) RegInvImage {
60-
aSet := a.ToSet()
61-
bSet := b.ToSet()
62-
cSet := aSet.Union(bSet)
63-
64-
return toRegistryInventory(cSet)
65-
}
66-
6727
// ToTagSet converts a TagSlice to a TagSet.
68-
func (a TagSlice) ToTagSet() TagSet {
69-
b := make(TagSet)
70-
for _, t := range a {
71-
// The value doesn't matter.
72-
b[t] = nil
73-
}
74-
75-
return b
28+
func (a TagSlice) ToTagSet() container.Set[image.Tag] {
29+
return container.NewSet(a...)
7630
}
7731

7832
// Minus is a set operation.
79-
func (a TagSlice) Minus(b TagSlice) TagSet {
33+
func (a TagSlice) Minus(b TagSlice) container.Set[image.Tag] {
8034
aSet := a.ToTagSet()
8135
bSet := b.ToTagSet()
8236
cSet := aSet.Minus(bSet)
@@ -85,7 +39,7 @@ func (a TagSlice) Minus(b TagSlice) TagSet {
8539
}
8640

8741
// Union is a set operation.
88-
func (a TagSlice) Union(b TagSlice) TagSet {
42+
func (a TagSlice) Union(b TagSlice) container.Set[image.Tag] {
8943
aSet := a.ToTagSet()
9044
bSet := b.ToTagSet()
9145
cSet := aSet.Union(bSet)
@@ -94,57 +48,10 @@ func (a TagSlice) Union(b TagSlice) TagSet {
9448
}
9549

9650
// Intersection is a set operation.
97-
func (a TagSlice) Intersection(b TagSlice) TagSet {
51+
func (a TagSlice) Intersection(b TagSlice) container.Set[image.Tag] {
9852
aSet := a.ToTagSet()
9953
bSet := b.ToTagSet()
10054
cSet := aSet.Intersection(bSet)
10155

10256
return cSet
10357
}
104-
105-
// ToSet converts a TagSet to a Set.
106-
func (a TagSet) ToSet() container.Set {
107-
b := make(container.Set)
108-
for t := range a {
109-
// The value doesn't matter.
110-
b[t] = nil
111-
}
112-
113-
return b
114-
}
115-
116-
func setToTagSet(a container.Set) TagSet {
117-
b := make(TagSet)
118-
for k := range a {
119-
b[k.(image.Tag)] = nil //nolint: errcheck
120-
}
121-
122-
return b
123-
}
124-
125-
// Minus is a set operation.
126-
func (a TagSet) Minus(b TagSet) TagSet {
127-
aSet := a.ToSet()
128-
bSet := b.ToSet()
129-
cSet := aSet.Minus(bSet)
130-
131-
return setToTagSet(cSet)
132-
}
133-
134-
// Union is a set operation.
135-
func (a TagSet) Union(b TagSet) TagSet {
136-
aSet := a.ToSet()
137-
bSet := b.ToSet()
138-
cSet := aSet.Union(bSet)
139-
140-
return setToTagSet(cSet)
141-
}
142-
143-
// Intersection is a set operation.
144-
func (a TagSet) Intersection(b TagSet) TagSet {
145-
aSet := a.ToSet()
146-
bSet := b.ToSet()
147-
cSet := aSet.Intersection(bSet)
148-
149-
return setToTagSet(cSet)
150-
}

0 commit comments

Comments
 (0)