-
-
Notifications
You must be signed in to change notification settings - Fork 22
yaml merge Array Options
- Introduction
- Merging All Elements Together
- Block RHS Elements
- Overwrite LHS Arrays
- Accept Only Unique Elements
- Configuration File Options
This document is part of the body of knowledge about yaml-merge, one of the reference command-line tools provided by the YAML Path project.
The yaml-merge command-line tool enables users to control how it merges Arrays (AKA Lists or Sequences). This is different from merging Arrays-of-Hashes, discussed elsewhere. By default, every Array element in the RHS document will be appended to LHS Arrays at the same location. The available Array merge options include:
-
all(the default) is as described above; every element is appended from the RHS document to the LHS document. -
leftcauses RHS Arrays to be ignored when an Array already exists at the same location within the LHS document. RHS Arrays are retained only where there is no LHS Array at the same location. -
rightcauses LHS Arrays to be entirely replaced by the RHS Array when an Array exists at the same location within both documents. LHS Arrays are retained only where there is no RHS Array at the same location. -
uniqueignores RHS Array elements which already exist within the LHS Array, appending the rest.
Each of these options will be explored in the following sections. All sections will use these two documents for their discussions:
File: LHS.yaml
---
strings:
- one
- two
integers:
- 1
- 2
lhs_exclusive:
- trueFile: RHS.yaml
---
strings:
- two
- three
integers:
- 2
- 3
rhs_exclusive:
- trueThis is the default mode and ensures that every Array element from every merge document is retained, including duplicates. When the two example documents are merged with --arrays=all or -A all, the resulting document becomes:
---
strings:
- one
- two
- two
- three
integers:
- 1
- 2
- 2
- 3
lhs_exclusive:
- true
rhs_exclusive:
- trueShould you need to retain only LHS elements, blocking all RHS elements from Arrays at the same location within the YAML documents, use --arrays=left or -A left. Doing so produces this result from the example documents:
---
strings:
- one
- two
integers:
- 1
- 2
lhs_exclusive:
- true
rhs_exclusive:
- trueNotice that only the rhs_exclusive Array in RHS.yaml was retained from the RHS document.
Should you need to completely overwrite preexisting Array elements in the LHS document, retaining only LHS Arrays with no Arrays in the RHS document at the same locations, use --arrays=right or -A right. Doing so produces this result from the example documents:
---
strings:
- two
- three
integers:
- 2
- 3
lhs_exclusive:
- true
rhs_exclusive:
- trueNotice that the only Array from LHS.yaml to survive the merge was that in lhs_exclusive.
When you need to merge only Array elements which do not already exist within the LHS Arrays, use --arrays=unique or -A unique. Merging the example documents with this option yields:
---
strings:
- one
- two
- three
integers:
- 1
- 2
- 3
lhs_exclusive:
- true
rhs_exclusive:
- trueNotice that all duplicate Array elements from the RHS document were discarded during the merge.
The yaml-merge tool can read per YAML Path merging options from an INI-Style configuration file via its --config (-c) argument. Whereas the --arrays (-A) argument supplies an overarching mode for merging Arrays, using a configuration file permits far more precise control whenever you need a different mode for specific parts of the merge documents.
The [defaults] section permits a key named, arrays, which behaves identically to the --arrays (-A) command-line argument to the yaml-merge tool. The [defaults]arrays setting is overridden by the same-named command-line argument, when supplied. In practice, this file may look like:
File merge-options.ini
[defaults]
arrays = allNote the spaces around the = sign are optional but only an = sign may be used to separate each key from its value.
The [rules] section takes any YAML Paths as keys and any of the Array merge modes that are available to the --arrays (-A) command-line argument. This enables extremely fine precision for applying the available modes.
Using the same two documents as all prior examples, adding a configuration file with these contents:
[rules]
/strings = left
/integers = right... would produce this merged document:
---
strings:
- one
- two
integers:
- 2
- 3
lhs_exclusive:
- true
rhs_exclusive:
- trueNotice the following:
- The Array at
/stringsretained only elements from LHS.yaml due to the INI-defined rule which applied to it:left. - The Array at
/integerswas wholly overwritten by the elements from RHS.yaml due to the INI-defined rule which applied to it:right.