0

I am trying to declare a repeatable part of the list, but it seems that I misunderstand the yaml syntax.

_test:
  _const: &const
  - a: a
    b: b
  - c: c
    d: d    
data:
  list_ef:
  -
    <<: *const
  - e: e
    f: f
  list_gh:
  -
    <<: *const
  - g: g
    h: h

so, i need to get data section like this:

data:
  list_ef:
  - a: a
    b: b
  - c: c
    d: d  
  - e: e
    f: f
  list_gh:
  - a: a
    b: b
  - c: c
    d: d
  - g: g
    h: h

but all I get is either error меssages or a nested list or something like this (nested too):

--- 
_test: 
  _const: 
    - 
      a: a
      b: b
    - 
      c: c
      d: d
data: 
  list_ef: 
    - 
      ? "<<"
      : 
        - 
          a: a
          b: b
        - 
          c: c
          d: d
    - 
      e: e
      f: f
  list_gh: 
    - 
      ? "<<"
      : 
        - 
          a: a
          b: b
        - 
          c: c
          d: d
    - 
      g: g
      h: h

What should be the correct syntax for this?

EugenOS
  • 31
  • 6
  • I believe this is a duplicate of the above question. The `<<` syntax is an [extension](http://yaml.org/type/merge.html) to YAML not supported by all YAML software, and is for merging mappings, but not arrays. The above question discusses several possible approaches to your problem, but there is no one straightforward answer for this. I believe GitLab's YAML parser supports something like this with arrays too, but it's a non-standard extension. – Iguananaut Sep 01 '21 at 14:30
  • In fact, in the case of GitLab, I'm not even sure they support any special syntax. Instead you may need a little additional logic in parsing your data structures. For example, I've done something like `key: [*array1, *array2]` where `*arrayN` is an anchor to an array. You then need to modify your handling of the value of `key` to support flattening of nested arrays. – Iguananaut Sep 01 '21 at 14:32

0 Answers0