use serde::{Serialize, de::DeserializeOwned};
use serde_json::Value;
const PROBE: &str = "\u{0}feldera-ignored-setting-probe";
pub fn ignored_settings<T>(input: &Value) -> Vec<String>
where
T: DeserializeOwned + Serialize,
{
let Some(baseline) = round_trip::<T>(input) else {
return Vec::new();
};
let mut candidates = Vec::new();
find_dropped_keys(input, &baseline, &mut Vec::new(), &mut candidates);
candidates
.into_iter()
.filter(|path| {
let mut probe = input.clone();
*lookup_mut(&mut probe, path) = Value::String(PROBE.into());
round_trip::<T>(&probe).as_ref() == Some(&baseline)
})
.map(|path| format_path(&path))
.collect()
}
#[derive(Clone)]
enum Segment {
Key(String),
Index(usize),
}
fn round_trip<T>(value: &Value) -> Option<Value>
where
T: DeserializeOwned + Serialize,
{
let t = serde_json::from_value::<T>(value.clone()).ok()?;
serde_json::to_value(t).ok()
}
fn find_dropped_keys(
input: &Value,
output: &Value,
path: &mut Vec<Segment>,
dropped: &mut Vec<Vec<Segment>>,
) {
match (input, output) {
(Value::Object(input), Value::Object(output)) => {
for (key, value) in input {
path.push(Segment::Key(key.clone()));
match output.get(key) {
Some(output) => find_dropped_keys(value, output, path, dropped),
None => dropped.push(path.clone()),
}
path.pop();
}
}
(Value::Array(input), Value::Array(output)) if input.len() == output.len() => {
for (index, (input, output)) in input.iter().zip(output).enumerate() {
path.push(Segment::Index(index));
find_dropped_keys(input, output, path, dropped);
path.pop();
}
}
_ => (),
}
}
fn lookup_mut<'a>(mut value: &'a mut Value, path: &[Segment]) -> &'a mut Value {
for segment in path {
value = match segment {
Segment::Key(key) => &mut value[key.as_str()],
Segment::Index(index) => &mut value[*index],
};
}
value
}
fn format_path(path: &[Segment]) -> String {
let mut s = String::new();
for segment in path {
match segment {
Segment::Key(key) => {
if !s.is_empty() {
s.push('.');
}
s.push_str(key);
}
Segment::Index(index) => s.push_str(&format!("[{index}]")),
}
}
s
}
#[cfg(test)]
mod test {
use super::ignored_settings;
use crate::config::PipelineConfig;
use serde_json::json;
fn ignored(input: serde_json::Value) -> Vec<String> {
let mut settings = ignored_settings::<PipelineConfig>(&input);
settings.sort();
settings
}
#[test]
fn known_settings_are_not_ignored() {
assert_eq!(ignored(json!({})), Vec::<String>::new());
assert_eq!(
ignored(json!({
"workers": 4,
"storage": null,
"max_rss_mb": 1024,
"inputs": {
"t.unnamed-0": {
"stream": "t",
"transport": {"name": "datagen", "config": {"plan": [{}]}},
}
},
})),
Vec::<String>::new()
);
}
#[test]
fn explicit_defaults_are_not_ignored() {
assert_eq!(
ignored(json!({
"inputs": {
"c": {
"stream": "t",
"transport": {"name": "datagen", "config": {}},
"distributed": false,
}
},
})),
Vec::<String>::new()
);
}
#[test]
fn unknown_settings_are_ignored() {
assert_eq!(
ignored(json!({
"wokers": 4,
"dev_tweaks": {"buffer_size": 1},
"storage": {"bogus": null},
"inputs": {
"t.unnamed-0": {
"stream": "t",
"transport": {
"name": "file_input",
"config": {"path": "x", "bogus": 2},
},
"bogus": 4,
}
},
})),
vec![
"inputs.t.unnamed-0.bogus",
"inputs.t.unnamed-0.transport.config.bogus",
"storage.bogus",
"wokers",
]
);
}
}