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

Handle flat JSON escape dot syntax #37

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Changes from all commits
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
103 changes: 81 additions & 22 deletions src/op/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,32 @@ fn get_key(data: &Value, key: KeyType) -> Option<Value> {
}
}

pub fn split_with_escape(input: &str, delimiter: char) -> Vec<String> {
let mut result = Vec::new();
let mut slice = String::new();
let mut escape = false;

for c in input.chars() {
if escape {
slice.push(c);
escape = false;
} else if c == '\\' {
escape = true;
} else if c == delimiter {
result.push(slice.clone());
slice.clear();
} else {
slice.push(c);
}
}

if !slice.is_empty() {
result.push(slice);
}

result
}

fn get_str_key<K: AsRef<str>>(data: &Value, key: K) -> Option<Value> {
let k = key.as_ref();
if k == "" {
Expand All @@ -235,30 +261,63 @@ fn get_str_key<K: AsRef<str>>(data: &Value, key: K) -> Option<Value> {
match data {
Value::Object(_) | Value::Array(_) | Value::String(_) => {
// Exterior ref in case we need to make a new value in the match.
k.split(".").fold(Some(data.clone()), |acc, i| match acc? {
// If the current value is an object, try to get the value
Value::Object(map) => map.get(i).map(Value::clone),
// If the current value is an array, we need an integer
// index. If integer conversion fails, return None.
Value::Array(arr) => i
.parse::<i64>()
.ok()
.and_then(|i| get(&arr, i))
.map(Value::clone),
// Same deal if it's a string.
Value::String(s) => {
let s_chars: Vec<char> = s.chars().collect();
i.parse::<i64>()
split_with_escape(k, '.')
.into_iter()
.fold(Some(data.clone()), |acc, i| match acc? {
// If the current value is an object, try to get the value
Value::Object(map) => map.get(&i).map(Value::clone),
// If the current value is an array, we need an integer
// index. If integer conversion fails, return None.
Value::Array(arr) => i
.parse::<i64>()
.ok()
.and_then(|i| get(&s_chars, i))
.map(|c| c.to_string())
.map(Value::String)
}
// This handles cases where we've got an un-indexable
// type or similar.
_ => None,
})
.and_then(|i| get(&arr, i))
.map(Value::clone),
// Same deal if it's a string.
Value::String(s) => {
let s_chars: Vec<char> = s.chars().collect();
i.parse::<i64>()
.ok()
.and_then(|i| get(&s_chars, i))
.map(|c| c.to_string())
.map(Value::String)
}
// This handles cases where we've got an un-indexable
// type or similar.
_ => None,
})
}
_ => None,
}
}

#[cfg(test)]
mod tests {
use super::*;

// All the tests cases have been discussed here: https://github.com/Bestowinc/json-logic-rs/pull/37
fn cases() -> Vec<(&'static str, Vec<&'static str>)> {
vec![
("", vec![]),
("foo", vec!["foo"]),
("foo.bar", vec!["foo", "bar"]),
(r#"foo\.bar"#, vec!["foo.bar"]),
(r#"foo\.bar.biz"#, vec!["foo.bar", "biz"]),
(r#"foo\\.bar"#, vec!["foo\\", "bar"]),
(r#"foo\\.bar\.biz"#, vec!["foo\\", "bar.biz"]),
(r#"foo\\\.bar"#, vec!["foo\\.bar"]),
(r#"foo\\\.bar.biz"#, vec!["foo\\.bar", "biz"]),
(r#"foo\\bar"#, vec!["foo\\bar"]),
(r#"foo\\bar.biz"#, vec!["foo\\bar", "biz"]),
(r#"foo\\bar\.biz"#, vec!["foo\\bar.biz"]),
(r#"foo\\bar\\.biz"#, vec!["foo\\bar\\", "biz"]),
]
}

#[test]
fn test_split_with_escape() {
cases()
.into_iter()
.for_each(|(input, exp)| assert_eq!(split_with_escape(&input, '.'), exp));
}
}
Loading