Skip to content

Commit

Permalink
don't check for the correct tree shape for empty tree
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnhoekstra committed Jun 19, 2024
1 parent 1fbbd04 commit a1a0c80
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
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
22 changes: 2 additions & 20 deletions macros/src/main/scala-3/enumeratum/ValueEnumMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,8 @@ In SBT settings:

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
})
}
}.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
val (valueField, valueParamIndex): (Symbol, Int) = ctorParams.zipWithIndex.find{ case (p, _) => p.name == "value"}.getOrElse {
report.errorAndAbort(s"Could not find 'value' field in ${tpeSym.name}")
}

type IsValue[T <: ValueType] = T
Expand Down

0 comments on commit a1a0c80

Please sign in to comment.