Skip to content
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

Update scala 3 macro to support a hierarchy of sealed traits #371

Merged
merged 1 commit into from
Jul 21, 2023
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
Expand Up @@ -135,6 +135,22 @@ class ValueEnumSpec extends AnyFunSpec with Matchers with ValueEnumHelpers {
""" should (compile)
}

it("should compile when there is a hierarchy of sealed traits") {
"""
sealed abstract class Top(val value: Int) extends IntEnumEntry
sealed trait Middle extends Top

case object Top extends IntEnum[Top] {
case object One extends Top(1)
case object Two extends Top(2)
case object Three extends Top(3) with Middle
case object Four extends Top(4) with Middle

val values = findValues
}
""" should compile
}

it("should fail to compile when there are non literal values") {
"""
sealed abstract class ContentTypeRepeated(val value: Long, name: String) extends LongEnumEntry
Expand Down
18 changes: 16 additions & 2 deletions macros/src/main/scala-3/enumeratum/ValueEnumMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ object ValueEnumMacros {
tpe: Type[A],
valueTpe: Type[ValueType]
)(using cls: ClassTag[ValueType]): Expr[IndexedSeq[A]] = {
type TakeHead[Head <: A & Singleton, Tail <: Tuple] = Head *: Tail
type SingletonHead[Head <: A & Singleton, Tail <: Tuple] = Head *: Tail
type OtherHead[Head <: A, Tail <: Tuple] = Head *: Tail

type SumOf[X <: A, T <: Tuple] = Mirror.SumOf[X] {
type MirroredElemTypes = T
Expand Down Expand Up @@ -186,7 +187,7 @@ In SBT settings:
values: Map[TypeRepr, ValueType]
)(using tupleTpe: Type[T]): Either[String, Expr[List[A]]] =
tupleTpe match {
case '[TakeHead[h, tail]] => {
case '[SingletonHead[h, tail]] => {
val htpr = TypeRepr.of[h]

(for {
Expand Down Expand Up @@ -224,6 +225,19 @@ In SBT settings:
}
}

case '[OtherHead[h, tail]] =>
Expr.summon[Mirror.SumOf[h]] match {
case Some(sum) =>
sum.asTerm.tpe.asType match {
case '[SumOf[a, t]] => collect[Tuple.Concat[t, tail]](instances, values)

case _ => Left(s"Invalid `Mirror.SumOf[${TypeRepr.of[h].show}]")
}

case None =>
Left(s"Missing `Mirror.SumOf[${TypeRepr.of[h].show}]`")
}

case '[EmptyTuple] => {
val allowAlias = repr <:< TypeRepr.of[AllowAlias]

Expand Down