Skip to content

Commit

Permalink
use type params in cache key so multiple instances can be correctly d…
Browse files Browse the repository at this point in the history
…erived for generic classes
  • Loading branch information
oker1 committed Aug 5, 2021
1 parent 48c1bd2 commit c16a5d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/scala/io/findify/flinkadt/api/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ package object api extends LowPrioImplicits {
def combine[T <: Product: ClassTag: TypeTag](
ctx: CaseClass[TypeInformation, T]
): TypeInformation[T] = {
cache.get(ctx.typeName.full) match {
val cacheKey = s"${ctx.typeName.full}_${ctx.typeName.typeArguments}"
cache.get(cacheKey) match {
case Some(cached) => cached.asInstanceOf[TypeInformation[T]]
case None =>
val clazz = classTag[T].runtimeClass.asInstanceOf[Class[T]]
Expand All @@ -60,7 +61,7 @@ package object api extends LowPrioImplicits {
params = ctx.parameters,
ser = serializer
)
cache.put(ctx.typeName.full, ti)
cache.put(cacheKey, ti)
ti
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/scala/io/findify/flinkadt/SerializerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.findify.flinkadt.SerializerTest.{
Bar2,
Foo,
Foo2,
Generic,
ListADT,
Nested,
Node,
Expand Down Expand Up @@ -142,6 +143,13 @@ class SerializerTest extends AnyFlatSpec with Matchers with Inspectors {
roundtrip(ser, ListADT(List(Foo("a"))))
}

it should "derive multiple instances of generic class" in {
val ser = implicitly[TypeInformation[Generic[SimpleOption]]].createSerializer(null)
val ser2 = implicitly[TypeInformation[Generic[Simple]]].createSerializer(null)
all(ser, Generic(SimpleOption(None), Bar(0)))
all(ser2, Generic(Simple(0, "asd"), Bar(0)))
}

def roundtrip[T](ser: TypeSerializer[T], in: T) = {
val out = new ByteArrayOutputStream()
ser.serialize(in, new DataOutputViewStreamWrapper(out))
Expand Down Expand Up @@ -219,5 +227,7 @@ object SerializerTest {

case class SimpleOption(a: Option[String])

case class Generic[T](a: T, b: ADT)

case class ListADT(a: List[ADT])
}

0 comments on commit c16a5d8

Please sign in to comment.