Skip to content

Commit

Permalink
golangci-lint: revive: enable use-any
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Nov 20, 2023
1 parent 9c0c49a commit 0e73168
Show file tree
Hide file tree
Showing 60 changed files with 572 additions and 568 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ linters-settings:
- name: empty-lines
severity: warning
disabled: false
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#use-any
- name: use-any
severity: warning
disabled: false

issues:
# The default exclusion rules are a bit too permissive, so copying the relevant ones below
Expand Down
2 changes: 1 addition & 1 deletion cli-plugins/manager/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ func wrapAsPluginError(err error, msg string) error {

// NewPluginError creates a new pluginError, analogous to
// errors.Errorf.
func NewPluginError(msg string, args ...interface{}) error {
func NewPluginError(msg string, args ...any) error {
return &pluginError{cause: errors.Errorf(msg, args...)}
}
4 changes: 2 additions & 2 deletions cli/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func UserAgent() string {
}

var defaultStoreEndpoints = []store.NamedTypeGetter{
store.EndpointTypeGetter(docker.DockerEndpoint, func() interface{} { return &docker.EndpointMeta{} }),
store.EndpointTypeGetter(docker.DockerEndpoint, func() any { return &docker.EndpointMeta{} }),
}

// RegisterDefaultStoreEndpoints registers a new named endpoint
Expand All @@ -528,7 +528,7 @@ func RegisterDefaultStoreEndpoints(ep ...store.NamedTypeGetter) {
// DefaultContextStoreConfig returns a new store.Config with the default set of endpoints configured.
func DefaultContextStoreConfig() store.Config {
return store.NewConfig(
func() interface{} { return &DockerContext{} },
func() any { return &DockerContext{} },
defaultStoreEndpoints...,
)
}
2 changes: 1 addition & 1 deletion cli/command/config/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func RunConfigInspect(dockerCli command.Cli, opts InspectOptions) error {
opts.Format = "pretty"
}

getRef := func(id string) (interface{}, []byte, error) {
getRef := func(id string) (any, []byte, error) {
return client.ConfigInspectWithRaw(ctx, id)
}
f := opts.Format
Expand Down
2 changes: 1 addition & 1 deletion cli/command/container/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func runInspect(dockerCli command.Cli, opts inspectOptions) error {
client := dockerCli.Client()
ctx := context.Background()

getRefFunc := func(ref string) (interface{}, []byte, error) {
getRefFunc := func(ref string) (any, []byte, error) {
return client.ContainerInspectWithRaw(ctx, ref, opts.size)
}
return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
Expand Down
8 changes: 4 additions & 4 deletions cli/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
// DockerContext is a typed representation of what we put in Context metadata
type DockerContext struct {
Description string
AdditionalFields map[string]interface{}
AdditionalFields map[string]any
}

// MarshalJSON implements custom JSON marshalling
func (dc DockerContext) MarshalJSON() ([]byte, error) {
s := map[string]interface{}{}
s := map[string]any{}
if dc.Description != "" {
s["Description"] = dc.Description
}
Expand All @@ -29,7 +29,7 @@ func (dc DockerContext) MarshalJSON() ([]byte, error) {

// UnmarshalJSON implements custom JSON marshalling
func (dc *DockerContext) UnmarshalJSON(payload []byte) error {
var data map[string]interface{}
var data map[string]any
if err := json.Unmarshal(payload, &data); err != nil {
return err
}
Expand All @@ -39,7 +39,7 @@ func (dc *DockerContext) UnmarshalJSON(payload []byte) error {
dc.Description = v.(string)
default:
if dc.AdditionalFields == nil {
dc.AdditionalFields = make(map[string]interface{})
dc.AdditionalFields = make(map[string]any)
}
dc.AdditionalFields[k] = v
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/context/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func createNewContext(contextStore store.ReaderWriter, o *CreateOptions) error {
return errors.Wrap(err, "unable to create docker endpoint config")
}
contextMetadata := store.Metadata{
Endpoints: map[string]interface{}{
Endpoints: map[string]any{
docker.DockerEndpoint: dockerEP,
},
Metadata: command.DockerContext{
Expand Down
6 changes: 3 additions & 3 deletions cli/command/context/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) *test.FakeCli {
t.Helper()
dir := t.TempDir()
storeConfig := store.NewConfig(
func() interface{} { return &command.DockerContext{} },
store.EndpointTypeGetter(docker.DockerEndpoint, func() interface{} { return &docker.EndpointMeta{} }),
func() any { return &command.DockerContext{} },
store.EndpointTypeGetter(docker.DockerEndpoint, func() any { return &docker.EndpointMeta{} }),
)
contextStore := &command.ContextStoreWithDefault{
Store: store.New(dir, storeConfig),
Resolver: func() (*command.DefaultContext, error) {
return &command.DefaultContext{
Meta: store.Metadata{
Endpoints: map[string]interface{}{
Endpoints: map[string]any{
docker.DockerEndpoint: docker.EndpointMeta{
Host: "unix:///var/run/docker.sock",
},
Expand Down
2 changes: 1 addition & 1 deletion cli/command/context/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
}

func runInspect(dockerCli command.Cli, opts inspectOptions) error {
getRefFunc := func(ref string) (interface{}, []byte, error) {
getRefFunc := func(ref string) (any, []byte, error) {
c, err := dockerCli.ContextStore().GetMetadata(ref)
if err != nil {
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion cli/command/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestDockerContextMetadataKeepAdditionalFields(t *testing.T) {
c := DockerContext{
Description: "test",
AdditionalFields: map[string]interface{}{
AdditionalFields: map[string]any{
"foo": "bar",
},
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command/defaultcontextstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type EndpointDefaultResolver interface {
// returns nil, nil, nil.
//
//nolint:dupword // ignore "Duplicate words (nil,) found"
ResolveDefault() (interface{}, *store.EndpointTLSData, error)
ResolveDefault() (any, *store.EndpointTLSData, error)
}

// ResolveDefaultContext creates a Metadata for the current CLI invocation parameters
Expand All @@ -55,7 +55,7 @@ func ResolveDefaultContext(opts *cliflags.ClientOptions, config store.Config) (*
Endpoints: make(map[string]store.EndpointTLSData),
}
contextMetadata := store.Metadata{
Endpoints: make(map[string]interface{}),
Endpoints: make(map[string]any),
Metadata: DockerContext{
Description: "",
},
Expand Down
10 changes: 5 additions & 5 deletions cli/command/defaultcontextstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ type testContext struct {
Bar string `json:"another_very_recognizable_field_name"`
}

var testCfg = store.NewConfig(func() interface{} { return &testContext{} },
store.EndpointTypeGetter("ep1", func() interface{} { return &endpoint{} }),
store.EndpointTypeGetter("ep2", func() interface{} { return &endpoint{} }),
var testCfg = store.NewConfig(func() any { return &testContext{} },
store.EndpointTypeGetter("ep1", func() any { return &endpoint{} }),
store.EndpointTypeGetter("ep2", func() any { return &endpoint{} }),
)

func testDefaultMetadata() store.Metadata {
return store.Metadata{
Endpoints: map[string]interface{}{
Endpoints: map[string]any{
"ep1": endpoint{Foo: "bar"},
},
Metadata: testContext{Bar: "baz"},
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestErrCreateDefault(t *testing.T) {
meta := testDefaultMetadata()
s := testStore(t, meta, store.ContextTLSData{})
err := s.CreateOrUpdate(store.Metadata{
Endpoints: map[string]interface{}{
Endpoints: map[string]any{
"ep1": endpoint{Foo: "bar"},
},
Metadata: testContext{Bar: "baz"},
Expand Down
4 changes: 2 additions & 2 deletions cli/command/formatter/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,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
Expand Down Expand Up @@ -226,7 +226,7 @@ func (c *ContainerContext) Status() 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)
Expand Down
4 changes: 2 additions & 2 deletions cli/command/formatter/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unix, State: "running"},
}
expectedCreated := time.Unix(unix, 0).String()
expectedJSONs := []map[string]interface{}{
expectedJSONs := []map[string]any{
{
"Command": "\"\"",
"CreatedAt": expectedCreated,
Expand Down Expand Up @@ -380,7 +380,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
}
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
msg := fmt.Sprintf("Output: line %d: %s", i, line)
var m map[string]interface{}
var m map[string]any
err := json.Unmarshal([]byte(line), &m)
assert.NilError(t, err, msg)
assert.Check(t, is.DeepEqual(expectedJSONs[i], m), msg)
Expand Down
6 changes: 3 additions & 3 deletions cli/command/formatter/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (

// SubContext defines what Context implementation should provide
type SubContext interface {
FullHeader() interface{}
FullHeader() any
}

// SubHeaderContext is a map destined to formatter header (table format)
Expand All @@ -39,10 +39,10 @@ func (c SubHeaderContext) Label(name string) string {

// HeaderContext provides the subContext interface for managing headers
type HeaderContext struct {
Header interface{}
Header any
}

// FullHeader returns the header as an interface
func (c *HeaderContext) FullHeader() interface{} {
func (c *HeaderContext) FullHeader() any {
return c.Header
}
2 changes: 1 addition & 1 deletion cli/command/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Context struct {

// internal element
finalFormat string
header interface{}
header any
buffer *bytes.Buffer
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type fakeSubContext struct {
Name string
}

func (f fakeSubContext) FullHeader() interface{} {
func (f fakeSubContext) FullHeader() any {
return map[string]string{"Name": "NAME"}
}

Expand Down
10 changes: 5 additions & 5 deletions cli/command/formatter/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (

// MarshalJSON marshals x into json
// It differs a bit from encoding/json MarshalJSON function for formatter
func MarshalJSON(x interface{}) ([]byte, error) {
func MarshalJSON(x any) ([]byte, error) {
m, err := marshalMap(x)
if err != nil {
return nil, err
}
return json.Marshal(m)
}

// marshalMap marshals x to map[string]interface{}
func marshalMap(x interface{}) (map[string]interface{}, error) {
// marshalMap marshals x to map[string]any
func marshalMap(x any) (map[string]any, error) {
val := reflect.ValueOf(x)
if val.Kind() != reflect.Ptr {
return nil, errors.Errorf("expected a pointer to a struct, got %v", val.Kind())
Expand All @@ -32,7 +32,7 @@ func marshalMap(x interface{}) (map[string]interface{}, error) {
return nil, errors.Errorf("expected a pointer to a struct, got a pointer to %v", valElem.Kind())
}
typ := val.Type()
m := make(map[string]interface{})
m := make(map[string]any)
for i := 0; i < val.NumMethod(); i++ {
k, v, err := marshalForMethod(typ.Method(i), val.Method(i))
if err != nil {
Expand All @@ -49,7 +49,7 @@ var unmarshallableNames = map[string]struct{}{"FullHeader": {}}

// marshalForMethod returns the map key and the map value for marshalling the method.
// It returns ("", nil, nil) for valid but non-marshallable parameter. (e.g. "unexportedFunc()")
func marshalForMethod(typ reflect.Method, val reflect.Value) (string, interface{}, error) {
func marshalForMethod(typ reflect.Method, val reflect.Value) (string, any, error) {
if val.Kind() != reflect.Func {
return "", nil, errors.Errorf("expected func, got %v", val.Kind())
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/formatter/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (d *dummy) FullHeader() string {
return "FullHeader(should not be marshalled)"
}

var dummyExpected = map[string]interface{}{
var dummyExpected = map[string]any{
"Func1": "Func1",
"Func4": 4,
"Func5": dummyType("Func5"),
Expand Down
4 changes: 2 additions & 2 deletions cli/command/formatter/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestVolumeContextWriteJSON(t *testing.T) {
{Driver: "foo", Name: "foobar_baz"},
{Driver: "bar", Name: "foobar_bar"},
}
expectedJSONs := []map[string]interface{}{
expectedJSONs := []map[string]any{
{"Availability": "N/A", "Driver": "foo", "Group": "N/A", "Labels": "", "Links": "N/A", "Mountpoint": "", "Name": "foobar_baz", "Scope": "", "Size": "N/A", "Status": "N/A"},
{"Availability": "N/A", "Driver": "bar", "Group": "N/A", "Labels": "", "Links": "N/A", "Mountpoint": "", "Name": "foobar_bar", "Scope": "", "Size": "N/A", "Status": "N/A"},
}
Expand All @@ -158,7 +158,7 @@ func TestVolumeContextWriteJSON(t *testing.T) {
}
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
msg := fmt.Sprintf("Output: line %d: %s", i, line)
var m map[string]interface{}
var m map[string]any
err := json.Unmarshal([]byte(line), &m)
assert.NilError(t, err, msg)
assert.Check(t, is.DeepEqual(expectedJSONs[i], m), msg)
Expand Down
4 changes: 2 additions & 2 deletions cli/command/idresolver/idresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func New(apiClient client.APIClient, noResolve bool) *IDResolver {
}
}

func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string, error) {
func (r *IDResolver) get(ctx context.Context, t any, id string) (string, error) {
switch t.(type) {
case swarm.Node:
node, _, err := r.client.NodeInspectWithRaw(ctx, id)
Expand Down Expand Up @@ -55,7 +55,7 @@ func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string,
// Resolve will attempt to resolve an ID to a Name by querying the manager.
// Results are stored into a cache.
// If the `-n` flag is used in the command-line, resolution is disabled.
func (r *IDResolver) Resolve(ctx context.Context, t interface{}, id string) (string, error) {
func (r *IDResolver) Resolve(ctx context.Context, t any, id string) (string, error) {
if r.noResolve {
return id, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func runInspect(dockerCli command.Cli, opts inspectOptions) error {
client := dockerCli.Client()
ctx := context.Background()

getRefFunc := func(ref string) (interface{}, []byte, error) {
getRefFunc := func(ref string) (any, []byte, error) {
return client.ImageInspectWithRaw(ctx, ref)
}
return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
Expand Down
Loading

0 comments on commit 0e73168

Please sign in to comment.