From 877d9884cbf655a4bb7f81a6cfb85b23a57c5f46 Mon Sep 17 00:00:00 2001 From: Lloyd Date: Wed, 24 Aug 2016 17:46:54 +0900 Subject: [PATCH] Fix ValueEnums not working with private constructors (#67) 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. --- .../values/CustomEnumPrivateConstructor.scala | 25 +++++++++++++++++++ .../enumeratum/values/ValueEnumSpec.scala | 1 + .../enumeratum/ValueEnumMacros.scala | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 enumeratum-core/compat/src/test/scala-2.11/enumeratum/values/CustomEnumPrivateConstructor.scala diff --git a/enumeratum-core/compat/src/test/scala-2.11/enumeratum/values/CustomEnumPrivateConstructor.scala b/enumeratum-core/compat/src/test/scala-2.11/enumeratum/values/CustomEnumPrivateConstructor.scala new file mode 100644 index 00000000..8e85b5a5 --- /dev/null +++ b/enumeratum-core/compat/src/test/scala-2.11/enumeratum/values/CustomEnumPrivateConstructor.scala @@ -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") +} \ No newline at end of file diff --git a/enumeratum-core/compat/src/test/scala-2.11/enumeratum/values/ValueEnumSpec.scala b/enumeratum-core/compat/src/test/scala-2.11/enumeratum/values/ValueEnumSpec.scala index 4792226b..65474bb7 100644 --- a/enumeratum-core/compat/src/test/scala-2.11/enumeratum/values/ValueEnumSpec.scala +++ b/enumeratum-core/compat/src/test/scala-2.11/enumeratum/values/ValueEnumSpec.scala @@ -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") { diff --git a/macros/compat/src/main/scala-2.11/enumeratum/ValueEnumMacros.scala b/macros/compat/src/main/scala-2.11/enumeratum/ValueEnumMacros.scala index bb662db6..dc896c45 100644 --- a/macros/compat/src/main/scala-2.11/enumeratum/ValueEnumMacros.scala +++ b/macros/compat/src/main/scala-2.11/enumeratum/ValueEnumMacros.scala @@ -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 }