Friday 24 June 2016

Setting Optional Sections in the Typesafe Config File


If your configuration lets users invent new sections, you may not have all paths up front,
 and may be unable to set up defaults in reference.conf for dynamic paths.
In Scala, you could write an enrichment class to use the idiomatic Option syntax to optionally set an item in config file:
implicit class RichConfig(val underlying: Config) extends AnyVal {
  def getOptionalString(path: String): Option[String] = 
  if (underlying.hasPath(path)) {
     Some(underlying.getBoolean(path))
  } else {
    None
}}
val item = myConfig.getOptionalString("option_item")

No comments:

Post a Comment