avidbots / flatland

A 2D robot simulator for ROS
BSD 3-Clause "New" or "Revised" License
107 stars 65 forks source link

yaml_preprocessor: add support for $include and $[include] #93

Closed pbelanger-avid closed 1 year ago

pbelanger-avid commented 1 year ago

This commit adds support for a special $include keyword in the YAML preprocessor. This new keyword is similar to the existing $eval keyword and allows building the robot.model.yaml up from multiple separate files.

$include can process both absolute filenames as well as relative filenames, which are interpreted relative to the file currently being processed.

The included document is also processed by the YAML preprocessor, and so can include nested $includes and $evals.

This commit also updates the documentation and tests.

Some usage examples:

# Example of $include: 
# project/param/parent.yaml
foo: 123
bar: $include child.yaml # Looks in current directory (project/param/) for relative filenames
baz: $include /absolute/path/to/project/param/child.yaml # or an absolute path can be specified

# project/param/child.yaml
a: 1
b: 2

# result of YAML preprocessing parent.yaml:
foo: 123
bar:
  a: 1
  b: 2
baz:
  a: 1
  b: 2

# example of $[include]: 
# parent.yaml
foo:
  - first
  - $[include] child.yaml
  - last

#child.yaml
one
---
a: foo
b: baz
---
three

# result of YAML preprocessing of parent.yaml:
foo:
  - first
  - one
  - a: foo
    b: baz
  - three
  - last
pbelanger-avid commented 1 year ago

Note: currently this does not detect or handle recursive includes. (eg. a.yaml including itself)

pbelanger-avid commented 1 year ago

The second commit adds a second "sequence include" form, $[include], that can be used to dynamically populate sequences (such as the plugin list in my particular use case) with YAML elements pulled from individual documents in the included file.