Skip to content

Commit

Permalink
Merge pull request #213 from theiterators/instances-year-int
Browse files Browse the repository at this point in the history
Changing Year instance representation - from String to Int
  • Loading branch information
pk044 committed Jun 20, 2022
2 parents 0efa601 + 0e70d05 commit 802b098
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait TimeInstances
with OffsetTimeString
with PeriodString
with YearMonthString
with YearString
with YearInt
with ZonedDateTimeString
with ZoneIdString
with ZoneOffsetString
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pl.iterators.kebs.instances.time

import pl.iterators.kebs.instances.InstanceConverter
import pl.iterators.kebs.instances.time.YearInt.YearFormat

import java.time.Year

trait YearInt {
implicit val yearFormatter: InstanceConverter[Year, Int] =
InstanceConverter[Year, Int](_.getValue, Year.of, Some(YearFormat))
}
object YearInt {
private[instances] val YearFormat = "ISO-8601 standard format e.g. 2007"
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,18 @@ class TimeInstancesTests extends AnyFunSuite with Matchers with TimeInstances {
assertThrows[DecodeErrorException](ico.decode(value))
}

test("Year to String") {
val ico = implicitly[InstanceConverter[Year, String]]
val value = "2007"
val obj = Year.parse(value)
test("Year to Int") {
val ico = implicitly[InstanceConverter[Year, Int]]
val value = 2007
val obj = Year.of(value)

ico.encode(obj) shouldBe value
ico.decode(value) shouldBe obj
}

test("Year wrong format exception") {
val ico = implicitly[InstanceConverter[Year, String]]
val value = "NotAYear"
val ico = implicitly[InstanceConverter[Year, Int]]
val value = Int.MinValue

assertThrows[DecodeErrorException](ico.decode(value))
}
Expand Down
12 changes: 6 additions & 6 deletions spray-json/src/test/scala/instances/TimeInstancesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,18 @@ class TimeInstancesTests extends AnyFunSuite with Matchers with DefaultJsonProto

test("Year standard format") {
val jf = implicitly[JsonFormat[Year]]
val value = "2007"
val obj = Year.parse(value)
val value = 2007
val obj = Year.of(value)

jf.write(obj) shouldBe JsString(value)
jf.read(JsString(value)) shouldBe obj
jf.write(obj) shouldBe JsNumber(value)
jf.read(JsNumber(value)) shouldBe obj
}

test("Year wrong format exception") {
val jf = implicitly[JsonFormat[Year]]
val value = "NotAYear"
val value = Int.MinValue

assertThrows[DecodeErrorException](jf.read(JsString(value)))
assertThrows[DecodeErrorException](jf.read(JsNumber(value)))
}

test("YearMonth standard format") {
Expand Down

0 comments on commit 802b098

Please sign in to comment.