Skip to content

Commit

Permalink
Support empty string for safe access operator
Browse files Browse the repository at this point in the history
Currently only an empty json string, i.e. a string in a string was supported: `'""'`.
  • Loading branch information
ModProg committed Nov 27, 2022
1 parent 7a0e1b7 commit 742a783
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions crates/simplexpr/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,24 +232,31 @@ impl SimplExpr {

let is_safe = *safe == AccessType::Safe;

match val.as_json_value()? {
serde_json::Value::Array(val) => {
let index = index.as_i32()?;
let indexed_value = val.get(index as usize).unwrap_or(&serde_json::Value::Null);
Ok(DynVal::from(indexed_value).at(*span))
}
serde_json::Value::Object(val) => {
let indexed_value = val
.get(&index.as_string()?)
.or_else(|| val.get(&index.as_i32().ok()?.to_string()))
.unwrap_or(&serde_json::Value::Null);
Ok(DynVal::from(indexed_value).at(*span))
}
serde_json::Value::String(val) if val.is_empty() && is_safe => {
Ok(DynVal::from(&serde_json::Value::Null).at(*span))
// Needs to be done first as `as_json_value` fails on empty string
if is_safe && val.as_string()?.is_empty() {
Ok(DynVal::from(&serde_json::Value::Null).at(*span))
} else {
match val.as_json_value()? {
serde_json::Value::Array(val) => {
let index = index.as_i32()?;
let indexed_value = val.get(index as usize).unwrap_or(&serde_json::Value::Null);
Ok(DynVal::from(indexed_value).at(*span))
}
serde_json::Value::Object(val) => {
let indexed_value = val
.get(&index.as_string()?)
.or_else(|| val.get(&index.as_i32().ok()?.to_string()))
.unwrap_or(&serde_json::Value::Null);
Ok(DynVal::from(indexed_value).at(*span))
}
// TODO decide if this should be removed
// this would be a json string in a string: '""'
serde_json::Value::String(val) if val.is_empty() && is_safe => {
Ok(DynVal::from(&serde_json::Value::Null).at(*span))
}
serde_json::Value::Null if is_safe => Ok(DynVal::from(&serde_json::Value::Null).at(*span)),
_ => Err(EvalError::CannotIndex(format!("{}", val)).at(*span)),
}
serde_json::Value::Null if is_safe => Ok(DynVal::from(&serde_json::Value::Null).at(*span)),
_ => Err(EvalError::CannotIndex(format!("{}", val)).at(*span)),
}
}
SimplExpr::FunctionCall(span, function_name, args) => {
Expand Down Expand Up @@ -393,6 +400,7 @@ mod tests {
string_to_string(r#""Hello""#) => Ok(DynVal::from("Hello".to_string())),
safe_access_to_existing(r#"{ "a": { "b": 2 } }.a?.b"#) => Ok(DynVal::from(2)),
safe_access_to_missing(r#"{ "a": { "b": 2 } }.b?.b"#) => Ok(DynVal::from(&serde_json::Value::Null)),
safe_access_to_empty(r#"""?.test"#) => Ok(serde_json::Value::Null.into()),
normal_access_to_existing(r#"{ "a": { "b": 2 } }.a.b"#) => Ok(DynVal::from(2)),
normal_access_to_missing(r#"{ "a": { "b": 2 } }.b.b"#) => Err(super::EvalError::CannotIndex("null".to_string())),
}
Expand Down

0 comments on commit 742a783

Please sign in to comment.