Reputation: 880
I have property file application-dev.yml with content:
spring.profiles: dev
config:
value: test
property: test2
foo:
value: test
property: test2
bar:
value: test
property: test2
There are some redundant values to my properties and in case I ever want to change test
to blah
and test2
to blah2
, I would have to go to every instance of the text and change it.
Is there some way I can (in Java terms) declare a variable and assign it to `test' and just use the variable throughout my yaml file so that I consolidate the text and make it easier to change in the future if needed?
Upvotes: 7
Views: 10647
Reputation: 39708
YAML does have anchors and aliases, which allow you to identify a certain value by giving it an anchor, and later reference it again with an alias:
spring.profiles: dev
config:
value: &a test
property: &b test2
foo:
value: *a
property: *b
bar:
value: *a
property: *b
Mind that this is not the same as a variable. YAML serializes a node graph, which is in its most common form, i.e. without using anchors/aliases, a tree structure. You can use anchors & aliases to serialize any directed graph structure, including cyclic graphs. This is what the feature is for.
The difference to a variable is that you do not have a definition of the value – instead, you add the anchor to the first occurrence of the value. You can then reference it in subsequent occurrences. Other differences include that you cannot process the value in any way, e.g. via string concatenation or interpolation. For example, if you want some key to have the value testtest2
, you cannot express that via the aliases *a
and *b
.
Upvotes: 14