Sreehari Puliyakkot
Sreehari Puliyakkot

Reputation: 736

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?

Upvotes: 0

Views: 2749

Answers (1)

flyx
flyx

Reputation: 39638

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.

Upvotes: 1

Related Questions