Skip to content

Commit

Permalink
Add ChronoUnit ValueReader (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich committed Aug 10, 2021
1 parent 13d8365 commit f14bd7c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/scala/net/ceedubs/ficus/Ficus.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ trait FicusInstances
with ISOZonedDateTimeReader
with PeriodReader
with LocalDateReader
with ChronoUnitReader
with URIReaders
with URLReader
with InetSocketAddressReaders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ trait AllValueReaderInstances
with TryReader
with ConfigValueReader
with PeriodReader
with ChronoUnitReader
with LocalDateReader

object AllValueReaderInstances extends AllValueReaderInstances
16 changes: 16 additions & 0 deletions src/main/scala/net/ceedubs/ficus/readers/ChronoUnitReader.scala
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit f14bd7c

Please sign in to comment.