Skip to content
Draft
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
78 changes: 41 additions & 37 deletions core/src/main/scala/magnolia1/impl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ object CaseClassDerivation:
} { params => product.fromProduct(Tuple.fromArray(params)) }
}

private trait ParamFactory[TC[_], P]:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where one would need to add an @implicitNotFound annotation in order to fix the failing tests related to (compilation) error messages.

Something along the lines of:

@implicitNotFound("No given instance of type ${TC} (for type ${P}) was found")

def tc: SerializableFunction0[TC[P]]
def isType(v: Any): Boolean

private object ParamFactory:
inline given paramFactory[TC[_], P]: ParamFactory[TC, P] =
new ParamFactory[TC, P]:
def tc: SerializableFunction0[TC[P]] =
new SerializableFunction0[TC[P]]:
def apply(): TC[P] = summonInline[TC[P]]
def isType(v: Any): Boolean = (v: @unchecked).isInstanceOf[P]

inline def paramsFromMaps[Typeclass[_], A, Labels <: Tuple, Params <: Tuple](
annotations: Map[String, List[Any]],
inheritedAnnotations: Map[String, List[Any]],
Expand All @@ -104,43 +116,35 @@ object CaseClassDerivation:
defaults: Map[String, Option[() => Any]],
idx: Int = 0
): List[CaseClass.Param[Typeclass, A]] =
inline erasedValue[(Labels, Params)] match
case _: (EmptyTuple, EmptyTuple) =>
Nil
case _: ((l *: ltail), (p *: ptail)) =>
val label = constValue[l].asInstanceOf[String]
val tc = new SerializableFunction0[Typeclass[p]]:
override def apply(): Typeclass[p] = summonInline[Typeclass[p]]
val d =
defaults.get(label).flatten match {
case Some(evaluator) =>
new SerializableFunction0[Option[p]]:
override def apply(): Option[p] =
val v = evaluator()
if ((v: @unchecked).isInstanceOf[p]) Some(v.asInstanceOf[p])
else None
case _ =>
new SerializableFunction0[Option[p]]:
override def apply(): Option[p] = None
}
paramFromMaps[Typeclass, A, p](
label,
CallByNeed.createLazy(tc),
CallByNeed.createValueEvaluator(d),
repeated,
annotations,
inheritedAnnotations,
typeAnnotations,
idx
) ::
paramsFromMaps[Typeclass, A, ltail, ptail](
annotations,
inheritedAnnotations,
typeAnnotations,
repeated,
defaults,
idx + 1
)
val labels: List[String] =
summonAll[Tuple.Map[Labels, ValueOf]].toList.asInstanceOf[List[ValueOf[? <: String]]].map(_.value)
type PFactory[P] = ParamFactory[Typeclass, P]
val factories: List[PFactory[?]] =
summonAll[Tuple.Map[Params, PFactory]].toList.asInstanceOf[List[PFactory[?]]]
labels.zip(factories).zipWithIndex.map { case ((label, f: PFactory[p]), i) =>
val d =
defaults(label) match {
case Some(evaluator) =>
new SerializableFunction0[Option[p]]:
override def apply(): Option[p] =
val v = evaluator()
if (f.isType(v)) Some(v.asInstanceOf[p])
else None
case _ =>
new SerializableFunction0[Option[p]]:
override def apply(): Option[p] = None
}
paramFromMaps[Typeclass, A, p](
label,
CallByNeed.createLazy(f.tc),
CallByNeed.createValueEvaluator(d),
repeated,
annotations,
inheritedAnnotations,
typeAnnotations,
idx + i
)
}

private def paramFromMaps[Typeclass[_], A, p](
label: String,
Expand Down