vector/common/
expansion.rs

1use std::{collections::HashMap, sync::LazyLock};
2
3use regex::Regex;
4
5use crate::event::Value;
6
7static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[^0-9A-Za-z_]").unwrap());
8fn slugify_text(input: &str) -> String {
9    let result = RE.replace_all(input, "_");
10    result.to_lowercase()
11}
12
13/// Expands the given possibly template-able `key_s` and `value_s`, and return the expanded owned pairs
14/// it would also insert the pairs into either `static_pairs` or `dynamic_pairs` depending on the template-ability of `key_s`.
15pub(crate) fn pair_expansion(
16    key_s: &str,
17    value_s: &str,
18    static_pairs: &mut HashMap<String, String>,
19    dynamic_pairs: &mut HashMap<String, String>,
20) -> Result<HashMap<String, String>, serde_json::Error> {
21    let mut expanded_pairs = HashMap::new();
22    if let Some(opening_prefix) = key_s.strip_suffix('*') {
23        let output: serde_json::map::Map<String, serde_json::Value> =
24            serde_json::from_str(value_s)?;
25
26        // key_* -> key_one, key_two, key_three
27        // * -> one, two, three
28        for (k, v) in output {
29            let key = slugify_text(&format!("{opening_prefix}{k}"));
30            let val = Value::from(v).to_string_lossy().into_owned();
31            if val == "<null>" {
32                warn!("Encountered \"null\" value for dynamic pair. key: {}", key);
33                continue;
34            }
35            if let Some(prev) = dynamic_pairs.insert(key.clone(), val.clone()) {
36                warn!(
37                    "Encountered duplicated dynamic pair. \
38                                key: {}, value: {:?}, discarded value: {:?}",
39                    key, val, prev
40                );
41            };
42            expanded_pairs.insert(key, val);
43        }
44    } else {
45        static_pairs.insert(key_s.to_string(), value_s.to_string());
46        expanded_pairs.insert(key_s.to_string(), value_s.to_string());
47    }
48    Ok(expanded_pairs)
49}