#yaml

yaml-merge-keys

Implementation of the YAML merge key extension. http://yaml.org/type/merge.html

8 releases (4 breaking)

0.5.1 Dec 6, 2021
0.5.0 Nov 22, 2021
0.4.1 Jun 2, 2020
0.4.0 Oct 15, 2019
0.1.0 Mar 30, 2017

#160 in Encoding

Download history 10450/week @ 2022-10-08 11591/week @ 2022-10-15 13136/week @ 2022-10-22 12087/week @ 2022-10-29 18344/week @ 2022-11-05 25672/week @ 2022-11-12 12719/week @ 2022-11-19 13941/week @ 2022-11-26 14904/week @ 2022-12-03 13696/week @ 2022-12-10 13473/week @ 2022-12-17 6398/week @ 2022-12-24 11696/week @ 2022-12-31 16887/week @ 2023-01-07 16320/week @ 2023-01-14 13384/week @ 2023-01-21

59,330 downloads per month
Used in 8 crates (6 directly)

MIT/Apache

37KB
822 lines

Yaml merge keys

This crate implements support for the Merge Key Language-Independent Type for YAML draft specification for YAML documents from the yaml-rust and serde_yaml (with the serde_yaml feature) crates.

When a mapping in a YAML document contains a << key, its value should be either a mapping or a sequence of mappings. For each mapping, it is merged into the parent mapping where the parent mapping wins conflicts (so that it may override keys from the merge set).


lib.rs:

YAML Merge Keys

The YAML Merge Key extension is not supported by the core YAML crate, but can be implemented after parsing. This crate transforms a parsed YAML document and merges dictionaries together.

Usage

This crate provides a function which implements the YAML Merge Key extension. Given a YAML document from yaml-rust (or serde_yaml with the serde_yaml feature), it will return a YAML document with the merge keys removed and merged into their owning dictionaries.

# extern crate yaml_rust;
# extern crate yaml_merge_keys;
# #[cfg(feature = "serde_yaml")]
# extern crate serde_yaml;
use yaml_rust::YamlLoader;
use yaml_merge_keys::merge_keys;

// YAML document contents.
let raw = "\
ref: &ref
    merged_key: merged
    added_key: merged
dict:
    <<: *ref
    top_key: given
    merged_key: given
";
let merged = "\
ref:
    merged_key: merged
    added_key: merged
dict:
    top_key: given
    merged_key: given
    added_key: merged
";

// Parse the YAML documents.
let raw_yaml = YamlLoader::load_from_str(raw).unwrap().remove(0);
let merged_yaml = YamlLoader::load_from_str(merged).unwrap().remove(0);

// Merge the keys.
let merged_keys = merge_keys(raw_yaml).unwrap();

// The keys have been merged.
assert_eq!(merged_keys, merged_yaml);

// Using `serde_yaml` is also supported with the feature.
#[cfg(feature = "serde_yaml")]
{
    use yaml_merge_keys::merge_keys_serde;

    let raw_yaml = serde_yaml::from_str(raw).unwrap();
    let merged_yaml: serde_yaml::Value = serde_yaml::from_str(merged).unwrap();

    let merged_keys = merge_keys_serde(raw_yaml).unwrap();

    assert_eq!(merged_keys, merged_yaml);
}

Example

---
- &CENTER { x: 1, y: 2 }
- &LEFT { x: 0, y: 2 }
- &BIG { r: 10 }
- &SMALL { r: 1 }

# All the following maps are equal:

- # Explicit keys
  x: 1
  y: 2
  r: 10
  label: center/big

- # Merge one map
  << : *CENTER
  r: 10
  label: center/big

- # Merge multiple maps
  << : [ *CENTER, *BIG ]
  label: center/big

- # Override
  << : [ *BIG, *LEFT, *SMALL ]
  x: 1
  label: center/big

Dependencies

~0.4–1MB
~24K SLoC