From 542c575d4f2b858ace204cbc4c1239ac926d83ec Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Fri, 20 Dec 2024 15:46:29 +0100 Subject: [PATCH] Make `UnmarshalChainConfigJSON` non generic to support `extra` interface arguments --- params/json.libevm.go | 4 ++-- params/json.libevm_test.go | 33 +++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/params/json.libevm.go b/params/json.libevm.go index 84995ae67053..79a4bc41c8dc 100644 --- a/params/json.libevm.go +++ b/params/json.libevm.go @@ -65,7 +65,7 @@ func (c *ChainConfig) UnmarshalJSON(data []byte) (err error) { // `data` is decoded into `config` and `data` is decoded into `extra`. // - `extra` is nil: // `data` is decoded into `config` and the extra is ignored. -func UnmarshalChainConfigJSON[T any](data []byte, config *ChainConfig, extra *T, reuseJSONRoot bool) (err error) { +func UnmarshalChainConfigJSON(data []byte, config *ChainConfig, extra any, reuseJSONRoot bool) (err error) { err = json.Unmarshal(data, (*chainConfigWithoutMethods)(config)) switch { case err != nil: @@ -78,7 +78,7 @@ func UnmarshalChainConfigJSON[T any](data []byte, config *ChainConfig, extra *T, } default: jsonExtra := struct { - Extra *T `json:"extra"` + Extra any `json:"extra"` }{ Extra: extra, } diff --git a/params/json.libevm_test.go b/params/json.libevm_test.go index 4c60cedbec8c..aa87883524b0 100644 --- a/params/json.libevm_test.go +++ b/params/json.libevm_test.go @@ -153,53 +153,67 @@ func TestUnmarshalChainConfigJSON(t *testing.T) { testCases := map[string]struct { jsonData string // string for convenience + extra any reuseJSONRoot bool expectedConfig ChainConfig expectedExtra any errMessage string }{ "invalid_json": { - expectedExtra: testExtra{}, + extra: &testExtra{}, + expectedExtra: &testExtra{}, errMessage: "decoding root chain config: unexpected end of JSON input", }, + "nil_extra": { + jsonData: `{"chainId": 1}`, + extra: &testExtra{}, + expectedConfig: ChainConfig{ChainID: big.NewInt(1)}, + expectedExtra: &testExtra{}, + }, "no_extra_at_extra_key": { jsonData: `{"chainId": 1}`, + extra: &testExtra{}, expectedConfig: ChainConfig{ChainID: big.NewInt(1)}, - expectedExtra: testExtra{}, + expectedExtra: &testExtra{}, }, "no_extra_at_root_depth": { jsonData: `{"chainId": 1}`, + extra: &testExtra{}, reuseJSONRoot: true, expectedConfig: ChainConfig{ChainID: big.NewInt(1)}, - expectedExtra: testExtra{}, + expectedExtra: &testExtra{}, }, "wrong_extra_type_at_extra_key": { jsonData: `{"chainId": 1, "extra": 1}`, + extra: &testExtra{}, expectedConfig: ChainConfig{ChainID: big.NewInt(1)}, - expectedExtra: testExtra{}, + expectedExtra: &testExtra{}, errMessage: "decoding extra config to *params.testExtra: " + "json: cannot unmarshal number into Go struct field " + ".extra of type params.testExtra", }, "wrong_extra_type_at_root_depth": { jsonData: `{"chainId": 1, "field": 1}`, + extra: &testExtra{}, reuseJSONRoot: true, expectedConfig: ChainConfig{ChainID: big.NewInt(1)}, - expectedExtra: testExtra{}, + expectedExtra: &testExtra{}, errMessage: "decoding extra config to *params.testExtra: " + "json: cannot unmarshal number into Go struct field " + "testExtra.field of type string", }, "extra_success_at_extra_key": { jsonData: `{"chainId": 1, "extra": {"field":"value"}}`, + extra: &testExtra{}, expectedConfig: ChainConfig{ChainID: big.NewInt(1)}, - expectedExtra: testExtra{Field: "value"}, + expectedExtra: &testExtra{Field: "value"}, }, "extra_success_at_root_depth": { jsonData: `{"chainId": 1, "field":"value"}`, + extra: &testExtra{}, reuseJSONRoot: true, expectedConfig: ChainConfig{ChainID: big.NewInt(1)}, - expectedExtra: testExtra{Field: "value"}, + expectedExtra: &testExtra{Field: "value"}, }, } @@ -210,15 +224,14 @@ func TestUnmarshalChainConfigJSON(t *testing.T) { data := []byte(testCase.jsonData) config := ChainConfig{} - var extra testExtra - err := UnmarshalChainConfigJSON(data, &config, &extra, testCase.reuseJSONRoot) + err := UnmarshalChainConfigJSON(data, &config, testCase.extra, testCase.reuseJSONRoot) if testCase.errMessage == "" { require.NoError(t, err) } else { require.EqualError(t, err, testCase.errMessage) } assert.Equal(t, testCase.expectedConfig, config) - assert.Equal(t, testCase.expectedExtra, extra) + assert.Equal(t, testCase.expectedExtra, testCase.extra) }) } }