如何在 YAML 中正确合并 dict 和 anchor

How to correctly merge dict with anchor in YAML

请帮助将 YAML 中带有锚点的字典合并到文件中,我尝试这样做但是 YAML return 错误的结果。感谢帮助

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" 

<< 适用于映射,但您尝试使其适用于序列。这不可能。您可以通过别名单独包含公共文件,例如:

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" 

合并是一项 non-standard YAML 功能。序列没有类似的特征。我所知道的任何实现都无法提供您需要使用一条命令将所有 common_files 添加到 文件 之前。