如何在 YAML 中正确地将 dict 与锚点合并

发布于 2025-01-10 10:04:33 字数 596 浏览 0 评论 0原文

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

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" 

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" 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-01-17 10:04:33

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

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" 

合并是非标准 YAML 功能。序列没有类似的功能。据我所知,没有任何实现可以提供您使用一个命令将所有common_files添加到文件所需的功能。

<< 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文