-
Notifications
You must be signed in to change notification settings - Fork 38
Defining Property Keys
Property keys can be defined by delegated properties that delegate to a property type. This lets you refer to them with the same names in code and property files.
Simple "magic" keys can be defined as vals at package scope. For example:
val port by intType
val service by uriType
Keys defined this way are named after the property. In a properties file, these keys would be used like:
port=8080
service=http://backend.example.com/service
Any underscores in the property name are converted to hyphens in the key name, so the following declaration defines a key named web-service
:
val web_service by uriType
The PropertyGroup
abstract class lets you group related configuration keys and refer to them with the same syntax in code and property files.
Define groups of property keys by deriving objects from PropertyGroup
that have read-only properties (vals) of the object that delegate to a property type.
For example, the following declaration defines three properties, a URI property named database.uri
and two string properties named database.username
and database.password
.
object database : PropertyGroup() {
val uri by uriType
val username by stringType
val password by stringType
}
To read the properties defined above from a Configuration object:
val config : Configuration = loadConfigurationSomehow()
val datasource = ExampleDataSource().apply {
uri = config[database.uri]
username = config[database.username]
password = config[database.password]
}
Keys are referred to by the same name in both code and property files. For example, in a properties file:
database.uri=snarl://rdf.example.com/
database.username=admin
database.password=SDE!DR0923D
If necessary you can define a property keys directly by instantiating the Key
class. The constructor takes the name and property type as parameters.
val server_port = Key("server.port", intType)