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/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{}{}
}
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
}
}