Skip to content

Commit 63c7cef

Browse files
authored
Don't cache nested classes with generic type parameters (#243)
* Don't cache nested classes with generic type parameters * Don't use debug variable in tests
1 parent a0c9c89 commit 63c7cef

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

modules/flink-1-api/src/test/scala-3/org/apache/flinkx/api/GenericCaseClassScala3Test.scala

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package org.apache.flinkx.api
22

33
import org.apache.flink.api.common.typeinfo.TypeInformation
4+
import org.apache.flinkx.api.serializer.{CoproductSerializer, ListCCSerializer, ScalaCaseClassSerializer}
45
import org.apache.flinkx.api.serializers.*
5-
import org.apache.flinkx.api.typeinfo.ProductTypeInformation
6+
import org.apache.flinkx.api.typeinfo.{CoproductTypeInformation, ProductTypeInformation}
67
import org.scalatest.flatspec.AnyFlatSpec
78
import org.scalatest.matchers.should
89

@@ -45,6 +46,28 @@ class GenericCaseClassScala3Test extends AnyFlatSpec with should.Matchers {
4546
aBasketInfo.asInstanceOf[ProductTypeInformation[A]].getFieldTypes()(0) should be theSameInstanceAs aInfo
4647
}
4748

49+
"Nested generics" should "be resolved correctly" in {
50+
val intTypeInfo: TypeInformation[Option[Option[Int]]] = generateTypeInfo[Int]
51+
val stringTypeInfo: TypeInformation[Option[Option[String]]] = generateTypeInfo[String]
52+
53+
intTypeInfo shouldNot be theSameInstanceAs stringTypeInfo
54+
}
55+
56+
it should "work with multiple type parameters" in {
57+
val intTypeInfo = generateEitherTypeInfo[Int]
58+
val boolTypeInfo = generateEitherTypeInfo[Boolean]
59+
60+
intTypeInfo shouldNot be theSameInstanceAs boolTypeInfo
61+
62+
}
63+
64+
def generateTypeInfo[A: TypeInformation]: TypeInformation[Option[Option[A]]] = {
65+
deriveTypeInformation
66+
}
67+
68+
def generateEitherTypeInfo[A: TypeInformation]: TypeInformation[Either[Option[A], Int]] = {
69+
deriveTypeInformation
70+
}
4871
}
4972

5073
object GenericCaseClassScala3Test {

modules/flink-common-api/src/main/scala-3/org/apache/flinkx/api/TypeTagMacro.scala

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ object TypeTagMacro:
77
def gen[A: Type](using q: Quotes): Expr[TypeTag[A]] =
88
import q.reflect.*
99

10-
val A = TypeRepr.of[A]
11-
val symA = A.typeSymbol
12-
val flagsA = symA.flags
13-
val isModuleExpr = Expr(flagsA.is(Flags.Module))
14-
val isCachableExpr = Expr(A match {
15-
// this type is not cachable if one of its type args is abstract
16-
case a: AppliedType => !a.args.exists { t => t.typeSymbol.isAbstractType }
17-
case _ => true
18-
})
10+
def check(r: TypeRepr): Boolean =
11+
r match {
12+
case a: AppliedType =>
13+
!a.args.exists { t => t.typeSymbol.isAbstractType } && a.args.forall { t => check(t) }
14+
case _ => true
15+
}
16+
17+
val A = TypeRepr.of[A]
18+
val symA = A.typeSymbol
19+
val flagsA = symA.flags
20+
val isModuleExpr = Expr(flagsA.is(Flags.Module))
21+
val isCachableExpr = Expr(check(A))
22+
1923
val toStringExpr = Expr(A.show)
2024

2125
'{

0 commit comments

Comments
 (0)