Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cheatcodes): add JSON / TOML parsing with default fallbacks #8900

Open
tynes opened this issue Feb 8, 2024 · 4 comments
Open

feat(cheatcodes): add JSON / TOML parsing with default fallbacks #8900

tynes opened this issue Feb 8, 2024 · 4 comments
Labels
A-cheatcodes Area: cheatcodes good first issue Good for newcomers T-feature Type: feature

Comments

@tynes
Copy link
Contributor

tynes commented Feb 8, 2024

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

@mds1
Copy link
Collaborator

mds1 commented Feb 8, 2024

@klkvr @mattsse In the spirit of #3782, any objections to adding these overloads as native cheats first, then updating StdJson.sol to have the appropriate read* wrapper methods?

@klkvr
Copy link
Member

klkvr commented Feb 8, 2024

yep, it makes sense

@tynes
Copy link
Contributor Author

tynes commented Mar 6, 2024

We are currently using the following implementation

    function _readOr(string memory json, string memory key, uint256 defaultValue) internal view returns (uint256) {
        return vm.keyExists(json, key) ? stdJson.readUint(json, key) : defaultValue;
    }

So not super opinionated on this needing to be a native cheat or not

@mds1 mds1 transferred this issue from foundry-rs/forge-std Sep 18, 2024
@mds1 mds1 changed the title JSON Parsing with Default Fallbacks JSON/TOML Parsing with Default Fallbacks Sep 18, 2024
@mds1
Copy link
Collaborator

mds1 commented Sep 18, 2024

foundry-rs/forge-std#605 will add this functionality to forge-std, but without the native cheats. Therefore I transferred this issue to the upstream foundry repo. Once the native cheats are added we can simply replace the implementations of the new methods in StdJson and StdToml libraries

@zerosnacks zerosnacks changed the title JSON/TOML Parsing with Default Fallbacks feat(cheatcodes): add JSON / TOML parsing with default fallbacks Sep 21, 2024
@zerosnacks zerosnacks added good first issue Good for newcomers T-feature Type: feature A-cheatcodes Area: cheatcodes labels Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes good first issue Good for newcomers T-feature Type: feature
Projects
None yet
Development

No branches or pull requests

4 participants