Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing Year instance representation - from String to Int #213

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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