forked from ceedubs/ficus
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
5e202ea
commit 5609b7f
Showing
6 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/scala/net/ceedubs/ficus/readers/LocalDateReader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
src/main/scala/net/ceedubs/ficus/readers/PeriodReader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/test/scala/net/ceedubs/ficus/readers/LocalDateReaderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
src/test/scala/net/ceedubs/ficus/readers/PeriodReaderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |