Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make entries with legacy class names backwards compatible #14

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.apache.pekko.actor.ActorRef
import org.apache.pekko.persistence.journal.Tagged
import org.apache.pekko.persistence.query.{EventEnvelope, Offset}
import org.apache.pekko.persistence.{AtomicWrite, PersistentRepr}
import org.apache.pekko.serialization.{Serialization, SerializerWithStringManifest}
import org.apache.pekko.serialization.{Serialization, Serializer, SerializerWithStringManifest}

import scala.collection.immutable.{Seq => ISeq}
import scala.util.{Failure, Success, Try}
Expand Down Expand Up @@ -48,17 +48,27 @@ case class Serialized[C <: AnyRef](bytes: Array[Byte],

val hint = "ser"
lazy val content: C = {

val clazz = loadClass.getClassFor[X forSome { type X <: AnyRef }](className)
val backwardsCompatClazz = loadClass.getClassFor[X forSome { type X <: AnyRef }](className.replace("akka", "org.apache.pekko"))
bdoyle0182 marked this conversation as resolved.
Show resolved Hide resolved
Try(tryDeserialize(clazz, clazz.flatMap(c => Try(ser.serializerFor(c)))))
.recover({
case _ => tryDeserialize(backwardsCompatClazz, backwardsCompatClazz.flatMap(c => Try(ser.serializerFor(c))))
}) match {
case Failure(x) => throw x
case Success(deser) => deser
}
}

val tried = (serializedManifest,serializerId,clazz.flatMap(c => Try(ser.serializerFor(c)))) match {
private def tryDeserialize(clazz: Try[Class[_ <: X forSome {type X <: AnyRef}]],
serializer: Try[Serializer]): C = {
val tried = (serializedManifest, serializerId, serializer) match {
// Manifest was serialized, class exists ~ prefer read-time configuration
case (Some(manifest), _, Success(clazzSer)) =>
ser.deserialize(bytes, clazzSer.identifier, manifest)

// No manifest id serialized, prefer read-time configuration
case (None, _, Success(clazzSer)) =>
ser.deserialize[X forSome { type X <: AnyRef }](bytes, clazzSer.identifier, clazz.toOption)
ser.deserialize[X forSome {type X <: AnyRef}](bytes, clazzSer.identifier, clazz.toOption)

// Manifest, id were serialized, class doesn't exist - use write-time configuration
case (Some(manifest), Some(id), Failure(_)) =>
Expand All @@ -67,11 +77,11 @@ case class Serialized[C <: AnyRef](bytes: Array[Byte],
// Below cases very unlikely to succeed

// No manifest id serialized, class doesn't exist - use write-time configuration
case (None, Some(id),Failure(_)) =>
ser.deserialize[X forSome { type X <: AnyRef }](bytes, id, clazz.toOption)
case (None, Some(id), Failure(_)) =>
ser.deserialize[X forSome {type X <: AnyRef}](bytes, id, clazz.toOption)

// fall back
case (_,None, Failure(_)) =>
case (_, None, Failure(_)) =>
ser.deserialize(bytes, clazz.get)
}

Expand Down