Icons, docs formats
This commit is contained in:
@@ -67,6 +67,40 @@ pub fn extract_json_field(text: &str, field_path: &str) -> Option<String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Flattens a JSON object into dot-separated field paths mapped to each leaf scalar's raw text form
|
||||
/// (string/number/bool), for [`crate::engine::auto_declare_params`] when a line is JSON
|
||||
/// (`crate::logfmt::detect_format`) rather than logfmt — the JSON equivalent of
|
||||
/// `crate::logfmt::extract_pairs`'s `key=value` scan. Nested objects recurse, dot-joining the path
|
||||
/// (`"foo": {"bar": 1}` yields `"foo.bar"`); arrays and null are skipped since there's no single leaf
|
||||
/// scalar to declare a param against, matching `extract_json_field`'s own restriction to scalars.
|
||||
/// Returns an empty vec for anything that isn't a JSON object, same as `extract_pairs` returns empty
|
||||
/// for a line with no `key=value` pairs.
|
||||
pub fn flatten_json_object(text: &str) -> Vec<(String, String)> {
|
||||
let Ok(serde_json::Value::Object(root)) = serde_json::from_str::<serde_json::Value>(text) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut results = Vec::new();
|
||||
flatten_json_object_into(&root, "", &mut results);
|
||||
results
|
||||
}
|
||||
|
||||
fn flatten_json_object_into(
|
||||
object: &serde_json::Map<String, serde_json::Value>,
|
||||
prefix: &str,
|
||||
results: &mut Vec<(String, String)>,
|
||||
) {
|
||||
for (key, value) in object {
|
||||
let path = if prefix.is_empty() { key.clone() } else { format!("{prefix}.{key}") };
|
||||
match value {
|
||||
serde_json::Value::String(s) => results.push((path, s.clone())),
|
||||
serde_json::Value::Number(n) => results.push((path, n.to_string())),
|
||||
serde_json::Value::Bool(b) => results.push((path, b.to_string())),
|
||||
serde_json::Value::Object(nested) => flatten_json_object_into(nested, &path, results),
|
||||
serde_json::Value::Null | serde_json::Value::Array(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `rename_all = "lowercase"` matches other `kind`-style enums, so JSON bodies use "string"/"number"/"timestamp".
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
|
||||
Reference in New Issue
Block a user