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

Merging Lists #469

Closed
RishabhMalviya opened this issue Jan 4, 2021 · 3 comments
Closed

Merging Lists #469

RishabhMalviya opened this issue Jan 4, 2021 · 3 comments
Labels

Comments

@RishabhMalviya
Copy link

I've been trying to find a way to merge lists in YAML. There are a number of StackOverflow questions trying to do the same:

  1. https://stackoverflow.com/questions/24090177/how-to-merge-yaml-arrays
  2. https://stackoverflow.com/questions/9254178/is-there-yaml-syntax-for-sharing-part-of-a-list-or-map
  3. https://stackoverflow.com/questions/19502522/extend-an-array-in-yaml

All of the solutions suggested are workarounds:

  1. Combining lists like this:
base_list: &base
    - 1
    - 2

extended: &ext
    - 3

extended_list:
    [*base, *ext]

requires the resultant list to be flattened, which leads to suggestions like adding a !flatten tag that will then have to be specially processed by whatever is the underlying parser (e.g., PyYAML).

  1. Other people suggest using the merge key type feature. Which would mean you are combining keys, all having null values.

Unfortunately, these workarounds aren't viable for me, because I need to do this in a docker-compose.yml file; so I have no control over the parser's behavior.

I would like to understand why this is not possible in YAML? I'm assuming there is something fundamental to the YAML Information Model that makes this impossible, since some of those SO posts were 8 years old. Any pointers are appreciated.

Ideally, I would like to help decide on some syntax, and then make the necessary changes in this repo. These changes would then transitively become available to use in docker-compose (which is also a Python package) whenever they decide to upgrade the underlying pyyaml parser version.

@gsmethells
Copy link

+1

@gsmethells
Copy link

yaml/yaml#35 talks about it at length and yaml/yaml#48 tries to suggest new syntax to YAML. I hope it goes somewhere some day soon!

@ingydotnet
Copy link
Member

ingydotnet commented Oct 14, 2021

The right way to do this is something like:

foo: !concat [*list1, *list2]

which you can do now by registering a function for !concat. You might use !++ to do it cleaner.

YAML 1.3 in design now, will likely make that easy to do in a standard way possibly without even needing the explicit tag. It will almost certainly have standard functions defined for things like merging.

@RishabhMalviya k8s might not let you do this directly at this time, but you could preprocess with PyYAML. Or preprocess with many other non-YAML preprocessing solutions.

PS You can now engage directly with YAML language design team using https://matrix.to/#/#chat:yaml.io

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants