diff options
Diffstat (limited to 'core-rest/src')
5 files changed, 63 insertions, 0 deletions
diff --git a/core-rest/src/main/scala/xyz/driver/core/generators.scala b/core-rest/src/main/scala/xyz/driver/core/generators.scala index d00b6dd..0a4a7ab 100644 --- a/core-rest/src/main/scala/xyz/driver/core/generators.scala +++ b/core-rest/src/main/scala/xyz/driver/core/generators.scala @@ -65,6 +65,8 @@ object generators { def nextNumericId[T](maxValue: Int): Id[T] = Id[T](nextInt(maxValue).toString) + def nextUuidId[T](): UuidId[T] = UuidId[T](nextUuid()) + def nextName[T](maxLength: Int = DefaultMaxLength): Name[T] = Name[T](nextString(maxLength)) def nextNonEmptyName[T](maxLength: Int = DefaultMaxLength): NonEmptyName[T] = diff --git a/core-rest/src/main/scala/xyz/driver/core/json.scala b/core-rest/src/main/scala/xyz/driver/core/json.scala index 40888aa..23373c6 100644 --- a/core-rest/src/main/scala/xyz/driver/core/json.scala +++ b/core-rest/src/main/scala/xyz/driver/core/json.scala @@ -37,6 +37,24 @@ object json extends PathMatchers with Unmarshallers { } } + implicit def uuidIdFormat[T]: RootJsonFormat[UuidId[T]] = new RootJsonFormat[UuidId[T]] { + def write(id: UuidId[T]) = JsString(id.toString) + + def read(value: JsValue): UuidId[T] = value match { + case JsString(id) if Try(UUID.fromString(id)).isSuccess => UuidId[T](UUID.fromString(id)) + case _ => throw DeserializationException("Id expects UUID") + } + } + + implicit def numericIdFormat[T]: RootJsonFormat[NumericId[T]] = new RootJsonFormat[NumericId[T]] { + def write(id: NumericId[T]) = JsString(id.toString) + + def read(value: JsValue): NumericId[T] = value match { + case JsString(id) if Try(id.toLong).isSuccess => NumericId[T](id.toLong) + case _ => throw DeserializationException("Id expects number") + } + } + implicit def taggedFormat[F, T](implicit underlying: JsonFormat[F], convert: F => F @@ T = null): JsonFormat[F @@ T] = new JsonFormat[F @@ T] { import tagging._ diff --git a/core-rest/src/main/scala/xyz/driver/core/rest/directives/PathMatchers.scala b/core-rest/src/main/scala/xyz/driver/core/rest/directives/PathMatchers.scala index 218c9ae..0d60893 100644 --- a/core-rest/src/main/scala/xyz/driver/core/rest/directives/PathMatchers.scala +++ b/core-rest/src/main/scala/xyz/driver/core/rest/directives/PathMatchers.scala @@ -28,6 +28,12 @@ trait PathMatchers { } } + def UuidIdInPath[T]: PathMatcher1[UuidId[T]] = + AkkaPathMatchers.JavaUUID.map((id: UUID) => UuidId[T](id)) + + def NumericIdInPath[T]: PathMatcher1[NumericId[T]] = + AkkaPathMatchers.LongNumber.map((id: Long) => NumericId[T](id)) + def NameInPath[T]: PathMatcher1[Name[T]] = new PathMatcher1[Name[T]] { def apply(path: Path) = path match { case Path.Segment(segment, tail) => Matched(tail, Tuple1(Name[T](segment))) diff --git a/core-rest/src/main/scala/xyz/driver/core/rest/directives/Unmarshallers.scala b/core-rest/src/main/scala/xyz/driver/core/rest/directives/Unmarshallers.scala index 6c45d15..93a9a52 100644 --- a/core-rest/src/main/scala/xyz/driver/core/rest/directives/Unmarshallers.scala +++ b/core-rest/src/main/scala/xyz/driver/core/rest/directives/Unmarshallers.scala @@ -16,6 +16,16 @@ trait Unmarshallers { Id[A](UUID.fromString(str).toString) } + implicit def uuidIdUnmarshaller[A]: Unmarshaller[String, UuidId[A]] = + Unmarshaller.strict[String, UuidId[A]] { str => + UuidId[A](UUID.fromString(str)) + } + + implicit def numericIdUnmarshaller[A]: Unmarshaller[Long, NumericId[A]] = + Unmarshaller.strict[Long, NumericId[A]] { x => + NumericId[A](x) + } + implicit def paramUnmarshaller[T](implicit reader: JsonReader[T]): Unmarshaller[String, T] = Unmarshaller.firstOf( Unmarshaller.strict((JsString(_: String)) andThen reader.read), diff --git a/core-rest/src/test/scala/xyz/driver/core/JsonTest.scala b/core-rest/src/test/scala/xyz/driver/core/JsonTest.scala index fd693f9..9f54c04 100644 --- a/core-rest/src/test/scala/xyz/driver/core/JsonTest.scala +++ b/core-rest/src/test/scala/xyz/driver/core/JsonTest.scala @@ -2,6 +2,7 @@ package xyz.driver.core import java.net.InetAddress import java.time.{Instant, LocalDate} +import java.util.UUID import akka.http.scaladsl.model.Uri import akka.http.scaladsl.server.PathMatcher @@ -41,6 +42,32 @@ class JsonTest extends WordSpec with Matchers with Inspectors { } } + "Json format for UuidId" should { + "read and write correct JSON" in { + + val referenceId = UuidId[String](UUID.fromString("c21c0ba6-05a2-4d4b-87ba-2405a5e83e64")) + + val writtenJson = json.uuidIdFormat.write(referenceId) + writtenJson.prettyPrint should be("\"c21c0ba6-05a2-4d4b-87ba-2405a5e83e64\"") + + val parsedId = json.uuidIdFormat.read(writtenJson) + parsedId should be(referenceId) + } + } + + "Json format for NumericId" should { + "read and write correct JSON" in { + + val referenceId = NumericId[String](1312) + + val writtenJson = json.numericIdFormat.write(referenceId) + writtenJson.prettyPrint should be("\"1312\"") + + val parsedId = json.numericIdFormat.read(writtenJson) + parsedId should be(referenceId) + } + } + "Json format for @@" should { "read and write correct JSON" in { trait Irrelevant |