Skip to content

Commit 874b831

Browse files
authored
Merge pull request #6721 from thaJeztah/less_reflect
reduce some uses of reflect package
2 parents d20f30c + 8205124 commit 874b831

File tree

7 files changed

+56
-51
lines changed

7 files changed

+56
-51
lines changed

cli/command/container/opts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*network.EndpointSett
795795
// and only a single network is specified, omit the endpoint-configuration
796796
// on the client (the daemon will still create it when creating the container)
797797
if i == 0 && len(copts.netMode.Value()) == 1 {
798-
if ep == nil || reflect.DeepEqual(*ep, network.EndpointSettings{}) {
798+
if ep == nil || reflect.ValueOf(*ep).IsZero() {
799799
continue
800800
}
801801
}

cli/command/formatter/reflect_test.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
package formatter
55

66
import (
7-
"reflect"
87
"testing"
8+
9+
"gotest.tools/v3/assert"
10+
is "gotest.tools/v3/assert/cmp"
911
)
1012

1113
type dummy struct{}
@@ -45,24 +47,18 @@ var dummyExpected = map[string]any{
4547
func TestMarshalMap(t *testing.T) {
4648
d := dummy{}
4749
m, err := marshalMap(&d)
48-
if err != nil {
49-
t.Fatal(err)
50-
}
51-
if !reflect.DeepEqual(dummyExpected, m) {
52-
t.Fatalf("expected %+v, got %+v",
53-
dummyExpected, m)
54-
}
50+
assert.NilError(t, err)
51+
assert.Check(t, is.DeepEqual(m, dummyExpected))
5552
}
5653

5754
func TestMarshalMapBad(t *testing.T) {
58-
if _, err := marshalMap(nil); err == nil {
59-
t.Fatal("expected an error (argument is nil)")
60-
}
61-
if _, err := marshalMap(dummy{}); err == nil {
62-
t.Fatal("expected an error (argument is non-pointer)")
63-
}
55+
_, err := marshalMap(nil)
56+
assert.Check(t, is.Error(err, "expected a pointer to a struct, got invalid"), "expected an error (argument is nil)")
57+
58+
_, err = marshalMap(dummy{})
59+
assert.Check(t, is.Error(err, "expected a pointer to a struct, got struct"), "expected an error (argument is non-pointer)")
60+
6461
x := 42
65-
if _, err := marshalMap(&x); err == nil {
66-
t.Fatal("expected an error (argument is a pointer to non-struct)")
67-
}
62+
_, err = marshalMap(&x)
63+
assert.Check(t, is.Error(err, "expected a pointer to a struct, got a pointer to int"), "expected an error (argument is a pointer to non-struct)")
6864
}

cli/command/node/formatter.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package node
22

33
import (
4+
"bytes"
45
"encoding/base64"
56
"fmt"
6-
"reflect"
77
"strings"
88

99
"github.com/docker/cli/cli/command/formatter"
@@ -170,15 +170,23 @@ func (c *nodeContext) ManagerStatus() string {
170170
}
171171

172172
func (c *nodeContext) TLSStatus() string {
173-
if c.info.Cluster == nil || reflect.DeepEqual(c.info.Cluster.TLSInfo, swarm.TLSInfo{}) || reflect.DeepEqual(c.n.Description.TLSInfo, swarm.TLSInfo{}) {
173+
if c.info.Cluster == nil || isEmptyTLSInfo(c.info.Cluster.TLSInfo) || isEmptyTLSInfo(c.n.Description.TLSInfo) {
174174
return "Unknown"
175175
}
176-
if reflect.DeepEqual(c.n.Description.TLSInfo, c.info.Cluster.TLSInfo) {
176+
if equalTLSInfo(c.n.Description.TLSInfo, c.info.Cluster.TLSInfo) {
177177
return "Ready"
178178
}
179179
return "Needs Rotation"
180180
}
181181

182+
func isEmptyTLSInfo(t swarm.TLSInfo) bool {
183+
return t.TrustRoot == "" && len(t.CertIssuerSubject) == 0 && len(t.CertIssuerPublicKey) == 0
184+
}
185+
186+
func equalTLSInfo(t, o swarm.TLSInfo) bool {
187+
return t.TrustRoot == o.TrustRoot && bytes.Equal(t.CertIssuerSubject, o.CertIssuerSubject) && bytes.Equal(t.CertIssuerPublicKey, o.CertIssuerPublicKey)
188+
}
189+
182190
func (c *nodeContext) EngineVersion() string {
183191
return c.n.Description.Engine.EngineVersion
184192
}
@@ -320,8 +328,7 @@ func (ctx *nodeInspectContext) EngineVersion() string {
320328
}
321329

322330
func (ctx *nodeInspectContext) HasTLSInfo() bool {
323-
tlsInfo := ctx.Node.Description.TLSInfo
324-
return !reflect.DeepEqual(tlsInfo, swarm.TLSInfo{})
331+
return !isEmptyTLSInfo(ctx.Node.Description.TLSInfo)
325332
}
326333

327334
func (ctx *nodeInspectContext) TLSInfoTrustRoot() string {

cli/command/service/update_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"fmt"
66
"net/netip"
7-
"reflect"
87
"sort"
8+
"strconv"
99
"testing"
1010
"time"
1111

@@ -368,23 +368,23 @@ func TestUpdateHealthcheckTable(t *testing.T) {
368368
err: "--no-healthcheck conflicts with --health-* options",
369369
},
370370
}
371-
for i, c := range testCases {
372-
flags := newUpdateCommand(nil).Flags()
373-
for _, flag := range c.flags {
374-
flags.Set(flag[0], flag[1])
375-
}
376-
cspec := &swarm.ContainerSpec{
377-
Healthcheck: c.initial,
378-
}
379-
err := updateHealthcheck(flags, cspec)
380-
if c.err != "" {
381-
assert.Error(t, err, c.err)
382-
} else {
383-
assert.NilError(t, err)
384-
if !reflect.DeepEqual(cspec.Healthcheck, c.expected) {
385-
t.Errorf("incorrect result for test %d, expected health config:\n\t%#v\ngot:\n\t%#v", i, c.expected, cspec.Healthcheck)
371+
for i, tc := range testCases {
372+
t.Run(strconv.Itoa(i), func(t *testing.T) {
373+
flags := newUpdateCommand(nil).Flags()
374+
for _, flag := range tc.flags {
375+
assert.Check(t, flags.Set(flag[0], flag[1]))
386376
}
387-
}
377+
cspec := &swarm.ContainerSpec{
378+
Healthcheck: tc.initial,
379+
}
380+
err := updateHealthcheck(flags, cspec)
381+
if tc.err != "" {
382+
assert.Error(t, err, tc.err)
383+
} else {
384+
assert.NilError(t, err)
385+
assert.Check(t, is.DeepEqual(cspec.Healthcheck, tc.expected))
386+
}
387+
})
388388
}
389389
}
390390

cli/command/telemetry_utils_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"bytes"
55
"context"
66
"io"
7-
"reflect"
87
"strings"
98
"testing"
109

1110
"github.com/docker/cli/cli/streams"
11+
"github.com/google/go-cmp/cmp/cmpopts"
1212
"github.com/spf13/cobra"
1313
"go.opentelemetry.io/otel/attribute"
1414
"gotest.tools/v3/assert"
15+
is "gotest.tools/v3/assert/cmp"
1516
)
1617

1718
func setupCobraCommands() (*cobra.Command, *cobra.Command, *cobra.Command) {
@@ -139,7 +140,7 @@ func TestStdioAttributes(t *testing.T) {
139140
cli.Out().SetIsTerminal(tc.stdoutTty)
140141
actual := stdioAttributes(cli)
141142

142-
assert.Check(t, reflect.DeepEqual(actual, tc.expected))
143+
assert.Check(t, is.DeepEqual(actual, tc.expected, cmpopts.EquateComparable(attribute.Value{})))
143144
})
144145
}
145146
}
@@ -179,7 +180,7 @@ func TestAttributesFromError(t *testing.T) {
179180
t.Run(tc.testName, func(t *testing.T) {
180181
t.Parallel()
181182
actual := attributesFromError(tc.err)
182-
assert.Check(t, reflect.DeepEqual(actual, tc.expected))
183+
assert.Check(t, is.DeepEqual(actual, tc.expected, cmpopts.EquateComparable(attribute.Value{})))
183184
})
184185
}
185186
}

cli/command/volume/create_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
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.24
3+
14
package volume
25

36
import (
47
"errors"
58
"fmt"
69
"io"
7-
"reflect"
10+
"maps"
811
"sort"
912
"strings"
1013
"testing"
@@ -124,10 +127,10 @@ func TestVolumeCreateWithFlags(t *testing.T) {
124127
if options.Driver != expectedDriver {
125128
return client.VolumeCreateResult{}, fmt.Errorf("expected driver %q, got %q", expectedDriver, options.Driver)
126129
}
127-
if !reflect.DeepEqual(options.DriverOpts, expectedOpts) {
130+
if !maps.Equal(options.DriverOpts, expectedOpts) {
128131
return client.VolumeCreateResult{}, fmt.Errorf("expected drivers opts %v, got %v", expectedOpts, options.DriverOpts)
129132
}
130-
if !reflect.DeepEqual(options.Labels, expectedLabels) {
133+
if !maps.Equal(options.Labels, expectedLabels) {
131134
return client.VolumeCreateResult{}, fmt.Errorf("expected labels %v, got %v", expectedLabels, options.Labels)
132135
}
133136
return client.VolumeCreateResult{

cli/connhelper/connhelper_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package connhelper
22

33
import (
4-
"reflect"
54
"testing"
65

76
"gotest.tools/v3/assert"
@@ -27,7 +26,8 @@ func TestSSHFlags(t *testing.T) {
2726
}
2827

2928
for _, tc := range testCases {
30-
assert.DeepEqual(t, addSSHTimeout(tc.in), tc.out)
29+
result := addSSHTimeout(tc.in)
30+
assert.DeepEqual(t, result, tc.out)
3131
}
3232
}
3333

@@ -57,9 +57,7 @@ func TestDisablePseudoTerminalAllocation(t *testing.T) {
5757
for _, tc := range testCases {
5858
t.Run(tc.name, func(t *testing.T) {
5959
result := disablePseudoTerminalAllocation(tc.sshFlags)
60-
if !reflect.DeepEqual(result, tc.expected) {
61-
t.Errorf("expected %v, got %v", tc.expected, result)
62-
}
60+
assert.DeepEqual(t, result, tc.expected)
6361
})
6462
}
6563
}

0 commit comments

Comments
 (0)