diff options
Diffstat (limited to 'core-rest/src/main')
4 files changed, 36 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), |