laikovski
laikovski

Reputation: 25

How to correctly merge dict with anchor in YAML

Please help merge dict with anchor in YAML to files, I try to do this but YAML return wrong result. Thanks for help

common_files: &common_files
    - file_path: "link -1"
      content_pattern:
      - "text"
      - "text"
      one: "zoo"
      two: "foo"
      three: "fii" 
    - file_path: "link -2"
      content_pattern:
      - "text"
      - "text"
      one: "zoo"
      two: "foo"
      three: "fii" 

files: 
    - <<: *common_files
    - file_path: "link-3"
      content_pattern:
      - "text"
      - "text"
      one: "zoo"
      two: "foo"
      three: "fii" 

Upvotes: 2

Views: 3845

Answers (1)

flyx
flyx

Reputation: 39638

<< works on mappings, but you try to make it work on sequences. This is not possible. You can include the common files individually via alias, eg:

common_files:
    - &link1
      file_path: "link -1"
      content_pattern:
      - "text"
      - "text"
      one: "zoo"
      two: "foo"
      three: "fii" 
    - &link2
      file_path: "link -2"
      content_pattern:
      - "text"
      - "text"
      one: "zoo"
      two: "foo"
      three: "fii" 

files: 
    - *link1
    - *link2
    - file_path: "link-3"
      content_pattern:
      - "text"
      - "text"
      one: "zoo"
      two: "foo"
      three: "fii" 

Merge is a non-standard YAML feature. There is no similar feature for sequences. No implementation I know of provides what you'd need to prepend all common_files to files with one command.

Upvotes: 3

Related Questions