From 086561aec26fb9a0b5dea1d7057cfe44c336810a Mon Sep 17 00:00:00 2001 From: pfeme Date: Sun, 30 Jun 2024 22:38:08 +0200 Subject: [PATCH] not assign simpleEnum annotation --- .../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 57df11ebc..cc01759be 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 @@ -360,7 +360,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 {