Introduction
In this article, we will discuss The Scala Configuration API but before discussing The Scala Configuration API, let's know about the Play Framework.
Play is an open-source web application framework created by Guillaume Bort that follows the model–view–controller architectural pattern. Play was written in Scala and usable from other programming languages, e.g., Java. Play uses the Typesafe config library and provides a nice Scala wrapper called Configuration with more advanced Scala features.
The main aim of Play is to optimize the developer productivity by using convention over Configuration and display of errors in the browser. Play is heavily inspired by ASP.NET MVC, Ruby on Rails, and Django and is similar to this family of frameworks.
Now coming to the topic "The Scala Configuration API."
The Scala Configuration API
If you're unfamiliar with Typesafe config, don't worry; below, we will discuss the configuration file syntax and features.
We know that the configuration file used by the Play is based on the Typesafe config library.
Also, the configuration file of a Play application must be defined in conf/application.conf and uses the HOCON format.
The natural way to use Config is to have all configuration keys defined in reference.conf or application.conf. And if the key does not have a reasonable default value, it is set to null, which signifies "no value."
Let's discuss how we access the Configuration.
Accessing the Configuration
We will obtain a Configuration object through Dependency Injection (Dependency injection is a design pattern that helps separate our components' behavior from dependency resolution) or by passing an instance of Configuration to our component.
class CodingNinjas @Inject() (config: Configuration, c: ControllerComponents) extends AbstractController(c){
def getFoo = Action{
Ok(config.get[String]("data"))
}
}
The get method above is the most common method to get a single value in the configuration file.
// data = codingninjas
config.get[String]("foo")
// value = 8
config.get[Int]("value")
// bool = true
config.get[Boolean]("bool")
// listOfData = ["value", "bool"]
config.get[Seq[String]]("listOfData")
The Configuration also supports validating against a set of valid values.
config.getAndValidate[String]("data", Set("value", "bool"))
The most common types of implicit ConfigLoader that it accept are String, Int, and even Seq[String].
Now the question that comes to your mind What is ConfigLoader?
ConfigLoader
You can define your ConfigLoader or easily convert Configuration into a custom type. It is a great way to bring more type safety to your configuration use, and also this is used extensively in Play internally.
case class CodingNinjas(title: String, baseUri: URI)
object CodingNinjas{
implicit val configLoader: ConfigLoader[CodingNinjas] = new ConfigLoader[CodingNinjas]{
def load(rootConfig: Config, path: String): CodingNinjas ={
val config = rootConfig.getConfig(path)
CodingNinjas(title = config.getString("title"), baseUri = new URI(config.getString("baseUri")))
}
}
}
Then you can use Config.get as we did above:
config.get[CodingNinjas]("app.config")
Now let's discuss some optional configuration keys in The Scala Configuration API.
Optional Configuration Keys
Play's Configuration uses the getOptional[A] method to support optional configuration keys. It works like get[A], but it will return None if the key does not exist.
We recommend setting optional keys to null in your configuration file and using get[Option[A]] instead of the above method. Still, we provide this method for convenience in case you need to interface with libraries that use a configuration in a non-standard way.
Till now, we have briefly discussed The Scala Configuration API. So, below are some FAQs related to The Scala Configuration API.