-
Notifications
You must be signed in to change notification settings - Fork 6
Macros evaluator
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
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}!
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.
Macro resolution can be avoided by escaping the dollar sign with a backslash: Hello \${name}!
will be evaluated to "Hello ${name}!".
Macros can be nested:
names: [ John, Jane ]
index: 1
message: Hello ${names[${index}]}!
The message
node will be evaluated to "Hello Jane!".
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!".