Browse code

Move internal/sliceutil to daemon/internal/sliceutil

Signed-off-by: Derek McGowan <derek@mcg.dev>

Derek McGowan authored on 2025/07/25 04:15:19
Showing 16 changed files
... ...
@@ -19,6 +19,7 @@ import (
19 19
 	"github.com/docker/docker/daemon/container"
20 20
 	"github.com/docker/docker/daemon/internal/metrics"
21 21
 	"github.com/docker/docker/daemon/internal/multierror"
22
+	"github.com/docker/docker/daemon/internal/sliceutil"
22 23
 	"github.com/docker/docker/daemon/libnetwork"
23 24
 	"github.com/docker/docker/daemon/libnetwork/netlabel"
24 25
 	"github.com/docker/docker/daemon/libnetwork/scope"
... ...
@@ -26,7 +27,6 @@ import (
26 26
 	"github.com/docker/docker/daemon/network"
27 27
 	"github.com/docker/docker/daemon/pkg/opts"
28 28
 	"github.com/docker/docker/errdefs"
29
-	"github.com/docker/docker/internal/sliceutil"
30 29
 	"github.com/docker/docker/pkg/stringid"
31 30
 	"github.com/docker/docker/runconfig"
32 31
 	"github.com/docker/go-connections/nat"
... ...
@@ -13,7 +13,7 @@ import (
13 13
 	"github.com/containerd/log"
14 14
 	"github.com/containerd/platforms"
15 15
 	"github.com/distribution/reference"
16
-	"github.com/docker/docker/internal/sliceutil"
16
+	"github.com/docker/docker/daemon/internal/sliceutil"
17 17
 	imagespec "github.com/moby/docker-image-spec/specs-go/v1"
18 18
 	"github.com/moby/moby/api/types/backend"
19 19
 	imagetypes "github.com/moby/moby/api/types/image"
20 20
new file mode 100644
... ...
@@ -0,0 +1,34 @@
0
+// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
1
+//go:build go1.23
2
+
3
+package sliceutil
4
+
5
+func Dedup[T comparable](slice []T) []T {
6
+	keys := make(map[T]struct{})
7
+	out := make([]T, 0, len(slice))
8
+	for _, s := range slice {
9
+		if _, ok := keys[s]; !ok {
10
+			out = append(out, s)
11
+			keys[s] = struct{}{}
12
+		}
13
+	}
14
+	return out
15
+}
16
+
17
+func Map[S ~[]In, In, Out any](s S, fn func(In) Out) []Out {
18
+	res := make([]Out, len(s))
19
+	for i, v := range s {
20
+		res[i] = fn(v)
21
+	}
22
+	return res
23
+}
24
+
25
+func Mapper[In, Out any](fn func(In) Out) func([]In) []Out {
26
+	return func(s []In) []Out {
27
+		res := make([]Out, len(s))
28
+		for i, v := range s {
29
+			res[i] = fn(v)
30
+		}
31
+		return res
32
+	}
33
+}
0 34
new file mode 100644
... ...
@@ -0,0 +1,49 @@
0
+package sliceutil_test
1
+
2
+import (
3
+	"net/netip"
4
+	"strconv"
5
+	"testing"
6
+
7
+	"github.com/docker/docker/daemon/internal/sliceutil"
8
+)
9
+
10
+func TestMap(t *testing.T) {
11
+	s := []int{1, 2, 3}
12
+	m := sliceutil.Map(s, func(i int) int { return i * 2 })
13
+	if len(m) != len(s) {
14
+		t.Fatalf("expected len %d, got %d", len(s), len(m))
15
+	}
16
+	for i, v := range m {
17
+		if expected := s[i] * 2; v != expected {
18
+			t.Fatalf("expected %d, got %d", expected, v)
19
+		}
20
+	}
21
+}
22
+
23
+func TestMap_TypeConvert(t *testing.T) {
24
+	s := []int{1, 2, 3}
25
+	m := sliceutil.Map(s, func(i int) string { return strconv.Itoa(i) })
26
+	if len(m) != len(s) {
27
+		t.Fatalf("expected len %d, got %d", len(s), len(m))
28
+	}
29
+	for i, v := range m {
30
+		if expected := strconv.Itoa(s[i]); v != expected {
31
+			t.Fatalf("expected %s, got %s", expected, v)
32
+		}
33
+	}
34
+}
35
+
36
+func TestMapper(t *testing.T) {
37
+	s := []string{"1.2.3.4", "fe80::1"}
38
+	mapper := sliceutil.Mapper(netip.MustParseAddr)
39
+	m := mapper(s)
40
+	if len(m) != len(s) {
41
+		t.Fatalf("expected len %d, got %d", len(s), len(m))
42
+	}
43
+	for i, v := range m {
44
+		if expected := netip.MustParseAddr(s[i]); v != expected {
45
+			t.Fatalf("expected %s, got %s", expected, v)
46
+		}
47
+	}
48
+}
... ...
@@ -17,6 +17,7 @@ import (
17 17
 
18 18
 	"github.com/containerd/log"
19 19
 	"github.com/docker/docker/daemon/internal/otelutil"
20
+	"github.com/docker/docker/daemon/internal/sliceutil"
20 21
 	"github.com/docker/docker/daemon/libnetwork/datastore"
21 22
 	"github.com/docker/docker/daemon/libnetwork/driverapi"
22 23
 	"github.com/docker/docker/daemon/libnetwork/drivers/bridge/internal/firewaller"
... ...
@@ -35,7 +36,6 @@ import (
35 35
 	"github.com/docker/docker/daemon/libnetwork/scope"
36 36
 	"github.com/docker/docker/daemon/libnetwork/types"
37 37
 	"github.com/docker/docker/errdefs"
38
-	"github.com/docker/docker/internal/sliceutil"
39 38
 	"github.com/docker/docker/pkg/stringid"
40 39
 	"github.com/pkg/errors"
41 40
 	"github.com/vishvananda/netlink"
... ...
@@ -11,10 +11,10 @@ import (
11 11
 	"slices"
12 12
 
13 13
 	"github.com/containerd/log"
14
+	"github.com/docker/docker/daemon/internal/sliceutil"
14 15
 	"github.com/docker/docker/daemon/libnetwork/netutils"
15 16
 	"github.com/docker/docker/daemon/libnetwork/portmapperapi"
16 17
 	"github.com/docker/docker/daemon/libnetwork/types"
17
-	"github.com/docker/docker/internal/sliceutil"
18 18
 )
19 19
 
20 20
 // addPortMappings takes cfg, the configuration for port mappings, selects host
... ...
@@ -17,6 +17,7 @@ import (
17 17
 	"testing"
18 18
 
19 19
 	"github.com/containerd/log"
20
+	"github.com/docker/docker/daemon/internal/sliceutil"
20 21
 	"github.com/docker/docker/daemon/libnetwork/drivers/bridge/internal/firewaller"
21 22
 	"github.com/docker/docker/daemon/libnetwork/drvregistry"
22 23
 	"github.com/docker/docker/daemon/libnetwork/netlabel"
... ...
@@ -26,7 +27,6 @@ import (
26 26
 	"github.com/docker/docker/daemon/libnetwork/portmappers/nat"
27 27
 	"github.com/docker/docker/daemon/libnetwork/portmappers/routed"
28 28
 	"github.com/docker/docker/daemon/libnetwork/types"
29
-	"github.com/docker/docker/internal/sliceutil"
30 29
 	"github.com/docker/docker/internal/testutils/netnsutils"
31 30
 	"github.com/docker/docker/internal/testutils/storeutils"
32 31
 	"github.com/sirupsen/logrus"
... ...
@@ -14,6 +14,7 @@ import (
14 14
 	"sync"
15 15
 
16 16
 	"github.com/containerd/log"
17
+	"github.com/docker/docker/daemon/internal/sliceutil"
17 18
 	"github.com/docker/docker/daemon/libnetwork/datastore"
18 19
 	"github.com/docker/docker/daemon/libnetwork/driverapi"
19 20
 	"github.com/docker/docker/daemon/libnetwork/ipamapi"
... ...
@@ -22,7 +23,6 @@ import (
22 22
 	"github.com/docker/docker/daemon/libnetwork/scope"
23 23
 	"github.com/docker/docker/daemon/libnetwork/types"
24 24
 	"github.com/docker/docker/errdefs"
25
-	"github.com/docker/docker/internal/sliceutil"
26 25
 	"github.com/docker/docker/pkg/stringid"
27 26
 	"go.opentelemetry.io/otel"
28 27
 )
... ...
@@ -10,7 +10,7 @@ import (
10 10
 	"strings"
11 11
 	"testing"
12 12
 
13
-	"github.com/docker/docker/internal/sliceutil"
13
+	"github.com/docker/docker/daemon/internal/sliceutil"
14 14
 	"github.com/google/go-cmp/cmp/cmpopts"
15 15
 	"gotest.tools/v3/assert"
16 16
 	is "gotest.tools/v3/assert/cmp"
... ...
@@ -16,6 +16,7 @@ import (
16 16
 	"time"
17 17
 
18 18
 	"github.com/containerd/log"
19
+	"github.com/docker/docker/daemon/internal/sliceutil"
19 20
 	"github.com/docker/docker/daemon/libnetwork/datastore"
20 21
 	"github.com/docker/docker/daemon/libnetwork/driverapi"
21 22
 	"github.com/docker/docker/daemon/libnetwork/internal/netiputil"
... ...
@@ -29,7 +30,6 @@ import (
29 29
 	"github.com/docker/docker/daemon/libnetwork/scope"
30 30
 	"github.com/docker/docker/daemon/libnetwork/types"
31 31
 	"github.com/docker/docker/errdefs"
32
-	"github.com/docker/docker/internal/sliceutil"
33 32
 	"github.com/docker/docker/pkg/stringid"
34 33
 	"go.opentelemetry.io/otel"
35 34
 	"go.opentelemetry.io/otel/attribute"
... ...
@@ -9,8 +9,8 @@ import (
9 9
 	"sync"
10 10
 	"testing"
11 11
 
12
+	"github.com/docker/docker/daemon/internal/sliceutil"
12 13
 	"github.com/docker/docker/daemon/libnetwork/nlwrap"
13
-	"github.com/docker/docker/internal/sliceutil"
14 14
 	"github.com/vishvananda/netlink"
15 15
 	"github.com/vishvananda/netns"
16 16
 	"gotest.tools/v3/assert"
... ...
@@ -7,9 +7,9 @@ import (
7 7
 	"context"
8 8
 
9 9
 	"github.com/containerd/log"
10
+	"github.com/docker/docker/daemon/internal/sliceutil"
10 11
 	"github.com/docker/docker/daemon/libnetwork/portmapperapi"
11 12
 	"github.com/docker/docker/daemon/libnetwork/types"
12
-	"github.com/docker/docker/internal/sliceutil"
13 13
 )
14 14
 
15 15
 const driverName = "routed"
... ...
@@ -10,8 +10,8 @@ import (
10 10
 	"strings"
11 11
 
12 12
 	"github.com/docker/docker/daemon/internal/rootless/mountopts"
13
+	"github.com/docker/docker/daemon/internal/sliceutil"
13 14
 	"github.com/docker/docker/daemon/pkg/oci"
14
-	"github.com/docker/docker/internal/sliceutil"
15 15
 	"github.com/moby/moby/api/types"
16 16
 	"github.com/moby/sys/userns"
17 17
 	"github.com/opencontainers/runtime-spec/specs-go"
... ...
@@ -7,8 +7,8 @@ import (
7 7
 	"context"
8 8
 	"net/http"
9 9
 
10
+	"github.com/docker/docker/daemon/internal/sliceutil"
10 11
 	"github.com/docker/docker/daemon/server/httputils"
11
-	"github.com/docker/docker/internal/sliceutil"
12 12
 	"github.com/docker/docker/pkg/stringid"
13 13
 	"github.com/moby/moby/api/types/backend"
14 14
 	"github.com/moby/moby/api/types/container"
15 15
deleted file mode 100644
... ...
@@ -1,34 +0,0 @@
1
-// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2
-//go:build go1.23
3
-
4
-package sliceutil
5
-
6
-func Dedup[T comparable](slice []T) []T {
7
-	keys := make(map[T]struct{})
8
-	out := make([]T, 0, len(slice))
9
-	for _, s := range slice {
10
-		if _, ok := keys[s]; !ok {
11
-			out = append(out, s)
12
-			keys[s] = struct{}{}
13
-		}
14
-	}
15
-	return out
16
-}
17
-
18
-func Map[S ~[]In, In, Out any](s S, fn func(In) Out) []Out {
19
-	res := make([]Out, len(s))
20
-	for i, v := range s {
21
-		res[i] = fn(v)
22
-	}
23
-	return res
24
-}
25
-
26
-func Mapper[In, Out any](fn func(In) Out) func([]In) []Out {
27
-	return func(s []In) []Out {
28
-		res := make([]Out, len(s))
29
-		for i, v := range s {
30
-			res[i] = fn(v)
31
-		}
32
-		return res
33
-	}
34
-}
35 1
deleted file mode 100644
... ...
@@ -1,49 +0,0 @@
1
-package sliceutil_test
2
-
3
-import (
4
-	"net/netip"
5
-	"strconv"
6
-	"testing"
7
-
8
-	"github.com/docker/docker/internal/sliceutil"
9
-)
10
-
11
-func TestMap(t *testing.T) {
12
-	s := []int{1, 2, 3}
13
-	m := sliceutil.Map(s, func(i int) int { return i * 2 })
14
-	if len(m) != len(s) {
15
-		t.Fatalf("expected len %d, got %d", len(s), len(m))
16
-	}
17
-	for i, v := range m {
18
-		if expected := s[i] * 2; v != expected {
19
-			t.Fatalf("expected %d, got %d", expected, v)
20
-		}
21
-	}
22
-}
23
-
24
-func TestMap_TypeConvert(t *testing.T) {
25
-	s := []int{1, 2, 3}
26
-	m := sliceutil.Map(s, func(i int) string { return strconv.Itoa(i) })
27
-	if len(m) != len(s) {
28
-		t.Fatalf("expected len %d, got %d", len(s), len(m))
29
-	}
30
-	for i, v := range m {
31
-		if expected := strconv.Itoa(s[i]); v != expected {
32
-			t.Fatalf("expected %s, got %s", expected, v)
33
-		}
34
-	}
35
-}
36
-
37
-func TestMapper(t *testing.T) {
38
-	s := []string{"1.2.3.4", "fe80::1"}
39
-	mapper := sliceutil.Mapper(netip.MustParseAddr)
40
-	m := mapper(s)
41
-	if len(m) != len(s) {
42
-		t.Fatalf("expected len %d, got %d", len(s), len(m))
43
-	}
44
-	for i, v := range m {
45
-		if expected := netip.MustParseAddr(s[i]); v != expected {
46
-			t.Fatalf("expected %s, got %s", expected, v)
47
-		}
48
-	}
49
-}