Skip to content

Commit

Permalink
Merge pull request #16 from sageserpent-open/scala-object-serializer-…
Browse files Browse the repository at this point in the history
…supports-is-immutable

Scala object serializer supports copying.
  • Loading branch information
danischroeter authored Sep 26, 2024
2 parents 46419ac + 09e1485 commit 490b799
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ import scala.util.control.Exception.allCatch
class ScalaObjectSerializer[T] extends Serializer[T] {
private val cachedObj = MMap[Class[?], Option[T]]()

// NOTE: even if a standalone or companion Scala object contains mutable
// fields, the fact that there is only one of them in a process means that
// we don't want to make a copy, so this serializer's type is treated as
// always being immutable.
override def isImmutable: Boolean = true

// Does nothing
override def write(kser: Kryo, out: Output, obj: T): Unit = ()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.altoo.serialization.kryo.scala.serializer

import io.altoo.serialization.kryo.scala.testkit.AbstractKryoTest

import java.util.UUID

trait Snowflake {
val state: UUID = UUID.randomUUID()

override def hashCode(): Int = state.hashCode()

override def equals(another: Any): Boolean = another match {
case anotherSnowflake: Snowflake =>
// NOTE: don't worry about respecting different flavours of
// subclass, as all snowflakes are constructed different from
// each other to start with. Only copies can be equal!
this.state == anotherSnowflake.state
case _ => false
}
}

object standalone extends Snowflake

object ScalaObjectSerializerTest extends Snowflake

class ScalaObjectSerializerTest extends AbstractKryoTest {
private def configureKryo(): Unit = {
kryo.setRegistrationRequired(false)
// NOTE: to support building under Scala 2.12, use the Java approach of obtaining
// a singleton object's class at runtime, rather than `classOf[singleton.type]`
kryo.addDefaultSerializer(standalone.getClass, classOf[ScalaObjectSerializer[Any]])
kryo.addDefaultSerializer(ScalaObjectSerializerTest.getClass, classOf[ScalaObjectSerializer[Any]])
}

behavior of "ScalaObjectSerializer"

it should "round trip standalone and companion objects" in {
configureKryo()

(testSerializationOf(standalone) should be).theSameInstanceAs(standalone)

(testSerializationOf(ScalaObjectSerializerTest) should be).theSameInstanceAs(ScalaObjectSerializerTest)
}

it should "support copying of standalone and companion objects" in {
configureKryo()

(testCopyingOf(standalone) should be).theSameInstanceAs(standalone)

(testCopyingOf(ScalaObjectSerializerTest) should be).theSameInstanceAs(ScalaObjectSerializerTest)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ trait KryoSerializationTesting {
input.close()
obj1.asInstanceOf[T]
}

protected final def testCopyingOf[T](obj: T): T = {
val copy = kryo.copy(obj)

assert(copy == obj)

obj
}
}

0 comments on commit 490b799

Please sign in to comment.