0

I'm trying to add a map to an array of maps in ytt to modify a YAML doc.

I tried the below but it errors out and says it expects a map but getting an array.

https://gist.github.com/amalagaura/c8b5c7c92402120ed76dec95dfafb276

---
id: 1
type: book
awards:
  books:
  - id: 1
    title: International Botev
    reviewers:
      - id: 2
        name: PersonB
  - id: 2
    title: Dayton Literary Peace Prize
    reviewers:
      - id: 3
        name: PersonC
        
#! How do I add a map to an array of maps?
#@ load("@ytt:overlay", "overlay")

#@overlay/match by=overlay.all
---
awards:
  books:
    #@overlay/match by=overlay.all, expects="1+"
    #@overlay/match missing_ok=True
    reviewers:
      #@overlay/append
      - id: 1
        name: PersonA
      

Amala
  • 1,718
  • 1
  • 17
  • 29

1 Answers1

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

#! Add a map to an array of maps:

#@overlay/match by=overlay.all
---
awards:
  books: 
  #@overlay/match by=overlay.all, expects="1+"
   - reviewers:
       #@overlay/append
       - id: 1
         name: Person A

You were really close in your solution, all you really needed was to make reviewers an array item. If you want to be able to add reviewers to a book that does not have that key, then you will have to add a matcher on the array item and the map item; a gist is included below to see this behavior overlay in action. If you have more than one #@overlay/match annotation on the same item, the last one wins. There are plans to improve this behavior: https://github.com/k14s/ytt/issues/114.

https://get-ytt.io/#gist:https://gist.github.com/gcheadle-vmware/a6243ee73fa5cc139dba870690eb15c5