Skip to content
paradigmatic edited this page Jan 5, 2013 · 3 revisions

Configuration keys can be seen as a "path" to a value, such as a path to a file in a filesystem. The dot '.' acts as a kind of directory separator. Let config be:

site.url                  = http://www.example.com/3
site.credentials.username = bob
site.credentials.password = "b0b rul3z !!!"
upload.retrying           = on
upload.retrying.times     = 3

We can see this configuration as containing to blocks: site and upload. The site block contains in its turn another block: credential.

Dettaching a block

A new Configuration can be created by detaching a block:

val siteConfig = config detach "site"
val username = siteConfig[String]("credentials.username")
val password = siteConfig[String]("credentials.password")

or:

val credentialsConfig = config detach "site.credentials"
val username = credentialsConfig[String]("username")
val password = credentialsConfig[String]("password")  

It is possible to retrieve the prefix of a detached configuration using the prefix method:

config.prefix            //returns None    
siteConfig.prefix        //returns Some("site")
credentialsConfig.prefix //returns Some("site.credentials")

The methods prefixes returns a set with all top level prefixes:

config.prefixes            //returns Set("site", "upload")
siteConfig.prefixes        //returns Set("credentials")
credentialsConfig.prefixes //returns Set()

Finally, we can detach all top level block with detachAll method. It returns a Map[String,Configuration] where the keys are the prefixes and the value the corresponding detached block.

Attaching a block

A new Configuration can be created by attaching a block with a given prefix:

val ftpConfig = Config( "username" -> "public", "password" -> "", "port" -> 2323 )
val fullConfig = config.attach( "upload.ftp", ftpConfig )
val ftpPort = fullConfig[Int]( "upload.ftp.port" ) // ftpPort == 2323

Clone this wiki locally