Skip to content

Commit

Permalink
Fix ValueEnums not working with private constructors (#67)
Browse files Browse the repository at this point in the history
In 1.4.11, the macro started only looking for public constructor methods. There was no reason
for this change at all, so it has been reverted.
  • Loading branch information
lloydmeta authored Aug 24, 2016
1 parent 032d2b7 commit 877d988
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package enumeratum.values

/**
* Code from @zifeo
*/
trait CustomEnumEntry extends IntEnumEntry {
val value: Int
val name: String
}
trait CustomEnum[T <: CustomEnumEntry] extends IntEnum[T] {
def apply(name: String): T =
values.find(_.name == name).get
}
trait CustomEnumComparable[T <: CustomEnumEntry] {
this: T =>
def >=(that: T): Boolean =
this.value >= that.value
}
sealed abstract class CustomEnumPrivateConstructor private (val value: Int, val name: String)
extends CustomEnumEntry with CustomEnumComparable[CustomEnumPrivateConstructor]
object CustomEnumPrivateConstructor extends CustomEnum[CustomEnumPrivateConstructor] {
val values = findValues
case object A extends CustomEnumPrivateConstructor(10, "a")
case object B extends CustomEnumPrivateConstructor(20, "b")
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ValueEnumSpec extends FunSpec with Matchers with ValueEnumHelpers {
testNumericEnum("when using val members in the body", MovieGenre)
testNumericEnum("LongEnum that is nesting an IntEnum", Animal)
testNumericEnum("IntEnum that is nested inside a LongEnum", Animal.Mammalian)
testNumericEnum("Custom IntEnum with private constructors", CustomEnumPrivateConstructor)

describe("finding companion object") {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ object ValueEnumMacros {
val valueEntryTypeTpe = implicitly[c.WeakTypeTag[ValueEntryType]].tpe
val valueEntryTypeTpeMembers = valueEntryTypeTpe.members
valueEntryTypeTpeMembers.collect {
case m if m.isMethod && m.isPublic && m.isConstructor => m.asMethod.paramLists.flatten.map(_.asTerm.name)
case m if m.isMethod && m.isConstructor => m.asMethod.paramLists.flatten.map(_.asTerm.name)
}.toList
}

Expand Down

0 comments on commit 877d988

Please sign in to comment.