Skip to content

Commit

Permalink
fixed incorrect (but working) writer/reader generation for complex su…
Browse files Browse the repository at this point in the history
…m types
  • Loading branch information
Ковалев Георгий Алексеевич committed Oct 23, 2024
1 parent 0c971cc commit aa560cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aa560cb

Please sign in to comment.