Tra*_*ace 26 yaml indirection computed-properties template-variables
有没有办法在yaml中使用占位符,如下所示:
foo: &FOO
    <<propname>>: 
        type: number 
        default: <<default>>
bar:
    - *FOO 
       propname: "some_prop"
       default: "some default" 
dre*_*mac 66
请考虑以下示例YAML.它是格式良好的YAML语法,但它使用带有嵌入式表达式的(非标准)大括号占位符.
嵌入式表达式不会在YAML中产生所需的结果,因为它们不是本机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}}
    Thank you for your order!
以绿色标记的替换在标准YAML中很容易获得,使用锚点,别名和合并键.
黄色标记的替换在技术上可用于标准YAML,但不是没有自定义类型声明或其他一些绑定机制.
标记为YAML的RED中标记的替换不可用.然而,有一些解决方法和替代方案; 例如通过字符串格式化或字符串模板引擎(如python的yaml.load).
YAML经常请求的功能是能够插入任意变量占位符,这些占位符支持与同一(或转换)YAML文件中的其他内容相关的任意交叉引用和表达式.
YAML支持锚点和别名,但此功能不支持在YAML文本中的任何位置任意放置占位符和表达式.它们仅适用于YAML节点.
YAML还支持自定义类型声明,但这些不太常见,如果您接受来自可能不受信任的来源的YAML内容,则会产生安全隐患.
有YAML扩展库,但这些不是本机YAML规范的一部分.
str.format或sprintf样式功能使用Yglu结构模板,您的示例可以写成:
foo: !()
  !? $.propname: 
     type: number 
     default: !? $.default
bar:
  !apply .foo: 
    propname: "some_prop"
    default: "some default"
免责声明:我是作者或 Yglu。
| 归档时间: | 
 | 
| 查看次数: | 55771 次 | 
| 最近记录: |