Configurable Converters

For some types, PureConfig cannot automatically derive a reader because there are multiple ways to convert a configuration value to them. For instance, for LocalDate PureConfig cannot derive a reader because there are multiple DateTimeFormatters that can be used to convert a string into a LocalDate. Examples of different formats are yyyy-mm-dd, e.g. "2016-01-01", and yyyymmdd, e.g. "20160101". Another example is reading maps with non-string keys: unless a way to convert the keys to and from strings is provided, PureConfig won’t be able to derive a reader.

For those types, PureConfig provides a way to create readers from the necessary parameters. These methods can be found under the package pureconfig.configurable. Once the output of a pureconfig.configurable method for a certain type is in scope, PureConfig can start using that configured reader.

Define a case class to hold your configuration, and create a configurable reader:

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import pureconfig._
import pureconfig.configurable._
import pureconfig.ConvertHelpers._

case class Conf(date: LocalDate, intMap: Map[Int, Int])

implicit val localDateConvert = localDateConfigConvert(DateTimeFormatter.ISO_DATE)
implicit val intMapReader = genericMapReader[Int, Int](catchReadError(_.toInt))

Then load the configuration:

ConfigSource.string("{ date: 2011-12-03, int-map: { 2: 4, 4: 16 } }").load[Conf]
// res0: ConfigReader.Result[Conf] = Right(
//   Conf(2011-12-03, Map(4 -> 16, 2 -> 4))
// )