Add the `merge` utility function to extend the yaml package
What does this MR do and why?
This introduces a merge
utility function to be used with Document
and Collection
objects from the yaml
package (v >= 2.x
).
It enables deep-merging any Collection or JS Object into a yaml doc by introducing the merge
function:
import { parseDocument } from 'yaml';
import { merge } from '~/lib/utils/yaml';
const doc = parseDocument(`foo:
bar: baz
`)
merge(doc, { foo: { abc: 'def' } }) // Document{ foo: { bar: baz, abc: def } }
Implementation detail
This will use visit()
to walk the tree of the node that is to be merged. For any Pair
it finds it will setIn()
or addIn()
that Pair on the target Collection.
It will do the same with any Seq
items.
For Map
and Seq
, it will also move their commentBefore
, comment
and spaceBefore
to their respective first child node - this avoids overriding an existing comment on the target node if the incoming Map
or Seq
has a comment, too.
Why
This utility will be used by the Pipeline Wizard in order to deep-merge YAML partials into a single YAML file in the front-end.
Note
Ultimately, this functionality should be merged upstream into the package, track the progress of that effort at https://github.com/eemeli/yaml/pull/347
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.