Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
pablf committed Jun 30, 2024
1 parent 0c577ea commit 6c531a8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ 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 children = TypeRepr.of[T].typeSymbol.children
val childrenFields = children.map(_.declaredFields.length)
val childrenFieldsConstructor = children.map(_.caseFields.length)
val isSimpleEnum: Boolean = childrenFieldsConstructor.forall( _ == 0) && childrenFields.forall( _ <= numParentFields) && types.flatMap(_.children).isEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ object DeriveSchemaSpec extends ZIOSpecDefault with VersionSpecificDeriveSchemaS

sealed trait NonSimpleTrait
sealed trait NonSimpleMiddle extends NonSimpleTrait
case class NonSimpleLeaf() extends NonSimpleMiddle
case class NonSimpleLeaf() extends NonSimpleMiddle

sealed abstract class AbstractBaseClass(val x: Int)
final case class ConcreteClass1(override val x: Int, y: Int) extends AbstractBaseClass(x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ object JsonCodec {
private def enumEncoder[Z](schema: Schema.Enum[Z], cfg: Config, cases: Schema.Case[Z, _]*): ZJsonEncoder[Z] =
// if all cases are CaseClass0, encode as a String
if (schema.annotations.exists(_.isInstanceOf[simpleEnum])) {
if (!cases.forall(_.schema.isInstanceOf[Schema.CaseClass0[_]])) throw new Throwable("@simpleEnum annotation should not be added to non-simple enumerations")
if (!cases.forall(_.schema.isInstanceOf[Schema.CaseClass0[_]]))
throw new Throwable("@simpleEnum annotation should not be added to non-simple enumerations")
val caseMap: Map[Z, String] =
schema.nonTransientCases
.map(
Expand Down Expand Up @@ -701,7 +702,8 @@ object JsonCodec {

// if all cases are CaseClass0, decode as String
if (schema.annotations.exists(_.isInstanceOf[simpleEnum])) {
if (!cases.forall(_.schema.isInstanceOf[Schema.CaseClass0[_]])) throw new Throwable("@simpleEnum annotation should not be added to non-simple enumerations")
if (!cases.forall(_.schema.isInstanceOf[Schema.CaseClass0[_]]))
throw new Throwable("@simpleEnum annotation should not be added to non-simple enumerations")
val caseMap: Map[String, Z] =
cases.map(case_ => case_.id -> case_.schema.asInstanceOf[Schema.CaseClass0[Z]].defaultConstruct()).toMap
ZJsonDecoder.string.mapOrFail(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1769,8 +1769,9 @@ object JsonCodecSpec extends ZIOSpecDefault {
sealed trait NonSimpleEnum

object NonSimpleEnum {
case class Leaf(a: Int) extends NonSimpleEnum
implicit val schema: zio.schema.Schema[NonSimpleEnum] = zio.schema.DeriveSchema.gen[NonSimpleEnum].annotate(simpleEnum())
case class Leaf(a: Int) extends NonSimpleEnum
implicit val schema: zio.schema.Schema[NonSimpleEnum] =
zio.schema.DeriveSchema.gen[NonSimpleEnum].annotate(simpleEnum())
}

sealed trait Color
Expand Down

0 comments on commit 6c531a8

Please sign in to comment.