- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 23
yaml merge Set Options
- Introduction
- Accept Only Unique Values
- Block RHS Values
- Accept Only RHS Values
- 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 unordered Sets.  By default, only the unique values from both LHS and RHS Sets at the same path within their respective documents are merged together.  The available Set merge options include:
- 
unique(the default) ignores RHS Set values which already exist within the LHS Set, appending the rest.
- 
leftcauses RHS Sets to be ignored when a Set already exists at the same path within the LHS document. RHS Sets are retained only where there is no LHS Set at the same path.
- 
rightcauses LHS Sets to be entirely replaced by the RHS Set when a Set exists at the same path within both documents. LHS Sets are retained only where there is no RHS Set at the same path.
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 merge mode for Sets.  When you need to merge only Set values which do not already exist within the LHS Sets, use --sets=unique or -E 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 Set values from the RHS document were discarded during the merge.
Should you need to retain only LHS values, blocking all RHS values from Sets at the same path within the YAML documents, use --sets=left or -E 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 Set in RHS.yaml was retained from the RHS document.
Should you need to completely overwrite preexisting Set values in the LHS document, retaining only LHS Sets with no Sets in the RHS document at the same paths, use --sets=right or -E 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 Set from LHS.yaml to survive the merge was that in lhs_exclusive.
The yaml-merge tool can read per YAML Path merging options from an INI-Style configuration file via its --config (-c) argument.  Whereas the --sets (-E) argument supplies an overarching mode for merging Sets, 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, sets, which behaves identically to the --sets (-E) command-line argument to the yaml-merge tool.  The [defaults]sets setting is overridden by the same-named command-line argument, when supplied.  In practice, this file may look like:
File merge-options.ini
[defaults]
sets = uniqueNote 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 Set merge modes that are available to the --sets (-E) 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 Set at /stringsretained only values from LHS.yaml due to the INI-defined rule which applied to it:left.
- The Set at /integerswas wholly overwritten by the values from RHS.yaml due to the INI-defined rule which applied to it:right.