Skip to content

Commit

Permalink
Fix #941 by adding an ability to derive codecs for Scala 3 enum value…
Browse files Browse the repository at this point in the history
…s with mixed traits
  • Loading branch information
plokhotnyuk committed Sep 9, 2022
1 parent f66afd9 commit 9d254c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,9 @@ object JsonCodecMaker {
case ConstantType(_) => true
case _ => false

def isEnumOrModuleValue(tpe: TypeRepr): Boolean =
tpe.isSingleton && (tpe.typeSymbol.flags.is(Flags.Enum) || tpe.typeSymbol.flags.is(Flags.Module))
def isEnumOrModuleValue(tpe: TypeRepr): Boolean = tpe.isSingleton &&
(tpe.typeSymbol.flags.is(Flags.Enum) || tpe.typeSymbol.flags.is(Flags.Module) ||
tpe.typeSymbol.flags.is(Flags.EmptyFlags) && !isConstType(tpe)) // FIXME: Remove after fix in Dotty: https://github.com/lampepfl/dotty/issues/16008

def getEnclosingClass(sym: Symbol): Symbol =
if (sym.isClassDef) sym
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ enum FooEnum[A[_]]:
case Bar[A[_]](a: A[Int]) extends FooEnum[A]
case Baz[A[_]](a: A[String]) extends FooEnum[A]

trait MyMarker

enum MyEnum(val value: String):
case Marked extends MyEnum("item1") with MyMarker
case Simple extends MyEnum("item2")

object MyEnum:
implicit val codec: JsonValueCodec[MyEnum] = JsonCodecMaker.make // FIXME: Remove after fix in Dotty: https://github.com/lampepfl/dotty/issues/16008

class JsonCodecMakerNewEnumSpec extends VerifyingSpec {
"JsonCodecMaker.make generate codecs which" should {
"serialize and deserialize Scala3 enums" in {
Expand Down

0 comments on commit 9d254c6

Please sign in to comment.