Skip to content

Commit

Permalink
Review & rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
cchantep committed Nov 27, 2022
1 parent 79aeb2a commit ee55e85
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 30 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ jobs:
- java: 11
scala: 2.11.12
- java: 11
scala: 2.12.16
scala: 2.12.17
- java: 11
scala: 2.13.8
scala: 2.13.10
- java: 11
scala: 3.2.1-RC1
scala: 3.2.1
runs-on: ubuntu-latest
env:
SCALAJS_TEST_OPT: full
Expand All @@ -42,7 +42,7 @@ jobs:
sbt -v ++${{ matrix.scala }} scalafmtCheck scalafmtSbtCheck scala_2_11/test:compile scala_2_11/test:doc
sbt -v ++${{ matrix.scala }} scala_2_11/test
;;
2.12.16)
2.12.17)
sbt -v ++${{ matrix.scala }} test:compile test:doc
sbt -v ++${{ matrix.scala }} coverage test coverageReport
sbt -v ++${{ matrix.scala }} coverageAggregate
Expand All @@ -58,7 +58,7 @@ jobs:
find $HOME/.cache/coursier/v1 -name "ivydata-*.properties" -delete || true
find $HOME/.sbt -name "*.lock" -delete || true
- name: Upload coverage to Codecov
if: ${{ matrix.scala == '2.12.16' }}
if: ${{ matrix.scala == '2.12.17' }}
uses: codecov/codecov-action@v2
with:
fail_ci_if_error: true
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import sbtbuildinfo.BuildInfoPlugin.autoImport._
import sbtcrossproject.CrossPlugin.autoImport.{CrossType, crossProject}

lazy val scala_2_11Version = "2.11.12"
lazy val scala_2_12Version = "2.12.16"
lazy val scala_2_13Version = "2.13.8"
lazy val scala_3Version = "3.2.1-RC1"
lazy val scala_2_12Version = "2.12.17"
lazy val scala_2_13Version = "2.13.10"
lazy val scala_3Version = "3.2.1"
lazy val scalaVersionsAll =
Seq(scala_2_11Version, scala_2_12Version, scala_2_13Version, scala_3Version)

Expand Down
6 changes: 6 additions & 0 deletions enumeratum-core/src/test/scala/enumeratum/EnumSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class EnumSpec extends AnyFunSpec with Matchers with EnumSpecCompat {
)
)
}

it("should contain instance of subclass") {
import Inheritance._

Word.values should be(IndexedSeq(Word.Hello, Word.Hi))
}
}

describe("#withName") {
Expand Down
13 changes: 13 additions & 0 deletions enumeratum-core/src/test/scala/enumeratum/Models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,16 @@ object InTheWoods {

}
}

object Inheritance {
sealed trait Word extends EnumEntry

object Word extends Enum[Word] {
sealed class Greeting extends Word

lazy val values = findValues

case object Hello extends Greeting
case object Hi extends Greeting
}
}
37 changes: 17 additions & 20 deletions macros/src/main/scala-3/enumeratum/EnumMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ object EnumMacros:
def findValuesImpl[A](using tpe: Type[A], q: Quotes): Expr[IndexedSeq[A]] = {
import q.reflect.*

// println(s"${child.show} :: ${child.typeSymbol.methodMember("findValues")}")
val definingTpeSym = Symbol.spliceOwner.maybeOwner.maybeOwner

if (!definingTpeSym.flags.is(Flags.Module)) {
report.errorAndAbort(
// Root must be a module to have same behaviour
s"The enum (i.e. the class containing the case objects and the call to `findValues`) must be an object: ${definingTpeSym.fullName}"
)
}

val repr = validateType[A]
val subclasses = enclosedSubClasses[A](q)(repr)

Expand Down Expand Up @@ -88,25 +98,11 @@ object EnumMacros:
)(using tpe: Type[T]): List[q.reflect.TypeRepr] = {
import q.reflect.*

// TODO: Use SumOf?
given quotes: q.type = q

@annotation.tailrec
def isObject(sym: Symbol, ok: Boolean = false): Boolean = {
if (!sym.flags.is(Flags.Module)) {
val owner = sym.maybeOwner

if (
owner == defn.RootClass ||
owner.flags.is(Flags.Package) ||
owner.isAnonymousFunction
) {
report.errorAndAbort(
// Root must be a module to have same behaviour
"The enum (i.e. the class containing the case objects and the call to `findValues`) must be an object"
)
}

false
} else {
val owner = sym.maybeOwner
Expand Down Expand Up @@ -139,27 +135,29 @@ object EnumMacros:
children: List[Tree],
out: List[TypeRepr]
): List[TypeRepr] = {
val childTpr = children.headOption.collect {
val childTpr: Option[TypeRepr] = children.headOption.collect {
case tpd: Typed =>
tpd.tpt.tpe

case vd: ValDef =>
vd.tpt.tpe

case cd: ClassDef =>
cd.constructor.returnTpt.tpe
cd.symbol.typeRef match {
case TypeRef(prefix, _) =>
prefix.select(cd.symbol)

}
}

childTpr match {
case Some(child) => {
child.asType match {
case '[IsEntry[t]] => {
case ct @ '[IsEntry[t]] => {
val tpeSym = child.typeSymbol
// TODO: Check is subtype (same in Scala2?)

if (!isObject(tpeSym)) {
subclasses(children.tail, out)
subclasses(tpeSym.children.map(_.tree) ::: children.tail, out)
} else {
subclasses(children.tail, child :: out)
}
Expand All @@ -177,7 +175,6 @@ object EnumMacros:

tpr.classSymbol
.flatMap { cls =>
// TODO: cls.typeMembers
val types = subclasses(cls.children.map(_.tree), Nil)

if (types.isEmpty) None else Some(types)
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.7.1
sbt.version = 1.8.0
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.1")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.2.0")

addSbtPlugin(("org.scoverage" % "sbt-scoverage" % "2.0.2").exclude("org.scala-lang.modules", "*"))
addSbtPlugin(("org.scoverage" % "sbt-scoverage" % "2.0.5").exclude("org.scala-lang.modules", "*"))

addSbtPlugin(("org.scoverage" % "sbt-coveralls" % "1.3.2").exclude("org.scala-lang.modules", "*"))

0 comments on commit ee55e85

Please sign in to comment.