From 2f8fe5d91800fc4602c6bbdaa663c6f9772963a2 Mon Sep 17 00:00:00 2001 From: pfeme Date: Wed, 24 May 2023 11:29:31 +0200 Subject: [PATCH 1/6] added simpleEnum annotation --- .../scala-2/zio/schema/DeriveSchema.scala | 62 ++++++++++++++----- .../scala-3/zio/schema/DeriveSchema.scala | 9 ++- .../scala/zio/schema/DeriveSchemaSpec.scala | 5 +- .../scala/zio/schema/codec/JsonCodec.scala | 2 +- .../src/main/scala/zio/schema/Schema.scala | 4 +- .../zio/schema/annotation/simpleEnum.scala | 10 +++ .../schema/meta/ExtensibleMetaSchema.scala | 21 +++++-- 7 files changed, 86 insertions(+), 27 deletions(-) create mode 100644 zio-schema/shared/src/main/scala/zio/schema/annotation/simpleEnum.scala diff --git a/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala b/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala index c1ddb7714..06d455e9f 100644 --- a/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala +++ b/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala @@ -501,23 +501,53 @@ object DeriveSchema { } } + val isSimpleEnum: Boolean = + !tpe.typeSymbol.asClass.knownDirectSubclasses.map {subtype => + subtype.typeSignature.decls.sorted.collect { + case p: TermSymbol if p.isCaseAccessor && !p.isMethod => p + }.size + }.exists( _ > 0) + + val hasSimpleEnum: Boolean = + tpe.typeSymbol.annotations.exists(_.tree.tpe =:= typeOf[_root_.zio.schema.annotation.simpleEnum]) + @nowarn - val typeAnnotations: List[Tree] = - tpe.typeSymbol.annotations.collect { - case annotation if !(annotation.tree.tpe <:< JavaAnnotationTpe) => - annotation.tree match { - case q"new $annConstructor(..$annotationArgs)" => - q"new ${annConstructor.tpe.typeSymbol}(..$annotationArgs)" - case q"new $annConstructor()" => - q"new ${annConstructor.tpe.typeSymbol}()" - case tree => - c.warning(c.enclosingPosition, s"Unhandled annotation tree $tree") - EmptyTree - } - case annotation => - c.warning(c.enclosingPosition, s"Unhandled annotation ${annotation.tree}") - EmptyTree - }.filter(_ != EmptyTree) + val typeAnnotations: List[Tree] = (isSimpleEnum, hasSimpleEnum) match { + case (true, false) => + tpe.typeSymbol.annotations.collect { + case annotation if !(annotation.tree.tpe <:< JavaAnnotationTpe) => + annotation.tree match { + case q"new $annConstructor(..$annotationArgs)" => + q"new ${annConstructor.tpe.typeSymbol}(..$annotationArgs)" + case q"new $annConstructor()" => + q"new ${annConstructor.tpe.typeSymbol}()" + case tree => + c.warning(c.enclosingPosition, s"Unhandled annotation tree $tree") + EmptyTree + } + case annotation => + c.warning(c.enclosingPosition, s"Unhandled annotation ${annotation.tree}") + EmptyTree + }.filter(_ != EmptyTree).prepended(q"new _root_.zio.schema.annotation.simpleEnum()") + case (false, true) => + c.abort(c.enclosingPosition, s"${show(tpe)} must be a simple Enum") + case _ => + tpe.typeSymbol.annotations.collect { + case annotation if !(annotation.tree.tpe <:< JavaAnnotationTpe) => + annotation.tree match { + case q"new $annConstructor(..$annotationArgs)" => + q"new ${annConstructor.tpe.typeSymbol}(..$annotationArgs)" + case q"new $annConstructor()" => + q"new ${annConstructor.tpe.typeSymbol}()" + case tree => + c.warning(c.enclosingPosition, s"Unhandled annotation tree $tree") + EmptyTree + } + case annotation => + c.warning(c.enclosingPosition, s"Unhandled annotation ${annotation.tree}") + EmptyTree + }.filter(_ != EmptyTree) + } val selfRefName = c.freshName("ref") val selfRefIdent = Ident(TermName(selfRefName)) 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 4e67fd317..e463ede08 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 @@ -293,7 +293,14 @@ private case class DeriveSchema()(using val ctx: Quotes) extends ReflectionUtils val cases = typesAndLabels.map { case (tpe, label) => deriveCase[T](tpe, label, newStack) } - val annotationExprs = TypeRepr.of[T].typeSymbol.annotations.filter(filterAnnotation).map(_.asExpr) + val isSimpleEnum: Boolean = !types.map(_.labels.length).exists( _ > 0 ) + 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) + 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) + } val annotations = '{ zio.Chunk.fromIterable(${Expr.ofSeq(annotationExprs)}) } val typeInfo = '{TypeId.parse(${Expr(TypeRepr.of[T].show)})} diff --git a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala index dd37ad614..cc082972f 100644 --- a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala +++ b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala @@ -3,7 +3,7 @@ package zio.schema import scala.annotation.Annotation import zio.Chunk -import zio.schema.annotation.{ fieldName, optionalField } +import zio.schema.annotation.{ fieldName, optionalField, simpleEnum } import zio.test._ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaSpec { @@ -243,6 +243,9 @@ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaS implicit val schema: Schema[OptionalField] = DeriveSchema.gen[OptionalField] } + @simpleEnum sealed trait NonSimpleEnum + case class NonSimpleClass(name: String) extends NonSimpleEnum + override def spec: Spec[Environment, Any] = suite("DeriveSchemaSpec")( suite("Derivation")( test("correctly derives case class 0") { diff --git a/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala b/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala index 8c22480fa..f17564ee6 100644 --- a/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala +++ b/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala @@ -379,7 +379,7 @@ object JsonCodec { private def enumEncoder[Z](parentSchema: Schema.Enum[Z], cases: Schema.Case[Z, _]*): ZJsonEncoder[Z] = // if all cases are CaseClass0, encode as a String - if (cases.forall(_.schema.isInstanceOf[Schema.CaseClass0[_]])) { + if (parentSchema.annotations.exists(_.isInstanceOf[simpleEnum])) { val caseMap: Map[Z, String] = cases .filterNot(_.annotations.exists(_.isInstanceOf[transientCase])) .map( diff --git a/zio-schema/shared/src/main/scala/zio/schema/Schema.scala b/zio-schema/shared/src/main/scala/zio/schema/Schema.scala index 26eb6c168..73e6eb792 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/Schema.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/Schema.scala @@ -161,8 +161,8 @@ object Schema extends SchemaEquality { def defer[A](schema: => Schema[A]): Schema[A] = Lazy(() => schema) - def enumeration[A, C <: CaseSet.Aux[A]](id: TypeId, caseSet: C): Schema[A] = - EnumN(id, caseSet, Chunk.empty) + def enumeration[A, C <: CaseSet.Aux[A]](id: TypeId, caseSet: C, annotations: Chunk[Any] = Chunk.empty): Schema[A] = + EnumN(id, caseSet, annotations) def fail[A](message: String): Schema[A] = Fail(message) diff --git a/zio-schema/shared/src/main/scala/zio/schema/annotation/simpleEnum.scala b/zio-schema/shared/src/main/scala/zio/schema/annotation/simpleEnum.scala new file mode 100644 index 000000000..308ce926f --- /dev/null +++ b/zio-schema/shared/src/main/scala/zio/schema/annotation/simpleEnum.scala @@ -0,0 +1,10 @@ +package zio.schema.annotation + +import scala.annotation.StaticAnnotation + +/* + * Automatically applied in sealed traits with only case objects or case class without parameters. + * Gives error if it used in other types of enumerations. + */ + +final case class simpleEnum() extends StaticAnnotation diff --git a/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala b/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala index 8560c79de..68d51044c 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala @@ -8,6 +8,7 @@ import zio.constraintless.TypeList import zio.prelude._ import zio.schema._ import zio.{ Chunk, ChunkBuilder } +import zio.schema.annotation.simpleEnum sealed trait ExtensibleMetaSchema[BuiltIn <: TypeList] { self => def builtInInstances: SchemaInstances[BuiltIn] @@ -636,11 +637,9 @@ object ExtensibleMetaSchema { materialize(left, refs), materialize(right, refs) ) - case ExtensibleMetaSchema.Sum(id, _, elems, _) => - Schema.enumeration[Any, CaseSet.Aux[Any]]( - id, - elems.foldRight[CaseSet.Aux[Any]](CaseSet.Empty[Any]()) { - case (Labelled(label, ast), acc) => + case ExtensibleMetaSchema.Sum(id, _, elems, _) => { + val (cases, isSimple) = elems.foldRight[(CaseSet.Aux[Any], Boolean)]((CaseSet.Empty[Any](), true)) { + case (Labelled(label, ast), (acc, wasSimple)) => { val _case: Schema.Case[Any, Any] = Schema .Case[Any, Any]( label, @@ -650,9 +649,19 @@ object ExtensibleMetaSchema { _.isInstanceOf[Any], Chunk.empty ) - CaseSet.Cons(_case, acc) + val isSimple: Boolean = _case.schema match { + case _: Schema.CaseClass0[_] => true + case _ => false + } + (CaseSet.Cons(_case, acc), wasSimple && isSimple) + } } + Schema.enumeration[Any, CaseSet.Aux[Any]]( + id, + cases, + if(isSimple) Chunk(new simpleEnum()) else Chunk.empty ) + } case ExtensibleMetaSchema.Either(_, left, right, _) => Schema.either( materialize(left, refs), From b379935c10a2d42f5a7c661b217c7e85e71beb47 Mon Sep 17 00:00:00 2001 From: pfeme Date: Wed, 24 May 2023 23:02:48 +0200 Subject: [PATCH 2/6] added automaticallyAdded --- .../shared/src/main/scala-2/zio/schema/DeriveSchema.scala | 2 +- .../shared/src/main/scala-3/zio/schema/DeriveSchema.scala | 4 ++-- .../shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala | 4 +--- .../src/main/scala/zio/schema/annotation/simpleEnum.scala | 4 ++-- .../src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala | 2 +- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala b/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala index 06d455e9f..bb04e00c3 100644 --- a/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala +++ b/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala @@ -528,7 +528,7 @@ object DeriveSchema { case annotation => c.warning(c.enclosingPosition, s"Unhandled annotation ${annotation.tree}") EmptyTree - }.filter(_ != EmptyTree).prepended(q"new _root_.zio.schema.annotation.simpleEnum()") + }.filter(_ != EmptyTree).+:(q"new _root_.zio.schema.annotation.simpleEnum(true)") case (false, true) => c.abort(c.enclosingPosition, s"${show(tpe)} must be a simple Enum") case _ => 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 e463ede08..ac1065c8e 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 @@ -293,11 +293,11 @@ 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 = !types.map(_.labels.length).exists( _ > 0 ) + val isSimpleEnum: Boolean = !TypeRepr.of[T].typeSymbol.children.map(_.declaredFields.length).exists( _ > 0 ) 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) + case (true, false) => TypeRepr.of[T].typeSymbol.annotations.filter(filterAnnotation).map(_.asExpr).+:('{_root_.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/zio/schema/DeriveSchemaSpec.scala b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala index cc082972f..a43e9d330 100644 --- a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala +++ b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala @@ -3,7 +3,7 @@ package zio.schema import scala.annotation.Annotation import zio.Chunk -import zio.schema.annotation.{ fieldName, optionalField, simpleEnum } +import zio.schema.annotation.{ fieldName, optionalField } import zio.test._ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaSpec { @@ -243,8 +243,6 @@ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaS implicit val schema: Schema[OptionalField] = DeriveSchema.gen[OptionalField] } - @simpleEnum sealed trait NonSimpleEnum - case class NonSimpleClass(name: String) extends NonSimpleEnum override def spec: Spec[Environment, Any] = suite("DeriveSchemaSpec")( suite("Derivation")( diff --git a/zio-schema/shared/src/main/scala/zio/schema/annotation/simpleEnum.scala b/zio-schema/shared/src/main/scala/zio/schema/annotation/simpleEnum.scala index 308ce926f..567177400 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/annotation/simpleEnum.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/annotation/simpleEnum.scala @@ -3,8 +3,8 @@ package zio.schema.annotation import scala.annotation.StaticAnnotation /* - * Automatically applied in sealed traits with only case objects or case class without parameters. + * Automatically added in sealed traits with only case objects or case class without parameters. * Gives error if it used in other types of enumerations. */ -final case class simpleEnum() extends StaticAnnotation +final case class simpleEnum(automaticallyAdded: Boolean = false) extends StaticAnnotation diff --git a/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala b/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala index 68d51044c..562197567 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala @@ -659,7 +659,7 @@ object ExtensibleMetaSchema { Schema.enumeration[Any, CaseSet.Aux[Any]]( id, cases, - if(isSimple) Chunk(new simpleEnum()) else Chunk.empty + if(isSimple) Chunk(new simpleEnum(true)) else Chunk.empty ) } case ExtensibleMetaSchema.Either(_, left, right, _) => From 4eb9228cf66c85f392c31ef81db65dd9208ce563 Mon Sep 17 00:00:00 2001 From: pfeme Date: Thu, 25 May 2023 00:17:09 +0200 Subject: [PATCH 3/6] added automaticallyAdded --- .../scala-2/zio/schema/DeriveSchema.scala | 8 ++--- .../scala/zio/schema/DeriveSchemaSpec.scala | 1 - .../schema/meta/ExtensibleMetaSchema.scala | 34 +++++++++---------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala b/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala index bb04e00c3..d76868615 100644 --- a/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala +++ b/zio-schema-derivation/shared/src/main/scala-2/zio/schema/DeriveSchema.scala @@ -502,11 +502,11 @@ object DeriveSchema { } val isSimpleEnum: Boolean = - !tpe.typeSymbol.asClass.knownDirectSubclasses.map {subtype => + !tpe.typeSymbol.asClass.knownDirectSubclasses.map { subtype => subtype.typeSignature.decls.sorted.collect { - case p: TermSymbol if p.isCaseAccessor && !p.isMethod => p - }.size - }.exists( _ > 0) + case p: TermSymbol if p.isCaseAccessor && !p.isMethod => p + }.size + }.exists(_ > 0) val hasSimpleEnum: Boolean = tpe.typeSymbol.annotations.exists(_.tree.tpe =:= typeOf[_root_.zio.schema.annotation.simpleEnum]) diff --git a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala index a43e9d330..dd37ad614 100644 --- a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala +++ b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala @@ -243,7 +243,6 @@ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaS implicit val schema: Schema[OptionalField] = DeriveSchema.gen[OptionalField] } - override def spec: Spec[Environment, Any] = suite("DeriveSchemaSpec")( suite("Derivation")( test("correctly derives case class 0") { diff --git a/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala b/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala index 562197567..d406e2d08 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/meta/ExtensibleMetaSchema.scala @@ -7,8 +7,8 @@ import scala.collection.mutable import zio.constraintless.TypeList import zio.prelude._ import zio.schema._ -import zio.{ Chunk, ChunkBuilder } import zio.schema.annotation.simpleEnum +import zio.{ Chunk, ChunkBuilder } sealed trait ExtensibleMetaSchema[BuiltIn <: TypeList] { self => def builtInInstances: SchemaInstances[BuiltIn] @@ -639,27 +639,27 @@ object ExtensibleMetaSchema { ) case ExtensibleMetaSchema.Sum(id, _, elems, _) => { val (cases, isSimple) = elems.foldRight[(CaseSet.Aux[Any], Boolean)]((CaseSet.Empty[Any](), true)) { - case (Labelled(label, ast), (acc, wasSimple)) => { - val _case: Schema.Case[Any, Any] = Schema - .Case[Any, Any]( - label, - materialize(ast, refs).asInstanceOf[Schema[Any]], - identity[Any], - identity[Any], - _.isInstanceOf[Any], - Chunk.empty - ) - val isSimple: Boolean = _case.schema match { - case _: Schema.CaseClass0[_] => true - case _ => false - } - (CaseSet.Cons(_case, acc), wasSimple && isSimple) + case (Labelled(label, ast), (acc, wasSimple)) => { + val _case: Schema.Case[Any, Any] = Schema + .Case[Any, Any]( + label, + materialize(ast, refs).asInstanceOf[Schema[Any]], + identity[Any], + identity[Any], + _.isInstanceOf[Any], + Chunk.empty + ) + val isSimple: Boolean = _case.schema match { + case _: Schema.CaseClass0[_] => true + case _ => false } + (CaseSet.Cons(_case, acc), wasSimple && isSimple) } + } Schema.enumeration[Any, CaseSet.Aux[Any]]( id, cases, - if(isSimple) Chunk(new simpleEnum(true)) else Chunk.empty + if (isSimple) Chunk(new simpleEnum(true)) else Chunk.empty ) } case ExtensibleMetaSchema.Either(_, left, right, _) => From 4727f22ad7b08ac85e364c7c6792f03f75fed076 Mon Sep 17 00:00:00 2001 From: pfeme Date: Fri, 26 May 2023 11:21:09 +0200 Subject: [PATCH 4/6] added tests --- .../scala/zio/schema/DeriveSchemaSpec.scala | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala index dd37ad614..7b377c881 100644 --- a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala +++ b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSchemaSpec.scala @@ -3,7 +3,7 @@ package zio.schema import scala.annotation.Annotation import zio.Chunk -import zio.schema.annotation.{ fieldName, optionalField } +import zio.schema.annotation.{ fieldName, optionalField, simpleEnum } import zio.test._ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaSpec { @@ -243,6 +243,13 @@ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaS implicit val schema: Schema[OptionalField] = DeriveSchema.gen[OptionalField] } + @simpleEnum + sealed trait SimpleEnum1 + case class SimpleClass1() extends SimpleEnum1 + + sealed trait SimpleEnum2 + case class SimpleClass2() extends SimpleEnum2 + override def spec: Spec[Environment, Any] = suite("DeriveSchemaSpec")( suite("Derivation")( test("correctly derives case class 0") { @@ -449,6 +456,14 @@ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaS ) } assert(derived)(hasSameSchema(expected)) + }, + test("correctly derives simpleEnum with annotation") { + val derived = DeriveSchema.gen[SimpleEnum1] + assertTrue(derived.annotations == Chunk(simpleEnum(false))) + }, + test("correctly derives simpleEnum without annotation") { + val derived = DeriveSchema.gen[SimpleEnum2] + assertTrue(derived.annotations == Chunk(simpleEnum(true))) } ), versionSpecificSuite From 0b43f3623f466566927ddb9cbbd059129b241714 Mon Sep 17 00:00:00 2001 From: pfeme Date: Fri, 26 May 2023 13:06:04 +0200 Subject: [PATCH 5/6] fmt --- .../src/main/scala/zio/schema/annotation/recordName.scala | 2 -- 1 file changed, 2 deletions(-) diff --git a/zio-schema/shared/src/main/scala/zio/schema/annotation/recordName.scala b/zio-schema/shared/src/main/scala/zio/schema/annotation/recordName.scala index c3c33efe1..8b1378917 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/annotation/recordName.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/annotation/recordName.scala @@ -1,3 +1 @@ -package zio.schema.annotation -final case class recordName(name: String) extends scala.annotation.StaticAnnotation From 88c3961fb3ca6e37b887187e66fd2fbce47db68a Mon Sep 17 00:00:00 2001 From: pfeme Date: Fri, 26 May 2023 13:30:28 +0200 Subject: [PATCH 6/6] restored recordName --- .../src/main/scala/zio/schema/annotation/recordName.scala | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/zio-schema/shared/src/main/scala/zio/schema/annotation/recordName.scala b/zio-schema/shared/src/main/scala/zio/schema/annotation/recordName.scala index 8b1378917..69be3f2a9 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/annotation/recordName.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/annotation/recordName.scala @@ -1 +1,5 @@ +package zio.schema.annotation +import scala.annotation.StaticAnnotation + +final case class recordName(name: String) extends StaticAnnotation