From 1550a144416b46706f4f0018462c8e4683d3849a Mon Sep 17 00:00:00 2001 From: zjumathcode Date: Tue, 2 Dec 2025 15:25:18 +0800 Subject: [PATCH] refactor: replace interface{} with any for clarity and modernization Signed-off-by: zjumathcode --- cmd/compose/events.go | 2 +- cmd/formatter/container.go | 4 ++-- cmd/formatter/formatter.go | 2 +- cmd/formatter/json.go | 4 ++-- cmd/formatter/logs.go | 4 ++-- internal/tracing/docker_context.go | 8 ++++---- internal/tracing/tracing_test.go | 10 +++++----- pkg/e2e/assert.go | 2 +- pkg/watch/watcher_darwin.go | 4 ++-- 9 files changed, 20 insertions(+), 20 deletions(-) diff --git a/cmd/compose/events.go b/cmd/compose/events.go index 79b8b82047b..7f8a4a77df4 100644 --- a/cmd/compose/events.go +++ b/cmd/compose/events.go @@ -72,7 +72,7 @@ func runEvents(ctx context.Context, dockerCli command.Cli, backendOptions *Backe Until: opts.until, Consumer: func(event api.Event) error { if opts.json { - marshal, err := json.Marshal(map[string]interface{}{ + marshal, err := json.Marshal(map[string]any{ "time": event.Timestamp, "type": "container", "service": event.Service, diff --git a/cmd/formatter/container.go b/cmd/formatter/container.go index 622d6a3e27e..488caf4afad 100644 --- a/cmd/formatter/container.go +++ b/cmd/formatter/container.go @@ -105,7 +105,7 @@ type ContainerContext struct { // used in the template. It's currently only used to detect use of the .Size // field which (if used) automatically sets the '--size' option when making // the API call. - FieldsUsed map[string]interface{} + FieldsUsed map[string]any } // NewContainerContext creates a new context for rendering containers @@ -274,7 +274,7 @@ func (c *ContainerContext) Networks() string { // Size returns the container's size and virtual size (e.g. "2B (virtual 21.5MB)") func (c *ContainerContext) Size() string { if c.FieldsUsed == nil { - c.FieldsUsed = map[string]interface{}{} + c.FieldsUsed = map[string]any{} } c.FieldsUsed["Size"] = struct{}{} srw := units.HumanSizeWithPrecision(float64(c.c.SizeRw), 3) diff --git a/cmd/formatter/formatter.go b/cmd/formatter/formatter.go index 265eff8c5e8..6af92c478db 100644 --- a/cmd/formatter/formatter.go +++ b/cmd/formatter/formatter.go @@ -26,7 +26,7 @@ import ( ) // Print prints formatted lists in different formats -func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func(w io.Writer), headers ...string) error { +func Print(toJSON any, format string, outWriter io.Writer, writerFn func(w io.Writer), headers ...string) error { switch strings.ToLower(format) { case TABLE, PRETTY, "": return PrintPrettySection(outWriter, writerFn, headers...) diff --git a/cmd/formatter/json.go b/cmd/formatter/json.go index afadb0c814b..b09e721aa27 100644 --- a/cmd/formatter/json.go +++ b/cmd/formatter/json.go @@ -24,12 +24,12 @@ import ( const standardIndentation = " " // ToStandardJSON return a string with the JSON representation of the interface{} -func ToStandardJSON(i interface{}) (string, error) { +func ToStandardJSON(i any) (string, error) { return ToJSON(i, "", standardIndentation) } // ToJSON return a string with the JSON representation of the interface{} -func ToJSON(i interface{}, prefix string, indentation string) (string, error) { +func ToJSON(i any, prefix string, indentation string) (string, error) { buffer := &bytes.Buffer{} encoder := json.NewEncoder(buffer) encoder.SetEscapeHTML(false) diff --git a/cmd/formatter/logs.go b/cmd/formatter/logs.go index a97055b37a3..1ee35e96b32 100644 --- a/cmd/formatter/logs.go +++ b/cmd/formatter/logs.go @@ -87,7 +87,7 @@ func (l *logConsumer) register(name string) *presenter { l.presenters.Store(name, p) l.computeWidth() if l.prefix { - l.presenters.Range(func(key, value interface{}) bool { + l.presenters.Range(func(key, value any) bool { p := value.(*presenter) p.setPrefix(l.width) return true @@ -137,7 +137,7 @@ func (l *logConsumer) Status(container, msg string) { func (l *logConsumer) computeWidth() { width := 0 - l.presenters.Range(func(key, value interface{}) bool { + l.presenters.Range(func(key, value any) bool { p := value.(*presenter) if len(p.name) > width { width = len(p.name) diff --git a/internal/tracing/docker_context.go b/internal/tracing/docker_context.go index 97e682ac7c5..5d5367b0231 100644 --- a/internal/tracing/docker_context.go +++ b/internal/tracing/docker_context.go @@ -84,18 +84,18 @@ func ConfigFromDockerContext(st store.Store, name string) (OTLPConfig, error) { return OTLPConfig{}, err } - var otelCfg interface{} + var otelCfg any switch m := meta.Metadata.(type) { case command.DockerContext: otelCfg = m.AdditionalFields[otelConfigFieldName] - case map[string]interface{}: + case map[string]any: otelCfg = m[otelConfigFieldName] } if otelCfg == nil { return OTLPConfig{}, nil } - otelMap, ok := otelCfg.(map[string]interface{}) + otelMap, ok := otelCfg.(map[string]any) if !ok { return OTLPConfig{}, fmt.Errorf( "unexpected type for field %q: %T (expected: %T)", @@ -115,7 +115,7 @@ func ConfigFromDockerContext(st store.Store, name string) (OTLPConfig, error) { // valueOrDefault returns the type-cast value at the specified key in the map // if present and the correct type; otherwise, it returns the default value for // T. -func valueOrDefault[T any](m map[string]interface{}, key string) T { +func valueOrDefault[T any](m map[string]any, key string) T { if v, ok := m[key].(T); ok { return v } diff --git a/internal/tracing/tracing_test.go b/internal/tracing/tracing_test.go index 557f13b4727..3d262631921 100644 --- a/internal/tracing/tracing_test.go +++ b/internal/tracing/tracing_test.go @@ -27,8 +27,8 @@ import ( ) var testStoreCfg = store.NewConfig( - func() interface{} { - return &map[string]interface{}{} + func() any { + return &map[string]any{} }, ) @@ -44,13 +44,13 @@ func TestExtractOtelFromContext(t *testing.T) { Name: "test", Metadata: command.DockerContext{ Description: t.Name(), - AdditionalFields: map[string]interface{}{ - "otel": map[string]interface{}{ + AdditionalFields: map[string]any{ + "otel": map[string]any{ "OTEL_EXPORTER_OTLP_ENDPOINT": "localhost:1234", }, }, }, - Endpoints: make(map[string]interface{}), + Endpoints: make(map[string]any), }) require.NoError(t, err) diff --git a/pkg/e2e/assert.go b/pkg/e2e/assert.go index 395e3bb9e19..25547203dbe 100644 --- a/pkg/e2e/assert.go +++ b/pkg/e2e/assert.go @@ -29,7 +29,7 @@ import ( func RequireServiceState(t testing.TB, cli *CLI, service string, state string) { t.Helper() psRes := cli.RunDockerComposeCmd(t, "ps", "--all", "--format=json", service) - var svc map[string]interface{} + var svc map[string]any require.NoError(t, json.Unmarshal([]byte(psRes.Stdout()), &svc), "Invalid `compose ps` JSON: command output: %s", psRes.Combined()) diff --git a/pkg/watch/watcher_darwin.go b/pkg/watch/watcher_darwin.go index e2f665a1b8f..16adcf7c25a 100644 --- a/pkg/watch/watcher_darwin.go +++ b/pkg/watch/watcher_darwin.go @@ -37,7 +37,7 @@ type fseventNotify struct { errors chan error stop chan struct{} - pathsWereWatching map[string]interface{} + pathsWereWatching map[string]any } func (d *fseventNotify) loop() { @@ -71,7 +71,7 @@ func (d *fseventNotify) initAdd(name string) { d.stream.Paths = append(d.stream.Paths, name) if d.pathsWereWatching == nil { - d.pathsWereWatching = make(map[string]interface{}) + d.pathsWereWatching = make(map[string]any) } d.pathsWereWatching[name] = struct{}{} }