diff --git a/src/main/scala/net/ceedubs/ficus/Ficus.scala b/src/main/scala/net/ceedubs/ficus/Ficus.scala index 98fb8be..87baef3 100644 --- a/src/main/scala/net/ceedubs/ficus/Ficus.scala +++ b/src/main/scala/net/ceedubs/ficus/Ficus.scala @@ -18,6 +18,7 @@ trait FicusInstances with ISOZonedDateTimeReader with PeriodReader with LocalDateReader + with ChronoUnitReader with URIReaders with URLReader with InetSocketAddressReaders diff --git a/src/main/scala/net/ceedubs/ficus/readers/AllValueReaderInstances.scala b/src/main/scala/net/ceedubs/ficus/readers/AllValueReaderInstances.scala index 51d39cd..93c0eff 100644 --- a/src/main/scala/net/ceedubs/ficus/readers/AllValueReaderInstances.scala +++ b/src/main/scala/net/ceedubs/ficus/readers/AllValueReaderInstances.scala @@ -12,6 +12,7 @@ trait AllValueReaderInstances with TryReader with ConfigValueReader with PeriodReader + with ChronoUnitReader with LocalDateReader object AllValueReaderInstances extends AllValueReaderInstances diff --git a/src/main/scala/net/ceedubs/ficus/readers/ChronoUnitReader.scala b/src/main/scala/net/ceedubs/ficus/readers/ChronoUnitReader.scala new file mode 100644 index 0000000..90eaa15 --- /dev/null +++ b/src/main/scala/net/ceedubs/ficus/readers/ChronoUnitReader.scala @@ -0,0 +1,16 @@ +package net.ceedubs.ficus.readers + +import com.typesafe.config.Config + +import java.time.temporal.ChronoUnit + +trait ChronoUnitReader { + implicit val chronoUnitReader: ValueReader[ChronoUnit] = new ValueReader[ChronoUnit] { + + /** Reads the value at the path `path` in the Config */ + override def read(config: Config, path: String): ChronoUnit = + ChronoUnit.valueOf(config.getString(path).toUpperCase) + } +} + +object ChronoUnitReader extends ChronoUnitReader diff --git a/src/test/scala/net/ceedubs/ficus/readers/ChronoUnitReaderSpec.scala b/src/test/scala/net/ceedubs/ficus/readers/ChronoUnitReaderSpec.scala new file mode 100644 index 0000000..0230c67 --- /dev/null +++ b/src/test/scala/net/ceedubs/ficus/readers/ChronoUnitReaderSpec.scala @@ -0,0 +1,38 @@ +package net.ceedubs.ficus +package readers + +import java.time.temporal.ChronoUnit + +import com.typesafe.config.ConfigFactory +import Ficus.{chronoUnitReader, toFicusConfig} + +class ChronoUnitReaderSpec extends Spec { + def is = s2""" + The ChronoUnitReader should + read a ChronoUnit $readChronoUnit + read a lower case ChronoUnit $readChronoUnitLowerCase + """ + + def readChronoUnit = { + val cfg = ConfigFactory.parseString(s""" + | foo { + | chrono-unit = "MILLIS" + | } + """.stripMargin) + val chronoUnit = cfg.as[ChronoUnit]("foo.chrono-unit") + val expected = ChronoUnit.MILLIS + chronoUnit should_== expected + } + + def readChronoUnitLowerCase = { + val cfg = ConfigFactory.parseString(s""" + | foo { + | chrono-unit = "millis" + | } + """.stripMargin) + val chronoUnit = cfg.as[ChronoUnit]("foo.chrono-unit") + val expected = ChronoUnit.MILLIS + chronoUnit should_== expected + } + +}