在yaml文件中有条件签入以显示正确的内容
如何在yaml文件中检查在yaml文件中有条件签入以显示正确的内容,yaml,Yaml,如何在yaml文件中检查if/else 比如: if %{attribute} attributes: shipping_comment: Shipping comment / Instructions else attributes: shipping_date: Date YAML是一种数据序列化语言,因此它并不意味着包含if/else风格的可执行语句:这是您使用的编程语言的责任 Ruby中的一个简单示例可以定义YAML配置文件,以确定要输出
if/else
比如:
if %{attribute}
attributes:
shipping_comment: Shipping comment / Instructions
else
attributes:
shipping_date: Date
YAML是一种数据序列化语言,因此它并不意味着包含
if
/else
风格的可执行语句:这是您使用的编程语言的责任
Ruby中的一个简单示例可以定义YAML配置文件,以确定要输出YAML文件中的哪个配置字符串,如下所示:
data.yml
attributes:
shipping_comment: Shipping comment / Instructions
shipping_date: Date
然后,在程序中,在中读取文件并在其中运行条件语句:
shipping.rb
#!/usr/bin/env ruby
require 'yaml'
config = YAML.load_file('data.yml')
attribute = true # your attribute to check here
if attribute
puts config['attributes']['shipping_comment']
else
puts config['attributes']['shipping_date']
end
现成的.yaml文件不包含任何条件逻辑,如下所示:
YAML是一种数据序列化语言,因此它并不包含if/else风格的可执行语句:这是您所使用的编程语言的责任 然而,在某些情况下,例如作为代码的基础设施,您可能没有Paul的解决方案那样的奢华。在这些情况下,大多数体面的基础设施工具提供了一种实现条件逻辑的内置方式 由于infra似乎不是您要查找的领域,因此我将不详细介绍如何编写每个工具解决方案,但对于像我一样最终出现在这里的任何人来说,这些文档对我很有帮助,并且可能对您很有用: