Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I add a map to an array of maps? #223

Closed
amalagaura opened this issue Oct 23, 2020 · 6 comments
Closed

Can I add a map to an array of maps? #223

amalagaura opened this issue Oct 23, 2020 · 6 comments
Labels
helping with an issue Debugging happening to identify the problem

Comments

@amalagaura
Copy link
Contributor

amalagaura commented Oct 23, 2020

I am not sure if this is supported and I am trying different overlay syntax. I am not really able to modify the source yamls so trying to do this with YTT.

---
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

So trying to add a map to the reviewers array in both books..

I created a StackOverflow ticket for it.
https://stackoverflow.com/questions/64504795/how-do-i-add-a-map-to-an-array-of-maps-in-ytt

@gcheadle-vmware
Copy link
Contributor

Hi @amalagaura!

Overlays are a great way to patch yaml and can certainly work for your case.

So trying to add a map to the reviewers array in both books.

You are really close and only two moves away, all you have to do is remove the overlay/match missing_ok=True, and make reviewers: an array item. Right now, having multiple overlay/match annotations cancel out all but the last one, so your #@overlay/match missing_ok=True annotation inadvertently canceled out the one above.

If you want the behavior of an overlay/match missing_ok=True, so that if a book does not have a reviewer key, one is added, you can achieve this with overlays as well. Here is a gist demonstrating that behavior: https://get-ytt.io/#gist:https://gist.github.com/gcheadle-vmware/a6243ee73fa5cc139dba870690eb15c5

@gcheadle-vmware gcheadle-vmware added the helping with an issue Debugging happening to identify the problem label Oct 23, 2020
@amalagaura
Copy link
Contributor Author

Awesome thanks!

@amalagaura
Copy link
Contributor Author

I think this example would be great in your overlay playground. Can I just add this in there with a PR or are you planning to do that?

@cari-lynn
Copy link
Contributor

Hey @amalagaura! We welcome PR's for documentation improvements. So thank you.

I took a look at the overlay playground and it looks like there is an append an array item example. The main difference being that the item we are matching in this case is an array item, and in the playground example it's a map.
In the match by key example there is an example on how to match on an array item.

There are a lot of combinations of overlay operations possible, and the two above come together to make a similar solution to the one in this issue. So I think for now, the current examples provide enough to show how to use overlay operations even though some examples may need to be used together.

Please let me know if you think there is some aspect that I may be missing here. Thanks

@amalagaura
Copy link
Contributor Author

As a newcomer I went through those examples but I was not able to combine the two examples and get it working on my own. It is up to you but I felt like the example provided above was new to me. I personally think it should be added as an additional example, but it is up to you.

@cari-lynn
Copy link
Contributor

We definitely want to improve that experience, and would love a PR for this example. If you can create a 16th example in the overlays section that puts matching an array item and appending an array item together that would be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
helping with an issue Debugging happening to identify the problem
Projects
None yet
Development

No branches or pull requests

3 participants