Skip to content

Commit 13d70b1

Browse files
zjumathcodendeloof
authored andcommitted
refactor: replace interface{} with any for clarity and modernization
Signed-off-by: zjumathcode <[email protected]>
1 parent 72f4d65 commit 13d70b1

File tree

9 files changed

+20
-20
lines changed

9 files changed

+20
-20
lines changed

cmd/compose/events.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func runEvents(ctx context.Context, dockerCli command.Cli, backendOptions *Backe
7272
Until: opts.until,
7373
Consumer: func(event api.Event) error {
7474
if opts.json {
75-
marshal, err := json.Marshal(map[string]interface{}{
75+
marshal, err := json.Marshal(map[string]any{
7676
"time": event.Timestamp,
7777
"type": "container",
7878
"service": event.Service,

cmd/formatter/container.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type ContainerContext struct {
105105
// used in the template. It's currently only used to detect use of the .Size
106106
// field which (if used) automatically sets the '--size' option when making
107107
// the API call.
108-
FieldsUsed map[string]interface{}
108+
FieldsUsed map[string]any
109109
}
110110

111111
// NewContainerContext creates a new context for rendering containers
@@ -274,7 +274,7 @@ func (c *ContainerContext) Networks() string {
274274
// Size returns the container's size and virtual size (e.g. "2B (virtual 21.5MB)")
275275
func (c *ContainerContext) Size() string {
276276
if c.FieldsUsed == nil {
277-
c.FieldsUsed = map[string]interface{}{}
277+
c.FieldsUsed = map[string]any{}
278278
}
279279
c.FieldsUsed["Size"] = struct{}{}
280280
srw := units.HumanSizeWithPrecision(float64(c.c.SizeRw), 3)

cmd/formatter/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
// Print prints formatted lists in different formats
29-
func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func(w io.Writer), headers ...string) error {
29+
func Print(toJSON any, format string, outWriter io.Writer, writerFn func(w io.Writer), headers ...string) error {
3030
switch strings.ToLower(format) {
3131
case TABLE, PRETTY, "":
3232
return PrintPrettySection(outWriter, writerFn, headers...)

cmd/formatter/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import (
2424
const standardIndentation = " "
2525

2626
// ToStandardJSON return a string with the JSON representation of the interface{}
27-
func ToStandardJSON(i interface{}) (string, error) {
27+
func ToStandardJSON(i any) (string, error) {
2828
return ToJSON(i, "", standardIndentation)
2929
}
3030

3131
// ToJSON return a string with the JSON representation of the interface{}
32-
func ToJSON(i interface{}, prefix string, indentation string) (string, error) {
32+
func ToJSON(i any, prefix string, indentation string) (string, error) {
3333
buffer := &bytes.Buffer{}
3434
encoder := json.NewEncoder(buffer)
3535
encoder.SetEscapeHTML(false)

cmd/formatter/logs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (l *logConsumer) register(name string) *presenter {
8787
l.presenters.Store(name, p)
8888
l.computeWidth()
8989
if l.prefix {
90-
l.presenters.Range(func(key, value interface{}) bool {
90+
l.presenters.Range(func(key, value any) bool {
9191
p := value.(*presenter)
9292
p.setPrefix(l.width)
9393
return true
@@ -137,7 +137,7 @@ func (l *logConsumer) Status(container, msg string) {
137137

138138
func (l *logConsumer) computeWidth() {
139139
width := 0
140-
l.presenters.Range(func(key, value interface{}) bool {
140+
l.presenters.Range(func(key, value any) bool {
141141
p := value.(*presenter)
142142
if len(p.name) > width {
143143
width = len(p.name)

internal/tracing/docker_context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ func ConfigFromDockerContext(st store.Store, name string) (OTLPConfig, error) {
8484
return OTLPConfig{}, err
8585
}
8686

87-
var otelCfg interface{}
87+
var otelCfg any
8888
switch m := meta.Metadata.(type) {
8989
case command.DockerContext:
9090
otelCfg = m.AdditionalFields[otelConfigFieldName]
91-
case map[string]interface{}:
91+
case map[string]any:
9292
otelCfg = m[otelConfigFieldName]
9393
}
9494
if otelCfg == nil {
9595
return OTLPConfig{}, nil
9696
}
9797

98-
otelMap, ok := otelCfg.(map[string]interface{})
98+
otelMap, ok := otelCfg.(map[string]any)
9999
if !ok {
100100
return OTLPConfig{}, fmt.Errorf(
101101
"unexpected type for field %q: %T (expected: %T)",
@@ -115,7 +115,7 @@ func ConfigFromDockerContext(st store.Store, name string) (OTLPConfig, error) {
115115
// valueOrDefault returns the type-cast value at the specified key in the map
116116
// if present and the correct type; otherwise, it returns the default value for
117117
// T.
118-
func valueOrDefault[T any](m map[string]interface{}, key string) T {
118+
func valueOrDefault[T any](m map[string]any, key string) T {
119119
if v, ok := m[key].(T); ok {
120120
return v
121121
}

internal/tracing/tracing_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
)
2828

2929
var testStoreCfg = store.NewConfig(
30-
func() interface{} {
31-
return &map[string]interface{}{}
30+
func() any {
31+
return &map[string]any{}
3232
},
3333
)
3434

@@ -44,13 +44,13 @@ func TestExtractOtelFromContext(t *testing.T) {
4444
Name: "test",
4545
Metadata: command.DockerContext{
4646
Description: t.Name(),
47-
AdditionalFields: map[string]interface{}{
48-
"otel": map[string]interface{}{
47+
AdditionalFields: map[string]any{
48+
"otel": map[string]any{
4949
"OTEL_EXPORTER_OTLP_ENDPOINT": "localhost:1234",
5050
},
5151
},
5252
},
53-
Endpoints: make(map[string]interface{}),
53+
Endpoints: make(map[string]any),
5454
})
5555
require.NoError(t, err)
5656

pkg/e2e/assert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
func RequireServiceState(t testing.TB, cli *CLI, service string, state string) {
3030
t.Helper()
3131
psRes := cli.RunDockerComposeCmd(t, "ps", "--all", "--format=json", service)
32-
var svc map[string]interface{}
32+
var svc map[string]any
3333
require.NoError(t, json.Unmarshal([]byte(psRes.Stdout()), &svc),
3434
"Invalid `compose ps` JSON: command output: %s",
3535
psRes.Combined())

pkg/watch/watcher_darwin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type fseventNotify struct {
3737
errors chan error
3838
stop chan struct{}
3939

40-
pathsWereWatching map[string]interface{}
40+
pathsWereWatching map[string]any
4141
}
4242

4343
func (d *fseventNotify) loop() {
@@ -71,7 +71,7 @@ func (d *fseventNotify) initAdd(name string) {
7171
d.stream.Paths = append(d.stream.Paths, name)
7272

7373
if d.pathsWereWatching == nil {
74-
d.pathsWereWatching = make(map[string]interface{})
74+
d.pathsWereWatching = make(map[string]any)
7575
}
7676
d.pathsWereWatching[name] = struct{}{}
7777
}

0 commit comments

Comments
 (0)