Skip to content

Commit

Permalink
feat(op): handle flat JSON escape dot syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
gquittet committed Jun 3, 2024
1 parent 6a9cf4f commit 82e3b77
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/op/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ fn get_key(data: &Value, key: KeyType) -> Option<Value> {
}
}

fn split_path(path: &str) -> impl Iterator<Item = String> + '_ {
let mut index = 0;
return path
.split(move |c: char| {
if c == '.' && path.chars().nth(index - 1).unwrap() != '\\' {
index += 1;
return true;
}
index += 1;
return false;
})
.map(|part| part.replace("\\.", "."));
}

fn get_str_key<K: AsRef<str>>(data: &Value, key: K) -> Option<Value> {
let k = key.as_ref();
if k == "" {
Expand All @@ -235,9 +249,9 @@ 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? {
split_path(k).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),
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
Expand Down

0 comments on commit 82e3b77

Please sign in to comment.