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();
}@Configuration
interface ApplicationConfiguration {
@Property("debug-mode")
fun debugMode(): Boolean
@Property("server")
fun server(): String
}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 ServerAnd finally, we can map it to our ApplicationConfiguration using the Warp.builder()
AppConfiguration configuration = Warp.builder(AppConfiguration.class)
.source(YamlConfigurationSource.read(...))
.build();val configuration = Warp.builder(AppConfiguration::class.java)
.source(YamlConfigurationSource.read(...))
.build();The YamlConfigurationSource.read(...) method takes in any of the following:
PathInputStreamReader
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();
}
}@Configuration
interface AppConfiguration {
@Property("database")
fun database(): Database
@Configuration
interface Database {
@Property("host")
fun host(): String
@Property("port")
fun port(): Int
}
}Serializable Types
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