diff options
author | Sergey Nastich <nastich@users.noreply.github.com> | 2018-09-10 19:05:58 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-10 19:05:58 -0400 |
commit | ccd6c4281fb0ddd0a74b79aa24f85834f5c9647a (patch) | |
tree | 1b46556d4e49ed8698fb04792b4ed024a1948b29 /src/main/scala/xyz/driver/core | |
parent | b3b426046a199307ce80d01b604115890724cb22 (diff) | |
download | driver-core-1.14.1.tar.gz driver-core-1.14.1.tar.bz2 driver-core-1.14.1.zip |
Add `@@ Trimmed` tag for Strings and Names to support by JSON readers (#211)v1.14.1
Diffstat (limited to 'src/main/scala/xyz/driver/core')
-rw-r--r-- | src/main/scala/xyz/driver/core/core.scala | 10 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/core/json.scala | 15 |
2 files changed, 19 insertions, 6 deletions
diff --git a/src/main/scala/xyz/driver/core/core.scala b/src/main/scala/xyz/driver/core/core.scala index a654e85..846bed3 100644 --- a/src/main/scala/xyz/driver/core/core.scala +++ b/src/main/scala/xyz/driver/core/core.scala @@ -128,4 +128,14 @@ package core { } final case class Base64(value: String) + + trait Trimmed + + object Trimmed { + import tagging._ + + implicit def string2Trimmed(str: String): String @@ Trimmed = str.trim().tagged[Trimmed] + + implicit def name2Trimmed[T](name: Name[T]): Name[T] @@ Trimmed = Name[T](name.value.trim()).tagged[Trimmed] + } } diff --git a/src/main/scala/xyz/driver/core/json.scala b/src/main/scala/xyz/driver/core/json.scala index 98725fb..d9319e9 100644 --- a/src/main/scala/xyz/driver/core/json.scala +++ b/src/main/scala/xyz/driver/core/json.scala @@ -22,8 +22,8 @@ import xyz.driver.core.domain.{Email, PhoneNumber} import xyz.driver.core.rest.errors._ import xyz.driver.core.time.{Time, TimeOfDay} -import scala.reflect.{ClassTag, classTag} import scala.reflect.runtime.universe._ +import scala.reflect.{ClassTag, classTag} import scala.util.Try import scala.util.control.NonFatal @@ -56,13 +56,16 @@ object json { } } - implicit def taggedFormat[F, T](implicit underlying: JsonFormat[F]): JsonFormat[F @@ T] = new JsonFormat[F @@ T] { - import tagging._ + implicit def taggedFormat[F, T](implicit underlying: JsonFormat[F], convert: F => F @@ T = null): JsonFormat[F @@ T] = + new JsonFormat[F @@ T] { + import tagging._ - override def write(obj: F @@ T): JsValue = underlying.write(obj) + private val transformReadValue = Option(convert).getOrElse((_: F).tagged[T]) - override def read(json: JsValue): F @@ T = underlying.read(json).tagged[T] - } + override def write(obj: F @@ T): JsValue = underlying.write(obj) + + override def read(json: JsValue): F @@ T = transformReadValue(underlying.read(json)) + } def NameInPath[T]: PathMatcher1[Name[T]] = new PathMatcher1[Name[T]] { def apply(path: Path) = path match { |