Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DeriveSchema for parameterized enums #607

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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])
Expand All @@ -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")(
Expand All @@ -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)))
}
)
)
Expand Down
Loading