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

Poc #3

Merged
merged 27 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0daff4c
init
HubertBalcerzak Apr 6, 2021
659d734
Added dependencies
MarconZet Apr 6, 2021
53d2573
Copied code from article
MarconZet Apr 6, 2021
4dd2c65
compilation fixes
HubertBalcerzak Apr 6, 2021
a709d1b
Application confi
MarconZet Apr 6, 2021
fd36c4e
add basic tests
HubertBalcerzak Apr 6, 2021
732d642
fix tests
HubertBalcerzak Apr 6, 2021
1b6a6f3
Added test with enumeration field type
MarconZet Apr 7, 2021
5751495
Added line that causes compilation to fail
MarconZet Apr 12, 2021
cdcb9da
reorganize dependencies
HubertBalcerzak Apr 17, 2021
826435e
use AkkaTestKit in tests, reorganize code
HubertBalcerzak Apr 17, 2021
9392fd4
add scalafmt
HubertBalcerzak Apr 19, 2021
b964abf
change package, add scalac options
HubertBalcerzak Apr 24, 2021
66599b6
add circleci config
HubertBalcerzak Apr 24, 2021
ac28923
bump akka version
HubertBalcerzak Apr 24, 2021
2b71a41
use AtomicReference
HubertBalcerzak Apr 24, 2021
8f141c9
fix formatting
HubertBalcerzak Apr 24, 2021
39d9e64
Adder runtime codex class checks
MarconZet Apr 24, 2021
22374a0
Merge remote-tracking branch 'origin/poc' into poc
MarconZet Apr 24, 2021
a3a5604
Changed Codecs from trait to object
MarconZet Apr 24, 2021
f7fdc0e
Created a Borer codec for OffsetDateTime
MarconZet Apr 26, 2021
2347a2f
Created a Borer codec for SourceRef and SinkRef
MarconZet Apr 27, 2021
2a5511c
Minor refactor
MarconZet Apr 27, 2021
703b6ad
Made akka dependencies provided
MarconZet Apr 28, 2021
61cf71a
Updated config for circleci
MarconZet Apr 28, 2021
108e066
Changed wording of RuntimeException when trying to serialise object t…
MarconZet Apr 28, 2021
9b526f8
Fixed errors on tests
MarconZet Apr 28, 2021
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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
*.class
*.log

.bsp/
.idea/

dist/*
target/
lib_managed/
src_managed/
project/boot/
project/plugins/project/
.history
.cache
.lib/
14 changes: 14 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Dependencies._

ThisBuild / scalaVersion := "2.12.13"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
HubertBalcerzak marked this conversation as resolved.
Show resolved Hide resolved
ThisBuild / organizationName := "example"

HubertBalcerzak marked this conversation as resolved.
Show resolved Hide resolved
lazy val root = (project in file("."))
.settings(
name := "akka-safer-serializer",
libraryDependencies ++= deps
)

// See https://www.scala-sbt.org/1.x/docs/Using-Sonatype.html for instructions on how to publish to Sonatype.
21 changes: 21 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sbt._

object Dependencies {
val borerVersion = "1.6.3"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already Borer 1.7.0 AFAICS

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or now 1.7.1 with #16 fixed ;)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops looks that 1.7+ is only for Scala 2.13... lemme discuss with the Borer author...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe I'll manage to migrate Hydra to 2.13 right now, lemme see :trollface:

val akkaVersion = "2.6.10"

val scalaTest = "org.scalatest" %% "scalatest" % "3.2.2"
val akkaTyped = "com.typesafe.akka" %% "akka-actor-typed" % akkaVersion
MarconZet marked this conversation as resolved.
Show resolved Hide resolved
val akkaTestKit = "com.typesafe.akka" %% "akka-actor-testkit-typed" % akkaVersion
val enumeratum = "com.beachape" %% "enumeratum" % "1.6.1"
val borerCore = "io.bullet" %% "borer-core" % borerVersion
val borerDerivation = "io.bullet" %% "borer-derivation" % borerVersion

val deps = Seq(
scalaTest % Test,
akkaTestKit % Test,
akkaTyped,
enumeratum,
borerCore,
borerDerivation)
}
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.4.9
PawelLipski marked this conversation as resolved.
Show resolved Hide resolved
40 changes: 40 additions & 0 deletions src/main/scala/example/CborAkkaSerializer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package example

import akka.serialization.Serializer
import io.bullet.borer.{Cbor, Codec, Decoder, Encoder}

import scala.reflect.ClassTag


trait CborAkkaSerializer[Ser] extends Serializer {
PawelLipski marked this conversation as resolved.
Show resolved Hide resolved

private var registrations: List[(Class[_], Codec[_])] = Nil
HubertBalcerzak marked this conversation as resolved.
Show resolved Hide resolved

protected def register[T <: Ser: Encoder: Decoder: ClassTag]: Unit = {
registrations ::= scala.reflect.classTag[T].runtimeClass -> Codec.of[T]
}

override def includeManifest: Boolean = true

override def toBinary(o: AnyRef): Array[Byte] = {
val codec = getCodec(o.getClass, "encoding")
val encoder = codec.encoder.asInstanceOf[Encoder[AnyRef]]
Cbor.encode(o)(encoder).toByteArray
}

override def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef = {
val codec = getCodec(manifest.get, "decoding")
val decoder = codec.decoder.asInstanceOf[Decoder[AnyRef]]
Cbor.decode(bytes).to[AnyRef](decoder).value
}

private def getCodec(classValue: Class[_], action: String): Codec[_] = {
registrations
.collectFirst {
case (clazz, codec) if clazz.isAssignableFrom(classValue) => codec
}
.getOrElse {
throw new RuntimeException(s"$action of $classValue is not configured")
}
}
}
10 changes: 10 additions & 0 deletions src/test/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
akka.actor {
serializers {
borer-cbor = "example.BorerAkkaSerializer"
HubertBalcerzak marked this conversation as resolved.
Show resolved Hide resolved
}
serialization-bindings {
"example.BorerSerializable" = borer-cbor
}
enable-additional-serialization-bindings = on
allow-java-serialization = off
}
10 changes: 10 additions & 0 deletions src/test/scala/example/Codecs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package example

import io.bullet.borer.Codec
import io.bullet.borer.derivation.MapBasedCodecs.deriveAllCodecs

trait Codecs {
MarconZet marked this conversation as resolved.
Show resolved Hide resolved
implicit lazy val animalCodec: Codec[Animal] = deriveAllCodecs
implicit lazy val greetingCodec: Codec[Greeting] = deriveAllCodecs
implicit lazy val zooCodec: Codec[Zoo] = deriveAllCodecs
}
54 changes: 54 additions & 0 deletions src/test/scala/example/Data.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package example

trait BorerSerializable

sealed abstract class Animal extends BorerSerializable

sealed trait Zoo extends BorerSerializable {
def primaryAttraction: Animal
}


object Zoo {

final case class NorthZoo(primaryAttraction: Animal) extends Zoo

final case class SouthZoo(primaryAttraction: Animal) extends Zoo

final case class GreetingZoo(primaryAttraction: Animal, greeting: Greeting) extends Zoo

}

object Animal {

final case class Lion(name: String) extends Animal

final case class Elephant(name: String, age: Int) extends Animal

final case object Tiger extends Animal

trait NoCompile

// Uncommenting this results in failure in compilation
// final case class InvalidAnimal(noCompile: NoCompile) extends Animal

}

import enumeratum._

import scala.collection.immutable.IndexedSeq

sealed trait Greeting extends EnumEntry

object Greeting extends Enum[Greeting] {
val values: IndexedSeq[Greeting] = findValues

case object Hello extends Greeting

case object GoodBye extends Greeting

case object Hi extends Greeting

case object Bye extends Greeting

}
9 changes: 9 additions & 0 deletions src/test/scala/example/TestBorerAkkaSerializer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package example

class TestBorerAkkaSerializer extends CborAkkaSerializer[BorerSerializable] with Codecs {

override def identifier: Int = 19923

register[Zoo]
register[Animal]
}
37 changes: 37 additions & 0 deletions src/test/scala/example/TestBorerAkkaSerializerSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package example

import akka.actor.testkit.typed.scaladsl.{ActorTestKit, SerializationTestKit}
import akka.actor.typed.ActorSystem
import com.typesafe.config.ConfigFactory
import example.Animal.{Lion, Tiger}
import example.Zoo.{GreetingZoo, NorthZoo}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

class TestBorerAkkaSerializerSpec extends AnyWordSpecLike with Matchers {

"BorerAkkaSerializer" should {

val config = ConfigFactory.load("application.conf").withFallback(ConfigFactory.load())
val testKit: ActorTestKit = ActorTestKit(config)
val system: ActorSystem[Nothing] = testKit.system
val serializationTestKit = new SerializationTestKit(system)

"serialize singleton" in {
serializationTestKit.verifySerialization(Tiger)
}

"serialize final case class" in {
serializationTestKit.verifySerialization(Lion("lion"))
}

"serialize nested case class" in {
serializationTestKit.verifySerialization(NorthZoo(Lion("lion")))
}

"serialize case class with enumeration" in {
serializationTestKit.verifySerialization(GreetingZoo(Lion("lion"), Greeting.Hello))
}
}

}