Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 71 additions & 3 deletions rust/agama-utils/src/api/storage/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ use merge::Merge;
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Clone, Debug, Default, Serialize, Deserialize, Merge, utoipa::ToSchema)]
#[derive(Clone, Debug, Default, Serialize, Deserialize, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct Config {
#[serde(skip_serializing_if = "Option::is_none")]
#[merge(strategy = merge::option::overwrite_none)]
pub storage: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
#[merge(strategy = merge::option::overwrite_none)]
pub legacy_autoyast_storage: Option<Value>,
}

Expand All @@ -38,3 +36,73 @@ impl Config {
self.storage.is_some() || self.legacy_autoyast_storage.is_some()
}
}

impl Merge for Config {
fn merge(&mut self, other: Self) {
if let Some(storage) = &mut self.storage {
if let Some(other_storage) = other.storage {
merge_values_as_objects(storage, other_storage);
}
} else {
self.storage = other.storage;
}

// No need to merge both values because it is just an array of drives.
if self.legacy_autoyast_storage.is_none() {
self.legacy_autoyast_storage = other.legacy_autoyast_storage;
}
}
}

// Merge to serde_json::Value structs.
//
// Both Value structs are supposed to represent JSON objects.
fn merge_values_as_objects(left: &mut Value, right: Value) {
let Value::Object(left_object) = left else {
return;
};

let Value::Object(right_object) = right else {
return;
};

for (k, v) in right_object {
left_object.entry(k).or_insert(v);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect right_object takes precedence, for example:

config.storage.drives = ...
other.storage.drives = ...
config.storage.drives == other.storage.drives #=> false
config.merge(other)
config.storage.drives == other.storage.drives #=> true

With the current implementation, #merge actually means "set to the left object the missing properties provided by the right object". I expected this other meaning: "set to the left object all the properties provided by the right object". What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your confusion, but in merge the left side is the "new configuration". Think about the "right" object as the default values (that belongs to the old configuration). So I would expect to use the "left" values if available.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this, I could have implemented this as a separate strategy, but as I do not expect to reuse it, implementing Merge looks fine.

}
}

#[cfg(test)]
mod tests {
use merge::Merge;

use super::*;

#[test]
fn test_merge_with_default_config() {
let mut config: Config = serde_json::from_str(r#"{ "storage": { "drives": [] }}"#).unwrap();
let original = Config::default();

config.merge(original);
assert!(config.storage.is_some());
}

#[test]
fn test_merge_storage_key() {
let mut config: Config = serde_json::from_str(r#"{ "storage": { "drives": [] }}"#).unwrap();
let original: Config = serde_json::from_str(r#"{ "storage": { "mdRaids": [] }}"#).unwrap();

config.merge(original);
let value = config.storage.unwrap();
assert!(value.get("drives").is_some());
assert!(value.get("mdRaids").is_some());
}

#[test]
fn test_merge_with_no_storage() {
let mut config = Config::default();
let original: Config = serde_json::from_str(r#"{ "storage": { "drives": [] }}"#).unwrap();

config.merge(original);
assert!(config.storage.is_some());
}
}
Loading