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: added vm.keyExistsJson #392

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1661,8 +1661,12 @@ interface Vm {
// limitations and caveats of the JSON parsing cheats.

/// Checks if `key` exists in a JSON object.
#[cheatcode(group = Json)]
/// `keyExists` is being deprecated in favor of `keyExistsJson`. It will be removed in future versions.
#[cheatcode(group = Json, status = Deprecated)]
function keyExists(string calldata json, string calldata key) external view returns (bool);
/// Checks if `key` exists in a JSON object.
#[cheatcode(group = Json)]
function keyExistsJson(string calldata json, string calldata key) external view returns (bool);

/// ABI-encodes a JSON object.
#[cheatcode(group = Json)]
Expand Down
19 changes: 15 additions & 4 deletions crates/cheatcodes/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ use std::{borrow::Cow, collections::BTreeMap, fmt::Write};
impl Cheatcode for keyExistsCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { json, key } = self;
let json = parse_json_str(json)?;
let values = select(&json, key)?;
let exists = !values.is_empty();
Ok(exists.abi_encode())
check_json_key_exists(json, key)
}
}

impl Cheatcode for keyExistsJsonCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { json, key } = self;
check_json_key_exists(json, key)
}
}

Expand Down Expand Up @@ -280,6 +284,13 @@ impl Cheatcode for writeJson_1Call {
}
}

pub(super) fn check_json_key_exists(json: &str, key: &str) -> Result {
let json = parse_json_str(json)?;
let values = select(&json, key)?;
let exists = !values.is_empty();
Ok(exists.abi_encode())
}

fn parse_json(json: &str, path: &str) -> Result {
let value = parse_json_str(json)?;
let selected = select(&value, path)?;
Expand Down
8 changes: 7 additions & 1 deletion crates/evm/traces/src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,9 @@ impl CallTraceDecoder {
"parseJsonBytes32" |
"parseJsonBytes32Array" |
"writeJson" |
// `keyExists` is being deprecated in favor of `keyExistsJson`. It will be removed in future versions.
"keyExists" |
"keyExistsJson" |
"serializeBool" |
"serializeUint" |
"serializeInt" |
Expand All @@ -447,7 +449,11 @@ impl CallTraceDecoder {
} else {
let mut decoded = func.abi_decode_input(&data[SELECTOR_LEN..], false).ok()?;
let token =
if func.name.as_str() == "parseJson" || func.name.as_str() == "keyExists" {

MexicanAce marked this conversation as resolved.
Show resolved Hide resolved
if func.name.as_str() == "parseJson" ||
// `keyExists` is being deprecated in favor of `keyExistsJson`. It will be removed in future versions.
func.name.as_str() == "keyExists" ||
func.name.as_str() == "keyExistsJson" {
"<JSON file>"
} else {
"<stringified JSON>"
Expand Down
22 changes: 22 additions & 0 deletions testdata/cheats/Json.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,28 @@ contract WriteJsonTest is DSTest {
assertEq(decodedData.a, 123);
}

function test_checkKeyExistsJson() public {
string memory path = "fixtures/Json/write_complex_test.json";
string memory json = vm.readFile(path);
bool exists = vm.keyExistsJson(json, ".a");
assertTrue(exists);

// TODO: issue deprecation warning
exists = vm.keyExists(json, ".a");
assertTrue(exists);
}

function test_checkKeyDoesNotExistJson() public {
string memory path = "fixtures/Json/write_complex_test.json";
string memory json = vm.readFile(path);
bool exists = vm.keyExistsJson(json, ".d");
assertTrue(!exists);

// TODO: issue deprecation warning
exists = vm.keyExists(json, ".d");
assertTrue(!exists);
}

function test_checkKeyExists() public {
string memory path = "fixtures/Json/write_complex_test.json";
string memory json = vm.readFile(path);
Expand Down
1 change: 1 addition & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading