59

Suppose I have:

base_array:
  -1
  -2

how could I do something like:

my_array: << base_array
  -3

so that my_array was [1,2,3]

Update: I should specify that I want the extending to occur inside the YAML itself.

user229044
  • 232,980
  • 40
  • 330
  • 338
Ben G
  • 26,091
  • 34
  • 103
  • 170
  • 2
    @meagar: Yaml has the *, &, and << operators for references and merging. You can extend a hash reference, why shouldnt you be able to extend an array reference? – Ben G Oct 21 '13 at 19:16
  • Yes, I realized you were trying to extend a hash, rather than simply add additional data at arbitrary points. – user229044 Oct 21 '13 at 19:17
  • I'm trying to extend an array, not a hash. – Ben G Oct 21 '13 at 19:18
  • 1
    possible duplicate of [Is there a way to alias/anchor an array in YAML?](http://stackoverflow.com/questions/4948933/is-there-a-way-to-alias-anchor-an-array-in-yaml) – Stefan Oct 21 '13 at 19:19
  • looks like it is a dupe.. was hoping there'd be some sort of method to DRY up YAMLs in this regard but apparently not.. – Ben G Oct 21 '13 at 19:40
  • 1
    possible duplicate of [Is there YAML syntax for sharing part of a list or map?](http://stackoverflow.com/questions/9254178/is-there-yaml-syntax-for-sharing-part-of-a-list-or-map) – Anthon Jun 06 '15 at 05:42
  • 3
    Possible duplicate of [How to merge YAML arrays?](https://stackoverflow.com/questions/24090177/how-to-merge-yaml-arrays) – Yves M. Sep 28 '17 at 08:21
  • 2
    Hope it helps - https://github.com/yaml/yaml/issues/35 – DAIRAV Apr 04 '18 at 06:28
  • Please also tell about your post-processing environment (e.g. java, python, etc.) and the concrete context (what content will your arrays contain, for which purpose). That may help finding the best solution. – hc_dev Feb 06 '19 at 10:09
  • As a fallback, if the application code is also maintained by you, one can recursively merge them there, similar to how I did it [here](https://stackoverflow.com/questions/59832361/extract-set-of-leaf-values-found-in-nested-dicts-and-lists-excluding-none). – Asclepius Jan 26 '20 at 20:58

2 Answers2

12

Since the already commented issue#35 exists, merge-keys << doesn't help you. It only merges/inserts referenced keys into a map (see YAML docs merge). Instead you should work with sequences and use anchor & and alias *.

So your example should look like this:

base_list: &base
    - 1
    - 2

extended: &ext
    - 3

extended_list:
    [*base, *ext]

Will give result in output like this (JSON):

{
  "base_list": [
    1, 
    2
  ], 
  "extended": [
    3
  ], 
  "extended_list": [
    [
      1, 
      2
    ], 
    [
      3
    ]
  ]
} 

Although not exactly what you expected, but maybe your parsing/loading environment can flatten the nested array/list to a simple array/list.

You can always test YAML online, for example use:

hc_dev
  • 8,389
  • 1
  • 26
  • 38
2

I needed to do the same thing but to run on Azure DevOps Pipeline. In particular, I had to update the stage dependency dynamically. How I did it:

dependents: [Stage_A, Stage_B]
otherDependents: [Stage_C] # This needed to be added by policy to the pipeline's execution
dependsOn:
- ${{ each dependent in dependents }}:
  - ${{ dependent }}
- ${{ each dependent in otherDependents }}:
  - ${{ dependent }}

Doing so resulted in the required setup:

dependents: [Stage_A, Stage_B]
otherDependents: [Stage_C] # This needed to be added by policy to the pipeline's execution
dependsOn:
  - Stage_A
  - Stage_B
  - Stage_C

I say dynamically because the variable dependents came from a template to which I had to append Stage_C.

raviabhiram
  • 671
  • 2
  • 8
  • 21