From f8ed83aa53525f9b20a23cd4d47f38fc8e21d377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Wed, 24 May 2023 12:32:08 +0200 Subject: [PATCH] push: Collect empty objects in FlattenedJson --- .../src/push/condition/flattened_json.rs | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/ruma-common/src/push/condition/flattened_json.rs b/crates/ruma-common/src/push/condition/flattened_json.rs index 53fa674c..676afa95 100644 --- a/crates/ruma-common/src/push/condition/flattened_json.rs +++ b/crates/ruma-common/src/push/condition/flattened_json.rs @@ -27,10 +27,16 @@ impl FlattenedJson { fn flatten_value(&mut self, value: JsonValue, path: String) { match value { JsonValue::Object(fields) => { - for (key, value) in fields { - let key = escape_key(&key); - let path = if path.is_empty() { key } else { format!("{path}.{key}") }; - self.flatten_value(value, path); + if fields.is_empty() { + if self.map.insert(path.clone(), FlattenedJsonValue::EmptyObject).is_some() { + warn!("Duplicate path in flattened JSON: {path}"); + } + } else { + for (key, value) in fields { + let key = escape_key(&key); + let path = if path.is_empty() { key } else { format!("{path}.{key}") }; + self.flatten_value(value, path); + } } } value => { @@ -211,6 +217,9 @@ pub enum FlattenedJsonValue { /// Represents an array. Array(Vec), + + /// Represents an empty object. + EmptyObject, } impl FlattenedJsonValue { @@ -300,7 +309,7 @@ impl PartialEq for FlattenedJsonValue { Self::Bool(b) => other.as_bool() == Some(*b), Self::Integer(i) => other.as_integer() == Some(*i), Self::String(s) => other.as_str() == Some(s), - Self::Array(_) => false, + Self::Array(_) | Self::EmptyObject => false, } } } @@ -322,7 +331,8 @@ mod tests { "number": 10, "array": [1, 2], "boolean": true, - "null": null + "null": null, + "empty_object": {} }"#, ) .unwrap(); @@ -336,6 +346,7 @@ mod tests { "array".into() => vec![int!(1).into(), int!(2).into()].into(), "boolean".into() => true.into(), "null".into() => FlattenedJsonValue::Null, + "empty_object".into() => FlattenedJsonValue::EmptyObject, } ); }