Use placeholders in yaml

拈花ヽ惹草 提交于 2019-11-26 01:59:40

问题


Is there a way to use placeholders in yaml like this:

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

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

回答1:


Context

  • YAML version 1.2
  • user wishes to
    • include variable placeholders in YAML
    • have placeholders replaced with computed values, upon yaml.load
    • be able to use placeholders for both YAML mapping keys and values

Problem

  • YAML does not natively support variable placeholders
  • Anchors and Aliases do allow for some level of abstraction and indirection, but these do not work as variable placeholders that can be inserted into arbitrary regions throughout the YAML text. They must be placed as separate YAML nodes
  • There are some add-on libraries that support arbitrary variable placeholders, but they are not part of the native YAML specification

Example

Consider the following example YAML. It is well-formed YAML syntax, however it uses (non-standard) curly-brace placeholders with embedded expressions.

The embedded expressions do not produce the desired result in YAML, because they are not part of the native YAML specification. Nevertheless, they are used in this example only to help illustrate what is available with standard YAML and what is not.

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!

Explanation

  • The substitutions marked in GREEN are readily available in standard YAML, using anchors, aliases, and merge keys.

  • The substitutions marked in YELLOW are technically available in standard YAML, but not without a custom type declaration, or some other binding mechanism.

  • The substitutions marked in RED are not available in standard YAML. Yet there are workarounds and alternatives; such as through string formatting or string template engines (such as python's str.format).

Details

A frequently-requested feature for YAML is the ability to insert arbitrary variable placeholders that support arbitrary cross-references and expressions that relate to the other content in the same (or transcluded) YAML file(s).

YAML supports anchors and aliases, but this feature does not support arbitrary placement of placeholders and expressions anywhere in the YAML text. They only work with YAML nodes.

YAML also supports custom type declarations, however these are less common, and there are security implications if you accept YAML content from potentially untrusted sources.

YAML addon libraries

There are YAML extension libraries, but these are not part of the native YAML spec.

  • Ansible
    • https://docs.ansible.com/ansible-container/container_yml/template.html
    • (supports many extensions to YAML, however it is an Orchestration tool, which is overkill if you just want YAML)
  • https://github.com/kblomqvist/yasha
  • https://github.com/dreftymac/dynamic.yaml
  • https://bitbucket.org/djarvis/yamlp

Workarounds

  • Use YAML in conjunction with a template system, such as Jinja2 or Twig
  • Use a YAML extension library
  • Use sprintf or str.format style functionality from the hosting language

Alternatives

  • YTT YAML Templating essentially a fork of YAML with additional features that may be closer to the goal specified in the OP.
  • Jsonnet shares some similarity with YAML, but with additional features that may be closer to the goal specified in the OP.

See also

  • 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?
  • https://learnxinyminutes.com/docs/yaml/
  • https://github.com/dreftymac/awesome-yaml
  • YAML merge keys
  • YAML merge keys



回答2:


I suppose https://get-ytt.io/ would be an acceptable solution to your problem



来源:https://stackoverflow.com/questions/41620674/use-placeholders-in-yaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!