From 49304395c275299c8506989074b707960c7352d0 Mon Sep 17 00:00:00 2001 From: Thijs Broersen <4889512+ThijsBroersen@users.noreply.github.com> Date: Mon, 17 Jun 2024 07:16:22 +0200 Subject: [PATCH] refactor: scala 3 const enum: simplify and cleanup (#1126) --- .../zio/json/JsonDecoderVersionSpecific.scala | 8 ------- .../zio/json/JsonEncoderVersionSpecific.scala | 3 --- .../scala-3/zio/json/union_derivation.scala | 22 ++++--------------- 3 files changed, 4 insertions(+), 29 deletions(-) diff --git a/zio-json/shared/src/main/scala-3/zio/json/JsonDecoderVersionSpecific.scala b/zio-json/shared/src/main/scala-3/zio/json/JsonDecoderVersionSpecific.scala index 64f51eae..ad020b00 100644 --- a/zio-json/shared/src/main/scala-3/zio/json/JsonDecoderVersionSpecific.scala +++ b/zio-json/shared/src/main/scala-3/zio/json/JsonDecoderVersionSpecific.scala @@ -17,12 +17,4 @@ trait DecoderLowPriorityVersionSpecific { case _ => Left("expected one of: " + values.toList.mkString(", ")) } ) - - inline given constStringToEnum[T <: String](using IsConst[T] =:= true): JsonDecoder[T] = - JsonDecoder.string.mapOrFail( - { - case raw if raw == constValue[T] => Right(constValue[T]) - case _ => Left("expected one of: " + constValue[T]) - } - ) } diff --git a/zio-json/shared/src/main/scala-3/zio/json/JsonEncoderVersionSpecific.scala b/zio-json/shared/src/main/scala-3/zio/json/JsonEncoderVersionSpecific.scala index 97d62311..17f501c4 100644 --- a/zio-json/shared/src/main/scala-3/zio/json/JsonEncoderVersionSpecific.scala +++ b/zio-json/shared/src/main/scala-3/zio/json/JsonEncoderVersionSpecific.scala @@ -10,7 +10,4 @@ private[json] trait EncoderLowPriorityVersionSpecific { inline given unionOfStringEnumeration[T](using IsUnionOf[String, T]): JsonEncoder[T] = JsonEncoder.string.asInstanceOf[JsonEncoder[T]] - - inline given constStringToEnum[T <: String](using IsConst[T] =:= true): JsonEncoder[T] = - JsonEncoder.string.narrow[T] } diff --git a/zio-json/shared/src/main/scala-3/zio/json/union_derivation.scala b/zio-json/shared/src/main/scala-3/zio/json/union_derivation.scala index 0195b89c..05309296 100644 --- a/zio-json/shared/src/main/scala-3/zio/json/union_derivation.scala +++ b/zio-json/shared/src/main/scala-3/zio/json/union_derivation.scala @@ -4,22 +4,6 @@ import scala.compiletime.* import scala.deriving.* import scala.quoted.* -@scala.annotation.implicitNotFound("${A} is not a union type") -sealed trait IsUnion[A] - -object IsUnion: - - private val singleton: IsUnion[Any] = new IsUnion[Any] {} - - transparent inline given derived[A]: IsUnion[A] = ${ deriveImpl[A] } - - private def deriveImpl[A](using quotes: Quotes, t: Type[A]): Expr[IsUnion[A]] = - import quotes.reflect.* - val tpe: TypeRepr = TypeRepr.of[A] - tpe.dealias match - case o: OrType => ('{ IsUnion.singleton.asInstanceOf[IsUnion[A]] }).asExprOf[IsUnion[A]] - case other => report.errorAndAbort(s"${tpe.show} is not a Union") - @scala.annotation.implicitNotFound("${A} is not a union of ${T}") sealed trait IsUnionOf[T, A] @@ -44,10 +28,12 @@ object IsUnionOf: else report.errorAndAbort(s"${o.show} is not a subtype of ${bound.show}") tpe.dealias match - case o: OrType => + case o: OrType => validateTypes(o) ('{ IsUnionOf.singleton.asInstanceOf[IsUnionOf[T, A]] }).asExprOf[IsUnionOf[T, A]] - case other => report.errorAndAbort(s"${tpe.show} is not a Union") + case o => + if o <:< bound then ('{ IsUnionOf.singleton.asInstanceOf[IsUnionOf[T, A]] }).asExprOf[IsUnionOf[T, A]] + else report.errorAndAbort(s"${tpe.show} is not a Union") object UnionDerivation: transparent inline def constValueUnionTuple[T, A](using IsUnionOf[T, A]): Tuple = ${ constValueUnionTupleImpl[T, A] }