Add `SettingsSources::<T>::json_merge_with` function (#10869)
Marshall Bowers
created
This PR adds a `json_merge_with` function to `SettingsSources::<T>` to
allow JSON merging settings from custom sources.
This should help avoid repeating the actual merging logic when all that
needs to be customized is which sources are being respected.
Release Notes:
- N/A
@@ -116,17 +116,26 @@ impl<'a, T: Serialize> SettingsSources<'a, T> {
.chain(self.project.iter().copied())
}
- /// Returns the settings after performing a JSON merge of the customizations into the- /// default settings.
+ /// Returns the settings after performing a JSON merge of the provided customizations.
///
- /// More-specific customizations win out over the less-specific ones.- pub fn json_merge<O: DeserializeOwned>(&self) -> Result<O> {
+ /// Customizations later in the iterator win out over the earlier ones.
+ pub fn json_merge_with<O: DeserializeOwned>(
+ customizations: impl Iterator<Item = &'a T>,
+ ) -> Result<O> {
let mut merged = serde_json::Value::Null;
- for value in self.defaults_and_customizations() {
+ for value in customizations {
merge_non_null_json_value_into(serde_json::to_value(value).unwrap(), &mut merged);
}
Ok(serde_json::from_value(merged)?)
}
+
+ /// Returns the settings after performing a JSON merge of the customizations into the
+ /// default settings.
+ ///
+ /// More-specific customizations win out over the less-specific ones.
+ pub fn json_merge<O: DeserializeOwned>(&'a self) -> Result<O> {
+ Self::json_merge_with(self.defaults_and_customizations())
+ }
}
#[derive(Clone, Copy)]