I have two documents:
---
resources:
- index: 1
- index: 2
and
resources:
- index: 2
- index: 3
- index: 4
My desired output is:
resources:
- index: 1
- index: 2
- index: 3
- index: 4
I tried
#@ load("@ytt:overlay", "overlay")
#@ load("@ytt:data", "data")
#@overlay/match by=overlay.all, expects="1+"
---
resources:
#@overlay/match by=lambda idx,left,right: data.values.left[idx]==data.values.right[idx], missing_ok=True
#@overlay/replace
- index: 1
- index: 2
#@ load("@ytt:overlay", "overlay")
#@ load("@ytt:data", "data")
#@overlay/match by=overlay.all, expects="1+"
---
resources:
#@overlay/match by=lambda idx,left,right: data.values.left[idx]==data.values.right[idx], missing_ok=True
#@overlay/replace
- index: 2
- index: 3
- index: 3
This does not work. How do I use the array values to make ytt match by logic?
Yeah, this looks like a job for the built-in overlay key matcher.
Turn your second document into an overlay that will be applied on the first document. That first document will be a plain YAML document.
ytt Playground
Where:
out-of-the-box,
ytt's@overlay/match by=when given a string value, will assume you're naming the key on which to match (here,index) (docs).within an Overlay, each array item is its own edit. In an Overlay, the default action on an array item is
@overlay/append. If you want a different action (e.g. you want to merge), then you need to specify that for each array item. That's why there's an@overlay/match by="index"on each array item in the solution.when applying an Overlay,
yttfor each key, will look for that same key in the target document. If it doesn't find that key, that's an error. You can letyttknow that you want new keys to be added by including themissing_ok=Trueattribute.@overlay/match-child-defaults.Here, I've annotated
resources:that way. This means that the children ofresources:all have themissing_ok=Trueattribute: new keys will be added.