Skip to content

Name templating

Andy Boothe edited this page Jan 18, 2025 · 2 revisions

In an effort to minimize boilerplate, Rapier provides basic templating for configuration names. This allows users to provision configuration data using a name that is evaluated at runtime based on the contents of an environment variable or system property.

Example

The following code provisions an AWS SSM parameter that computes its name from the STAGE environment variable.

/**
 * If the environment variable "STAGE" has the value "PROD", then this provisions
 * the environment variable "/prod/timeout". If the environment variable "STAGE" is
 * not present, then the generated code would throw an `IllegalStateException` on
 * initialization.
 */
@AwsSsmStringParameter(value="/${env.STAGE}/timeout", defaultValue="30000")
public long timeout();

Syntax

Rapier modules that support templating use the following syntax:

  • ${env.NAME} - Replace with the value of the NAME environment variable if present, or else throw IllegalStateException
  • ${env.NAME:-default} - Replace with the value of the NAME environment variable if present, or else use the value "default"
  • ${sys.name} - Replace with the value of the name system property if present, or else throw IllegalStateException
  • ${sys.name:-default} - Replace with the value of the name system property if present, or else use the value "default"

Only configuration value names may be templated.

Testing

Rapier modules that support name templating provide features for using custom variables when testing. For example, to perform testing with the rapier-environment-variable module, use:

final Map<String, String> customEnvironmentVariables = Map.of(
    "FOO", "BAR",
    "ALPHA", "BRAVO");
final Map<String, String> customSystemProperties = Map.of(
    "com.example.foo", "bar",
    "com.example.alpha", "bravo");
final ExampleComponent component = DaggerExampleComponent.builder()
    .rapierExampleComponentEnvironmentVariableModule(
        new RapierExampleComponentEnvironmentVariableModule(
            customEnvironmentVariables,
            customSystemProperties)))
    .build();

For more information about a specific Rapier module, check that module's README.

Clone this wiki locally