pub struct MergedMapping<'a> { /* private fields */ }Expand description
A view over a Mapping that transparently expands
alias references (*name) and merge keys (<<:).
MergedMapping borrows an AnchorRegistry and collects its entries
when it is created: the merge sources are followed and alias values are
resolved then, so an undefined alias or invalid merge key is reported
by new. The underlying syntax tree is not modified and
the returned nodes are live CST nodes, but entries added, removed or
replaced after the view was created are not seen.
Direct keys in the base mapping always shadow keys contributed by merge
sources, matching the YAML 1.1 merge-key semantics. When a merge value is
a sequence of aliases (<<: [*a, *b]), earlier aliases take precedence
over later ones, again per the YAML 1.1 spec.
Returned values are YamlNodes backed by real
CST nodes, so they preserve the original formatting and quoting style.
This view is read-only. Mapping::set replaces the
value node you pass; it does not turn a resolved map back into <<: *name.
§Examples
use yaml_edit::{Document, anchor_resolution::{DocumentResolvedExt, MappingMergedExt}};
use std::str::FromStr;
let yaml = r#"
defaults: &defaults
timeout: 30
retries: 3
production:
<<: *defaults
host: prod.example.com
timeout: 60
"#;
let doc = Document::from_str(yaml).unwrap();
let root = doc.as_mapping().unwrap();
let registry = doc.build_anchor_registry();
let prod = root.get_mapping("production").unwrap();
let merged = prod.merged(®istry).unwrap();
// Direct key wins over merged value.
assert_eq!(merged.get("timeout").unwrap().to_i64(), Some(60));
// Merged key from defaults is visible.
assert_eq!(merged.get("retries").unwrap().to_i64(), Some(3));
// Direct-only key is visible.
assert!(merged.get("host").is_some());
// `<<` itself is hidden.
assert!(merged.get("<<").is_none());Implementations§
Source§impl<'a> MergedMapping<'a>
impl<'a> MergedMapping<'a>
Sourcepub fn new(
base: Mapping,
registry: &'a AnchorRegistry,
) -> Result<Self, ResolveError>
pub fn new( base: Mapping, registry: &'a AnchorRegistry, ) -> Result<Self, ResolveError>
Create a new merged view over base, resolving aliases against registry.
Fails if a << operand in base, or in a mapping merged into it,
is an undefined alias or not a mapping, if a value visible
through the view is an undefined alias, or if a key is or holds an
undefined or recursive alias.
Sourcepub fn base(&self) -> &Mapping
pub fn base(&self) -> &Mapping
Return the underlying (un-merged) Mapping.
Useful when you want to mutate the original mapping or read its raw (non-resolved) contents.
Sourcepub fn registry(&self) -> &AnchorRegistry
pub fn registry(&self) -> &AnchorRegistry
Return the AnchorRegistry this view resolves against.
Sourcepub fn get(&self, key: impl AsYaml) -> Option<YamlNode>
pub fn get(&self, key: impl AsYaml) -> Option<YamlNode>
Get the value associated with key, resolving aliases and merge keys.
Direct entries in the base mapping take precedence over keys
contributed by <<: merge sources. The merge key itself is hidden:
looking up "<<" only finds a quoted '<<' key, which is an ordinary
string.
If the matched value is an alias (*name), the resolved target node
is returned.
Sourcepub fn contains_key(&self, key: impl AsYaml) -> bool
pub fn contains_key(&self, key: impl AsYaml) -> bool
Returns true if a value would be returned by get for
key.
Sourcepub fn iter(&self) -> impl Iterator<Item = (YamlNode, YamlNode)> + '_
pub fn iter(&self) -> impl Iterator<Item = (YamlNode, YamlNode)> + '_
Iterate over (key, value) pairs in the merged view.
Direct entries appear first, in their original document order.
Merged-in entries follow, with duplicates (and any direct-key matches
already yielded) filtered out. Keys are compared as YAML values, so
the integer 1 and the string '1' are distinct. The << merge key
itself is never yielded.
Sourcepub fn keys(&self) -> impl Iterator<Item = YamlNode> + '_
pub fn keys(&self) -> impl Iterator<Item = YamlNode> + '_
Iterate over the keys of the merged view.
Sourcepub fn values(&self) -> impl Iterator<Item = YamlNode> + '_
pub fn values(&self) -> impl Iterator<Item = YamlNode> + '_
Iterate over the values of the merged view.
Sourcepub fn get_merged(
&self,
key: impl AsYaml,
) -> Result<Option<MergedMapping<'a>>, ResolveError>
pub fn get_merged( &self, key: impl AsYaml, ) -> Result<Option<MergedMapping<'a>>, ResolveError>
Get a nested mapping by key and return it as another MergedMapping
that shares the same registry.
Returns Ok(None) if the key does not exist or the value is not a
mapping, and fails as new does on the nested mapping.