foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.06k stars 1.65k forks source link

feat: JSON + TOML Parsing with Default Fallbacks #7052

Open mds1 opened 6 months ago

mds1 commented 6 months ago

Component

Forge

Describe the feature you would like

Ref https://github.com/foundry-rs/forge-std/issues/508:

It would be really convenient to be able to set default values in the JSON parsing helpers with a third argument that return the default value when the key is not present in the passed in JSON string.

An example:

readBytes32(string memory json, string memory key, bytes32 default);

This would be similar to the UX of vm.envOr which reads an env var and then returns the default value if it doesn't exist. I am not particular about using overloading or following the Or suffix naming scheme. This would prevent parsing errors for when keys are not present in the JSON. I think it could be implemented by wrapping the low level vm JSON calls with try/catch

To facilitate https://github.com/foundry-rs/foundry/issues/3782, we'll first add these overloads as native cheats first, then updating StdJson.sol in forge-std to have the appropriate read* wrapper methods

cc @tynes

Additional context

No response

zerosnacks commented 5 months ago

For future reference, this request would include adding the appropriate mirror functions for TOML so feature parity is maintained.