From 952fae386ddb131c9fe8a3044a349a8b6554f4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Femen=C3=ADa?= <131800808+pablf@users.noreply.github.com> Date: Fri, 15 Sep 2023 13:27:06 +0200 Subject: [PATCH] Fix DeriveSchema for parameterized enums (#607) * fix enum derive * fix test --- .../src/main/scala-3/zio/schema/DeriveSchema.scala | 5 +++-- .../zio/schema/VersionSpecificDeriveSchemaSpec.scala | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/zio-schema-derivation/shared/src/main/scala-3/zio/schema/DeriveSchema.scala b/zio-schema-derivation/shared/src/main/scala-3/zio/schema/DeriveSchema.scala index 02207f36e..359ab429b 100644 --- a/zio-schema-derivation/shared/src/main/scala-3/zio/schema/DeriveSchema.scala +++ b/zio-schema-derivation/shared/src/main/scala-3/zio/schema/DeriveSchema.scala @@ -324,11 +324,12 @@ private case class DeriveSchema()(using val ctx: Quotes) extends ReflectionUtils val cases = typesAndLabels.map { case (tpe, label) => deriveCase[T](tpe, label, newStack) } - val isSimpleEnum: Boolean = !TypeRepr.of[T].typeSymbol.children.map(_.declaredFields.length).exists( _ > 0 ) + val numParentFields: Int = TypeRepr.of[T].typeSymbol.declaredFields.length + val isSimpleEnum: Boolean = !TypeRepr.of[T].typeSymbol.children.map(_.declaredFields.length).exists( _ > numParentFields ) val hasSimpleEnumAnn: Boolean = TypeRepr.of[T].typeSymbol.hasAnnotation(TypeRepr.of[_root_.zio.schema.annotation.simpleEnum].typeSymbol) val annotationExprs = (isSimpleEnum, hasSimpleEnumAnn) match { - case (true, false) => TypeRepr.of[T].typeSymbol.annotations.filter(filterAnnotation).map(_.asExpr).+:('{_root_.zio.schema.annotation.simpleEnum(true)}) + case (true, false) => TypeRepr.of[T].typeSymbol.annotations.filter(filterAnnotation).map(_.asExpr).+:('{zio.schema.annotation.simpleEnum(true)}) case (false, true) => throw new Exception(s"${TypeRepr.of[T].typeSymbol.name} must be a simple Enum") case _ => TypeRepr.of[T].typeSymbol.annotations.filter(filterAnnotation).map(_.asExpr) } diff --git a/zio-schema-derivation/shared/src/test/scala-3/zio/schema/VersionSpecificDeriveSchemaSpec.scala b/zio-schema-derivation/shared/src/test/scala-3/zio/schema/VersionSpecificDeriveSchemaSpec.scala index bd9aa814e..1c1bce328 100644 --- a/zio-schema-derivation/shared/src/test/scala-3/zio/schema/VersionSpecificDeriveSchemaSpec.scala +++ b/zio-schema-derivation/shared/src/test/scala-3/zio/schema/VersionSpecificDeriveSchemaSpec.scala @@ -1,6 +1,8 @@ package zio.schema +import zio.Chunk import zio.test.* +import zio.schema.annotation.simpleEnum trait VersionSpecificDeriveSchemaSpec extends ZIOSpecDefault { case class ContainerFields(field1: Option[String]) @@ -20,6 +22,12 @@ trait VersionSpecificDeriveSchemaSpec extends ZIOSpecDefault { final case class AutoDerives(i: Int) derives Schema + enum Colour(val rgb: Int) { + case Red extends Colour(0xff0000) + case Green extends Colour(0x00ff00) + case Blue extends Colour(0x0000ff) + } + def versionSpecificSuite = Spec.labeled( "Scala 3 specific tests", suite("Derivation")( @@ -35,6 +43,10 @@ trait VersionSpecificDeriveSchemaSpec extends ZIOSpecDefault { AutoDerives.apply ) assert(Schema[AutoDerives])(hasSameSchema(expected)) + }, + test("correctly assigns simpleEnum to enum") { + val derived: Schema[Colour] = DeriveSchema.gen[Colour] + assertTrue(derived.annotations == Chunk(simpleEnum(true))) } ) )