Add SettingsSources::<T>::json_merge_with
function (#10869)
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
This commit is contained in:
parent
63c529552c
commit
029eb67043
2 changed files with 18 additions and 12 deletions
|
@ -5,7 +5,6 @@ use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use settings::{Settings, SettingsSources};
|
use settings::{Settings, SettingsSources};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use util::merge_non_null_json_value_into;
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug, Default, Clone, JsonSchema)]
|
#[derive(Deserialize, Serialize, Debug, Default, Clone, JsonSchema)]
|
||||||
pub struct ExtensionSettings {
|
pub struct ExtensionSettings {
|
||||||
|
@ -42,10 +41,8 @@ impl Settings for ExtensionSettings {
|
||||||
type FileContent = Self;
|
type FileContent = Self;
|
||||||
|
|
||||||
fn load(sources: SettingsSources<Self::FileContent>, _cx: &mut AppContext) -> Result<Self> {
|
fn load(sources: SettingsSources<Self::FileContent>, _cx: &mut AppContext) -> Result<Self> {
|
||||||
let mut merged = serde_json::Value::Null;
|
SettingsSources::<Self::FileContent>::json_merge_with(
|
||||||
for value in [sources.default].into_iter().chain(sources.user) {
|
[sources.default].into_iter().chain(sources.user),
|
||||||
merge_non_null_json_value_into(serde_json::to_value(value).unwrap(), &mut merged);
|
)
|
||||||
}
|
|
||||||
Ok(serde_json::from_value(merged)?)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,16 +116,25 @@ impl<'a, T: Serialize> SettingsSources<'a, T> {
|
||||||
.chain(self.project.iter().copied())
|
.chain(self.project.iter().copied())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the settings after performing a JSON merge of the provided customizations.
|
||||||
|
///
|
||||||
|
/// 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 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
|
/// Returns the settings after performing a JSON merge of the customizations into the
|
||||||
/// default settings.
|
/// default settings.
|
||||||
///
|
///
|
||||||
/// More-specific customizations win out over the less-specific ones.
|
/// More-specific customizations win out over the less-specific ones.
|
||||||
pub fn json_merge<O: DeserializeOwned>(&self) -> Result<O> {
|
pub fn json_merge<O: DeserializeOwned>(&'a self) -> Result<O> {
|
||||||
let mut merged = serde_json::Value::Null;
|
Self::json_merge_with(self.defaults_and_customizations())
|
||||||
for value in self.defaults_and_customizations() {
|
|
||||||
merge_non_null_json_value_into(serde_json::to_value(value).unwrap(), &mut merged);
|
|
||||||
}
|
|
||||||
Ok(serde_json::from_value(merged)?)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue