Code Monkey home page Code Monkey logo

Comments (1)

LukeKeywalker avatar LukeKeywalker commented on June 6, 2024

Of course I need to deserialize the data the same way it was serialized, so I looked the code for that up in the akka-persistence-dynamodb library and came up with the solution. It's possible to deserialize Journal events using ByteArrayJournalSerializer class like this:

sealed trait EventDecodingResult[E]
final case class Successful[E](event: E)  extends EventDecodingResult[E]
final case class Failed[E](error: String) extends EventDecodingResult[E]

class EventDecoder[E](val serializer: ByteArrayJournalSerializer, val journalConfig: JournalPluginConfig) {
  def decode(recordImage: Map[String, AttributeValue]): EventDecodingResult[E] = {
    result(serializer.deserialize(recordImage.toJournalRow(journalConfig)), 1.second) match {
      case (representation, _, _) => Successful(representation.payload.asInstanceOf[E])
      case _                      => Failed("could not decode event")
    }
  }
}

object EventDecoder {
  def apply[E](actorSystem: ActorSystem[AnyRef]): EventDecoder[E] = {
    val serialization     = SerializationExtension(actorSystem)
    val journalConfigPath = "j5ik2o.dynamo-db-journal"
    val journalConfig     = fromConfig(load.getConfig(journalConfigPath))
    val serializer        = new ByteArrayJournalSerializer(serialization, journalConfig.tagSeparator, None, None)

    new EventDecoder[E](serializer, journalConfig)
  }
}

object Implicits {
  implicit class AttributeValueMapImplicits(val recordImage: Map[String, AttributeValue]) {
    def toJournalRow(journalConfig: JournalPluginConfig): JournalRow = {
      val columnsConfig = journalConfig.columnsDefConfig
      JournalRow(
        persistenceId = PersistenceId(recordImage(columnsConfig.persistenceIdColumnName).getS),
        sequenceNumber = SequenceNumber(recordImage(columnsConfig.sequenceNrColumnName).getN.toLong),
        deleted = recordImage(columnsConfig.deletedColumnName).getBOOL,
        message = recordImage.get(columnsConfig.messageColumnName).map(_.getB.array()).get,
        ordering = recordImage(columnsConfig.orderingColumnName).getN.toLong,
        tags = recordImage.get(columnsConfig.tagsColumnName).map(_.getS)
      )
    }
  }
}

from akka-persistence-dynamodb.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.