Skip to content

Commit

Permalink
Merge pull request #10 from oker1/instance-cache-type-params
Browse files Browse the repository at this point in the history
use type params in cache key
  • Loading branch information
shuttie authored Aug 26, 2021
2 parents 48c1bd2 + c16a5d8 commit 4ec44ba
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 4ec44ba

Please sign in to comment.