Skip to content

Commit

Permalink
feat(params): UnmarshalChainConfig function
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Dec 17, 2024
1 parent bd44839 commit 32afb0b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
24 changes: 24 additions & 0 deletions params/json.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,27 @@ func toJSONRawMessages(v any) (map[string]json.RawMessage, error) {
}
return msgs, nil
}

// UnmarshalChainConfig JSON decodes the given data into the config and the extra
// pointer arguments. The root JSON keys except the "extra" key are decoded into
// the config argument, and the object at the "extra" key, if present, is decoded
// into the extra argument. Note the extra argument must be a non-nil pointer or
// an error would be returned.
func UnmarshalChainConfig(data []byte, config *ChainConfig, extra any) (err error) {
err = json.Unmarshal(data, config)
if err != nil {
return fmt.Errorf("json decoding root chain config: %w", err)
}

jsonExtra := struct {
Extra any `json:"extra"`
}{
Extra: extra,
}
err = json.Unmarshal(data, &jsonExtra)
if err != nil {
return fmt.Errorf("json decoding extra chain config: %w", err)
}

return nil
}
58 changes: 58 additions & 0 deletions params/json.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math/big"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ava-labs/libevm/libevm/pseudo"
Expand Down Expand Up @@ -144,3 +145,60 @@ func TestChainConfigJSONRoundTrip(t *testing.T) {
})
}
}

func Test_UnmarshalChainConfig(t *testing.T) {
t.Parallel()

type testExtra struct {
Field string `json:"field"`
}

testCases := map[string]struct {
jsonData string // string for convenience
expectedConfig ChainConfig
expectedExtra any
errMessage string
}{
"invalid_json": {
expectedExtra: testExtra{},
errMessage: "json decoding root chain config: unexpected end of JSON input",
},
"no_extra": {
jsonData: `{"chainId": 1}`,
expectedConfig: ChainConfig{ChainID: big.NewInt(1)},
expectedExtra: testExtra{},
},
"wrong_extra_type": {
jsonData: `{"chainId": 1, "extra": 1}`,
expectedConfig: ChainConfig{ChainID: big.NewInt(1)},
expectedExtra: testExtra{},
errMessage: "json decoding extra chain config: " +
"json: cannot unmarshal number into Go struct field " +
".extra of type params.testExtra",
},
"extra_success": {
jsonData: `{"chainId": 1, "extra": {"field":"value"}}`,
expectedConfig: ChainConfig{ChainID: big.NewInt(1)},
expectedExtra: testExtra{Field: "value"},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

data := []byte(testCase.jsonData)
config := ChainConfig{}
var extra testExtra
err := UnmarshalChainConfig(data, &config, &extra)
if testCase.errMessage == "" {
assert.NoError(t, err)

Check failure on line 196 in params/json.libevm_test.go

View workflow job for this annotation

GitHub Actions / lint

require-error: for error assertions use require (testifylint)
} else {
assert.EqualError(t, err, testCase.errMessage)

Check failure on line 198 in params/json.libevm_test.go

View workflow job for this annotation

GitHub Actions / lint

require-error: for error assertions use require (testifylint)
}
assert.Equal(t, testCase.expectedConfig, config)
assert.Equal(t, testCase.expectedExtra, extra)
})
}
}

0 comments on commit 32afb0b

Please sign in to comment.