[英]YAML object properties
I am defining code review configuration in YAML. 我在YAML中定义代码审查配置。 I need to keep the config as compact as possible without explicitly defining xml/json like name value pairs. 我需要保持配置尽可能紧凑,而无需像名称/值对那样显式定义xml / json。
- group: Naming convention
severity: medium
rules:
- name: Check API naming convention
type: pattern
element: api.@name
pattern: '.*\-.*\-\d\.\d'
properties:
- exit-on-fail
- skip-and-proceed
- etc.
What I don't like here is defining the tag "properties" to add the actions. 我在这里不喜欢定义标签“ properties”来添加操作。 Can the actions exist at the object level? 动作可以存在于对象级别吗?
Indentation-wise, they can exist on the object level: 就缩进而言,它们可以存在于对象级别:
- group: Naming convention
severity: medium
rules:
- name: Check API naming convention
type: pattern
element: api.@name
pattern: '.*\-.*\-\d\.\d'
properties:
- exit-on-fail
- skip-and-proceed
- etc.
This works because YAML sees -
as indentation and therefore, this still creates a list as value for the properties:
key. 之所以可行-
是因为YAML将-
视为缩进,因此,仍会创建一个列表作为properties:
键的值。
To compactify, you can also write them inline like daggett suggested: 为了简化,您也可以像daggett建议的那样以内联方式编写它们:
- group: Naming convention
severity: medium
rules:
- name: Check API naming convention
type: pattern
element: api.@name
pattern: '.*\-.*\-\d\.\d'
properties: [exit-on-fail, skip-and-proceed, etc]
Finally, you can put them into your object mapping as long as they do not share a name with any other fields : 最后, 只要它们不与任何其他字段共享名称 ,就可以将它们放入对象映射中 :
- group: Naming convention
severity: medium
rules:
- name: Check API naming convention
type: pattern
element: api.@name
pattern: '.*\-.*\-\d\.\d'
? exit-on-fail
? skip-and-proceed
? etc.
This creates three additional key-value pairs in your object, with the three properties being the keys and the empty string (possibly a null value depending on the YAML implementation you use) being the value. 这将在您的对象中创建三个附加的键值对,其中三个属性为键,而空字符串(根据您使用的YAML实现,可能为空值)为该值。 If you do this, you will need to write a custom constructor to load this into a native data structure, because you need to differentiate between the object fields and the actions. 如果这样做,您将需要编写一个自定义构造函数以将其加载到本机数据结构中,因为您需要区分对象字段和动作。 How to do this, again, depends on your YAML implementation. 同样,如何执行此操作取决于您的YAML实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.