0

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?

Rakesh Adhikesavan
  • 11,966
  • 18
  • 51
  • 76

1 Answers1

1

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.

#@ load("@ytt:overlay", "overlay")

#@overlay/match by=overlay.all
---
#@overlay/match-child-defaults missing_ok=True
resources:
  #@overlay/match by="index"
  - index: 2
    more: True
  #@overlay/match by="index"
  - index: 3
  #@overlay/match by="index"
  - index: 4

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, ytt for 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 let ytt know that you want new keys to be added by including the missing_ok=True attribute.

    • this gets verbose quickly, adding this to every node in the overlay. There's a short-hand annotation that lets you set it once at the top of a tree of nodes: @overlay/match-child-defaults.
      Here, I've annotated resources: that way. This means that the children of resources: all have the missing_ok=True attribute: new keys will be added.
JTigger
  • 394
  • 2
  • 10