Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace interface{} with any #568

Merged
merged 4 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ jobs:
go fmt ./...
git diff --exit-code

- name: Ensure no use of empty interface (should be any)
run: |
! egrep -R --exclude-dir .git 'interface\{\}'

docs:
runs-on: ubuntu-latest

Expand Down
4 changes: 2 additions & 2 deletions client/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Change struct {
var ErrNoData = fmt.Errorf("data entry not found")

// Get unmarshals into value the kind-specific data with the provided key.
func (c *Change) Get(key string, value interface{}) error {
func (c *Change) Get(key string, value any) error {
raw := c.data[key]
if raw == nil {
return ErrNoData
Expand All @@ -68,7 +68,7 @@ type Task struct {
}

// Get unmarshals into value the kind-specific data with the provided key.
func (t *Task) Get(key string, value interface{}) error {
func (t *Task) Get(key string, value any) error {
raw := t.Data[key]
if raw == nil {
return ErrNoData
Expand Down
4 changes: 2 additions & 2 deletions client/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (cs *clientSuite) TestStartChecks(c *check.C) {
c.Assert(cs.req.Method, check.Equals, "POST")
c.Assert(cs.req.URL.Path, check.Equals, "/v1/checks")

var body map[string]interface{}
var body map[string]any
c.Assert(json.NewDecoder(cs.req.Body).Decode(&body), check.IsNil)
c.Check(body, check.HasLen, 2)
c.Check(body["action"], check.Equals, "start")
Expand All @@ -103,7 +103,7 @@ func (cs *clientSuite) TestStopChecks(c *check.C) {
c.Assert(cs.req.Method, check.Equals, "POST")
c.Assert(cs.req.URL.Path, check.Equals, "/v1/checks")

var body map[string]interface{}
var body map[string]any
c.Assert(json.NewDecoder(cs.req.Body).Decode(&body), check.IsNil)
c.Check(body, check.HasLen, 2)
c.Check(body["action"], check.Equals, "stop")
Expand Down
20 changes: 10 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type RequestResponse struct {
// DecodeResult decodes the endpoint-specific result payload that is included as part of
// sync and async request responses. The decoding is performed with the standard JSON
// package, so the usual field tags should be used to prepare the type for decoding.
func (resp *RequestResponse) DecodeResult(result interface{}) error {
func (resp *RequestResponse) DecodeResult(result any) error {
reader := bytes.NewReader(resp.Result)
dec := json.NewDecoder(reader)
dec.UseNumber()
Expand Down Expand Up @@ -165,7 +165,7 @@ type clientWebsocket interface {
}

type jsonWriter interface {
WriteJSON(v interface{}) error
WriteJSON(v any) error
}

func New(config *Config) (*Client, error) {
Expand Down Expand Up @@ -386,7 +386,7 @@ func (rq *defaultRequester) Do(ctx context.Context, opts *RequestOptions) (*Requ
}, nil
}

func decodeInto(reader io.Reader, v interface{}) error {
func decodeInto(reader io.Reader, v any) error {
dec := json.NewDecoder(reader)
if err := dec.Decode(v); err != nil {
r := dec.Buffered()
Expand All @@ -413,9 +413,9 @@ type response struct {

// Error is the real value of response.Result when an error occurs.
type Error struct {
Kind string `json:"kind"`
Value interface{} `json:"value"`
Message string `json:"message"`
Kind string `json:"kind"`
Value any `json:"value"`
Message string `json:"message"`

StatusCode int
}
Expand Down Expand Up @@ -497,12 +497,12 @@ func (client *Client) SysInfo() (*SysInfo, error) {
}

type debugAction struct {
Action string `json:"action"`
Params interface{} `json:"params,omitempty"`
Action string `json:"action"`
Params any `json:"params,omitempty"`
}

// DebugPost sends a POST debug action to the server with the provided parameters.
func (client *Client) DebugPost(action string, params interface{}, result interface{}) error {
func (client *Client) DebugPost(action string, params any, result any) error {
body, err := json.Marshal(debugAction{
Action: action,
Params: params,
Expand All @@ -528,7 +528,7 @@ func (client *Client) DebugPost(action string, params interface{}, result interf
}

// DebugGet sends a GET debug action to the server with the provided parameters.
func (client *Client) DebugGet(action string, result interface{}, params map[string]string) error {
func (client *Client) DebugGet(action string, result any, params map[string]string) error {
urlParams := url.Values{"action": []string{action}}
for k, v := range params {
urlParams.Set(k, v)
Expand Down
54 changes: 27 additions & 27 deletions client/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (s *execSuite) TestExitZero(c *C) {
Command: []string{"true"},
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"true"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"true"},
})
err := s.wait(c, process)
c.Assert(err, IsNil)
Expand All @@ -85,8 +85,8 @@ func (s *execSuite) TestExitNonZero(c *C) {
Command: []string{"false"},
}
process, reqBody := s.exec(c, opts, 1)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"false"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"false"},
})
err := s.wait(c, process)
exitError, ok := err.(*client.ExitError)
Expand All @@ -100,8 +100,8 @@ func (s *execSuite) TestTimeout(c *C) {
Timeout: time.Second,
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"sleep", "3"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"sleep", "3"},
"timeout": "1s",
})
err := s.wait(c, process)
Expand All @@ -126,9 +126,9 @@ func (s *execSuite) TestOtherOptions(c *C) {
Stderr: io.Discard,
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"echo", "foo"},
"environment": map[string]interface{}{"K1": "V1", "K2": "V2"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"echo", "foo"},
"environment": map[string]any{"K1": "V1", "K2": "V2"},
"working-dir": "WD",
"user-id": 1000.0,
"user": "bob",
Expand All @@ -148,8 +148,8 @@ func (s *execSuite) TestWaitChangeError(c *C) {
Command: []string{"foo"},
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"foo"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"foo"},
})

// Make /v1/changes/{id}/wait return a "change error"
Expand All @@ -173,8 +173,8 @@ func (s *execSuite) TestWaitTasksError(c *C) {
Command: []string{"foo"},
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"foo"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"foo"},
})

// Make /v1/changes/{id}/wait return no tasks
Expand All @@ -197,8 +197,8 @@ func (s *execSuite) TestWaitExitCodeError(c *C) {
Command: []string{"foo"},
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"foo"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"foo"},
})

// Make /v1/changes/{id}/wait return no exit code
Expand All @@ -225,8 +225,8 @@ func (s *execSuite) TestSendSignal(c *C) {
Command: []string{"server"},
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"server"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"server"},
})
err := process.SendSignal("SIGHUP")
c.Assert(err, IsNil)
Expand All @@ -243,8 +243,8 @@ func (s *execSuite) TestSendResize(c *C) {
Command: []string{"server"},
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"server"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"server"},
})
err := process.SendResize(150, 50)
c.Assert(err, IsNil)
Expand All @@ -268,8 +268,8 @@ func (s *execSuite) TestOutputCombined(c *C) {
Stdout: &stdout,
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"/bin/sh", "-c", "echo OUT; echo ERR >err"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"/bin/sh", "-c", "echo OUT; echo ERR >err"},
})
err := s.wait(c, process)
c.Assert(err, IsNil)
Expand All @@ -293,8 +293,8 @@ func (s *execSuite) TestOutputSplit(c *C) {
Stderr: &stderr,
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"/bin/sh", "-c", "echo OUT; echo ERR >err"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"/bin/sh", "-c", "echo OUT; echo ERR >err"},
"split-stderr": true,
})
err := s.wait(c, process)
Expand All @@ -315,8 +315,8 @@ func (s *execSuite) TestStdinAndStdout(c *C) {
Stdout: &stdout,
}
process, reqBody := s.exec(c, opts, 0)
c.Assert(reqBody, DeepEquals, map[string]interface{}{
"command": []interface{}{"awk", "{ print toupper($0) }"},
c.Assert(reqBody, DeepEquals, map[string]any{
"command": []any{"awk", "{ print toupper($0) }"},
})
err := s.wait(c, process)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -360,7 +360,7 @@ func (w *testWebsocket) Close() error {
return nil
}

func (w *testWebsocket) WriteJSON(v interface{}) error {
func (w *testWebsocket) WriteJSON(v any) error {
data, err := json.Marshal(v)
if err != nil {
return err
Expand All @@ -369,7 +369,7 @@ func (w *testWebsocket) WriteJSON(v interface{}) error {
return nil
}

func (s *execSuite) exec(c *C, opts *client.ExecOptions, exitCode int) (process *client.ExecProcess, requestBody map[string]interface{}) {
func (s *execSuite) exec(c *C, opts *client.ExecOptions, exitCode int) (process *client.ExecProcess, requestBody map[string]any) {
s.addResponses("123", exitCode)
process, err := s.cli.Exec(opts)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion client/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (fi *FileInfo) IsDir() bool {
}

// Sys returns the underlying data source (always nil for client.FileInfo).
func (fi *FileInfo) Sys() interface{} {
func (fi *FileInfo) Sys() any {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions client/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ services:
c.Check(cs.req.Method, check.Equals, "POST")
c.Check(cs.req.URL.Path, check.Equals, "/v1/layers")
c.Check(cs.req.URL.Query(), check.HasLen, 0)
var body map[string]interface{}
var body map[string]any
c.Assert(json.NewDecoder(cs.req.Body).Decode(&body), check.IsNil)
c.Assert(body, check.DeepEquals, map[string]interface{}{
c.Assert(body, check.DeepEquals, map[string]any{
"action": "add",
"combine": option.combine,
"label": "foo",
Expand Down
12 changes: 6 additions & 6 deletions client/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func (cs *clientSuite) TestStartStop(c *check.C) {
c.Check(cs.req.Method, check.Equals, "POST")
c.Check(cs.req.URL.Path, check.Equals, "/v1/services")

var body map[string]interface{}
var body map[string]any
c.Assert(json.NewDecoder(cs.req.Body).Decode(&body), check.IsNil)
c.Check(body, check.HasLen, 2)
c.Check(body["action"], check.Equals, action)
c.Check(body["services"], check.DeepEquals, []interface{}{"one", "two"})
c.Check(body["services"], check.DeepEquals, []any{"one", "two"})
}
}

Expand All @@ -78,7 +78,7 @@ func (cs *clientSuite) TestAutostart(c *check.C) {
c.Check(cs.req.Method, check.Equals, "POST")
c.Check(cs.req.URL.Path, check.Equals, "/v1/services")

var body map[string]interface{}
var body map[string]any
c.Assert(json.NewDecoder(cs.req.Body).Decode(&body), check.IsNil)
c.Check(body, check.HasLen, 2)
c.Check(body["action"], check.Equals, "autostart")
Expand Down Expand Up @@ -130,11 +130,11 @@ func (cs *clientSuite) TestRestart(c *check.C) {
c.Check(cs.req.Method, check.Equals, "POST")
c.Check(cs.req.URL.Path, check.Equals, "/v1/services")

var body map[string]interface{}
var body map[string]any
c.Assert(json.NewDecoder(cs.req.Body).Decode(&body), check.IsNil)
c.Check(body, check.HasLen, 2)
c.Check(body["action"], check.Equals, "restart")
c.Check(body["services"], check.DeepEquals, []interface{}{"one", "two"})
c.Check(body["services"], check.DeepEquals, []any{"one", "two"})
}

func (cs *clientSuite) TestReplan(c *check.C) {
Expand All @@ -154,7 +154,7 @@ func (cs *clientSuite) TestReplan(c *check.C) {
c.Check(cs.req.Method, check.Equals, "POST")
c.Check(cs.req.URL.Path, check.Equals, "/v1/services")

var body map[string]interface{}
var body map[string]any
c.Assert(json.NewDecoder(cs.req.Body).Decode(&body), check.IsNil)
c.Check(body, check.HasLen, 2)
c.Check(body["action"], check.Equals, "replan")
Expand Down
6 changes: 3 additions & 3 deletions client/signals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func (cs *clientSuite) TestSignals(c *C) {
c.Check(cs.req.Method, Equals, "POST")
c.Check(cs.req.URL.Path, Equals, "/v1/signals")

var body map[string]interface{}
var body map[string]any
err = json.NewDecoder(cs.req.Body).Decode(&body)
c.Assert(err, IsNil)
c.Assert(body, DeepEquals, map[string]interface{}{
c.Assert(body, DeepEquals, map[string]any{
"signal": "SIGHUP",
"services": []interface{}{"s1", "s2"},
"services": []any{"s1", "s2"},
})
}
6 changes: 3 additions & 3 deletions internals/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func (s *BasePebbleSuite) RedirectClientToTestServer(handler func(http.ResponseW
}

// DecodedRequestBody returns the JSON-decoded body of the request.
func DecodedRequestBody(c *C, r *http.Request) map[string]interface{} {
var body map[string]interface{}
func DecodedRequestBody(c *C, r *http.Request) map[string]any {
var body map[string]any
decoder := json.NewDecoder(r.Body)
decoder.UseNumber()
err := decoder.Decode(&body)
Expand All @@ -108,7 +108,7 @@ func DecodedRequestBody(c *C, r *http.Request) map[string]interface{} {
}

// EncodeResponseBody writes JSON-serialized body to the response writer.
func EncodeResponseBody(c *C, w http.ResponseWriter, body interface{}) {
func EncodeResponseBody(c *C, w http.ResponseWriter, body any) {
encoder := json.NewEncoder(w)
err := encoder.Encode(body)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion internals/cli/cmd_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ services:
}
}`)
} else {
c.Check(body, check.DeepEquals, map[string]interface{}{
c.Check(body, check.DeepEquals, map[string]any{
"action": "add",
"combine": combine,
"label": "foo",
Expand Down
Loading
Loading