问题描述
有没有办法像这样在 yaml 中使用占位符:
foo: &FOO<<道具名称>>:类型:数字默认:<>酒吧:- *FOO道具名称:some_prop"默认值:一些默认值"
Context
- YAML 1.2 版
- 用户希望
- 在 YAML 中包含变量占位符
- 在
yaml.load
时用计算值替换占位符 - 能够为 YAML 映射键和值使用占位符
问题
- YAML 本身不支持变量占位符.
- 锚点和别名几乎提供了所需的功能,但它们不能作为可以插入到整个 YAML 文本中任意区域的变量占位符.它们必须作为单独的 YAML 节点放置.
- 有一些附加库支持任意变量占位符,但它们不是本机 YAML 规范的一部分.
示例
考虑以下示例 YAML.它是格式良好的 YAML 语法,但它使用(非标准)花括号占位符和嵌入式表达式.
嵌入的表达式不会在 YAML 中产生所需的结果,因为它们不是本机 YAML 规范的一部分.然而,在本示例中使用它们只是为了帮助说明标准 YAML 中哪些可用,哪些不可用.
part01_customer_info:cust_fname:荷马"cust_lname: "Himpson";cust_motto:我喜欢甜甜圈!"cust_email: [email protected]_government_info:govt_sales_taxrate:1.15part01_purchase_info:prch_unit_label:培根包裹的花式釉面甜甜圈";prch_unit_price: 3.00prch_unit_quant:7prch_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:休斯顿ship_state: 六边形part03_email_info:cust_email: "{{cust_email}}";mail_subject:感谢您订购 DoughNutz!mail_notes: |我们希望 mail_greeting 具有所有预期值带有填充的占位符(而不是花括号).邮件问候: |问候 {{cust_fname}} {{cust_lname}}!我们喜欢您的座右铭{{cust_motto}}";我们同意你的看法!您的总购买价格为 {{prch_total_cost}}
说明
下面是一个内嵌图像,说明了带有绿色、黄色和红色区域的示例.
GREEN 中标记的替换在标准 YAML 中很容易获得,使用锚点、别名和
详情
YAML 的一个经常请求的功能是能够插入任意变量占位符,这些占位符支持与同一内容中的其他内容相关的任意交叉引用和表达式(或 嵌入) YAML 文件.
YAML 支持锚点和别名,但此功能不支持在 YAML 文本中的任何位置任意放置占位符和表达式.它们仅适用于 YAML 节点.
YAML 还支持 自定义类型声明,但是这些不太常见,如果您接受来自可能不受信任的来源的 YAML 内容,则会存在安全隐患.
YAML 插件库
有 YAML 扩展库,但这些不是本机 YAML 规范的一部分.
- Ansible
- https://docs.ansible.com/ansible-container/container_yml/模板.html
- (支持 YAML 的许多扩展,但它是一个编排工具,如果你只想要 YAML,这有点矫枉过正)
- https://github.com/kblomqvist/yasha
- https://bitbucket.org/djarvis/yamlp
解决方法
- 将 YAML 与模板系统(例如 Jinja2 或 Twig)结合使用
- 使用 YAML 扩展库
- 使用宿主语言中的
sprintf
或str.format
样式功能
替代方案
- YTT YAML 模板本质上是 YAML 的一个分支,具有可能更接近 OP 中指定的目标的附加功能.
- Jsonnet 与 YAML 有一些相似之处,但具有可能更接近 OP 中指定目标的附加功能.
另见
在这里
- 配置文件中的 YAML 变量立>
- 在 Python 中加载与 Jinja2 嵌套的 YAML
- YAML 中的字符串插值
- 如何参考YAML设置"来自同一个 YAML 文件中的其他地方?
- 将 YAML 与变量一起使用
- 如何包含 YAML 文件在另一个里面?
- 在 rails 国际化 yml 文件中传递变量
- 一个 YAML 对象可以引用另一个对象吗?
- 是否有使用 rails 在 yaml 中引用常量的方法?
- 带有嵌套 Jinja 的 YAML
- YAML 合并键
- YAML 合并键
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
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"
解决方案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 almost provide the desired functionality, 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: [email protected] 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}}
Explanation
Below is an inline image that illustrates the example with colored regions in green, yellow and red.
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://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
orstr.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
Here at SO
- 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
Outside 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 中使用占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- Ansible