diff options
author | Jakob Odersky <jakob@driver.xyz> | 2018-06-29 17:56:06 -0700 |
---|---|---|
committer | Jakob Odersky <jakob@driver.xyz> | 2018-07-01 16:16:52 -0700 |
commit | b6859c8560af601d716729d29094a156c9c01503 (patch) | |
tree | 031929da54c659ff5cd58835962c53459b498629 | |
parent | 901b02274fdfc08030443aac2f1760fc479b3816 (diff) | |
download | driver-core-b6859c8560af601d716729d29094a156c9c01503.tar.gz driver-core-b6859c8560af601d716729d29094a156c9c01503.tar.bz2 driver-core-b6859c8560af601d716729d29094a156c9c01503.zip |
Move shared classes (IDs, Formats, etc) to shared source folder
* The JSON format object was split into traits and akka-specific
unmarshallers are moved into a separate 'Directives' trait.
* The singleton object xyz.driver.core.json is now deprecated.
These changes should be source compatible, although they are not
binary compatible.
25 files changed, 587 insertions, 565 deletions
@@ -8,37 +8,53 @@ lazy val core = crossProject(JSPlatform, JVMPlatform) .crossType(CrossType.Full) .in(file(".")) .enablePlugins(Library) + .disablePlugins(Linting) .settings( + scalacOptions in Compile ++= Seq( + "-language:higherKinds", + "-language:implicitConversions", + "-language:postfixOps", + "-language:reflectiveCalls", // TODO this should be discouraged + "-unchecked", + "-deprecation", + "-feature", + "-encoding", + "utf8", + "-Xlint:_,-unused,-missing-interpolator", + "-Ywarn-numeric-widen", + "-Ywarn-dead-code", + "-Ywarn-unused:_,-explicits,-implicits" + ), libraryDependencies ++= Seq( - "xyz.driver" %%% "spray-json-derivation" % "0.4.5" + "xyz.driver" %%% "spray-json-derivation" % "0.4.7", + "com.beachape" %%% "enumeratum" % "1.5.13", + "org.scalaz" %%% "scalaz-core" % "7.2.24", + "eu.timepit" %%% "refined" % "0.9.1", + "org.scalatest" %%% "scalatest" % "3.0.5" % "test" ) ) .jvmSettings(libraryDependencies ++= Seq( - "xyz.driver" %% "tracing" % "0.1.2", - "com.typesafe.akka" %% "akka-actor" % "2.5.13", - "com.typesafe.akka" %% "akka-stream" % "2.5.13", - "com.typesafe.akka" %% "akka-http-core" % akkaHttpV, - "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpV, - "com.typesafe.akka" %% "akka-http-testkit" % akkaHttpV, - "com.pauldijou" %% "jwt-core" % "0.16.0", - "org.scalatest" %% "scalatest" % "3.0.5" % "test", - "org.scalacheck" %% "scalacheck" % "1.14.0" % "test", - "org.scalaz" %% "scalaz-core" % "7.2.24", - "com.github.swagger-akka-http" %% "swagger-akka-http" % "0.14.0", - "com.typesafe.scala-logging" %% "scala-logging" % "3.9.0", - "eu.timepit" %% "refined" % "0.9.0", - "com.typesafe.slick" %% "slick" % "3.2.3", - "com.beachape" %% "enumeratum" % "1.5.13", - "org.mockito" % "mockito-core" % "1.9.5" % Test, - "com.amazonaws" % "aws-java-sdk-s3" % "1.11.342", - "com.google.cloud" % "google-cloud-pubsub" % "1.31.0", - "com.google.cloud" % "google-cloud-storage" % "1.31.0", - "com.typesafe" % "config" % "1.3.3", - "ch.qos.logback" % "logback-classic" % "1.2.3", - "ch.qos.logback.contrib" % "logback-json-classic" % "0.1.5", - "ch.qos.logback.contrib" % "logback-jackson" % "0.1.5", - "com.googlecode.libphonenumber" % "libphonenumber" % "8.9.7" + "xyz.driver" %% "tracing" % "0.1.2", + "com.typesafe.akka" %% "akka-actor" % "2.5.13", + "com.typesafe.akka" %% "akka-stream" % "2.5.13", + "com.typesafe.akka" %% "akka-http-core" % akkaHttpV, + "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpV, + "com.typesafe.akka" %% "akka-http-testkit" % akkaHttpV, + "com.pauldijou" %% "jwt-core" % "0.16.0", + "org.scalacheck" %% "scalacheck" % "1.14.0" % "test", + "com.github.swagger-akka-http" %% "swagger-akka-http" % "0.14.0", + "com.typesafe.scala-logging" %% "scala-logging" % "3.9.0", + "com.typesafe.slick" %% "slick" % "3.2.3", + "org.mockito" % "mockito-core" % "1.9.5" % Test, + "com.amazonaws" % "aws-java-sdk-s3" % "1.11.342", + "com.google.cloud" % "google-cloud-pubsub" % "1.31.0", + "com.google.cloud" % "google-cloud-storage" % "1.31.0", + "com.typesafe" % "config" % "1.3.3", + "ch.qos.logback" % "logback-classic" % "1.2.3", + "ch.qos.logback.contrib" % "logback-json-classic" % "0.1.5", + "ch.qos.logback.contrib" % "logback-jackson" % "0.1.5", + "com.googlecode.libphonenumber" % "libphonenumber" % "8.9.7" )) lazy val coreJVM = core.jvm -lazy val coreJS = core.js +lazy val coreJS = core.js diff --git a/jvm/src/main/scala/xyz/driver/core/domain.scala b/jvm/src/main/scala/xyz/driver/core/domain.scala deleted file mode 100644 index fa3b5c4..0000000 --- a/jvm/src/main/scala/xyz/driver/core/domain.scala +++ /dev/null @@ -1,46 +0,0 @@ -package xyz.driver.core - -import com.google.i18n.phonenumbers.PhoneNumberUtil -import scalaz.Equal -import scalaz.std.string._ -import scalaz.syntax.equal._ - -object domain { - - final case class Email(username: String, domain: String) { - override def toString: String = username + "@" + domain - } - - object Email { - implicit val emailEqual: Equal[Email] = Equal.equal { - case (left, right) => left.toString.toLowerCase === right.toString.toLowerCase - } - - def parse(emailString: String): Option[Email] = { - Some(emailString.split("@")) collect { - case Array(username, domain) => Email(username, domain) - } - } - } - - final case class PhoneNumber(countryCode: String = "1", number: String) { - override def toString: String = s"+$countryCode $number" - } - - object PhoneNumber { - - private val phoneUtil = PhoneNumberUtil.getInstance() - - def parse(phoneNumber: String): Option[PhoneNumber] = { - val phone = scala.util.Try(phoneUtil.parseAndKeepRawInput(phoneNumber, "US")).toOption - - val validated = phone match { - case None => None - case Some(pn) => - if (!phoneUtil.isValidNumber(pn)) None - else Some(pn) - } - validated.map(pn => PhoneNumber(pn.getCountryCode.toString, pn.getNationalNumber.toString)) - } - } -} diff --git a/jvm/src/main/scala/xyz/driver/core/domain/package.scala b/jvm/src/main/scala/xyz/driver/core/domain/package.scala new file mode 100644 index 0000000..a76e83c --- /dev/null +++ b/jvm/src/main/scala/xyz/driver/core/domain/package.scala @@ -0,0 +1,24 @@ +package xyz.driver.core + +import com.google.i18n.phonenumbers.PhoneNumberUtil + +package object domain { + + private val phoneUtil = PhoneNumberUtil.getInstance() + + /** Enhances the PhoneNumber companion object with methods only available on the JVM. */ + implicit class JvmPhoneNumber(val number: PhoneNumber.type) extends AnyVal { + def parse(phoneNumber: String): Option[PhoneNumber] = { + val phone = scala.util.Try(phoneUtil.parseAndKeepRawInput(phoneNumber, "US")).toOption + + val validated = phone match { + case None => None + case Some(pn) => + if (!phoneUtil.isValidNumber(pn)) None + else Some(pn) + } + validated.map(pn => PhoneNumber(pn.getCountryCode.toString, pn.getNationalNumber.toString)) + } + } + +} diff --git a/jvm/src/main/scala/xyz/driver/core/json.scala b/jvm/src/main/scala/xyz/driver/core/json.scala index de1df31..ca5a47b 100644 --- a/jvm/src/main/scala/xyz/driver/core/json.scala +++ b/jvm/src/main/scala/xyz/driver/core/json.scala @@ -1,401 +1,20 @@ package xyz.driver.core -import java.net.InetAddress -import java.util.{TimeZone, UUID} - -import akka.http.scaladsl.marshalling.{Marshaller, Marshalling} -import akka.http.scaladsl.model.Uri.Path -import akka.http.scaladsl.server.PathMatcher.{Matched, Unmatched} -import akka.http.scaladsl.server._ import akka.http.scaladsl.unmarshalling.Unmarshaller import enumeratum._ -import eu.timepit.refined.api.{Refined, Validate} -import eu.timepit.refined.collection.NonEmpty -import eu.timepit.refined.refineV -import spray.json._ -import xyz.driver.core.auth.AuthCredentials -import xyz.driver.core.date.{Date, DayOfWeek, Month} -import xyz.driver.core.domain.{Email, PhoneNumber} -import xyz.driver.core.rest.errors._ -import xyz.driver.core.time.{Time, TimeOfDay} - -import scala.reflect.runtime.universe._ -import scala.util.Try - -object json { - import DefaultJsonProtocol._ - - private def UuidInPath[T]: PathMatcher1[Id[T]] = - PathMatchers.JavaUUID.map((id: UUID) => Id[T](id.toString.toLowerCase)) - - def IdInPath[T]: PathMatcher1[Id[T]] = UuidInPath[T] | new PathMatcher1[Id[T]] { - def apply(path: Path) = path match { - case Path.Segment(segment, tail) => Matched(tail, Tuple1(Id[T](segment))) - case _ => Unmatched - } - } - - implicit def paramUnmarshaller[T](implicit reader: JsonReader[T]): Unmarshaller[String, T] = - Unmarshaller.firstOf( - Unmarshaller.strict((JsString(_: String)) andThen reader.read), - stringToValueUnmarshaller[T] - ) - - implicit def idFormat[T]: RootJsonFormat[Id[T]] = new RootJsonFormat[Id[T]] { - def write(id: Id[T]) = JsString(id.value) - - def read(value: JsValue): Id[T] = value match { - case JsString(id) if Try(UUID.fromString(id)).isSuccess => Id[T](id.toLowerCase) - case JsString(id) => Id[T](id) - case _ => throw DeserializationException("Id expects string") - } - } - - implicit def taggedFormat[F, T](implicit underlying: JsonFormat[F]): JsonFormat[F @@ T] = new JsonFormat[F @@ T] { - import tagging._ - - override def write(obj: F @@ T): JsValue = underlying.write(obj) - - override def read(json: JsValue): F @@ T = underlying.read(json).tagged[T] - } - - 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))) - case _ => Unmatched - } - } - - implicit def nameFormat[T] = new RootJsonFormat[Name[T]] { - def write(name: Name[T]) = JsString(name.value) - - def read(value: JsValue): Name[T] = value match { - case JsString(name) => Name[T](name) - case _ => throw DeserializationException("Name expects string") - } - } - - def TimeInPath: PathMatcher1[Time] = - PathMatcher("""[+-]?\d*""".r) flatMap { string => - try Some(Time(string.toLong)) - catch { case _: IllegalArgumentException => None } - } - - implicit val timeFormat = new RootJsonFormat[Time] { - def write(time: Time) = JsObject("timestamp" -> JsNumber(time.millis)) - - def read(value: JsValue): Time = value match { - case JsObject(fields) => - fields - .get("timestamp") - .flatMap { - case JsNumber(millis) => Some(Time(millis.toLong)) - case _ => None - } - .getOrElse(throw DeserializationException("Time expects number")) - case _ => throw DeserializationException("Time expects number") - } - } - - implicit object localTimeFormat extends JsonFormat[java.time.LocalTime] { - private val formatter = TimeOfDay.getFormatter - def read(json: JsValue): java.time.LocalTime = json match { - case JsString(chars) => - java.time.LocalTime.parse(chars) - case _ => deserializationError(s"Expected time string got ${json.toString}") - } - - def write(obj: java.time.LocalTime): JsValue = { - JsString(obj.format(formatter)) - } - } - - implicit object timeZoneFormat extends JsonFormat[java.util.TimeZone] { - override def write(obj: TimeZone): JsValue = { - JsString(obj.getID()) - } - - override def read(json: JsValue): TimeZone = json match { - case JsString(chars) => - java.util.TimeZone.getTimeZone(chars) - case _ => deserializationError(s"Expected time zone string got ${json.toString}") - } - } - - implicit val timeOfDayFormat: RootJsonFormat[TimeOfDay] = jsonFormat2(TimeOfDay.apply) - - implicit val dayOfWeekFormat: JsonFormat[DayOfWeek] = new enumeratum.EnumJsonFormat(DayOfWeek) - - implicit val dateFormat = new RootJsonFormat[Date] { - def write(date: Date) = JsString(date.toString) - def read(value: JsValue): Date = value match { - case JsString(dateString) => - Date - .fromString(dateString) - .getOrElse( - throw DeserializationException(s"Misformated ISO 8601 Date. Expected YYYY-MM-DD, but got $dateString.")) - case _ => throw DeserializationException(s"Date expects a string, but got $value.") - } - } - - implicit val monthFormat = new RootJsonFormat[Month] { - def write(month: Month) = JsNumber(month) - def read(value: JsValue): Month = value match { - case JsNumber(month) if 0 <= month && month <= 11 => Month(month.toInt) - case _ => throw DeserializationException("Expected a number from 0 to 11") - } - } - - def RevisionInPath[T]: PathMatcher1[Revision[T]] = - PathMatcher("""[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}""".r) flatMap { string => - Some(Revision[T](string)) - } - - implicit def revisionFromStringUnmarshaller[T]: Unmarshaller[String, Revision[T]] = - Unmarshaller.strict[String, Revision[T]](Revision[T]) - - implicit def revisionFormat[T]: RootJsonFormat[Revision[T]] = new RootJsonFormat[Revision[T]] { - def write(revision: Revision[T]) = JsString(revision.id.toString) - - def read(value: JsValue): Revision[T] = value match { - case JsString(revision) => Revision[T](revision) - case _ => throw DeserializationException("Revision expects uuid string") - } - } - - implicit val base64Format = new RootJsonFormat[Base64] { - def write(base64Value: Base64) = JsString(base64Value.value) - - def read(value: JsValue): Base64 = value match { - case JsString(base64Value) => Base64(base64Value) - case _ => throw DeserializationException("Base64 format expects string") - } - } - - implicit val emailFormat = new RootJsonFormat[Email] { - def write(email: Email) = JsString(email.username + "@" + email.domain) - def read(json: JsValue): Email = json match { - case JsString(value) => - Email.parse(value).getOrElse { - deserializationError("Expected '@' symbol in email string as Email, but got " + json.toString) - } - - case _ => - deserializationError("Expected string as Email, but got " + json.toString) - } - } - - implicit val phoneNumberFormat = jsonFormat2(PhoneNumber.apply) - - implicit val authCredentialsFormat = new RootJsonFormat[AuthCredentials] { - override def read(json: JsValue): AuthCredentials = { - json match { - case JsObject(fields) => - val emailField = fields.get("email") - val identifierField = fields.get("identifier") - val passwordField = fields.get("password") - - (emailField, identifierField, passwordField) match { - case (_, _, None) => - deserializationError("password field must be set") - case (Some(JsString(em)), _, Some(JsString(pw))) => - val email = Email.parse(em).getOrElse(throw deserializationError(s"failed to parse email $em")) - AuthCredentials(email.toString, pw) - case (_, Some(JsString(id)), Some(JsString(pw))) => AuthCredentials(id.toString, pw.toString) - case (None, None, _) => deserializationError("identifier must be provided") - case _ => deserializationError(s"failed to deserialize ${json.prettyPrint}") - } - case _ => deserializationError(s"failed to deserialize ${json.prettyPrint}") - } - } - - override def write(obj: AuthCredentials): JsValue = JsObject( - "identifier" -> JsString(obj.identifier), - "password" -> JsString(obj.password) - ) - } - - implicit object inetAddressFormat extends JsonFormat[InetAddress] { - override def read(json: JsValue): InetAddress = json match { - case JsString(ipString) => - Try(InetAddress.getByName(ipString)) - .getOrElse(deserializationError(s"Invalid IP Address: $ipString")) - case _ => deserializationError(s"Expected string for IP Address, got $json") - } - - override def write(obj: InetAddress): JsValue = - JsString(obj.getHostAddress) - } +@deprecated( + "Using static JSON formats from singleton objects can require to many wildcard imports. It is " + + "recommended to stack format traits into a single protocol.", + "driver-core 1.11.5" +) +object json extends CoreJsonFormats with rest.Unmarshallers with rest.PathMatchers { self => object enumeratum { - def enumUnmarshaller[T <: EnumEntry](enum: Enum[T]): Unmarshaller[String, T] = - Unmarshaller.strict { value => - enum.withNameOption(value).getOrElse(unrecognizedValue(value, enum.values)) - } - - trait HasJsonFormat[T <: EnumEntry] { enum: Enum[T] => - - implicit val format: JsonFormat[T] = new EnumJsonFormat(enum) - - implicit val unmarshaller: Unmarshaller[String, T] = - Unmarshaller.strict { value => - enum.withNameOption(value).getOrElse(unrecognizedValue(value, enum.values)) - } - } - - class EnumJsonFormat[T <: EnumEntry](enum: Enum[T]) extends JsonFormat[T] { - override def read(json: JsValue): T = json match { - case JsString(name) => enum.withNameOption(name).getOrElse(unrecognizedValue(name, enum.values)) - case _ => deserializationError("Expected string as enumeration value, but got " + json.toString) - } - - override def write(obj: T): JsValue = JsString(obj.entryName) - } - - private def unrecognizedValue(value: String, possibleValues: Seq[Any]): Nothing = - deserializationError(s"Unexpected value $value. Expected one of: ${possibleValues.mkString("[", ", ", "]")}") + rest.Directives.enumUnmarshaller(enum) + type HasJsonFormat[T <: EnumEntry] = self.HasJsonFormat[T] + type EnumJsonFormat[T <: EnumEntry] = self.EnumJsonFormat[T] } - class EnumJsonFormat[T](mapping: (String, T)*) extends RootJsonFormat[T] { - private val map = mapping.toMap - - override def write(value: T): JsValue = { - map.find(_._2 == value).map(_._1) match { - case Some(name) => JsString(name) - case _ => serializationError(s"Value $value is not found in the mapping $map") - } - } - - override def read(json: JsValue): T = json match { - case JsString(name) => - map.getOrElse(name, throw DeserializationException(s"Value $name is not found in the mapping $map")) - case _ => deserializationError("Expected string as enumeration value, but got " + json.toString) - } - } - - class ValueClassFormat[T: TypeTag](writeValue: T => BigDecimal, create: BigDecimal => T) extends JsonFormat[T] { - def write(valueClass: T) = JsNumber(writeValue(valueClass)) - def read(json: JsValue): T = json match { - case JsNumber(value) => create(value) - case _ => deserializationError(s"Expected number as ${typeOf[T].getClass.getName}, but got " + json.toString) - } - } - - class GadtJsonFormat[T: TypeTag]( - typeField: String, - typeValue: PartialFunction[T, String], - jsonFormat: PartialFunction[String, JsonFormat[_ <: T]]) - extends RootJsonFormat[T] { - - def write(value: T): JsValue = { - - val valueType = typeValue.applyOrElse(value, { v: T => - deserializationError(s"No Value type for this type of ${typeOf[T].getClass.getName}: " + v.toString) - }) - - val valueFormat = - jsonFormat.applyOrElse(valueType, { f: String => - deserializationError(s"No Json format for this type of $valueType") - }) - - valueFormat.asInstanceOf[JsonFormat[T]].write(value) match { - case JsObject(fields) => JsObject(fields ++ Map(typeField -> JsString(valueType))) - case _ => serializationError(s"${typeOf[T].getClass.getName} serialized not to a JSON object") - } - } - - def read(json: JsValue): T = json match { - case JsObject(fields) => - val valueJson = JsObject(fields.filterNot(_._1 == typeField)) - fields(typeField) match { - case JsString(valueType) => - val valueFormat = jsonFormat.applyOrElse(valueType, { t: String => - deserializationError(s"Unknown ${typeOf[T].getClass.getName} type ${fields(typeField)}") - }) - valueFormat.read(valueJson) - case _ => - deserializationError(s"Unknown ${typeOf[T].getClass.getName} type ${fields(typeField)}") - } - case _ => - deserializationError(s"Expected Json Object as ${typeOf[T].getClass.getName}, but got " + json.toString) - } - } - - object GadtJsonFormat { - - def create[T: TypeTag](typeField: String)(typeValue: PartialFunction[T, String])( - jsonFormat: PartialFunction[String, JsonFormat[_ <: T]]) = { - - new GadtJsonFormat[T](typeField, typeValue, jsonFormat) - } - } - - /** - * Provides the JsonFormat for the Refined types provided by the Refined library. - * - * @see https://github.com/fthomas/refined - */ - implicit def refinedJsonFormat[T, Predicate]( - implicit valueFormat: JsonFormat[T], - validate: Validate[T, Predicate]): JsonFormat[Refined[T, Predicate]] = - new JsonFormat[Refined[T, Predicate]] { - def write(x: T Refined Predicate): JsValue = valueFormat.write(x.value) - def read(value: JsValue): T Refined Predicate = { - refineV[Predicate](valueFormat.read(value))(validate) match { - case Right(refinedValue) => refinedValue - case Left(refinementError) => deserializationError(refinementError) - } - } - } - - def NonEmptyNameInPath[T]: PathMatcher1[NonEmptyName[T]] = new PathMatcher1[NonEmptyName[T]] { - def apply(path: Path) = path match { - case Path.Segment(segment, tail) => - refineV[NonEmpty](segment) match { - case Left(_) => Unmatched - case Right(nonEmptyString) => Matched(tail, Tuple1(NonEmptyName[T](nonEmptyString))) - } - case _ => Unmatched - } - } - - implicit def nonEmptyNameFormat[T](implicit nonEmptyStringFormat: JsonFormat[Refined[String, NonEmpty]]) = - new RootJsonFormat[NonEmptyName[T]] { - def write(name: NonEmptyName[T]) = JsString(name.value.value) - - def read(value: JsValue): NonEmptyName[T] = - NonEmptyName[T](nonEmptyStringFormat.read(value)) - } - - implicit val serviceExceptionFormat: RootJsonFormat[ServiceException] = - GadtJsonFormat.create[ServiceException]("type") { - case _: InvalidInputException => "InvalidInputException" - case _: InvalidActionException => "InvalidActionException" - case _: ResourceNotFoundException => "ResourceNotFoundException" - case _: ExternalServiceException => "ExternalServiceException" - case _: ExternalServiceTimeoutException => "ExternalServiceTimeoutException" - case _: DatabaseException => "DatabaseException" - } { - case "InvalidInputException" => jsonFormat(InvalidInputException, "message") - case "InvalidActionException" => jsonFormat(InvalidActionException, "message") - case "ResourceNotFoundException" => jsonFormat(ResourceNotFoundException, "message") - case "ExternalServiceException" => - jsonFormat(ExternalServiceException, "serviceName", "serviceMessage", "serviceException") - case "ExternalServiceTimeoutException" => jsonFormat(ExternalServiceTimeoutException, "message") - case "DatabaseException" => jsonFormat(DatabaseException, "message") - } - - val jsValueToStringMarshaller: Marshaller[JsValue, String] = - Marshaller.strict[JsValue, String](value => Marshalling.Opaque[String](() => value.compactPrint)) - - def valueToStringMarshaller[T](implicit jsonFormat: JsonWriter[T]): Marshaller[T, String] = - jsValueToStringMarshaller.compose[T](jsonFormat.write) - - val stringToJsValueUnmarshaller: Unmarshaller[String, JsValue] = - Unmarshaller.strict[String, JsValue](value => value.parseJson) - - def stringToValueUnmarshaller[T](implicit jsonFormat: JsonReader[T]): Unmarshaller[String, T] = - stringToJsValueUnmarshaller.map[T](jsonFormat.read) } diff --git a/jvm/src/main/scala/xyz/driver/core/rest/Directives.scala b/jvm/src/main/scala/xyz/driver/core/rest/Directives.scala new file mode 100644 index 0000000..8e5983b --- /dev/null +++ b/jvm/src/main/scala/xyz/driver/core/rest/Directives.scala @@ -0,0 +1,83 @@ +package xyz.driver.core.rest + +import java.util.UUID + +import akka.http.scaladsl.marshalling.{Marshaller, Marshalling} +import akka.http.scaladsl.model.Uri.Path +import akka.http.scaladsl.server.PathMatcher.{Matched, Unmatched} +import akka.http.scaladsl.server.{PathMatchers => AkkaPathMatchers, Directives => AkkaDirectives, _} +import akka.http.scaladsl.unmarshalling.Unmarshaller +import enumeratum._ +import eu.timepit.refined.collection.NonEmpty +import eu.timepit.refined.refineV +import spray.json._ +import xyz.driver.core.time.Time +import xyz.driver.core.{CoreJsonFormats, Id, Name, NonEmptyName} + +trait Directives extends AkkaDirectives with Unmarshallers with PathMatchers +object Directives extends Directives + +trait PathMatchers { + + private def UuidInPath[T]: PathMatcher1[Id[T]] = + AkkaPathMatchers.JavaUUID.map((id: UUID) => Id[T](id.toString.toLowerCase)) + + def IdInPath[T]: PathMatcher1[Id[T]] = UuidInPath[T] | new PathMatcher1[Id[T]] { + def apply(path: Path) = path match { + case Path.Segment(segment, tail) => Matched(tail, Tuple1(Id[T](segment))) + case _ => Unmatched + } + } + + 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))) + case _ => Unmatched + } + } + + def TimeInPath: PathMatcher1[Time] = + PathMatcher("""[+-]?\d*""".r) flatMap { string => + try Some(Time(string.toLong)) + catch { case _: IllegalArgumentException => None } + } + + def NonEmptyNameInPath[T]: PathMatcher1[NonEmptyName[T]] = new PathMatcher1[NonEmptyName[T]] { + def apply(path: Path) = path match { + case Path.Segment(segment, tail) => + refineV[NonEmpty](segment) match { + case Left(_) => Unmatched + case Right(nonEmptyString) => Matched(tail, Tuple1(NonEmptyName[T](nonEmptyString))) + } + case _ => Unmatched + } + } + +} + +trait Unmarshallers { + + implicit def paramUnmarshaller[T](implicit reader: JsonReader[T]): Unmarshaller[String, T] = + Unmarshaller.firstOf( + Unmarshaller.strict((JsString(_: String)) andThen reader.read), + stringToValueUnmarshaller[T] + ) + + def enumUnmarshaller[T <: EnumEntry](enum: Enum[T]): Unmarshaller[String, T] = + Unmarshaller.strict { value => + enum.withNameOption(value).getOrElse(CoreJsonFormats.unrecognizedValue(value, enum.values)) + } + + val jsValueToStringMarshaller: Marshaller[JsValue, String] = + Marshaller.strict[JsValue, String](value => Marshalling.Opaque[String](() => value.compactPrint)) + + def valueToStringMarshaller[T](implicit jsonFormat: JsonWriter[T]): Marshaller[T, String] = + jsValueToStringMarshaller.compose[T](jsonFormat.write) + + val stringToJsValueUnmarshaller: Unmarshaller[String, JsValue] = + Unmarshaller.strict[String, JsValue](value => value.parseJson) + + def stringToValueUnmarshaller[T](implicit jsonFormat: JsonReader[T]): Unmarshaller[String, T] = + stringToJsValueUnmarshaller.map[T](jsonFormat.read) + +} diff --git a/jvm/src/main/scala/xyz/driver/core/rest/RestService.scala b/jvm/src/main/scala/xyz/driver/core/rest/RestService.scala index 8d46d72..ca10c08 100644 --- a/jvm/src/main/scala/xyz/driver/core/rest/RestService.scala +++ b/jvm/src/main/scala/xyz/driver/core/rest/RestService.scala @@ -1,16 +1,16 @@ package xyz.driver.core.rest +import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model._ import akka.http.scaladsl.unmarshalling.{Unmarshal, Unmarshaller} import akka.stream.Materializer import scala.concurrent.{ExecutionContext, Future} import scalaz.{ListT, OptionT} +import spray.json._ +import xyz.driver.core.CoreJsonFormats -trait RestService extends Service { - - import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ - import spray.json._ +trait RestService extends Service with CoreJsonFormats with Unmarshallers with SprayJsonSupport { protected implicit val exec: ExecutionContext protected implicit val materializer: Materializer diff --git a/jvm/src/main/scala/xyz/driver/core/rest/package.scala b/jvm/src/main/scala/xyz/driver/core/rest/package.scala index f85c39a..550a71f 100644 --- a/jvm/src/main/scala/xyz/driver/core/rest/package.scala +++ b/jvm/src/main/scala/xyz/driver/core/rest/package.scala @@ -11,7 +11,6 @@ import akka.http.scaladsl.unmarshalling.Unmarshal import akka.stream.Materializer import akka.stream.scaladsl.Flow import akka.util.ByteString -import xyz.driver.tracing.TracingDirectives import scala.concurrent.Future import scala.util.Try @@ -19,8 +18,6 @@ import scalaz.{Functor, OptionT} import scalaz.Scalaz.{intInstance, stringInstance} import scalaz.syntax.equal._ -trait Service - trait HttpClient { def makeRequest(request: HttpRequest): Future[HttpResponse] } @@ -33,40 +30,6 @@ trait ServiceTransport { implicit mat: Materializer): Future[Unmarshal[ResponseEntity]] } -sealed trait SortingOrder -object SortingOrder { - case object Asc extends SortingOrder - case object Desc extends SortingOrder -} - -final case class SortingField(name: String, sortingOrder: SortingOrder) -final case class Sorting(sortingFields: Seq[SortingField]) - -final case class Pagination(pageSize: Int, pageNumber: Int) { - require(pageSize > 0, "Page size must be greater than zero") - require(pageNumber > 0, "Page number must be greater than zero") - - def offset: Int = pageSize * (pageNumber - 1) -} - -final case class ListResponse[+T](items: Seq[T], meta: ListResponse.Meta) - -object ListResponse { - - def apply[T](items: Seq[T], size: Int, pagination: Option[Pagination]): ListResponse[T] = - ListResponse( - items = items, - meta = ListResponse.Meta(size, pagination.fold(1)(_.pageNumber), pagination.fold(size)(_.pageSize))) - - final case class Meta(itemsCount: Int, pageNumber: Int, pageSize: Int) - - object Meta { - def apply(itemsCount: Int, pagination: Pagination): Meta = - Meta(itemsCount, pagination.pageNumber, pagination.pageSize) - } - -} - object `package` { implicit class OptionTRestAdditions[T](optionT: OptionT[Future, T]) { def responseOrNotFound(successCode: StatusCodes.Success = StatusCodes.OK)( @@ -76,27 +39,6 @@ object `package` { } } - object ContextHeaders { - val AuthenticationTokenHeader: String = "Authorization" - val PermissionsTokenHeader: String = "Permissions" - val AuthenticationHeaderPrefix: String = "Bearer" - val ClientFingerprintHeader: String = "X-Client-Fingerprint" - val TrackingIdHeader: String = "X-Trace" - val StacktraceHeader: String = "X-Stacktrace" - val OriginatingIpHeader: String = "X-Forwarded-For" - val ResourceCount: String = "X-Resource-Count" - val PageCount: String = "X-Page-Count" - val TraceHeaderName: String = TracingDirectives.TraceHeaderName - val SpanHeaderName: String = TracingDirectives.SpanHeaderName - } - - object AuthProvider { - val AuthenticationTokenHeader: String = ContextHeaders.AuthenticationTokenHeader - val PermissionsTokenHeader: String = ContextHeaders.PermissionsTokenHeader - val SetAuthenticationTokenHeader: String = "set-authorization" - val SetPermissionsTokenHeader: String = "set-permissions" - } - val AllowedHeaders: Seq[String] = Seq( "Origin", diff --git a/jvm/src/test/scala/xyz/driver/core/JsonTest.scala b/jvm/src/test/scala/xyz/driver/core/JsonTest.scala index fed2a9d..b8922ae 100644 --- a/jvm/src/test/scala/xyz/driver/core/JsonTest.scala +++ b/jvm/src/test/scala/xyz/driver/core/JsonTest.scala @@ -142,7 +142,7 @@ class JsonTest extends FlatSpec with Matchers { case object Val2 extends EnumVal case object Val3 extends EnumVal - val format = new EnumJsonFormat[EnumVal]("a" -> Val1, "b" -> Val2, "c" -> Val3) + val format = new EnumJsonFormat2[EnumVal]("a" -> Val1, "b" -> Val2, "c" -> Val3) val referenceEnumValue1 = Val2 val referenceEnumValue2 = Val3 @@ -226,29 +226,6 @@ class JsonTest extends FlatSpec with Matchers { }.getMessage shouldBe "Unexpected value Val4. Expected one of: [Val1, Val 2, Val/3]" } - // Should be defined outside of case to have a TypeTag - case class CustomWrapperClass(value: Int) - - "Json format for Value classes" should "read and write correct JSON" in { - - val format = new ValueClassFormat[CustomWrapperClass](v => BigDecimal(v.value), d => CustomWrapperClass(d.toInt)) - - val referenceValue1 = CustomWrapperClass(-2) - val referenceValue2 = CustomWrapperClass(10) - - val writtenJson1 = format.write(referenceValue1) - writtenJson1.prettyPrint should be("-2") - - val writtenJson2 = format.write(referenceValue2) - writtenJson2.prettyPrint should be("10") - - val parsedValue1 = format.read(writtenJson1) - val parsedValue2 = format.read(writtenJson2) - - parsedValue1 should be(referenceValue1) - parsedValue2 should be(referenceValue2) - } - "Json format for classes GADT" should "read and write correct JSON" in { import CustomGADT._ diff --git a/jvm/src/test/scala/xyz/driver/core/rest/DriverRouteTest.scala b/jvm/src/test/scala/xyz/driver/core/rest/DriverRouteTest.scala index d32fefd..247dc5a 100644 --- a/jvm/src/test/scala/xyz/driver/core/rest/DriverRouteTest.scala +++ b/jvm/src/test/scala/xyz/driver/core/rest/DriverRouteTest.scala @@ -4,16 +4,15 @@ import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.headers.Connection import akka.http.scaladsl.server.Directives.{complete => akkaComplete} -import akka.http.scaladsl.server.{Directives, Rejection, RejectionHandler, Route} +import akka.http.scaladsl.server.{RejectionHandler, Route} import akka.http.scaladsl.testkit.ScalatestRouteTest import com.typesafe.scalalogging.Logger import org.scalatest.{AsyncFlatSpec, Matchers} -import xyz.driver.core.logging.NoLogger -import xyz.driver.core.json.serviceExceptionFormat import xyz.driver.core.FutureExtensions +import xyz.driver.core.json.serviceExceptionFormat +import xyz.driver.core.logging.NoLogger import xyz.driver.core.rest.errors._ -import scala.collection.immutable import scala.concurrent.Future class DriverRouteTest diff --git a/shared/src/main/scala/xyz/driver/core/CoreJsonFormats.scala b/shared/src/main/scala/xyz/driver/core/CoreJsonFormats.scala new file mode 100644 index 0000000..68e741c --- /dev/null +++ b/shared/src/main/scala/xyz/driver/core/CoreJsonFormats.scala @@ -0,0 +1,321 @@ +package xyz.driver.core + +import java.net.InetAddress +import java.util.{TimeZone, UUID} + +import enumeratum._ +import eu.timepit.refined.api.{Refined, Validate} +import eu.timepit.refined.collection.NonEmpty +import eu.timepit.refined.refineV +import spray.json._ +import xyz.driver.core.auth.AuthCredentials +import xyz.driver.core.date.{Date, DayOfWeek, Month} +import xyz.driver.core.domain.{Email, PhoneNumber} +import xyz.driver.core.rest.errors._ +import xyz.driver.core.time.{Time, TimeOfDay} +import scala.reflect.runtime.universe._ +import scala.util.Try + +trait CoreJsonFormats extends DerivedJsonProtocol { + + implicit def idFormat[T]: RootJsonFormat[Id[T]] = new RootJsonFormat[Id[T]] { + def write(id: Id[T]) = JsString(id.value) + + def read(value: JsValue): Id[T] = value match { + case JsString(id) if Try(UUID.fromString(id)).isSuccess => Id[T](id.toLowerCase) + case JsString(id) => Id[T](id) + case _ => throw DeserializationException("Id expects string") + } + } + + @deprecated( + "Tagged types will be removed. Please open an issue in case they are needed for your use-case.", + "driver-core 1.11.5") + implicit def taggedFormat[F, T](implicit underlying: JsonFormat[F]): JsonFormat[F @@ T] = new JsonFormat[F @@ T] { + import tagging._ + override def write(obj: F @@ T): JsValue = underlying.write(obj) + override def read(json: JsValue): F @@ T = underlying.read(json).tagged[T] + } + + implicit def nameFormat[T]: RootJsonFormat[Name[T]] = new RootJsonFormat[Name[T]] { + def write(name: Name[T]) = JsString(name.value) + def read(value: JsValue): Name[T] = value match { + case JsString(name) => Name[T](name) + case _ => throw DeserializationException("Name expects string") + } + } + + implicit val timeFormat: RootJsonFormat[Time] = new RootJsonFormat[Time] { + def write(time: Time) = JsObject("timestamp" -> JsNumber(time.millis)) + + def read(value: JsValue): Time = value match { + case JsObject(fields) => + fields + .get("timestamp") + .flatMap { + case JsNumber(millis) => Some(Time(millis.toLong)) + case _ => None + } + .getOrElse(throw DeserializationException("Time expects number")) + case _ => throw DeserializationException("Time expects number") + } + } + + implicit object localTimeFormat extends JsonFormat[java.time.LocalTime] { + private val formatter = TimeOfDay.getFormatter + def read(json: JsValue): java.time.LocalTime = json match { + case JsString(chars) => + java.time.LocalTime.parse(chars) + case _ => deserializationError(s"Expected time string got ${json.toString}") + } + def write(obj: java.time.LocalTime): JsValue = { + JsString(obj.format(formatter)) + } + } + + implicit object timeZoneFormat extends JsonFormat[java.util.TimeZone] { + override def write(obj: TimeZone): JsValue = { + JsString(obj.getID()) + } + override def read(json: JsValue): TimeZone = json match { + case JsString(chars) => + java.util.TimeZone.getTimeZone(chars) + case _ => deserializationError(s"Expected time zone string got ${json.toString}") + } + } + + implicit val timeOfDayFormat: RootJsonFormat[TimeOfDay] = jsonFormat[TimeOfDay] + + implicit val dayOfWeekFormat: JsonFormat[DayOfWeek] = new EnumJsonFormat(DayOfWeek) + + implicit val dateFormat: RootJsonFormat[Date] = new RootJsonFormat[Date] { + def write(date: Date) = JsString(date.toString) + def read(value: JsValue): Date = value match { + case JsString(dateString) => + Date + .fromString(dateString) + .getOrElse( + throw DeserializationException(s"Misformated ISO 8601 Date. Expected YYYY-MM-DD, but got $dateString.")) + case _ => throw DeserializationException(s"Date expects a string, but got $value.") + } + } + + implicit val monthFormat: RootJsonFormat[Month] = new RootJsonFormat[Month] { + def write(month: Month) = JsNumber(month) + def read(value: JsValue): Month = value match { + case JsNumber(month) if 0 <= month && month <= 11 => Month(month.toInt) + case _ => throw DeserializationException("Expected a number from 0 to 11") + } + } + + implicit def revisionFormat[T]: RootJsonFormat[Revision[T]] = new RootJsonFormat[Revision[T]] { + def write(revision: Revision[T]) = JsString(revision.id.toString) + + def read(value: JsValue): Revision[T] = value match { + case JsString(revision) => Revision[T](revision) + case _ => throw DeserializationException("Revision expects uuid string") + } + } + + implicit val base64Format: RootJsonFormat[Base64] = new RootJsonFormat[Base64] { + def write(base64Value: Base64) = JsString(base64Value.value) + + def read(value: JsValue): Base64 = value match { + case JsString(base64Value) => Base64(base64Value) + case _ => throw DeserializationException("Base64 format expects string") + } + } + + implicit val emailFormat: RootJsonFormat[Email] = new RootJsonFormat[Email] { + def write(email: Email) = JsString(email.username + "@" + email.domain) + def read(json: JsValue): Email = json match { + + case JsString(value) => + Email.parse(value).getOrElse { + deserializationError("Expected '@' symbol in email string as Email, but got " + json.toString) + } + + case _ => + deserializationError("Expected string as Email, but got " + json.toString) + } + } + + implicit val phoneNumberFormat: RootJsonFormat[PhoneNumber] = jsonFormat[PhoneNumber] + + implicit val authCredentialsFormat: RootJsonFormat[AuthCredentials] = new RootJsonFormat[AuthCredentials] { + override def read(json: JsValue): AuthCredentials = { + json match { + case JsObject(fields) => + val emailField = fields.get("email") + val identifierField = fields.get("identifier") + val passwordField = fields.get("password") + + (emailField, identifierField, passwordField) match { + case (_, _, None) => + deserializationError("password field must be set") + case (Some(JsString(em)), _, Some(JsString(pw))) => + val email = Email.parse(em).getOrElse(throw deserializationError(s"failed to parse email $em")) + AuthCredentials(email.toString, pw) + case (_, Some(JsString(id)), Some(JsString(pw))) => AuthCredentials(id.toString, pw.toString) + case (None, None, _) => deserializationError("identifier must be provided") + case _ => deserializationError(s"failed to deserialize ${json.prettyPrint}") + } + case _ => deserializationError(s"failed to deserialize ${json.prettyPrint}") + } + } + + override def write(obj: AuthCredentials): JsValue = JsObject( + "identifier" -> JsString(obj.identifier), + "password" -> JsString(obj.password) + ) + } + + trait HasJsonFormat[T <: EnumEntry] { enum: Enum[T] => + implicit val format: JsonFormat[T] = new EnumJsonFormat(enum) + } + + class EnumJsonFormat[T <: EnumEntry](enum: Enum[T]) extends JsonFormat[T] { + override def read(json: JsValue): T = json match { + case JsString(name) => enum.withNameOption(name).getOrElse(CoreJsonFormats.unrecognizedValue(name, enum.values)) + case _ => deserializationError("Expected string as enumeration value, but got " + json.toString) + } + + override def write(obj: T): JsValue = JsString(obj.entryName) + } + + class EnumJsonFormat2[T](mapping: (String, T)*) extends RootJsonFormat[T] { + private val map = mapping.toMap + + override def write(value: T): JsValue = { + map.find(_._2 == value).map(_._1) match { + case Some(name) => JsString(name) + case _ => serializationError(s"Value $value is not found in the mapping $map") + } + } + + override def read(json: JsValue): T = json match { + case JsString(name) => + map.getOrElse(name, throw DeserializationException(s"Value $name is not found in the mapping $map")) + case _ => deserializationError("Expected string as enumeration value, but got " + json.toString) + } + } + + class ValueClassFormat[T: TypeTag](writeValue: T => BigDecimal, create: BigDecimal => T) extends JsonFormat[T] { + def write(valueClass: T) = JsNumber(writeValue(valueClass)) + + def read(json: JsValue): T = json match { + case JsNumber(value) => create(value) + case _ => deserializationError(s"Expected number as ${typeOf[T].getClass.getName}, but got " + json.toString) + } + } + + implicit object inetAddressFormat extends JsonFormat[InetAddress] { + override def read(json: JsValue): InetAddress = json match { + case JsString(ipString) => + Try(InetAddress.getByName(ipString)) + .getOrElse(deserializationError(s"Invalid IP Address: $ipString")) + case _ => deserializationError(s"Expected string for IP Address, got $json") + } + + override def write(obj: InetAddress): JsValue = + JsString(obj.getHostAddress) + } + + class GadtJsonFormat[T: TypeTag]( + typeField: String, + typeValue: PartialFunction[T, String], + jsonFormat: PartialFunction[String, JsonFormat[_ <: T]]) + extends RootJsonFormat[T] { + + def write(value: T): JsValue = { + + val valueType = typeValue.applyOrElse(value, { v: T => + deserializationError(s"No Value type for this type of ${typeOf[T].getClass.getName}: " + v.toString) + }) + + val valueFormat = + jsonFormat.applyOrElse(valueType, { f: String => + deserializationError(s"No Json format for this type of $valueType") + }) + + valueFormat.asInstanceOf[JsonFormat[T]].write(value) match { + case JsObject(fields) => JsObject(fields ++ Map(typeField -> JsString(valueType))) + case _ => serializationError(s"${typeOf[T].getClass.getName} serialized not to a JSON object") + } + } + + def read(json: JsValue): T = json match { + case JsObject(fields) => + val valueJson = JsObject(fields.filterNot(_._1 == typeField)) + fields(typeField) match { + case JsString(valueType) => + val valueFormat = jsonFormat.applyOrElse(valueType, { t: String => + deserializationError(s"Unknown ${typeOf[T].getClass.getName} type ${fields(typeField)}") + }) + valueFormat.read(valueJson) + case _ => + deserializationError(s"Unknown ${typeOf[T].getClass.getName} type ${fields(typeField)}") + } + case _ => + deserializationError(s"Expected Json Object as ${typeOf[T].getClass.getName}, but got " + json.toString) + } + } + + object GadtJsonFormat { + + def create[T: TypeTag](typeField: String)(typeValue: PartialFunction[T, String])( + jsonFormat: PartialFunction[String, JsonFormat[_ <: T]]) = { + + new GadtJsonFormat[T](typeField, typeValue, jsonFormat) + } + } + + /** + * Provides the JsonFormat for the Refined types provided by the Refined library. + * + * @see https://github.com/fthomas/refined + */ + implicit def refinedJsonFormat[T, Predicate]( + implicit valueFormat: JsonFormat[T], + validate: Validate[T, Predicate]): JsonFormat[Refined[T, Predicate]] = + new JsonFormat[Refined[T, Predicate]] { + def write(x: T Refined Predicate): JsValue = valueFormat.write(x.value) + def read(value: JsValue): T Refined Predicate = { + refineV[Predicate](valueFormat.read(value))(validate) match { + case Right(refinedValue) => refinedValue + case Left(refinementError) => deserializationError(refinementError) + } + } + } + + implicit def nonEmptyNameFormat[T](implicit nonEmptyStringFormat: JsonFormat[Refined[String, NonEmpty]]): RootJsonFormat[NonEmptyName[T]] = + new RootJsonFormat[NonEmptyName[T]] { + def write(name: NonEmptyName[T]) = JsString(name.value.value) + + def read(value: JsValue): NonEmptyName[T] = + NonEmptyName[T](nonEmptyStringFormat.read(value)) + } + + implicit val serviceExceptionFormat: RootJsonFormat[ServiceException] = + GadtJsonFormat.create[ServiceException]("type") { + case _: InvalidInputException => "InvalidInputException" + case _: InvalidActionException => "InvalidActionException" + case _: ResourceNotFoundException => "ResourceNotFoundException" + case _: ExternalServiceException => "ExternalServiceException" + case _: ExternalServiceTimeoutException => "ExternalServiceTimeoutException" + case _: DatabaseException => "DatabaseException" + } { + case "InvalidInputException" => jsonFormat(InvalidInputException, "message") + case "InvalidActionException" => jsonFormat(InvalidActionException, "message") + case "ResourceNotFoundException" => jsonFormat(ResourceNotFoundException, "message") + case "ExternalServiceException" => + jsonFormat(ExternalServiceException, "serviceName", "serviceMessage", "serviceException") + case "ExternalServiceTimeoutException" => jsonFormat(ExternalServiceTimeoutException, "message") + case "DatabaseException" => jsonFormat(DatabaseException, "message") + } +} + +private[core] object CoreJsonFormats { + def unrecognizedValue(value: String, possibleValues: Seq[Any]): Nothing = + deserializationError(s"Unexpected value $value. Expected one of: ${possibleValues.mkString("[", ", ", "]")}") +}
\ No newline at end of file diff --git a/jvm/src/main/scala/xyz/driver/core/auth.scala b/shared/src/main/scala/xyz/driver/core/auth.scala index 896bd89..896bd89 100644 --- a/jvm/src/main/scala/xyz/driver/core/auth.scala +++ b/shared/src/main/scala/xyz/driver/core/auth.scala diff --git a/jvm/src/main/scala/xyz/driver/core/core.scala b/shared/src/main/scala/xyz/driver/core/core.scala index 72237b9..ea05829 100644 --- a/jvm/src/main/scala/xyz/driver/core/core.scala +++ b/shared/src/main/scala/xyz/driver/core/core.scala @@ -1,8 +1,8 @@ package xyz.driver -import scalaz.{Equal, Monad, OptionT} import eu.timepit.refined.api.{Refined, Validate} import eu.timepit.refined.collection.NonEmpty +import scalaz.{Equal, Monad, OptionT} import xyz.driver.core.rest.errors.ExternalServiceException import scala.concurrent.{ExecutionContext, Future} @@ -118,11 +118,15 @@ package core { implicit def nonEmptyNameOrdering[T]: Ordering[NonEmptyName[T]] = Ordering.by(_.value.value) } + @deprecated("Base64 formats are rarely used in core and will be removed. Please implement the wrapper type in " + + "services, or open an issue if you think it should stay in core.", "driver-core 1.11.5") final case class Revision[T](id: String) object Revision { implicit def revisionEqual[T]: Equal[Revision[T]] = Equal.equal[Revision[T]](_.id == _.id) } + @deprecated("Base64 formats are rarely used in core and will be removed. Please implement the wrapper type in " + + "services, or open an issue if you think it should stay in core.", "driver-core 1.11.5") final case class Base64(value: String) } diff --git a/jvm/src/main/scala/xyz/driver/core/date.scala b/shared/src/main/scala/xyz/driver/core/date.scala index 5454093..5454093 100644 --- a/jvm/src/main/scala/xyz/driver/core/date.scala +++ b/shared/src/main/scala/xyz/driver/core/date.scala diff --git a/shared/src/main/scala/xyz/driver/core/domain.scala b/shared/src/main/scala/xyz/driver/core/domain.scala new file mode 100644 index 0000000..7b22a6e --- /dev/null +++ b/shared/src/main/scala/xyz/driver/core/domain.scala @@ -0,0 +1,27 @@ +package xyz.driver.core + +import scalaz.Equal + +package domain { + + final case class Email(username: String, domain: String) { + override def toString: String = username + "@" + domain + } + + object Email { + implicit val emailEqual: Equal[Email] = Equal.equal { + case (left, right) => left.toString.toLowerCase == right.toString.toLowerCase + } + + def parse(emailString: String): Option[Email] = { + Some(emailString.split("@")) collect { + case Array(username, domain) => Email(username, domain) + } + } + } + + final case class PhoneNumber(countryCode: String = "1", number: String) { + override def toString: String = s"+$countryCode $number" + } + +} diff --git a/jvm/src/main/scala/xyz/driver/core/future.scala b/shared/src/main/scala/xyz/driver/core/future.scala index 1ee3576..07b6ea4 100644 --- a/jvm/src/main/scala/xyz/driver/core/future.scala +++ b/shared/src/main/scala/xyz/driver/core/future.scala @@ -1,12 +1,9 @@ package xyz.driver.core -import com.typesafe.scalalogging.Logger - import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.{Failure, Success, Try} object future { - val log = Logger("Driver.Future") implicit class RichFuture[T](f: Future[T]) { def mapAll[U](pf: PartialFunction[Try[T], U])(implicit executionContext: ExecutionContext): Future[U] = { @@ -41,7 +38,7 @@ object future { middleState.future.foreach { case Right((_, pt)) => pt.complete(f) case Left((t1, _)) => // This should never happen - log.error(s"Logic error: tried to set Failure($err) but Left($t1) already set") + sys.error(s"Logic error: tried to set Failure($err) but Left($t1) already set") } } case Success(t) => @@ -52,7 +49,7 @@ object future { middleState.future.foreach { case Right((_, pt)) => pt.success(t) case Left((t1, _)) => // This should never happen - log.error(s"Logic error: tried to set Left($t) but Left($t1) already set") + sys.error(s"Logic error: tried to set Left($t) but Left($t1) already set") } } } @@ -63,7 +60,7 @@ object future { middleState.future.foreach { case Left((_, pu)) => pu.complete(f) case Right((u1, _)) => // This should never happen - log.error(s"Logic error: tried to set Failure($err) but Right($u1) already set") + sys.error(s"Logic error: tried to set Failure($err) but Right($u1) already set") } } case Success(u) => @@ -74,7 +71,7 @@ object future { middleState.future.foreach { case Left((_, pu)) => pu.success(u) case Right((u1, _)) => // This should never happen - log.error(s"Logic error: tried to set Right($u) but Right($u1) already set") + sys.error(s"Logic error: tried to set Right($u) but Right($u1) already set") } } } diff --git a/jvm/src/main/scala/xyz/driver/core/generators.scala b/shared/src/main/scala/xyz/driver/core/generators.scala index d57980e..d57980e 100644 --- a/jvm/src/main/scala/xyz/driver/core/generators.scala +++ b/shared/src/main/scala/xyz/driver/core/generators.scala diff --git a/shared/src/main/scala/xyz/driver/core/rest/AuthProvider.scala b/shared/src/main/scala/xyz/driver/core/rest/AuthProvider.scala new file mode 100644 index 0000000..19716bb --- /dev/null +++ b/shared/src/main/scala/xyz/driver/core/rest/AuthProvider.scala @@ -0,0 +1,8 @@ +package xyz.driver.core.rest + +object AuthProvider { + val AuthenticationTokenHeader: String = ContextHeaders.AuthenticationTokenHeader + val PermissionsTokenHeader: String = ContextHeaders.PermissionsTokenHeader + val SetAuthenticationTokenHeader: String = "set-authorization" + val SetPermissionsTokenHeader: String = "set-permissions" +} diff --git a/shared/src/main/scala/xyz/driver/core/rest/ContextHeaders.scala b/shared/src/main/scala/xyz/driver/core/rest/ContextHeaders.scala new file mode 100644 index 0000000..7efe84c --- /dev/null +++ b/shared/src/main/scala/xyz/driver/core/rest/ContextHeaders.scala @@ -0,0 +1,15 @@ +package xyz.driver.core.rest + +object ContextHeaders { + val AuthenticationTokenHeader: String = "Authorization" + val PermissionsTokenHeader: String = "Permissions" + val AuthenticationHeaderPrefix: String = "Bearer" + val ClientFingerprintHeader: String = "X-Client-Fingerprint" + val TrackingIdHeader: String = "X-Trace" + val StacktraceHeader: String = "X-Stacktrace" + val OriginatingIpHeader: String = "X-Forwarded-For" + val ResourceCount: String = "X-Resource-Count" + val PageCount: String = "X-Page-Count" + val TraceHeaderName = "Tracing-Trace-Id" + val SpanHeaderName = "Tracing-Span-Id" + }
\ No newline at end of file diff --git a/shared/src/main/scala/xyz/driver/core/rest/Service.scala b/shared/src/main/scala/xyz/driver/core/rest/Service.scala new file mode 100644 index 0000000..8216ab7 --- /dev/null +++ b/shared/src/main/scala/xyz/driver/core/rest/Service.scala @@ -0,0 +1,3 @@ +package xyz.driver.core.rest + +trait Service diff --git a/jvm/src/main/scala/xyz/driver/core/rest/auth/Authorization.scala b/shared/src/main/scala/xyz/driver/core/rest/auth/Authorization.scala index 1a5e9be..1a5e9be 100644 --- a/jvm/src/main/scala/xyz/driver/core/rest/auth/Authorization.scala +++ b/shared/src/main/scala/xyz/driver/core/rest/auth/Authorization.scala diff --git a/jvm/src/main/scala/xyz/driver/core/rest/auth/AuthorizationResult.scala b/shared/src/main/scala/xyz/driver/core/rest/auth/AuthorizationResult.scala index efe28c9..3602082 100644 --- a/jvm/src/main/scala/xyz/driver/core/rest/auth/AuthorizationResult.scala +++ b/shared/src/main/scala/xyz/driver/core/rest/auth/AuthorizationResult.scala @@ -1,10 +1,9 @@ package xyz.driver.core.rest.auth -import xyz.driver.core.auth.{Permission, PermissionsToken} - import scalaz.Scalaz.mapMonoid import scalaz.Semigroup import scalaz.syntax.semigroup._ +import xyz.driver.core.auth.{Permission, PermissionsToken} final case class AuthorizationResult(authorized: Map[Permission, Boolean], token: Option[PermissionsToken]) object AuthorizationResult { diff --git a/jvm/src/main/scala/xyz/driver/core/rest/errors/serviceException.scala b/shared/src/main/scala/xyz/driver/core/rest/errors/serviceException.scala index db289de..db289de 100644 --- a/jvm/src/main/scala/xyz/driver/core/rest/errors/serviceException.scala +++ b/shared/src/main/scala/xyz/driver/core/rest/errors/serviceException.scala diff --git a/jvm/src/main/scala/xyz/driver/core/rest/serviceRequestContext.scala b/shared/src/main/scala/xyz/driver/core/rest/serviceRequestContext.scala index 775106e..102c97b 100644 --- a/jvm/src/main/scala/xyz/driver/core/rest/serviceRequestContext.scala +++ b/shared/src/main/scala/xyz/driver/core/rest/serviceRequestContext.scala @@ -2,11 +2,10 @@ package xyz.driver.core.rest import java.net.InetAddress -import xyz.driver.core.auth.{AuthToken, PermissionsToken, User} -import xyz.driver.core.generators - import scalaz.Scalaz.{mapEqual, stringInstance} import scalaz.syntax.equal._ +import xyz.driver.core.auth.{AuthToken, PermissionsToken, User} +import xyz.driver.core.generators class ServiceRequestContext( val trackingId: String = generators.nextUuid().toString, diff --git a/shared/src/main/scala/xyz/driver/core/rest/sorting.scala b/shared/src/main/scala/xyz/driver/core/rest/sorting.scala new file mode 100644 index 0000000..ccdf150 --- /dev/null +++ b/shared/src/main/scala/xyz/driver/core/rest/sorting.scala @@ -0,0 +1,35 @@ +package xyz.driver.core.rest + +sealed trait SortingOrder +object SortingOrder { + case object Asc extends SortingOrder + case object Desc extends SortingOrder +} + +final case class SortingField(name: String, sortingOrder: SortingOrder) +final case class Sorting(sortingFields: Seq[SortingField]) + +final case class Pagination(pageSize: Int, pageNumber: Int) { + require(pageSize > 0, "Page size must be greater than zero") + require(pageNumber > 0, "Page number must be greater than zero") + + def offset: Int = pageSize * (pageNumber - 1) +} + +final case class ListResponse[+T](items: Seq[T], meta: ListResponse.Meta) + +object ListResponse { + + def apply[T](items: Seq[T], size: Int, pagination: Option[Pagination]): ListResponse[T] = + ListResponse( + items = items, + meta = ListResponse.Meta(size, pagination.fold(1)(_.pageNumber), pagination.fold(size)(_.pageSize))) + + final case class Meta(itemsCount: Int, pageNumber: Int, pageSize: Int) + + object Meta { + def apply(itemsCount: Int, pagination: Pagination): Meta = + Meta(itemsCount, pagination.pageNumber, pagination.pageSize) + } + +}
\ No newline at end of file diff --git a/jvm/src/main/scala/xyz/driver/core/time.scala b/shared/src/main/scala/xyz/driver/core/time.scala index 6dbd173..6dbd173 100644 --- a/jvm/src/main/scala/xyz/driver/core/time.scala +++ b/shared/src/main/scala/xyz/driver/core/time.scala |