Skip to content

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

New issue

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

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

Already on GitHub? # to your account

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

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

tynes opened this issue Feb 8, 2024 · 7 comments
Labels
A-cheatcodes Area: cheatcodes first issue A good way to start contributing 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 first issue A good way to start contributing T-feature Type: feature A-cheatcodes Area: cheatcodes labels Sep 21, 2024
@chiscookeke11
Copy link

Mind if I take this issue?

@wengDavo
Copy link

Would love to tackle this!

@TobieTom
Copy link

Hola I'm Tobias. Can I take over this particular issue? I've recently learned Cairo and Rust plus I have contributed to some projects recently and I have experience in building web apps from handling projects In university

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-cheatcodes Area: cheatcodes first issue A good way to start contributing T-feature Type: feature
Projects
None yet
Development

No branches or pull requests

7 participants