remove utils.Contains to prefer slice.ContainsFunc

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2025-05-27 14:00:21 +02:00 committed by Guillaume Lours
parent 2e71440bee
commit 12b73bea73
4 changed files with 16 additions and 23 deletions

View File

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

View File

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

View File

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

View File

@ -16,24 +16,15 @@
package utils package utils
import "reflect" import (
"slices"
// 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
}
// Remove removes all elements from origin slice // 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 var filtered []T
for _, v := range origin { for _, v := range origin {
if !Contains(elements, v) { if !slices.Contains(elements, v) {
filtered = append(filtered, v) filtered = append(filtered, v)
} }
} }