From 5609b7facb5b2a3cc09e028927909c3e20ecb2f8 Mon Sep 17 00:00:00 2001 From: chris vale Date: Wed, 23 Nov 2016 11:38:56 -0800 Subject: [PATCH] add readers for java.time.LocalDate and Period (#44) * add readers for java.time.LocalDate and Period * fix tests turns out I missed putting the interpolated call into the 'is' comprehension. --- src/main/scala/net/ceedubs/ficus/Ficus.scala | 2 +- .../readers/AllValueReaderInstances.scala | 2 +- .../ficus/readers/LocalDateReader.scala | 15 ++++++++ .../ceedubs/ficus/readers/PeriodReader.scala | 15 ++++++++ .../ficus/readers/LocalDateReaderSpec.scala | 25 +++++++++++++ .../ficus/readers/PeriodReaderSpec.scala | 37 +++++++++++++++++++ 6 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/main/scala/net/ceedubs/ficus/readers/LocalDateReader.scala create mode 100644 src/main/scala/net/ceedubs/ficus/readers/PeriodReader.scala create mode 100644 src/test/scala/net/ceedubs/ficus/readers/LocalDateReaderSpec.scala create mode 100644 src/test/scala/net/ceedubs/ficus/readers/PeriodReaderSpec.scala diff --git a/src/main/scala/net/ceedubs/ficus/Ficus.scala b/src/main/scala/net/ceedubs/ficus/Ficus.scala index b7d5e87..deb7d98 100644 --- a/src/main/scala/net/ceedubs/ficus/Ficus.scala +++ b/src/main/scala/net/ceedubs/ficus/Ficus.scala @@ -7,7 +7,7 @@ import scala.language.implicitConversions trait FicusInstances extends AnyValReaders with StringReader with SymbolReader with OptionReader with CollectionReaders with ConfigReader with DurationReaders with TryReader with ConfigValueReader with BigNumberReaders - with ISOZonedDateTimeReader + with ISOZonedDateTimeReader with PeriodReader with LocalDateReader object Ficus extends FicusInstances { implicit def toFicusConfig(config: Config): FicusConfig = SimpleFicusConfig(config) diff --git a/src/main/scala/net/ceedubs/ficus/readers/AllValueReaderInstances.scala b/src/main/scala/net/ceedubs/ficus/readers/AllValueReaderInstances.scala index 012060a..07cf129 100644 --- a/src/main/scala/net/ceedubs/ficus/readers/AllValueReaderInstances.scala +++ b/src/main/scala/net/ceedubs/ficus/readers/AllValueReaderInstances.scala @@ -2,6 +2,6 @@ package net.ceedubs.ficus.readers trait AllValueReaderInstances extends AnyValReaders with StringReader with SymbolReader with OptionReader with CollectionReaders with ConfigReader with DurationReaders with ArbitraryTypeReader - with TryReader with ConfigValueReader + with TryReader with ConfigValueReader with PeriodReader with LocalDateReader object AllValueReaderInstances extends AllValueReaderInstances diff --git a/src/main/scala/net/ceedubs/ficus/readers/LocalDateReader.scala b/src/main/scala/net/ceedubs/ficus/readers/LocalDateReader.scala new file mode 100644 index 0000000..e2ea642 --- /dev/null +++ b/src/main/scala/net/ceedubs/ficus/readers/LocalDateReader.scala @@ -0,0 +1,15 @@ +package net.ceedubs.ficus.readers + +import java.time.LocalDate + +import com.typesafe.config.Config + +trait LocalDateReader { + implicit val localDateReader: ValueReader[LocalDate] = new ValueReader[LocalDate] { + override def read(config: Config, path: String): LocalDate = { + LocalDate.parse(config.getString(path)) + } + } +} + +object LocalDateReader extends LocalDateReader diff --git a/src/main/scala/net/ceedubs/ficus/readers/PeriodReader.scala b/src/main/scala/net/ceedubs/ficus/readers/PeriodReader.scala new file mode 100644 index 0000000..2da253e --- /dev/null +++ b/src/main/scala/net/ceedubs/ficus/readers/PeriodReader.scala @@ -0,0 +1,15 @@ +package net.ceedubs.ficus.readers + +import java.time.Period + +import com.typesafe.config.Config + +trait PeriodReader { + implicit val periodReader: ValueReader[Period] = new ValueReader[Period] { + override def read(config: Config, path: String): Period = { + Period.parse(config.getString(path)) + } + } +} + +object PeriodReader extends PeriodReader diff --git a/src/test/scala/net/ceedubs/ficus/readers/LocalDateReaderSpec.scala b/src/test/scala/net/ceedubs/ficus/readers/LocalDateReaderSpec.scala new file mode 100644 index 0000000..0dc9723 --- /dev/null +++ b/src/test/scala/net/ceedubs/ficus/readers/LocalDateReaderSpec.scala @@ -0,0 +1,25 @@ +package net.ceedubs.ficus +package readers + +import java.time.LocalDate +import com.typesafe.config.ConfigFactory +import Ficus.{ toFicusConfig, localDateReader } + + +class LocalDateReaderSpec extends Spec { def is = s2""" + The LocalDateReader should + read a LocalDate in ISO format without a time-zone: $readLocalDate + """ + + def readLocalDate = { + val cfg = ConfigFactory.parseString( + s""" + | foo { + | date = "2003-01-03" + | } + """.stripMargin) + val localDate = cfg.as[LocalDate]("foo.date") + val expected = LocalDate.of(2003,1,3) + localDate should_==(expected) + } +} diff --git a/src/test/scala/net/ceedubs/ficus/readers/PeriodReaderSpec.scala b/src/test/scala/net/ceedubs/ficus/readers/PeriodReaderSpec.scala new file mode 100644 index 0000000..c532d78 --- /dev/null +++ b/src/test/scala/net/ceedubs/ficus/readers/PeriodReaderSpec.scala @@ -0,0 +1,37 @@ +package net.ceedubs.ficus +package readers + +import java.time.Period +import com.typesafe.config.ConfigFactory +import Ficus.{ toFicusConfig, periodReader } + +class PeriodReaderSpec extends Spec { def is = s2""" + The PeriodReader should + read a Period in ISO-8601 format $readPeriod + read a negative Period $readNegativePeriod + """ + + def readPeriod = { + val cfg = ConfigFactory.parseString( + s""" + | foo { + | interval = "P1Y3M10D" + | } + """.stripMargin) + val period = cfg.as[Period]("foo.interval") + val expected = Period.of(1,3,10) + period should_==(expected) + } + + def readNegativePeriod = { + val cfg = ConfigFactory.parseString( + s""" + | foo { + | interval = "P-1Y10M3D" + | } + """.stripMargin) + val period = cfg.as[Period]("foo.interval") + val expected = Period.of(-1,10,3) + period should_==(expected) + } +}