Skip to content

Commit

Permalink
add readers for java.time.LocalDate and Period (#44)
Browse files Browse the repository at this point in the history
* add readers for java.time.LocalDate and Period

* fix tests

turns out I missed putting the interpolated call into the 'is' comprehension.
  • Loading branch information
crispywalrus authored and kailuowang committed Nov 23, 2016
1 parent 5e202ea commit 5609b7f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/net/ceedubs/ficus/Ficus.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions src/main/scala/net/ceedubs/ficus/readers/LocalDateReader.scala
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions src/main/scala/net/ceedubs/ficus/readers/PeriodReader.scala
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions src/test/scala/net/ceedubs/ficus/readers/LocalDateReaderSpec.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
37 changes: 37 additions & 0 deletions src/test/scala/net/ceedubs/ficus/readers/PeriodReaderSpec.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 5609b7f

Please sign in to comment.