Skip to content

Don't cache nested classes with generic type parameters #243

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

Merged
merged 2 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.apache.flinkx.api

import org.apache.flink.api.common.typeinfo.TypeInformation
import org.apache.flinkx.api.serializer.{CoproductSerializer, ListCCSerializer, ScalaCaseClassSerializer}
import org.apache.flinkx.api.serializers.*
import org.apache.flinkx.api.typeinfo.ProductTypeInformation
import org.apache.flinkx.api.typeinfo.{CoproductTypeInformation, ProductTypeInformation}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should

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

"Nested generics" should "be resolved correctly" in {
val intTypeInfo: TypeInformation[Option[Option[Int]]] = generateTypeInfo[Int]
val stringTypeInfo: TypeInformation[Option[Option[String]]] = generateTypeInfo[String]

intTypeInfo shouldNot be theSameInstanceAs stringTypeInfo
}

it should "work with multiple type parameters" in {
val intTypeInfo = generateEitherTypeInfo[Int]
val boolTypeInfo = generateEitherTypeInfo[Boolean]

intTypeInfo shouldNot be theSameInstanceAs boolTypeInfo

}

def generateTypeInfo[A: TypeInformation]: TypeInformation[Option[Option[A]]] = {
deriveTypeInformation
}

def generateEitherTypeInfo[A: TypeInformation]: TypeInformation[Either[Option[A], Int]] = {
deriveTypeInformation
}
}

object GenericCaseClassScala3Test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ object TypeTagMacro:
def gen[A: Type](using q: Quotes): Expr[TypeTag[A]] =
import q.reflect.*

val A = TypeRepr.of[A]
val symA = A.typeSymbol
val flagsA = symA.flags
val isModuleExpr = Expr(flagsA.is(Flags.Module))
val isCachableExpr = Expr(A match {
// this type is not cachable if one of its type args is abstract
case a: AppliedType => !a.args.exists { t => t.typeSymbol.isAbstractType }
case _ => true
})
def check(r: TypeRepr): Boolean =
r match {
case a: AppliedType =>
!a.args.exists { t => t.typeSymbol.isAbstractType } && a.args.forall { t => check(t) }
case _ => true
}

val A = TypeRepr.of[A]
val symA = A.typeSymbol
val flagsA = symA.flags
val isModuleExpr = Expr(flagsA.is(Flags.Module))
val isCachableExpr = Expr(check(A))

val toStringExpr = Expr(A.show)

'{
Expand Down