Skip to content

Commit 12b73be

Browse files
ndeloofglours
authored andcommitted
remove utils.Contains to prefer slice.ContainsFunc
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 2e71440 commit 12b73be

4 files changed

Lines changed: 16 additions & 23 deletions

File tree

pkg/compose/dependencies.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import (
2626
"github.com/compose-spec/compose-go/v2/types"
2727
"github.com/docker/compose/v2/pkg/api"
2828
"golang.org/x/sync/errgroup"
29-
30-
"github.com/docker/compose/v2/pkg/utils"
3129
)
3230

3331
// ServiceStatus indicates the status of a service
@@ -120,7 +118,7 @@ func WithRootNodesAndDown(nodes []string) func(*graphTraversal) {
120118

121119
t.ignored = map[string]struct{}{}
122120
for k := range graph.Vertices {
123-
if !utils.Contains(want, k) {
121+
if !slices.Contains(want, k) {
124122
t.ignored[k] = struct{}{}
125123
}
126124
}

pkg/compose/generate.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22+
"slices"
2223
"strings"
2324

2425
"github.com/compose-spec/compose-go/v2/types"
2526
"github.com/docker/compose/v2/pkg/api"
26-
"github.com/docker/compose/v2/pkg/utils"
2727
"github.com/docker/docker/api/types/container"
2828
"github.com/docker/docker/api/types/filters"
2929
"github.com/docker/docker/api/types/mount"
@@ -54,8 +54,11 @@ func (s *composeService) Generate(ctx context.Context, options api.GenerateOptio
5454
if err != nil {
5555
return nil, err
5656
}
57+
5758
for _, ctr := range containersByIds {
58-
if !utils.Contains(containers, ctr) {
59+
if !slices.ContainsFunc(containers, func(summary container.Summary) bool {
60+
return summary.ID == ctr.ID
61+
}) {
5962
containers = append(containers, ctr)
6063
}
6164
}

pkg/compose/start.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"slices"
2324
"strings"
2425
"time"
2526

@@ -199,7 +200,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
199200
ofInterest := func(c containerType.Summary) bool {
200201
if len(services) > 0 {
201202
// we only watch some services
202-
return utils.Contains(services, c.Labels[api.ServiceLabel])
203+
return slices.Contains(services, c.Labels[api.ServiceLabel])
203204
}
204205
return true
205206
}
@@ -208,7 +209,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
208209
isRequired := func(c containerType.Summary) bool {
209210
if len(services) > 0 && len(required) > 0 {
210211
// we only watch some services
211-
return utils.Contains(required, c.Labels[api.ServiceLabel])
212+
return slices.Contains(required, c.Labels[api.ServiceLabel])
212213
}
213214
return true
214215
}
@@ -263,7 +264,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
263264
}
264265
if _, ok := watched[container.ID]; ok {
265266
eType := api.ContainerEventStopped
266-
if utils.Contains(replaced, container.ID) {
267+
if slices.Contains(replaced, container.ID) {
267268
utils.Remove(replaced, container.ID)
268269
eType = api.ContainerEventRecreated
269270
}
@@ -290,7 +291,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
290291
}
291292

292293
eType := api.ContainerEventExit
293-
if utils.Contains(replaced, container.ID) {
294+
if slices.Contains(replaced, container.ID) {
294295
utils.Remove(replaced, container.ID)
295296
eType = api.ContainerEventRecreated
296297
}

pkg/utils/slices.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,15 @@
1616

1717
package utils
1818

19-
import "reflect"
20-
21-
// Contains helps to detect if a non-comparable struct is part of an array
22-
// only use this method if you can't rely on existing golang Contains function of slices (https://pkg.go.dev/golang.org/x/exp/slices#Contains)
23-
func Contains[T any](origin []T, element T) bool {
24-
for _, v := range origin {
25-
if reflect.DeepEqual(v, element) {
26-
return true
27-
}
28-
}
29-
return false
30-
}
19+
import (
20+
"slices"
21+
)
3122

3223
// Remove removes all elements from origin slice
33-
func Remove[T any](origin []T, elements ...T) []T {
24+
func Remove[T comparable](origin []T, elements ...T) []T {
3425
var filtered []T
3526
for _, v := range origin {
36-
if !Contains(elements, v) {
27+
if !slices.Contains(elements, v) {
3728
filtered = append(filtered, v)
3829
}
3930
}

0 commit comments

Comments
 (0)