Spring Boot - extend yml configuration with custom yml tags

Viewed 367

I need to use yaml anchor references together with string concatenation inside application.yml for Spring Boot app. Motivation is to reuse existing configs and not duplicate them. For example, we have the following application.yml:

sources:
  - filter:
      expression: &mainFilter
                    level > 100
  - filter:
      expression: *mainFilter
  - filter:
      expression: *mainFilter and level < 200

I need that it will be resolved to the following:

sources:
  - filter:
      expression: &mainFilter
                    level > 100
  - filter:
      expression: level > 100
  - filter:
      expression: level > 100 and level < 200

expression: *mainFilter will be resolved correctly as it's yml anchors, but expression: *mainFilter and level < 200 will not work as yml doesn't support anchors with concatenation (app will fail on app start). According to yml string concatenation with custom tags, yml has notion as custom tags that we could define, like expression: !concat [*mainFilter, 'and level < 200']. Is it possible to define and register our own custom tags in Spring Boot yml configuration?

1 Answers

I had the same need -- composing new property values from existing property values -- and after first thinking like you that it requires custom tags, in spring you can use property placeholders instead:

mainFilter: "level > 100"
sources:
  - filter:
      expression: ${mainFilter}
  - filter:
      expression: "${mainFilter} and level < 200"
Related