From 0c971ccfcf555638179f29297296c9854c97a3f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D0=B2=D0=B0=D0=BB=D0=B5=D0=B2=20=D0=93=D0=B5?= =?UTF-8?q?=D0=BE=D1=80=D0=B3=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Wed, 23 Oct 2024 22:29:48 +0500 Subject: [PATCH 1/2] fixed incorrect (but working) writer/reader generation for complex sum types --- .../scala-3/tethys/derivation/ConfigurationMacroUtils.scala | 2 +- .../core/src/main/scala-3/tethys/derivation/Derivation.scala | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/core/src/main/scala-3/tethys/derivation/ConfigurationMacroUtils.scala b/modules/core/src/main/scala-3/tethys/derivation/ConfigurationMacroUtils.scala index b4193ba..519b8c7 100644 --- a/modules/core/src/main/scala-3/tethys/derivation/ConfigurationMacroUtils.scala +++ b/modules/core/src/main/scala-3/tethys/derivation/ConfigurationMacroUtils.scala @@ -689,7 +689,7 @@ trait ConfigurationMacroUtils: ) ) - val discriminators: List[Term] = getAllChildren(tpe).map { + val discriminators: List[Term] = getAllChildren(tpe).distinct.map { case tpe: TypeRef => Select(stub(tpe), symbol) case tpe: TermRef => diff --git a/modules/core/src/main/scala-3/tethys/derivation/Derivation.scala b/modules/core/src/main/scala-3/tethys/derivation/Derivation.scala index 688c9bc..234ae70 100644 --- a/modules/core/src/main/scala-3/tethys/derivation/Derivation.scala +++ b/modules/core/src/main/scala-3/tethys/derivation/Derivation.scala @@ -149,7 +149,7 @@ private[derivation] class DerivationMacro(val quotes: Quotes) ): Expr[JsonObjectWriter[T]] = val tpe = TypeRepr.of[T] val parsedConfig = parseSumConfig[T] - val types = getAllChildren(tpe) + val types = getAllChildren(tpe).distinct val (missingWriters, refs) = deriveMissingWritersForSum(types) val mirror = '{ summonInline[Mirror.SumOf[T]] } val writer = Block( @@ -492,7 +492,7 @@ private[derivation] class DerivationMacro(val quotes: Quotes) def deriveJsonReaderForSum[T: Type]: Expr[JsonReader[T]] = val tpe = TypeRepr.of[T] val parsed = parseSumConfig[T] - val children = getAllChildren(tpe) + val children = getAllChildren(tpe).distinct parsed.discriminator match case Some(DiscriminatorConfig(label, tpe, discriminators)) => tpe.asType match From aa560cb79d9c3960728009766b70832746ba0ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D0=B2=D0=B0=D0=BB=D0=B5=D0=B2=20=D0=93=D0=B5?= =?UTF-8?q?=D0=BE=D1=80=D0=B3=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Wed, 23 Oct 2024 22:58:17 +0500 Subject: [PATCH 2/2] fixed incorrect (but working) writer/reader generation for complex sum types --- .../derivation/ConfigurationMacroUtils.scala | 26 ++++++++++--------- .../tethys/derivation/Derivation.scala | 4 +-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/modules/core/src/main/scala-3/tethys/derivation/ConfigurationMacroUtils.scala b/modules/core/src/main/scala-3/tethys/derivation/ConfigurationMacroUtils.scala index 519b8c7..7f57a5d 100644 --- a/modules/core/src/main/scala-3/tethys/derivation/ConfigurationMacroUtils.scala +++ b/modules/core/src/main/scala-3/tethys/derivation/ConfigurationMacroUtils.scala @@ -689,7 +689,7 @@ trait ConfigurationMacroUtils: ) ) - val discriminators: List[Term] = getAllChildren(tpe).distinct.map { + val discriminators: List[Term] = getAllChildren(tpe).map { case tpe: TypeRef => Select(stub(tpe), symbol) case tpe: TermRef => @@ -752,17 +752,19 @@ trait ConfigurationMacroUtils: case '[t *: ts] => TypeRepr.of[t] :: typeReprsOf[ts] def getAllChildren(tpe: TypeRepr): List[TypeRepr] = - tpe.asType match - case '[t] => - Expr.summon[scala.deriving.Mirror.Of[t]] match - case Some('{ - $m: scala.deriving.Mirror.SumOf[t] { - type MirroredElemTypes = subs - } - }) => - typeReprsOf[subs].flatMap(getAllChildren) - case _ => - List(tpe) + def loop(tpe: TypeRepr): List[TypeRepr] = + tpe.asType match + case '[t] => + Expr.summon[scala.deriving.Mirror.Of[t]] match + case Some('{ + $m: scala.deriving.Mirror.SumOf[t] { + type MirroredElemTypes = subs + } + }) => + typeReprsOf[subs].flatMap(loop) + case _ => + List(tpe) + loop(tpe).distinct case class SelectedField(name: String, selector: Term) diff --git a/modules/core/src/main/scala-3/tethys/derivation/Derivation.scala b/modules/core/src/main/scala-3/tethys/derivation/Derivation.scala index 234ae70..688c9bc 100644 --- a/modules/core/src/main/scala-3/tethys/derivation/Derivation.scala +++ b/modules/core/src/main/scala-3/tethys/derivation/Derivation.scala @@ -149,7 +149,7 @@ private[derivation] class DerivationMacro(val quotes: Quotes) ): Expr[JsonObjectWriter[T]] = val tpe = TypeRepr.of[T] val parsedConfig = parseSumConfig[T] - val types = getAllChildren(tpe).distinct + val types = getAllChildren(tpe) val (missingWriters, refs) = deriveMissingWritersForSum(types) val mirror = '{ summonInline[Mirror.SumOf[T]] } val writer = Block( @@ -492,7 +492,7 @@ private[derivation] class DerivationMacro(val quotes: Quotes) def deriveJsonReaderForSum[T: Type]: Expr[JsonReader[T]] = val tpe = TypeRepr.of[T] val parsed = parseSumConfig[T] - val children = getAllChildren(tpe).distinct + val children = getAllChildren(tpe) parsed.discriminator match case Some(DiscriminatorConfig(label, tpe, discriminators)) => tpe.asType match