Skip to content

Commit

Permalink
scalafmt
Browse files Browse the repository at this point in the history
(cherry picked from commit e84c28b)
  • Loading branch information
ignasi35 authored and mergify-bot committed Aug 25, 2020
1 parent 2fc7155 commit 3f59de6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
Expand Up @@ -22,7 +22,7 @@ object JsonMigrations {

def apply(
currentVersion: Int,
supportedForwardVersion:Int,
supportedForwardVersion: Int,
transformation: (Int, JsValue) => JsValue,
classNameTransformation: (Int, String) => String
): JsonMigration =
Expand All @@ -39,10 +39,14 @@ object JsonMigrations {
* through the play-json transformation DSL)
*/
def transform[T: ClassTag](transformations: immutable.SortedMap[Int, Reads[JsObject]]): (String, JsonMigration) = {
val currentVersion= transformations.keys.last + 1
val currentVersion = transformations.keys.last + 1
transform(transformations, currentVersion, currentVersion)
}
def transform[T: ClassTag](transformations: immutable.SortedMap[Int, Reads[JsObject]], currentVersion:Int, supportedForwardVersion: Int): (String, JsonMigration) = {
def transform[T: ClassTag](
transformations: immutable.SortedMap[Int, Reads[JsObject]],
currentVersion: Int,
supportedForwardVersion: Int
): (String, JsonMigration) = {
val className = implicitly[ClassTag[T]].runtimeClass.getName
className -> new JsonMigration(currentVersion, supportedForwardVersion) {
override def transform(fromVersion: Int, json: JsObject): JsObject = {
Expand Down Expand Up @@ -92,7 +96,8 @@ abstract class JsonMigration(val currentVersion: Int, val supportedForwardVersio

require(
currentVersion <= supportedForwardVersion,
s"""The "currentVersion" [$currentVersion] of a JsonMigration must be less or equal to the "supportedForwardVersion" [$supportedForwardVersion].""")
s"""The "currentVersion" [$currentVersion] of a JsonMigration must be less or equal to the "supportedForwardVersion" [$supportedForwardVersion]."""
)

/**
* Override to provide transformation of the old JSON structure to the new
Expand Down
Expand Up @@ -98,13 +98,13 @@ private[lagom] final class PlayJsonSerializer(val system: ExtendedActorSystem, r
val renameMigration = migrations.get(manifestClassName)

val migratedManifest = renameMigration match {
case Some(migration) if fromVersion < migration.currentVersion =>
case Some(migration) if fromVersion < migration.currentVersion =>
migration.transformClassName(fromVersion, manifestClassName)
case Some(migration) if fromVersion == migration.currentVersion =>
case Some(migration) if fromVersion == migration.currentVersion =>
manifestClassName
case Some(migration) if fromVersion <= migration.supportedForwardVersion =>
case Some(migration) if fromVersion <= migration.supportedForwardVersion =>
migration.transformClassName(fromVersion, manifestClassName)
case Some(migration) if fromVersion > migration.supportedForwardVersion =>
case Some(migration) if fromVersion > migration.supportedForwardVersion =>
throw new IllegalStateException(
s"Migration supported version ${migration.supportedForwardVersion} is " +
s"behind version $fromVersion of deserialized type [$manifestClassName]"
Expand Down Expand Up @@ -138,7 +138,7 @@ private[lagom] final class PlayJsonSerializer(val system: ExtendedActorSystem, r
}

val migratedJson = transformMigration match {
case None => json
case None => json
case Some(migration) if fromVersion == migration.currentVersion => json
case Some(migration) if fromVersion <= migration.supportedForwardVersion =>
json match {
Expand All @@ -149,7 +149,6 @@ private[lagom] final class PlayJsonSerializer(val system: ExtendedActorSystem, r
}
}


val result = format.reads(migratedJson) match {
case JsSuccess(obj, _) => obj
case JsError(errors) =>
Expand Down
Expand Up @@ -119,7 +119,7 @@ object TestRegistry4 extends JsonSerializerRegistry {
)

def currentMigrationVersion: Int = 5
def supportedVersion: Int = currentMigrationVersion+1
def supportedVersion: Int = currentMigrationVersion + 1

import play.api.libs.json._
override def migrations = {
Expand All @@ -134,13 +134,12 @@ object TestRegistry4 extends JsonSerializerRegistry {
JsonMigrations.transform[MigratedEvent](
transformations,
currentMigrationVersion, // currentVersion
supportedVersion // forward-one support
supportedVersion // forward-one support
)
)
}
}


object TestRegistryWithCompression extends JsonSerializerRegistry {
implicit val innerFormat = Json.format[Inner]

Expand Down Expand Up @@ -301,36 +300,31 @@ class PlayJsonSerializerSpec extends WordSpec with Matchers {
deserialized should be(expectedEvent)
}


"downcast a future version" in withActorSystem(TestRegistry4) {
system =>
// Looks like MigratedEvent, except `newName` is called `newerName`. That field needs downcasting.
val newerJsonBytes = Json
.stringify(
JsObject(
Seq(
"addedField" -> JsNumber(2),
"newerName" -> JsString("some value")
)
"downcast a future version" in withActorSystem(TestRegistry4) { system =>
// Looks like MigratedEvent, except `newName` is called `newerName`. That field needs downcasting.
val newerJsonBytes = Json
.stringify(
JsObject(
Seq(
"addedField" -> JsNumber(2),
"newerName" -> JsString("some value")
)
)
.getBytes(StandardCharsets.UTF_8)
)
.getBytes(StandardCharsets.UTF_8)

val expectedEvent = MigratedEvent(addedField = 2, newName = "some value")
val expectedEvent = MigratedEvent(addedField = 2, newName = "some value")

val futureVersion = TestRegistry4.supportedVersion
val futureManifest = expectedVersionedManifest(classOf[MigratedEvent], futureVersion)
val serializeExt = SerializationExtension(system)
val serializer = serializeExt.findSerializerFor(expectedEvent).asInstanceOf[SerializerWithStringManifest]
val futureVersion = TestRegistry4.supportedVersion
val futureManifest = expectedVersionedManifest(classOf[MigratedEvent], futureVersion)
val serializeExt = SerializationExtension(system)
val serializer = serializeExt.findSerializerFor(expectedEvent).asInstanceOf[SerializerWithStringManifest]

val deserialized = serializer.fromBinary(newerJsonBytes, futureManifest)
val deserialized = serializer.fromBinary(newerJsonBytes, futureManifest)

deserialized should be(expectedEvent)
deserialized should be(expectedEvent)
}




"use compression when enabled and payload is bigger than threshold" in withActorSystem(TestRegistryWithCompression) {
system =>
val serializeExt = SerializationExtension(system)
Expand Down

0 comments on commit 3f59de6

Please sign in to comment.