Skip to content

dedica-team/config-functions

Repository files navigation

Spring Boot Config Functions

Call functions directly in your Spring Boot application.yaml:

release-version: "${fn.firstNonEmpty(${git.tags:}, ${git.commit.id.abbrev:}, unknown)}"

Getting Started

Add the dependency to your pom.xml:

<dependency>
    <groupId>team.dedica</groupId>
    <artifactId>config-functions</artifactId>
    <version>1.2.0</version>
</dependency>

That's it. The library is automatically activated via Spring Boot's EnvironmentPostProcessor mechanism.

Usage

Function calls are prefixed with fn. and can be used like any other property placeholder:

my-property: "${fn.functionName(arguments)}"

You can pass other Spring properties as arguments:

my-property: "${fn.functionName(${spring.application.name})}"

Available Functions

firstNonEmpty

Returns the first non-blank value from a comma-separated list of arguments.

my-property: "${fn.firstNonEmpty(,a,b)}"  # returns "a"

This is useful for defining fallback chains:

release-version: "${fn.firstNonEmpty(${git.tags:}, ${git.commit.id.abbrev:}, unknown)}"

In this example, the release version is resolved from the following sources in order:

  1. The Git tag (if available)
  2. The abbreviated commit hash (if available)
  3. The literal string unknown

Important: Define fallback values for optional properties

Always use the : suffix for properties that may not exist:

# Good - empty string fallback if the property is missing
"${fn.firstNonEmpty(${my.optional.property:}, fallback)}"

# Bad - if the property is missing, the literal string "${my.optional.property}" is used,
# which is non-empty and will be returned as-is
"${fn.firstNonEmpty(${my.optional.property}, fallback)}"

requireResolved

Checks that all property placeholders in the argument have been resolved. If any unresolved placeholders are detected, an exception is thrown at startup.

db-password: "${fn.requireResolved(${DB_PASSWORD})}"

If DB_PASSWORD is set, the resolved value is returned. If it is not set, the function will throw an error:

The following variables have not been resolved: ${DB_PASSWORD}

Multiple variables can be checked at once:

jdbc-url: "${fn.requireResolved(jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME})}"

If any of them are unresolved, all unresolved variables are listed in the error message.

base64Decode

Decodes a Base64-encoded argument and returns the result as string.

This is useful, when configuration values are passed as Base64-encoded strings, for example SSL certificates or keys provided via environment variables:

ssl-certificate: "${fn.base64Decode(${SSL_CERT_BASE64})}"

urlEncode

Encodes the argument for safe use in URLs. This is useful, when composing URLs from properties that may contain special characters such as @, #, or %:

jdbc-url: "jdbc:postgresql://user:${fn.urlEncode(${DB_PASSWORD})}@localhost:5432/mydb"

Requirements

  • Spring Boot 3.x
  • Java 17+

License

MIT

About

Adds a set of functions, that can be used in the application.yaml of a Spring Boot application.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages