Skip to content

Commit

Permalink
refactor: Replace all interface{} with any alias (sourcenetwork#805)
Browse files Browse the repository at this point in the history
- Resolves: sourcenetwork#456 
- Replace all occurrences of `interface{}` with `any` in all *.go files.
- Add linter rule to ensure `any` is always used over `interface{}`.

Command used: `find . -type f -name "*.go" -print0 | xargs -0 sed -i "s/interface{}/any/g"`
  • Loading branch information
shahzadlone authored Sep 16, 2022
1 parent 5f70fd4 commit 5882d98
Show file tree
Hide file tree
Showing 199 changed files with 1,454 additions and 1,452 deletions.
10 changes: 5 additions & 5 deletions api/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ type ctxPeerID struct{}

// DataResponse is the GQL top level object holding data for the response payload.
type DataResponse struct {
Data interface{} `json:"data"`
Data any `json:"data"`
}

// simpleDataResponse is a helper function that returns a DataResponse struct.
// Odd arguments are the keys and must be strings otherwise they are ignored.
// Even arguments are the values associated with the previous key.
// Odd arguments are also ignored if there are no following arguments.
func simpleDataResponse(args ...interface{}) DataResponse {
data := make(map[string]interface{})
func simpleDataResponse(args ...any) DataResponse {
data := make(map[string]any)

for i := 0; i < len(args); i += 2 {
if len(args) >= i+2 {
Expand Down Expand Up @@ -81,15 +81,15 @@ func (h *handler) handle(f http.HandlerFunc) http.HandlerFunc {
}
}

func getJSON(req *http.Request, v interface{}) error {
func getJSON(req *http.Request, v any) error {
err := json.NewDecoder(req.Body).Decode(v)
if err != nil {
return errors.Wrap(err, "unmarshal error")
}
return nil
}

func sendJSON(ctx context.Context, rw http.ResponseWriter, v interface{}, code int) {
func sendJSON(ctx context.Context, rw http.ResponseWriter, v any, code int) {
rw.Header().Set("Content-Type", "application/json")

b, err := json.Marshal(v)
Expand Down
12 changes: 6 additions & 6 deletions api/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,29 @@ import (
func TestSimpleDataResponse(t *testing.T) {
resp := simpleDataResponse("key", "value", "key2", "value2")
switch v := resp.Data.(type) {
case map[string]interface{}:
case map[string]any:
assert.Equal(t, "value", v["key"])
assert.Equal(t, "value2", v["key2"])
default:
t.Fatalf("data should be of type map[string]interface{} but got %T", resp.Data)
t.Fatalf("data should be of type map[string]any but got %T", resp.Data)
}

resp2 := simpleDataResponse("key", "value", "key2")
switch v := resp2.Data.(type) {
case map[string]interface{}:
case map[string]any:
assert.Equal(t, "value", v["key"])
assert.Equal(t, nil, v["key2"])
default:
t.Fatalf("data should be of type map[string]interface{} but got %T", resp.Data)
t.Fatalf("data should be of type map[string]any but got %T", resp.Data)
}

resp3 := simpleDataResponse("key", "value", 2, "value2")
switch v := resp3.Data.(type) {
case map[string]interface{}:
case map[string]any:
assert.Equal(t, "value", v["key"])
assert.Equal(t, nil, v["2"])
default:
t.Fatalf("data should be of type map[string]interface{} but got %T", resp.Data)
t.Fatalf("data should be of type map[string]any but got %T", resp.Data)
}
}

Expand Down
26 changes: 13 additions & 13 deletions api/http/handlerfuncs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type testOptions struct {
Body io.Reader
Headers map[string]string
ExpectedStatus int
ResponseData interface{}
ResponseData any
ServerOptions serverOptions
}

Expand All @@ -65,10 +65,10 @@ func TestRootHandler(t *testing.T) {
ResponseData: &resp,
})
switch v := resp.Data.(type) {
case map[string]interface{}:
case map[string]any:
assert.Equal(t, "Welcome to the DefraDB HTTP API. Use /graphql to send queries to the database", v["response"])
default:
t.Fatalf("data should be of type map[string]interface{} but got %T", resp.Data)
t.Fatalf("data should be of type map[string]any but got %T", resp.Data)
}
}

Expand All @@ -85,10 +85,10 @@ func TestPingHandler(t *testing.T) {
})

switch v := resp.Data.(type) {
case map[string]interface{}:
case map[string]any:
assert.Equal(t, "pong", v["response"])
default:
t.Fatalf("data should be of type map[string]interface{} but got %T", resp.Data)
t.Fatalf("data should be of type map[string]any but got %T", resp.Data)
}
}

Expand All @@ -108,10 +108,10 @@ func TestDumpHandlerWithNoError(t *testing.T) {
})

switch v := resp.Data.(type) {
case map[string]interface{}:
case map[string]any:
assert.Equal(t, "ok", v["response"])
default:
t.Fatalf("data should be of type map[string]interface{} but got %T", resp.Data)
t.Fatalf("data should be of type map[string]any but got %T", resp.Data)
}
}

Expand Down Expand Up @@ -591,11 +591,11 @@ type user {
})

switch v := resp.Data.(type) {
case map[string]interface{}:
case map[string]any:
assert.Equal(t, "success", v["result"])

default:
t.Fatalf("data should be of type map[string]interface{} but got %T\n%v", resp.Data, v)
t.Fatalf("data should be of type map[string]any but got %T\n%v", resp.Data, v)
}
}

Expand Down Expand Up @@ -764,15 +764,15 @@ query {
})

switch d := resp3.Data.(type) {
case map[string]interface{}:
case map[string]any:
switch val := d["val"].(type) {
case string:
assert.Equal(t, "pGNhZ2UYH2RuYW1lY0JvYmZwb2ludHMYWmh2ZXJpZmllZPU=", val)
default:
t.Fatalf("expecting string but got %T", val)
}
default:
t.Fatalf("expecting map[string]interface{} but got %T", d)
t.Fatalf("expecting map[string]any but got %T", d)
}
}

Expand All @@ -792,10 +792,10 @@ func TestPeerIDHandler(t *testing.T) {
})

switch v := resp.Data.(type) {
case map[string]interface{}:
case map[string]any:
assert.Equal(t, "12D3KooWFpi6VTYKLtxUftJKEyfX8jDfKi8n15eaygH8ggfYFZbR", v["peerID"])
default:
t.Fatalf("data should be of type map[string]interface{} but got %T", resp.Data)
t.Fatalf("data should be of type map[string]any but got %T", resp.Data)
}
}

Expand Down
4 changes: 2 additions & 2 deletions api/http/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestLoggerKeyValueOutput(t *testing.T) {
assert.Equal(t, float64(28), kv["Length"])
}

func readLog(path string) (map[string]interface{}, error) {
func readLog(path string) (map[string]any, error) {
// inspect the log file
f, err := os.Open(path)
if err != nil {
Expand All @@ -114,7 +114,7 @@ func readLog(path string) (map[string]interface{}, error) {
scanner.Scan()
logLine := scanner.Text()

kv := make(map[string]interface{})
kv := make(map[string]any)
err = json.Unmarshal([]byte(logLine), &kv)
if err != nil {
return nil, errors.WithStack(err)
Expand Down
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func indentJSON(b []byte) (string, error) {
}

type graphqlErrors struct {
Errors interface{} `json:"errors"`
Errors any `json:"errors"`
}

func hasGraphQLErrors(buf []byte) (bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion cli/peerid.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var peerIDCmd = &cobra.Command{
return fmt.Errorf("mashalling data response failed: %w", err)
}
cmd.Println(string(b))
} else if data, ok := r.Data.(map[string]interface{}); ok {
} else if data, ok := r.Data.(map[string]any); ok {
log.FeedbackInfo(cmd.Context(), data["peerID"].(string))
}

Expand Down
2 changes: 1 addition & 1 deletion cli/peerid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestGetPeerIDCmd(t *testing.T) {
t.Fatal(err)
}

r := make(map[string]interface{})
r := make(map[string]any)
err = json.Unmarshal(out, &r)
if err != nil {
t.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions client/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ type Collection interface {
//
// Returns an ErrInvalidUpdateTarget error if the target type is not supported.
// Returns an ErrInvalidUpdater error if the updater type is not supported.
UpdateWith(ctx context.Context, target interface{}, updater string) (*UpdateResult, error)
UpdateWith(ctx context.Context, target any, updater string) (*UpdateResult, error)
// UpdateWithFilter updates using a filter to target documents for update.
//
// The provided updater must be a string Patch, string Merge Patch, a parsed Patch, or parsed Merge Patch
// else an ErrInvalidUpdater will be returned.
UpdateWithFilter(ctx context.Context, filter interface{}, updater string) (*UpdateResult, error)
UpdateWithFilter(ctx context.Context, filter any, updater string) (*UpdateResult, error)
// UpdateWithKey updates using a DocKey to target a single document for update.
//
// The provided updater must be a string Patch, string Merge Patch, a parsed Patch, or parsed Merge Patch
Expand All @@ -112,11 +112,11 @@ type Collection interface {
// This operation will hard-delete all state relating to the given DocKey. This includes data, block, and head storage.
//
// Returns an ErrInvalidDeleteTarget if the target type is not supported.
DeleteWith(ctx context.Context, target interface{}) (*DeleteResult, error)
DeleteWith(ctx context.Context, target any) (*DeleteResult, error)
// DeleteWithFilter deletes documents matching the given filter.
//
// This operation will hard-delete all state relating to the given DocKey. This includes data, block, and head storage.
DeleteWithFilter(ctx context.Context, filter interface{}) (*DeleteResult, error)
DeleteWithFilter(ctx context.Context, filter any) (*DeleteResult, error)
// DeleteWithKey deletes using a DocKey to target a single document for delete.
//
// This operation will hard-delete all state relating to the given DocKey. This includes data, block, and head storage.
Expand Down
4 changes: 2 additions & 2 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ type DB interface {
}

type QueryResult struct {
Errors []interface{} `json:"errors,omitempty"`
Data interface{} `json:"data"`
Errors []any `json:"errors,omitempty"`
Data any `json:"data"`
}
Loading

0 comments on commit 5882d98

Please sign in to comment.