Skip to content

Commit

Permalink
Merge branch 'master' into mix-scala-2-and-3-macros
Browse files Browse the repository at this point in the history
  • Loading branch information
gregghz authored Sep 20, 2024
2 parents 724cb52 + 10a084d commit 519b37b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 74 deletions.
25 changes: 9 additions & 16 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ lazy val enumeratumReactiveMongoBson =
version := Versions.Core.head,
crossScalaVersions := scalaVersionsAll,
libraryDependencies += {
"org.reactivemongo" %% "reactivemongo-bson-api" % "1.1.0-RC9" % Provided
"org.reactivemongo" %% "reactivemongo-bson-api" % "1.1.0-RC12" % Provided
},
libraryDependencies += scalaXmlTest,
libraryDependencies ++= {
Expand Down Expand Up @@ -273,10 +273,10 @@ lazy val enumeratumPlayJson = crossProject(JSPlatform, JVMPlatform)
.jsSettings(jsTestSettings)
.settings(
name := "enumeratum-play-json",
version := "1.8.1-SNAPSHOT",
version := "1.8.2-SNAPSHOT",
crossScalaVersions := scalaVersionsAll,
libraryDependencies ++= Seq(
"org.playframework" %%% "play-json" % "3.0.0",
"org.playframework" %%% "play-json" % "3.0.4",
scalaXmlTest
),
libraryDependencies ++= {
Expand All @@ -302,11 +302,11 @@ lazy val enumeratumPlay = Project(id = "enumeratum-play", base = file("enumeratu
.settings(commonWithPublishSettings)
.settings(testSettings)
.settings(
version := "1.8.1-SNAPSHOT",
version := "1.8.2-SNAPSHOT",
// Play do not support 2.12 (default from common settings)
scalaVersion := scala_2_13Version,
crossScalaVersions := Seq(scala_2_13Version, scala_3Version),
libraryDependencies += ("org.playframework" %% "play" % "3.0.0")
libraryDependencies += ("org.playframework" %% "play" % "3.0.4")
.exclude("org.scala-lang.modules", "*"),
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "7.0.0" % Test,
libraryDependencies ++= {
Expand Down Expand Up @@ -390,14 +390,7 @@ lazy val enumeratumArgonaut = crossProject(JSPlatform, JVMPlatform)
version := Versions.Core.head,
crossScalaVersions := scalaVersionsAll,
libraryDependencies ++= {
val ver: String = {
if (scalaBinaryVersion.value == "3") {
"6.3.8"
} else {
"6.2.5"
}
}

val ver: String = "6.3.9"
Seq(
"io.argonaut" %%% "argonaut" % ver,
scalaXmlTest
Expand Down Expand Up @@ -427,7 +420,7 @@ lazy val enumeratumJson4s =
version := Versions.Core.head,
crossScalaVersions := scalaVersionsAll,
libraryDependencies ++= {
val ver = "4.0.3"
val ver = "4.0.7"

Seq(
"org.json4s" %% "json4s-core" % ver,
Expand Down Expand Up @@ -567,7 +560,7 @@ lazy val enumeratumDoobie =
.settings(testSettings)
.settings(
crossScalaVersions := scalaVersionsAll,
version := "1.7.6-SNAPSHOT",
version := "1.7.7-SNAPSHOT",
libraryDependencies += "org.tpolecat" %% "doobie-core" % "1.0.0-RC5",
libraryDependencies += scalaXmlTest,
libraryDependencies ++= {
Expand All @@ -585,7 +578,7 @@ lazy val enumeratumSlick =
.settings(commonWithPublishSettings)
.settings(testSettings)
.settings(
version := "1.7.7-SNAPSHOT",
version := "1.7.8-SNAPSHOT",
crossScalaVersions := scalaVersionsAll,
libraryDependencies ++= Seq(
("com.typesafe.slick" %% "slick" % "3.5.1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ class ValueEnumSpec extends AnyFunSpec with Matchers with ValueEnumHelpers {
""" shouldNot compile
}

it("should compile when the value constructor parameter is not first") {
"""
sealed abstract class MyStatus(final val idx: Int, final val value: String) extends StringEnumEntry
object MyStatus extends StringEnum[MyStatus] {
case object PENDING extends MyStatus(1, "PENDING")
val values = findValues
}
""" should compile
}

it("should compile even when values are repeated if AllowAlias is extended") {
"""
sealed abstract class ContentTypeRepeated(val value: Long, name: String) extends LongEnumEntry with AllowAlias
Expand Down
78 changes: 24 additions & 54 deletions macros/src/main/scala-3/enumeratum/ValueEnumMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,51 +133,28 @@ In SBT settings:
""")
}

val repr = TypeRepr.of[A](using tpe)
val repr = TypeRepr.of[A]
val tpeSym = repr.typeSymbol

val valueRepr = TypeRepr.of[ValueType]

val ctorParams = tpeSym.primaryConstructor.paramSymss.flatten

val enumFields = repr.typeSymbol.fieldMembers.flatMap { field =>
ctorParams.zipWithIndex.find { case (p, i) =>
p.name == field.name && (p.tree match {
case term: Term =>
term.tpe <:< valueRepr

case _ =>
false
})
val valueParamIndex = tpeSym.primaryConstructor.paramSymss
.filterNot(_.exists(_.isType))
.flatten
.zipWithIndex
.collectFirst {
case (p, i) if p.name == "value" => i
}
}.toSeq

val (valueField, valueParamIndex): (Symbol, Int) = {
if (enumFields.size == 1) {
enumFields.headOption
} else {
enumFields.find(_._1.name == "value")
}
}.getOrElse {
Symbol.newVal(tpeSym, "value", valueRepr, Flags.Abstract, Symbol.noSymbol) -> 0
}

type IsValue[T <: ValueType] = T

object ConstVal {
@annotation.tailrec
def unapply(tree: Tree): Option[Constant] = tree match {
case NamedArg(nme, v) if (nme == valueField.name) =>
unapply(v)

case ValDef(nme, _, Some(v)) if (nme == valueField.name) =>
unapply(v)

case lit @ Literal(const) if (lit.tpe <:< valueRepr) =>
Some(const)

case _ =>
None
case NamedArg("value", v) => unapply(v)
case ValDef("value", _, Some(v)) => unapply(v)
case lit @ Literal(const) if (lit.tpe <:< valueRepr) => Some(const)
case _ => None
}
}

Expand All @@ -193,24 +170,18 @@ In SBT settings:
(for {
vof <- Expr.summon[ValueOf[h]]
constValue <- htpr.typeSymbol.tree match {
case ClassDef(_, _, spr, _, rhs) => {
val fromCtor = spr
.collectFirst {
case Apply(Select(New(id), _), args) if id.tpe <:< repr => args
case Apply(TypeApply(Select(New(id), _), _), args) if id.tpe <:< repr => args
}
.flatMap(_.lift(valueParamIndex).collect { case ConstVal(const) =>
const
})

fromCtor
.orElse(rhs.collectFirst { case ConstVal(v) => v })
.flatMap { const =>
cls.unapply(const.value)
}

case ClassDef(_, _, parents, _, statements) => {
val fromCtor = valueParamIndex.flatMap { (ix: Int) =>
parents
.collectFirst {
case Apply(Select(New(id), _), args) if id.tpe <:< repr => args
case Apply(TypeApply(Select(New(id), _), _), args) if id.tpe <:< repr => args
}
.flatMap(_.lift(ix).collect { case ConstVal(const) => const })
}
def fromBody = statements.collectFirst { case ConstVal(v) => v }
fromCtor.orElse(fromBody).flatMap { const => cls.unapply(const.value) }
}

case _ =>
Option.empty[ValueType]
}
Expand All @@ -230,8 +201,7 @@ In SBT settings:
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 _ => Left(s"Invalid `Mirror.SumOf[${TypeRepr.of[h].show}]")
}

case None =>
Expand All @@ -248,7 +218,7 @@ In SBT settings:
}
.mkString(", ")

Left(s"Values for ${valueField.name} are not discriminated subtypes: ${details}")
Left(s"Values value are not discriminated subtypes: ${details}")
} else {
Right(Expr.ofList(instances.reverse))
}
Expand Down
6 changes: 6 additions & 0 deletions macros/src/test/scala/enumeratum/CompilationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,9 @@ object F {
case object F4 extends F(value = 4, "mike")

}

sealed abstract class G(val name: String, val value: Int)
object G {
val values = FindValEnums[G]
case object G1 extends G("gerald", 1)
}
8 changes: 4 additions & 4 deletions project/Versions.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
object Versions {

object Core {
val stable = "1.7.3"
val head = "1.7.4-SNAPSHOT"
val stable = "1.7.4"
val head = "1.7.5-SNAPSHOT"
}

object Macros {
val stable = "1.6.4"
val head = "1.6.5-SNAPSHOT"
val stable = "1.6.5"
val head = "1.6.6-SNAPSHOT"
}

}

0 comments on commit 519b37b

Please sign in to comment.