简体   繁体   中英

YAML object properties

I am defining code review configuration in YAML. I need to keep the config as compact as possible without explicitly defining xml/json like name value pairs.

- 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. 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.

To compactify, you can also write them inline like daggett suggested:

- 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM