Implement using @set values

This commit is contained in:
Nixon Enraght-Moony 2021-02-20 01:47:17 +00:00
parent cd5f603c31
commit dd4b938c7f
2 changed files with 19 additions and 6 deletions

View file

@ -7,18 +7,19 @@
// @is nested.json "$.index[*][?(@.name=='l1')].kind" \"module\"
// @is - "$.index[*][?(@.name=='l1')].inner.is_crate" false
// @count - "$.index[*][?(@.name=='l1')].inner.items[*]" 2
// @set l1_id = - "$.index[*][?(@.name=='l1')].id"
pub mod l1 {
// @is nested.json "$.index[*][?(@.name=='l3')].kind" \"module\"
// @is - "$.index[*][?(@.name=='l3')].inner.is_crate" false
// @count - "$.index[*][?(@.name=='l3')].inner.items[*]" 1
// @set l3_id = - "$.index[*][?(@.name=='l3')].id"
// @has - "$.index[*][?(@.name=='l1')].inner.items[*]" $l3_id
pub mod l3 {
// @is nested.json "$.index[*][?(@.name=='L4')].kind" \"struct\"
// @is - "$.index[*][?(@.name=='L4')].inner.struct_type" \"unit\"
// @set l4_id = - "$.index[*][?(@.name=='L4')].id"
// @has - "$.index[*][?(@.name=='l3')].inner.items[*]" $l4_id
pub struct L4;
}
// @is nested.json "$.index[*][?(@.inner.span=='l3::L4')].kind" \"import\"

View file

@ -207,9 +207,15 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
let val = cache.get_value(&command.args[0])?;
match select(&val, &command.args[1]) {
Ok(results) => {
let pat: Value = serde_json::from_str(&command.args[2]).unwrap();
!results.is_empty() && results.into_iter().any(|val| *val == pat)
// FIXME: Share the pat getting code with the `Is` branch.
let v_holder;
let pat: &Value = if command.args[2].starts_with("$") {
&cache.variables[&command.args[2][1..]]
} else {
v_holder = serde_json::from_str(&command.args[2]).unwrap();
&v_holder
};
!results.is_empty() && results.into_iter().any(|val| val == pat)
}
Err(_) => false,
}
@ -234,8 +240,14 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
let val = cache.get_value(&command.args[0])?;
match select(&val, &command.args[1]) {
Ok(results) => {
let pat: Value = serde_json::from_str(&command.args[2]).unwrap();
results.len() == 1 && *results[0] == pat
let v_holder;
let pat: &Value = if command.args[2].starts_with("$") {
&cache.variables[&command.args[2][1..]]
} else {
v_holder = serde_json::from_str(&command.args[2]).unwrap();
&v_holder
};
results.len() == 1 && results[0] == pat
}
Err(_) => false,
}