aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--src/main/scala/xyz/driver/core/auth.scala4
-rw-r--r--src/main/scala/xyz/driver/core/domain.scala34
-rw-r--r--src/main/scala/xyz/driver/core/json.scala26
-rw-r--r--src/test/scala/xyz/driver/core/JsonTest.scala23
5 files changed, 86 insertions, 4 deletions
diff --git a/README.md b/README.md
index 4c9e95a..f6229b1 100644
--- a/README.md
+++ b/README.md
@@ -7,10 +7,11 @@ Core library is used to provide ways to implement practices established in [Driv
* `core package` provides `Id` and `Name` implementations (with equal and ordering) and also `make` and `using` functions,
* `time` Primitives to deal with time and receive current times in code,
* `config` Contains method `loadDefaultConfig` with default way of providing config to the application,
+ * `domain` Common generic domain objects,
* `messages` Localization messages supporting different locales and methods to read from config,
* `database` Method for database initialization from config, `Id` and `Name` mapping and schema lifecycle,
* `rest` Wrapper over call to external REST API, authorization, context headers, does logging and stats call,
- * `json` Json formats for `Id`, `Name`, `Time`, `Revision` and converters for enums and value classes,
+ * `json` Json formats for `Id`, `Name`, `Time`, `Revision`, `Email`, `PhoneNumber` and converters for enums and value classes,
* `file` Stub for file storage web-service and implementations for S3 and FS `FileStorage`,
* `app` Base class for Driver service, which initializes swagger, app modules and its routes.
* `generators` Set of functions to prototype APIs. Combine with `faker` package,
diff --git a/src/main/scala/xyz/driver/core/auth.scala b/src/main/scala/xyz/driver/core/auth.scala
index a9f52e5..156931e 100644
--- a/src/main/scala/xyz/driver/core/auth.scala
+++ b/src/main/scala/xyz/driver/core/auth.scala
@@ -1,5 +1,7 @@
package xyz.driver.core
+import xyz.driver.core.domain.Email
+
object auth {
trait Permission
@@ -17,4 +19,6 @@ object auth {
final case class RefreshToken(value: String)
final case class PasswordHash(value: String)
+
+ final case class AuthCredentials(email: Email, password: String)
}
diff --git a/src/main/scala/xyz/driver/core/domain.scala b/src/main/scala/xyz/driver/core/domain.scala
new file mode 100644
index 0000000..f2629ee
--- /dev/null
+++ b/src/main/scala/xyz/driver/core/domain.scala
@@ -0,0 +1,34 @@
+package xyz.driver.core
+
+object domain {
+
+ final case class Email(username: String, domain: String) {
+ override def toString = username + "@" + domain
+ }
+
+ object Email {
+ 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 {
+ def parse(phoneNumberString: String): Option[PhoneNumber] = {
+ val onlyDigits = phoneNumberString.replaceAll("[^\\d.]", "")
+
+ if (onlyDigits.length < 10) None
+ else {
+ val tenDigitNumber = onlyDigits.takeRight(10)
+ val countryCode = Option(onlyDigits.dropRight(10)).filter(_.nonEmpty).getOrElse("1")
+
+ Some(PhoneNumber(countryCode, tenDigitNumber))
+ }
+ }
+ }
+}
diff --git a/src/main/scala/xyz/driver/core/json.scala b/src/main/scala/xyz/driver/core/json.scala
index 664ef48..3c0d8d4 100644
--- a/src/main/scala/xyz/driver/core/json.scala
+++ b/src/main/scala/xyz/driver/core/json.scala
@@ -5,13 +5,16 @@ import akka.http.scaladsl.server.PathMatcher.{Matched, Unmatched}
import akka.http.scaladsl.server.{PathMatcher, _}
import akka.http.scaladsl.unmarshalling.Unmarshaller
import spray.json.{DeserializationException, JsNumber, _}
+import xyz.driver.core.auth.AuthCredentials
import xyz.driver.core.revision.Revision
import xyz.driver.core.time.Time
import xyz.driver.core.date.Date
+import xyz.driver.core.domain.{Email, PhoneNumber}
import scala.reflect.runtime.universe._
object json {
+ import DefaultJsonProtocol._
def IdInPath[T]: PathMatcher1[Id[T]] = new PathMatcher1[Id[T]] {
def apply(path: Path) = path match {
@@ -74,9 +77,8 @@ object json {
Date
.fromString(dateString)
.getOrElse(
- throw new DeserializationException(
- s"Misformated ISO 8601 Date. Expected YYYY-MM-DD, but got $dateString."))
- case _ => throw new DeserializationException(s"Date expects a string, but got $value.")
+ 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.")
}
}
@@ -106,6 +108,24 @@ object json {
}
}
+ 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)
+ }
+
+ case _ =>
+ deserializationError("Expected string as Email, but got " + json)
+ }
+ }
+
+ implicit val phoneNumberFormat = jsonFormat2(PhoneNumber.apply)
+
+ implicit val authCredentialsFormat = jsonFormat2(AuthCredentials)
+
class EnumJsonFormat[T](mapping: (String, T)*) extends RootJsonFormat[T] {
private val map = mapping.toMap
diff --git a/src/test/scala/xyz/driver/core/JsonTest.scala b/src/test/scala/xyz/driver/core/JsonTest.scala
index ff804a9..8697b7f 100644
--- a/src/test/scala/xyz/driver/core/JsonTest.scala
+++ b/src/test/scala/xyz/driver/core/JsonTest.scala
@@ -6,6 +6,7 @@ import xyz.driver.core.revision.Revision
import xyz.driver.core.time.provider.SystemTimeProvider
import spray.json._
import xyz.driver.core.TestTypes.CustomGADT
+import xyz.driver.core.domain.{Email, PhoneNumber}
class JsonTest extends FlatSpec with Matchers {
@@ -65,6 +66,28 @@ class JsonTest extends FlatSpec with Matchers {
parsedRevision should be(referenceRevision)
}
+ "Json format for Email" should "read and write correct JSON" in {
+
+ val referenceEmail = Email("test", "drivergrp.com")
+
+ val writtenJson = json.emailFormat.write(referenceEmail)
+ writtenJson should be("\"test@drivergrp.com\"".parseJson)
+
+ val parsedEmail = json.emailFormat.read(writtenJson)
+ parsedEmail should be(referenceEmail)
+ }
+
+ "Json format for PhoneNumber" should "read and write correct JSON" in {
+
+ val referencePhoneNumber = PhoneNumber("1", "4243039608")
+
+ val writtenJson = json.phoneNumberFormat.write(referencePhoneNumber)
+ writtenJson should be("""{"countryCode":"1","number":"4243039608"}""".parseJson)
+
+ val parsedPhoneNumber = json.phoneNumberFormat.read(writtenJson)
+ parsedPhoneNumber should be(referencePhoneNumber)
+ }
+
"Json format for Enums" should "read and write correct JSON" in {
sealed trait EnumVal