yaml - 在 yaml 中使用占位符

标签 yaml computed-properties indirection template-variables

有没有办法像这样在 yaml 中使用占位符:

foo: &FOO
    <<propname>>: 
        type: number 
        default: <<default>>

bar:
    - *FOO 
       propname: "some_prop"
       default: "some default" 

最佳答案

上下文

  • YAML 版本 1.2
  • 用户希望
  • 在 YAML 中包含变量占位符
  • yaml.load 上用计算值替换占位符
  • 能够对 YAML 映射键和值使用占位符


  • 问题
  • YAML 本身不支持变量占位符。
  • anchor 和别名几乎提供了所需的功能,但它们不能作为可以插入到整个 YAML 文本中的任意区域的变量占位符。它们必须作为单独的 YAML 节点放置。
  • 有一些附加库支持任意变量占位符,但它们不是 native YAML 规范的一部分。

  • 示例
    考虑以下示例 YAML。它是格式良好的 YAML 语法,但它使用(非标准)花括号占位符和嵌入式表达式。
    嵌入的表达式不会在 YAML 中产生所需的结果,因为它们不是 native YAML 规范的一部分。然而,在本示例中使用它们只是为了帮助说明标准 YAML 中哪些可用,哪些不可用。
    part01_customer_info:
      cust_fname:   "Homer"
      cust_lname:   "Himpson"
      cust_motto:   "I love donuts!"
      cust_email:   homer@himpson.org
    
    part01_government_info:
      govt_sales_taxrate: 1.15
    
    part01_purchase_info:
      prch_unit_label:    "Bacon-Wrapped Fancy Glazed Donut"
      prch_unit_price:    3.00
      prch_unit_quant:    7
      prch_product_cost:  "{{prch_unit_price * prch_unit_quant}}"
      prch_total_cost:    "{{prch_product_cost * govt_sales_taxrate}}"   
    
    part02_shipping_info:
      cust_fname:   "{{cust_fname}}"
      cust_lname:   "{{cust_lname}}"
      ship_city:    Houston
      ship_state:   Hexas    
    
    part03_email_info:
      cust_email:     "{{cust_email}}"
      mail_subject:   Thanks for your DoughNutz order!
      mail_notes: |
        We want the mail_greeting to have all the expected values
        with filled-in placeholders (and not curly-braces).
      mail_greeting: |
        Greetings {{cust_fname}} {{cust_lname}}!
        
        We love your motto "{{cust_motto}}" and we agree with you!
        
        Your total purchase price is {{prch_total_cost}}
        
    
    说明
  • 下面是一个内嵌图像,它说明了带有绿色、黄色和红色区域的示例。
  • 用 GREEN 标记的替换在标准 YAML 中很容易获得,使用 anchor 、别名和 merge keys .
  • 用 YELLOW 标记的替换在技术上可用于标准 YAML,但并非没有 custom type declaration ,或其他一些绑定(bind)机制。
  • 以 RED 标记的替换在标准 YAML 中不可用。然而,也有解决方法和替代方案;如通过 string formatting或字符串模板引擎(例如 python 的 str.format )。

  • Image explaining the different types of variable substitution in YAML
    细节
    YAML 的一个经常请求的功能是能够插入任意变量占位符,这些占位符支持与同一(或 transcluded)YAML 文件中的其他内容相关的任意交叉引用和表达式。
    YAML 支持 anchor 和别名,但此功能不支持在 YAML 文本中任意放置占位符和表达式。它们仅适用于 YAML 节点。
    YAML 还支持 custom type declaration s,但是这些不太常见,如果您接受来自可能不受信任的来源的 YAML 内容,则会存在安全隐患。
    YAML 插件库
    有 YAML 扩展库,但这些不是 native YAML 规范的一部分。
  • Ansible
  • https://docs.ansible.com/ansible-container/container_yml/template.html
  • (支持 YAML 的许多扩展,但它是一个编排工具,如果你只想要 YAML,这有点矫枉过正)

  • https://github.com/kblomqvist/yasha
  • https://bitbucket.org/djarvis/yamlp

  • 解决方法
  • 将 YAML 与模板系统结合使用,例如 Jinja2 或 Twig
  • 使用 YAML 扩展库
  • 使用 sprintfstr.format来自托管语言的样式功能

  • 替代品
  • YTT YAML Templating本质上是 YAML 的一个分支,具有可能更接近 OP 中指定的目标的附加功能。
  • Jsonnet与 YAML 有一些相似之处,但具有可能更接近 OP 中指定目标的附加功能。

  • 也可以看看
    在这里
  • YAML variables in config files
  • Load YAML nested with Jinja2 in Python
  • String interpolation in YAML
  • how to reference a YAML "setting" from elsewhere in the same YAML file?
  • Use YAML with variables
  • How can I include a YAML file inside another?
  • Passing variables inside rails internationalization yml file
  • Can one YAML object refer to another?
  • is there a way to reference a constant in a yaml with rails?
  • YAML with nested Jinja
  • YAML merge keys
  • YAML merge keys

  • SO外
  • https://learnxinyminutes.com/docs/yaml/
  • https://github.com/dreftymac/awesome-yaml#variables
  • https://duckduckgo.com/?q=yaml+variables+in+config+file&t=h_&ia=web
  • 关于yaml - 在 yaml 中使用占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620674/

    相关文章:

    Linux Bash 如何使用 for 循环获取输出并将其放回脚本中

    c++ - 有间接仿函数吗?

    c++ - 在 C++ 中重载间接运算符

    symfony2 如何创建自己的自定义 yml 文件以在全局共享?

    php - 对具有不同服务参数的多个实体使用相同的实体监听器

    ruby - 如何在 YAML 中创建一个空数组?

    javascript - 计算属性或数据以传递给背景图像的样式绑定(bind)

    javascript - 通过 Object.assign 中的父赋值来分配嵌套在对象中的许多值

    go - Gomplate : bad character U+0022 '-'

    unit-testing - 使用 Jest 测试 Vue3 组件时如何模拟计算属性