Skip to content

Commit eb2cd44

Browse files
authored
refactor: replace interface{} with any for clarity and modernization (#2596)
Signed-off-by: weifangc <[email protected]>
1 parent b44cff5 commit eb2cd44

File tree

29 files changed

+118
-118
lines changed

29 files changed

+118
-118
lines changed

api/bind.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Binder interface {
1717

1818
var errInvalidType = errors.New("invalid type")
1919

20-
func Bind(r *http.Request, dest interface{}) error {
20+
func Bind(r *http.Request, dest any) error {
2121
if err := r.ParseForm(); err != nil {
2222
return err
2323
}

api/bind_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type TestStructPointer struct {
3838
}
3939

4040
// runTestBindForm is a helper that binds form data to dest and then runs the provided validation.
41-
func runTestBindForm(t *testing.T, dest interface{}, validate func(*testing.T, interface{})) {
41+
func runTestBindForm(t *testing.T, dest any, validate func(*testing.T, any)) {
4242
form := url.Values{
4343
"name": []string{"John Doe"},
4444
"age": []string{"30"},
@@ -61,7 +61,7 @@ func TestBindFormNonPointer(t *testing.T) {
6161
t.Parallel()
6262

6363
dest := &TestStructNonPointer{}
64-
validate := func(t *testing.T, dest interface{}) {
64+
validate := func(t *testing.T, dest any) {
6565
s, ok := dest.(*TestStructNonPointer)
6666

6767
require.True(t, ok)
@@ -80,7 +80,7 @@ func TestBindFormPointer(t *testing.T) {
8080
t.Parallel()
8181

8282
dest := &TestStructPointer{}
83-
validate := func(t *testing.T, dest interface{}) {
83+
validate := func(t *testing.T, dest any) {
8484
s, ok := dest.(*TestStructPointer)
8585

8686
require.True(t, ok)
@@ -296,7 +296,7 @@ func TestInvalidDestinationType(t *testing.T) {
296296

297297
testCases := []struct {
298298
name string
299-
dest interface{}
299+
dest any
300300
}{
301301
{"non-pointer", TestStructNonPointer{}},
302302
{"pointer to non-struct", new(string)},

api/handlers/exporter/exporter_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ func TestTransformToParticipantResponse(t *testing.T) {
149149
func TestExporterDecideds(t *testing.T) {
150150
tests := []struct {
151151
name string
152-
request map[string]interface{}
152+
request map[string]any
153153
setupMock func(*mockParticipantStore)
154154
expectedStatus int
155155
validateResp func(*testing.T, *httptest.ResponseRecorder)
156156
}{
157157
{
158158
name: "valid request - roles & slot range",
159-
request: map[string]interface{}{
159+
request: map[string]any{
160160
"from": 100,
161161
"to": 200,
162162
"roles": []string{"ATTESTER"},
@@ -197,7 +197,7 @@ func TestExporterDecideds(t *testing.T) {
197197
},
198198
{
199199
name: "valid request - pubkeys filter",
200-
request: map[string]interface{}{
200+
request: map[string]any{
201201
"from": 100,
202202
"to": 200,
203203
"roles": []string{"ATTESTER"},
@@ -232,7 +232,7 @@ func TestExporterDecideds(t *testing.T) {
232232
},
233233
{
234234
name: "invalid request - from > to",
235-
request: map[string]interface{}{
235+
request: map[string]any{
236236
"from": 200,
237237
"to": 100,
238238
"roles": []string{"ATTESTER"},
@@ -253,7 +253,7 @@ func TestExporterDecideds(t *testing.T) {
253253
},
254254
{
255255
name: "invalid request - no roles",
256-
request: map[string]interface{}{
256+
request: map[string]any{
257257
"from": 100,
258258
"to": 200,
259259
},
@@ -273,7 +273,7 @@ func TestExporterDecideds(t *testing.T) {
273273
},
274274
{
275275
name: "multiple roles",
276-
request: map[string]interface{}{
276+
request: map[string]any{
277277
"from": 100,
278278
"to": 200,
279279
"roles": []string{"ATTESTER", "PROPOSER"},
@@ -303,7 +303,7 @@ func TestExporterDecideds(t *testing.T) {
303303
},
304304
{
305305
name: "invalid request - invalid pubkey length",
306-
request: map[string]interface{}{
306+
request: map[string]any{
307307
"from": 100,
308308
"to": 200,
309309
"roles": []string{"ATTESTER"},
@@ -403,7 +403,7 @@ func TestExporterDecideds_ErrorGetAllParticipantsInRange(t *testing.T) {
403403
stores.Add(spectypes.BNRoleAttester, store)
404404

405405
exporter := NewExporter(zap.NewNop(), stores, nil, nil)
406-
reqData := map[string]interface{}{
406+
reqData := map[string]any{
407407
"from": 100,
408408
"to": 200,
409409
"roles": []string{"ATTESTER"},
@@ -447,7 +447,7 @@ func TestExporterDecideds_ErrorGetParticipantsInRange(t *testing.T) {
447447

448448
exporter := NewExporter(zap.NewNop(), stores, nil, nil)
449449

450-
reqData := map[string]interface{}{
450+
reqData := map[string]any{
451451
"from": 100,
452452
"to": 200,
453453
"roles": []string{"ATTESTER"},

api/handlers/node/node_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,19 @@ func TestHealthCheckJSONString(t *testing.T) {
253253
hc.Advanced.ListenAddresses = []string{"127.0.0.1:8000"}
254254

255255
s := hc.String()
256-
var result map[string]interface{}
256+
var result map[string]any
257257

258258
require.NoError(t, json.Unmarshal([]byte(s), &result))
259259
require.Equal(t, "bad: not enough connected peers", result["p2p"])
260260
require.Equal(t, "good", result["beacon_node"])
261261
require.Equal(t, "good", result["execution_node"])
262262
require.Equal(t, "good", result["event_syncer"])
263263

264-
advanced, ok := result["advanced"].(map[string]interface{})
264+
advanced, ok := result["advanced"].(map[string]any)
265265

266266
require.True(t, ok)
267267
require.Equal(t, float64(3), advanced["peers"])
268268
require.Equal(t, float64(3), advanced["inbound_conns"])
269269
require.Equal(t, float64(0), advanced["outbound_conns"])
270-
require.Equal(t, []interface{}{"127.0.0.1:8000"}, advanced["p2p_listen_addresses"])
270+
require.Equal(t, []any{"127.0.0.1:8000"}, advanced["p2p_listen_addresses"])
271271
}

api/handling_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestRender(t *testing.T) {
5656
{
5757
name: "fallthrough json when no fmt.stringer with text/plain",
5858
acceptHeader: "text/plain",
59-
response: map[string]interface{}{"key": "value"},
59+
response: map[string]any{"key": "value"},
6060
wantContentType: "application/json",
6161
wantBodySubstring: `"key":"value"`,
6262
},
@@ -70,14 +70,14 @@ func TestRender(t *testing.T) {
7070
{
7171
name: "explicit json rendering",
7272
acceptHeader: "application/json",
73-
response: map[string]interface{}{"foo": "bar", "baz": 123},
73+
response: map[string]any{"foo": "bar", "baz": 123},
7474
wantContentType: "application/json",
7575
wantBodySubstring: `"foo":"bar"`,
7676
},
7777
{
7878
name: "default json when no accept header",
7979
acceptHeader: "",
80-
response: map[string]interface{}{"key": "value"},
80+
response: map[string]any{"key": "value"},
8181
wantContentType: "application/json",
8282
wantBodySubstring: `"key":"value"`,
8383
},

api/server/server_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func setupTestServer(t *testing.T) *httptest.Server {
3636
router.Use(middlewareNodeVersion)
3737

3838
nodeIdentityHandler := func(w http.ResponseWriter, r *http.Request) {
39-
err := api.Render(w, r, map[string]interface{}{
39+
err := api.Render(w, r, map[string]any{
4040
"peer_id": "test-node-id",
4141
"addresses": []string{"test-address"},
4242
"subnets": "test-subnets",
@@ -48,7 +48,7 @@ func setupTestServer(t *testing.T) *httptest.Server {
4848
}
4949

5050
nodePeersHandler := func(w http.ResponseWriter, r *http.Request) {
51-
err := api.Render(w, r, []map[string]interface{}{
51+
err := api.Render(w, r, []map[string]any{
5252
{"id": "peer1", "addresses": []string{"addr1"}},
5353
})
5454
if err != nil {
@@ -57,9 +57,9 @@ func setupTestServer(t *testing.T) *httptest.Server {
5757
}
5858

5959
nodeTopicsHandler := func(w http.ResponseWriter, r *http.Request) {
60-
err := api.Render(w, r, map[string]interface{}{
60+
err := api.Render(w, r, map[string]any{
6161
"all_peers": []string{"peer1", "peer2"},
62-
"peers_by_topic": []map[string]interface{}{
62+
"peers_by_topic": []map[string]any{
6363
{
6464
"topic": "topic1",
6565
"peers": []string{"peer1"},
@@ -72,12 +72,12 @@ func setupTestServer(t *testing.T) *httptest.Server {
7272
}
7373

7474
nodeHealthHandler := func(w http.ResponseWriter, r *http.Request) {
75-
err := api.Render(w, r, map[string]interface{}{
75+
err := api.Render(w, r, map[string]any{
7676
"p2p": "good",
7777
"beacon_node": "good",
7878
"execution_node": "good",
7979
"event_syncer": "good",
80-
"advanced": map[string]interface{}{
80+
"advanced": map[string]any{
8181
"peers": 2,
8282
"inbound_conns": 1,
8383
"outbound_conns": 1,
@@ -90,8 +90,8 @@ func setupTestServer(t *testing.T) *httptest.Server {
9090
}
9191

9292
validatorsListHandler := func(w http.ResponseWriter, r *http.Request) {
93-
err := api.Render(w, r, map[string]interface{}{
94-
"data": []map[string]interface{}{
93+
err := api.Render(w, r, map[string]any{
94+
"data": []map[string]any{
9595
{"validator": "1", "pubkey": "0x123"},
9696
},
9797
})
@@ -101,8 +101,8 @@ func setupTestServer(t *testing.T) *httptest.Server {
101101
}
102102

103103
exporterDecidedsHandler := func(w http.ResponseWriter, r *http.Request) {
104-
err := api.Render(w, r, map[string]interface{}{
105-
"data": []map[string]interface{}{
104+
err := api.Render(w, r, map[string]any{
105+
"data": []map[string]any{
106106
{"slot": 1, "role": "attester"},
107107
},
108108
})

beacon/goclient/proposer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func createProposalResponseSafe(slot phase0.Slot, feeRecipient bellatrix.Executi
140140
block.Body.ExecutionPayloadHeader.FeeRecipient = feeRecipient
141141

142142
// Wrap in response structure
143-
response := map[string]interface{}{
143+
response := map[string]any{
144144
"data": block,
145145
}
146146
data, _ := json.Marshal(response)
@@ -156,7 +156,7 @@ func createProposalResponseSafe(slot phase0.Slot, feeRecipient bellatrix.Executi
156156
blockContents.Block.Body.ExecutionPayload.FeeRecipient = feeRecipient
157157

158158
// Wrap in response structure
159-
response := map[string]interface{}{
159+
response := map[string]any{
160160
"data": blockContents,
161161
}
162162
data, _ := json.Marshal(response)

beacon/goclient/signing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (gc *GoClient) DomainData(
9595
// object_root=hash_tree_root(ssz_object),
9696
// domain=domain,
9797
// ))
98-
func (gc *GoClient) ComputeSigningRoot(object interface{}, domain phase0.Domain) ([32]byte, error) {
98+
func (gc *GoClient) ComputeSigningRoot(object any, domain phase0.Domain) ([32]byte, error) {
9999
if object == nil {
100100
return [32]byte{}, errors.New("cannot compute signing root of nil")
101101
}

cli/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Global struct {
2727
}
2828

2929
// ProcessArgs processes and handles CLI arguments
30-
func ProcessArgs(cfg interface{}, a *Args, cmd *cobra.Command) {
30+
func ProcessArgs(cfg any, a *Args, cmd *cobra.Command) {
3131
configFlag := "config"
3232
cmd.PersistentFlags().StringVarP(&a.ConfigPath, configFlag, "c", "./config/config.yaml", "Path to configuration file")
3333
_ = cmd.MarkFlagRequired(configFlag)
@@ -40,7 +40,7 @@ func ProcessArgs(cfg interface{}, a *Args, cmd *cobra.Command) {
4040
cmd.SetUsageTemplate(envHelp + "\n" + cmd.UsageTemplate())
4141
}
4242

43-
func Prepare(cfg interface{}, a *Args) error {
43+
func Prepare(cfg any, a *Args) error {
4444
if a.ConfigPath != "" {
4545
err := cleanenv.ReadConfig(a.ConfigPath, cfg)
4646
if err != nil {

eth/executionclient/execution_client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func TestFetchHistoricalLogs_Subdivide(t *testing.T) {
346346

347347
wrapped := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
348348
raw, _ := io.ReadAll(r.Body)
349-
var req map[string]interface{}
349+
var req map[string]any
350350
_ = json.Unmarshal(raw, &req)
351351

352352
if req["method"] == "eth_getLogs" {
@@ -357,15 +357,15 @@ func TestFetchHistoricalLogs_Subdivide(t *testing.T) {
357357
return
358358
}
359359

360-
flt := req["params"].([]interface{})[0].(map[string]interface{})
360+
flt := req["params"].([]any)[0].(map[string]any)
361361
from, _ := strconv.ParseInt(strings.TrimPrefix(flt["fromBlock"].(string), "0x"), 16, 64)
362362
to, _ := strconv.ParseInt(strings.TrimPrefix(flt["toBlock"].(string), "0x"), 16, 64)
363363
if uint64(to-from) > tc.threshold {
364364
w.Header().Set("Content-Type", "application/json")
365-
_ = json.NewEncoder(w).Encode(map[string]interface{}{
365+
_ = json.NewEncoder(w).Encode(map[string]any{
366366
"jsonrpc": "2.0",
367367
"id": req["id"],
368-
"error": map[string]interface{}{
368+
"error": map[string]any{
369369
"code": -32005,
370370
"message": "query limit exceeded",
371371
},

0 commit comments

Comments
 (0)