Browse Source

remove utils.Contains to prefer slice.ContainsFunc

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 6 months ago
parent
commit
12b73bea73
4 changed files with 16 additions and 23 deletions
  1. 1 3
      pkg/compose/dependencies.go
  2. 5 2
      pkg/compose/generate.go
  3. 5 4
      pkg/compose/start.go
  4. 5 14
      pkg/utils/slices.go

+ 1 - 3
pkg/compose/dependencies.go

@@ -26,8 +26,6 @@ import (
 	"github.com/compose-spec/compose-go/v2/types"
 	"github.com/docker/compose/v2/pkg/api"
 	"golang.org/x/sync/errgroup"
-
-	"github.com/docker/compose/v2/pkg/utils"
 )
 
 // ServiceStatus indicates the status of a service
@@ -120,7 +118,7 @@ func WithRootNodesAndDown(nodes []string) func(*graphTraversal) {
 
 			t.ignored = map[string]struct{}{}
 			for k := range graph.Vertices {
-				if !utils.Contains(want, k) {
+				if !slices.Contains(want, k) {
 					t.ignored[k] = struct{}{}
 				}
 			}

+ 5 - 2
pkg/compose/generate.go

@@ -19,11 +19,11 @@ package compose
 import (
 	"context"
 	"fmt"
+	"slices"
 	"strings"
 
 	"github.com/compose-spec/compose-go/v2/types"
 	"github.com/docker/compose/v2/pkg/api"
-	"github.com/docker/compose/v2/pkg/utils"
 	"github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/api/types/filters"
 	"github.com/docker/docker/api/types/mount"
@@ -54,8 +54,11 @@ func (s *composeService) Generate(ctx context.Context, options api.GenerateOptio
 	if err != nil {
 		return nil, err
 	}
+
 	for _, ctr := range containersByIds {
-		if !utils.Contains(containers, ctr) {
+		if !slices.ContainsFunc(containers, func(summary container.Summary) bool {
+			return summary.ID == ctr.ID
+		}) {
 			containers = append(containers, ctr)
 		}
 	}

+ 5 - 4
pkg/compose/start.go

@@ -20,6 +20,7 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"slices"
 	"strings"
 	"time"
 
@@ -199,7 +200,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
 	ofInterest := func(c containerType.Summary) bool {
 		if len(services) > 0 {
 			// we only watch some services
-			return utils.Contains(services, c.Labels[api.ServiceLabel])
+			return slices.Contains(services, c.Labels[api.ServiceLabel])
 		}
 		return true
 	}
@@ -208,7 +209,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
 	isRequired := func(c containerType.Summary) bool {
 		if len(services) > 0 && len(required) > 0 {
 			// we only watch some services
-			return utils.Contains(required, c.Labels[api.ServiceLabel])
+			return slices.Contains(required, c.Labels[api.ServiceLabel])
 		}
 		return true
 	}
@@ -263,7 +264,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
 				}
 				if _, ok := watched[container.ID]; ok {
 					eType := api.ContainerEventStopped
-					if utils.Contains(replaced, container.ID) {
+					if slices.Contains(replaced, container.ID) {
 						utils.Remove(replaced, container.ID)
 						eType = api.ContainerEventRecreated
 					}
@@ -290,7 +291,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
 				}
 
 				eType := api.ContainerEventExit
-				if utils.Contains(replaced, container.ID) {
+				if slices.Contains(replaced, container.ID) {
 					utils.Remove(replaced, container.ID)
 					eType = api.ContainerEventRecreated
 				}

+ 5 - 14
pkg/utils/slices.go

@@ -16,24 +16,15 @@
 
 package utils
 
-import "reflect"
-
-// Contains helps to detect if a non-comparable struct is part of an array
-// 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)
-func Contains[T any](origin []T, element T) bool {
-	for _, v := range origin {
-		if reflect.DeepEqual(v, element) {
-			return true
-		}
-	}
-	return false
-}
+import (
+	"slices"
+)
 
 // Remove removes all elements from origin slice
-func Remove[T any](origin []T, elements ...T) []T {
+func Remove[T comparable](origin []T, elements ...T) []T {
 	var filtered []T
 	for _, v := range origin {
-		if !Contains(elements, v) {
+		if !slices.Contains(elements, v) {
 			filtered = append(filtered, v)
 		}
 	}