Skip to content

Commit

Permalink
feat: add Options::apply_merge()
Browse files Browse the repository at this point in the history
Merges `<<` keys into the surrounding mapping.

Closes: #2
  • Loading branch information
k9withabone committed Jun 3, 2024
1 parent 1519a20 commit aa2d0b8
Showing 1 changed file with 63 additions and 6 deletions.
69 changes: 63 additions & 6 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,64 @@ use crate::{Compose, YamlValue};
#[allow(missing_copy_implementations)] // Will include interpolation vars as a HashMap.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Options {
/// TODO
merge_anchors: bool,
/// Whether to perform merging of `<<` keys.
apply_merge: bool,
}

impl Options {
/// Set whether to merge `<<` keys into the surrounding mapping.
///
/// ```
/// use compose_spec::Compose;
///
/// let yaml = "
/// services:
/// one:
/// environment: &env
/// FOO: foo
/// BAR: bar
/// two:
/// environment:
/// <<: *env
/// BAR: baz
/// ";
///
/// let compose = Compose::options()
/// .apply_merge(true)
/// .from_yaml_str(yaml)
/// .unwrap();
///
/// let two_env = compose.services["two"]
/// .environment
/// .clone()
/// .into_map()
/// .unwrap();
///
/// assert_eq!(two_env["FOO"].as_ref().unwrap().as_string().unwrap(), "foo");
/// assert_eq!(two_env["BAR"].as_ref().unwrap().as_string().unwrap(), "baz");
/// ```
pub fn apply_merge(&mut self, apply_merge: bool) -> &mut Self {
self.apply_merge = apply_merge;
self
}

/// Return `true` if any options are set.
const fn any(&self) -> bool {
let Self { apply_merge } = *self;
apply_merge
}

/// Use the set options to deserialize a [`Compose`] file from a string slice of YAML.
///
/// # Errors
///
/// Returns an error if deserialization fails.
pub fn from_yaml_str(&self, yaml: &str) -> serde_yaml::Result<Compose> {
serde_yaml::from_str(yaml)
if self.any() {
self.from_yaml_value(serde_yaml::from_str(yaml)?)
} else {
serde_yaml::from_str(yaml)
}
}

/// Use the set options to deserialize a [`Compose`] file from an IO stream of YAML.
Expand All @@ -28,7 +74,11 @@ impl Options {
///
/// Returns an error if deserialization fails.
pub fn from_yaml_reader<R: Read>(&self, reader: R) -> serde_yaml::Result<Compose> {
serde_yaml::from_reader(reader)
if self.any() {
self.from_yaml_value(serde_yaml::from_reader(reader)?)
} else {
serde_yaml::from_reader(reader)
}
}

/// Use the set options to deserialize a [`Compose`] file from bytes of YAML.
Expand All @@ -37,15 +87,22 @@ impl Options {
///
/// Returns an error if deserialization fails.
pub fn from_yaml_slice(&self, slice: &[u8]) -> serde_yaml::Result<Compose> {
serde_yaml::from_slice(slice)
if self.any() {
self.from_yaml_value(serde_yaml::from_slice(slice)?)
} else {
serde_yaml::from_slice(slice)
}
}

/// Use the set options to deserialize a [`Compose`] file from a YAML [`Value`](YamlValue).
///
/// # Errors
///
/// Returns an error if deserialization fails.
pub fn from_yaml_value(&self, value: YamlValue) -> serde_yaml::Result<Compose> {
pub fn from_yaml_value(&self, mut value: YamlValue) -> serde_yaml::Result<Compose> {
if self.apply_merge {
value.apply_merge()?;
}
serde_yaml::from_value(value)
}
}

0 comments on commit aa2d0b8

Please # to comment.