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

vm.writeJson should also be able to add new keys #7232

Open
Tracked by #3801
wottpal opened this issue Feb 24, 2024 · 3 comments
Open
Tracked by #3801

vm.writeJson should also be able to add new keys #7232

wottpal opened this issue Feb 24, 2024 · 3 comments
Labels
A-cheatcodes Area: cheatcodes T-feature Type: feature

Comments

@wottpal
Copy link

wottpal commented Feb 24, 2024

Component

Forge

Describe the feature you would like

Currently, vm.writeJson is only able to update existing keys which makes it really cumbersome to use when deploying new contracts for the first time and printing them to a file. Instead, it should be able to add new key & value pairs with it, too.

It should also be possible to add nested keys where the parent(s) aren't existing yet as well.

Or at least, the docs should be updated with an easy example on how to add a new (nested) key to an existing JSON file.

From the docs:

This is useful to replace some values in a JSON file without having to first parse and then reserialize it. Note that the JSON path must indicate an existing key, so it’s not possible to add new keys this way.

Additional context

No response

@wottpal wottpal added the T-feature Type: feature label Feb 24, 2024
@wottpal wottpal changed the title vm.writeJson should also be able to add new keys vm.writeJson should also be able to add new keys Feb 24, 2024
@wottpal
Copy link
Author

wottpal commented Apr 8, 2024

Would be dope to get this implemented. Is there any blocker I'm not aware of? Or why was it implemented this way in the first place? :)

cc @DaniPopes

@Thegaram
Copy link

It looks like foundry uses jsonpath_lib::replace_with to implement this:

let value =
jsonpath_lib::replace_with(data, &canonicalize_json_path(valueKey), &mut |_| {
Some(json.clone())
})?;

And the jsonpath library currently does not provide a way to insert a new key.

@sakulstra
Copy link
Contributor

Just stumbled over this one as well - would imo be a quite big ux improvement to be able to write a json key directly.
Perhaps relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes T-feature Type: feature
Projects
Archived in project
Development

No branches or pull requests

5 participants