Skip to content

Commit

Permalink
chore: escape json path
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed Jan 6, 2025
1 parent 80ffd5b commit ac2b341
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions promkit/src/jsonz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,16 @@ pub struct PathIterator<'a> {
stack: Vec<(String, &'a serde_json::Value)>,
}

impl<'a> PathIterator<'a> {

Check warning on line 398 in promkit/src/jsonz.rs

View workflow job for this annotation

GitHub Actions / test

the following explicit lifetimes could be elided: 'a
fn escape_json_path_key(key: &str) -> String {
if key.contains('.') || key.contains('-') || key.contains('@') {
format!("\"{}\"", key)
} else {
key.to_string()
}
}
}

impl<'a> Iterator for PathIterator<'a> {

Check warning on line 408 in promkit/src/jsonz.rs

View workflow job for this annotation

GitHub Actions / test

the following explicit lifetimes could be elided: 'a
type Item = String;

Expand All @@ -403,10 +413,11 @@ impl<'a> Iterator for PathIterator<'a> {
match value {
serde_json::Value::Object(obj) => {
for (key, val) in obj.iter() {
let escaped = Self::escape_json_path_key(key);
let new_path = if current_path == "." {
format!(".{}", key)
format!(".{}", escaped)
} else {
format!("{}.{}", current_path, key)
format!("{}.{}", current_path, escaped)
};
self.stack.push((new_path, val));
}
Expand Down

0 comments on commit ac2b341

Please sign in to comment.