Skip to content

Segment: Anchors

William W. Kimball, Jr., MBA, MSIS edited this page May 22, 2021 · 19 revisions
  1. Introduction
  2. Includes YAML Merge Keys
  3. Especially Useful for Anchor Lists

Introduction

When your YAML data contains Anchors, you can select them by their unique name. For example:

anchored_hash: &anchor1
  with: child
  nodes: and values

another_anchored_hash:
  <<: *anchor1
  with: its
  own: children

Select the entire anchored hash, anchored_hash, via the name of its anchor: &anchor1 or /&anchor1 Note that Anchors are identified via the required & symbol.

Includes YAML Merge Keys

In addition to scalar Anchors, YAML Merge Keys are also directly accessible using this segment type. In the sample YAML data above, &anchor1 is used as a YAML Merge Key in another_anchored_hash. It can be directly accessed -- say, to delete it or compare its original key-value pairs against the importing Hash for changes -- via a YAML Path like another_anchored_hash[&anchor1] or /another_anchored_hash/&anchor1.

Especially Useful for Anchor Lists

This segment type is especially useful for selecting named anchor values from the typical aliases: list, commonly used in YAML files to define a unified location for any scalar values that are used more than once elsewhere within the same YAML file. For example:

aliases:
  - &anchorA Some value
  - &anchorB Another value
  - &anchorC True
  - &anchorD 5280

You could select the value, Another value, by using its array element index -- if you knew it -- or more simply by its known anchor name, like so: aliases[&anchorB] or /aliases[&anchorB]. Please note that the & symbol is always required to indicate an Anchor name. Further, the [] pair is always required when selecting from an Array (AKA: sequence, list).

Clone this wiki locally