From f5152fbb3e364f462ab76833d57c594a4d8735f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Femen=C3=ADa?= <131800808+pablf@users.noreply.github.com> Date: Tue, 9 Jul 2024 16:24:26 +0200 Subject: [PATCH] not assign simpleEnum annotation (#707) --- .../scala-3/zio/schema/DeriveSchema.scala | 4 ++- .../VersionSpecificDeriveSchemaSpec.scala | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) 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 e012fbac0..53566a978 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 @@ -364,7 +364,9 @@ private case class DeriveSchema()(using val ctx: Quotes) { val cases = typesAndLabels.map { case (tpe, label) => deriveCase[T](tpe, label, newStack) } val numParentFields: Int = TypeRepr.of[T].typeSymbol.declaredFields.length - val isSimpleEnum: Boolean = !TypeRepr.of[T].typeSymbol.children.map(_.declaredFields.length).exists( _ > numParentFields ) + val childrenFields = TypeRepr.of[T].typeSymbol.children.map(_.declaredFields.length) + val childrenFieldsConstructor = TypeRepr.of[T].typeSymbol.children.map(_.caseFields.length) + val isSimpleEnum: Boolean = childrenFieldsConstructor.forall( _ == 0) && childrenFields.forall( _ <= numParentFields) val hasSimpleEnumAnn: Boolean = TypeRepr.of[T].typeSymbol.hasAnnotation(TypeRepr.of[_root_.zio.schema.annotation.simpleEnum].typeSymbol) val docAnnotationExpr = TypeRepr.of[T].typeSymbol.docstring.map { docstring => 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 881c7bb08..68c7d195d 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 @@ -48,6 +48,23 @@ trait VersionSpecificDeriveSchemaSpec extends ZIOSpecDefault { @description("Red") case Red + enum NonSimpleEnum1: + case A(a: Int) + + enum NonSimpleEnum2(a: Int): + case A(b: Int) extends NonSimpleEnum2(0) + + + enum NonSimpleEnum3(a: Int): + case A(b: Int) extends NonSimpleEnum3(b) + + enum NonSimpleEnum4(val a: Int): + case A(override val a: Int) extends NonSimpleEnum4(a) + + enum NonSimpleEnum5(a: Int, b: String): + case A extends NonSimpleEnum5(0, "") + case B(n: Int) extends NonSimpleEnum5(n, "") + def versionSpecificSuite = Spec.labeled( "Scala 3 specific tests", suite("Derivation")( @@ -68,6 +85,18 @@ trait VersionSpecificDeriveSchemaSpec extends ZIOSpecDefault { val derived: Schema[Colour] = DeriveSchema.gen[Colour] assertTrue(derived.annotations == Chunk(simpleEnum(true))) }, + test("doesn't assigns simpleEnum to non-simple enum") { + val derived1: Schema[NonSimpleEnum1] = DeriveSchema.gen[NonSimpleEnum1] + val derived2: Schema[NonSimpleEnum2] = DeriveSchema.gen[NonSimpleEnum2] + val derived3: Schema[NonSimpleEnum3] = DeriveSchema.gen[NonSimpleEnum3] + val derived4: Schema[NonSimpleEnum4] = DeriveSchema.gen[NonSimpleEnum4] + val derived5: Schema[NonSimpleEnum5] = DeriveSchema.gen[NonSimpleEnum5] + assertTrue(derived1.annotations.isEmpty) && + assertTrue(derived2.annotations.isEmpty) && + assertTrue(derived3.annotations.isEmpty) && + assertTrue(derived4.annotations.isEmpty) && + assertTrue(derived5.annotations.isEmpty) + }, test("derive different annotations for parent and child in enum") { val parent = DeriveSchema.gen[ColourAnnotations] val child = parent match {