Skip to content

Commit

Permalink
Make UnmarshalChainConfigJSON non generic to support extra interf…
Browse files Browse the repository at this point in the history
…ace arguments
  • Loading branch information
qdm12 committed Dec 20, 2024
1 parent 30ea0f4 commit 542c575
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions params/json.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
}
Expand Down
33 changes: 23 additions & 10 deletions params/json.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
}

Expand All @@ -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)
})
}
}

0 comments on commit 542c575

Please sign in to comment.