Sunday, March 22, 2015

typesafe config in small data processing applications

I use typesafe's config for my applications. Typically, my applications involve a database connection used for data analysis. That means that each application really does not have a fixed, one and only one db connection. I need to specify different connections as I cycle through different database types for the analysis.
I use typesafe's config for specifying the database connection and other application information. I want some defaults for various parameters, but I also require a mandatory config file for the database that is provided by the user. To do this, I use a slightly different form of the standard ConfigFactory.load()pattern:
   val config = ConfigFactory.parseFile(Paths.get("myapp.conf").toFile).withFallback(ConfigFactory.load())
That helps me ensure that an application config file is called while still relying on the default machinery of the config module to find internal config files e.g. my app's default parameters or akka actors.
To force it to be mandatory, I require myapp.conf to define parameters and throw an exception (I don't need commercial grade user messages for this) if a setting is not found:
case class MyConfig(config: Config) {
  val myArg = config.getString("myConfigProperty")
}

No comments:

Post a Comment