Mapping a Configuration

A configuration mapping is simply defined as an @Configuration-annotated interface with an @Property-annotated methods.

@Configuration
interface AppConfiguration {
  @Property("debug-mode")
  boolean debugMode();
    
  @Property("server")
  String server();
}

See below for a table of serializable types.

Then, create a configuration in any supported format. For this example, we'll use YAML.

debug: true
server: My Server

And finally, we can map it to our ApplicationConfiguration using the Warp.builder()

AppConfiguration configuration = Warp.builder(AppConfiguration.class)
    .source(YamlConfigurationSource.read(...))
    .build();

The YamlConfigurationSource.read(...) method takes in any of the following:

  • Path

  • InputStream

  • Reader

Embedding a Configuration

Any other configuration can be embedded in another by specifying the type of a property as the other configuration. This works as the type arguments in Optional, Map and List as well.

@Configuration
interface AppConfiguration {
  @Property("database")
  Database database();
  
  @Configuration
  interface Database {
    @Property("host")
    String host();
    
    @Property("port")
    int port();
  }
}

Serializable Types

Type
Notes

Byte, byte

Short, short

Integer, int

Long, long

Float, float

Double, double

Boolean, boolean

String, CharSequence

Optional

If the value isn't present, Optional.empty() is returned

Map

Keys are deserialized as strings; if the value isn't present, Map.of() is returned

List

If the value isn't present, List.of() is returned

Enum

Case sensitive

Last updated