Skip to content

Macros evaluator

Adrien LAUER edited this page Feb 23, 2017 · 1 revision

Enabling macros

To enable macros, you must enable the org.seedstack.coffig.evaluator.MacroEvaluator class. This can be done when building the Coffig instance:

Coffig.builder()
      .withProviders(...)
      .withEvaluators(new MacroEvaluator())
      .build();

or by registering an evaluator in META-INF/services/org.seedstack.coffig.spi.ConfigurationEvaluator:

org.seedstack.coffig.evaluator.MacroEvaluator

Usage

Macros are references to other nodes in the global configuration tree and are replaced by the value of the referenced node. Macros use the ${} syntax:

name: World
message: Hello ${name}!

Evaluation

In the example above, the node message will be evaluated to "Hello World!". Note that the macro must specify the full path of the referenced node.

A macro is evaluated each time the containing node is mapped. This means that if the referenced node has been modified, the node containing the macro will change accordingly.

A macro is evaluated recursively so it can reference a node containing another macro and so on.

Escaping

Macro resolution can be avoided by escaping the dollar sign with a backslash: Hello \${name}! will be evaluated to "Hello ${name}!".

Nesting

Macros can be nested:

names: [ John, Jane ]
index: 1
message: Hello ${names[${index}]}!

The message node will be evaluated to "Hello Jane!".

Default value

If a macro references a non-existing node, it will be evaluated to an empty string. A macro can have a default value that can be a quoted string or another node reference.

name: World
message1: Hello ${name}!
message2: Hello ${foobar}!
message3: Hello ${foobar:'Robert'}!
message4: Hello ${foobar:name}!
  • The message1 node will be evaluated to "Hello World!".
  • The message2 node will be evaluated to "Hello !".
  • The message3 node will be evaluated to "Hello Robert!".
  • The message4 node will be evaluated to "Hello World!".
Clone this wiki locally