From 6676045bfc1efaa14cb62c5a89ebdbdf0cee848c Mon Sep 17 00:00:00 2001 From: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> Date: Fri, 28 Jun 2024 19:51:36 +0200 Subject: [PATCH] Generate in Scala 3 macro typeId without type parameters (#694) --- .../shared/src/main/scala-3/zio/schema/DeriveSchema.scala | 6 +++--- .../shared/src/test/scala/zio/schema/DeriveSpec.scala | 3 +++ .../test/scala-3/zio/schema/codec/DefaultValueSpec.scala | 4 ++-- .../src/main/scala/zio/schema/SchemaPlatformSpecific.scala | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) 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..4880ae6e4 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 @@ -121,7 +121,7 @@ private case class DeriveSchema()(using val ctx: Quotes) { val selfRefSymbol = Symbol.newVal(Symbol.spliceOwner, s"derivedSchema${stack.size}", TypeRepr.of[Schema[T]], Flags.Lazy, Symbol.noSymbol) val selfRef = Ref(selfRefSymbol) - val typeInfo = '{TypeId.parse(${Expr(TypeRepr.of[T].show)})} + val typeInfo = '{TypeId.parse(${Expr(TypeRepr.of[T].classSymbol.get.fullName.replaceAll("\\$", ""))})} val isEnumCase = Type.of[T] match { case '[reflect.Enum] => true case _ => false @@ -196,7 +196,7 @@ private case class DeriveSchema()(using val ctx: Quotes) { else TypeRepr.of[T].typeSymbol.annotations.filter (filterAnnotation).map (_.asExpr) val annotations = '{ zio.Chunk.fromIterable(${Expr.ofSeq(annotationExprs)}) ++ zio.Chunk.fromIterable(${Expr.ofSeq(docAnnotationExpr)}) } - val typeInfo = '{TypeId.parse(${Expr(TypeRepr.of[T].show)})} + val typeInfo = '{TypeId.parse(${Expr(TypeRepr.of[T].classSymbol.get.fullName.replaceAll("\\$", ""))})} val applied = if (labels.length <= 22) { @@ -374,7 +374,7 @@ private case class DeriveSchema()(using val ctx: Quotes) { } val annotations = '{ zio.Chunk.fromIterable(${Expr.ofSeq(annotationExprs)}) ++ zio.Chunk.fromIterable(${Expr.ofSeq(docAnnotationExpr.toList)}) } - val typeInfo = '{TypeId.parse(${Expr(TypeRepr.of[T].show)})} + val typeInfo = '{TypeId.parse(${Expr(TypeRepr.of[T].classSymbol.get.fullName.replaceAll("\\$", ""))})} val applied = if (cases.length <= 22) { val args = List(typeInfo) ++ cases :+ annotations diff --git a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSpec.scala b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSpec.scala index e82bac1f3..f034352b4 100644 --- a/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSpec.scala +++ b/zio-schema-derivation/shared/src/test/scala/zio/schema/DeriveSpec.scala @@ -184,6 +184,9 @@ import zio.{ Chunk, Scope } .fields(0) .annotations assertTrue( + capturedSchema.schema.asInstanceOf[Schema.Record[GenericRecordWithDefaultValue[Int]]].id == TypeId.parse( + "zio.schema.DeriveSpec.GenericRecordWithDefaultValue" + ), annotations.exists { a => a.isInstanceOf[fieldDefaultValue[_]] && a.asInstanceOf[fieldDefaultValue[Option[Int]]].value == None diff --git a/zio-schema-json/shared/src/test/scala-3/zio/schema/codec/DefaultValueSpec.scala b/zio-schema-json/shared/src/test/scala-3/zio/schema/codec/DefaultValueSpec.scala index cd65b6a00..4b2dd94bc 100644 --- a/zio-schema-json/shared/src/test/scala-3/zio/schema/codec/DefaultValueSpec.scala +++ b/zio-schema-json/shared/src/test/scala-3/zio/schema/codec/DefaultValueSpec.scala @@ -12,7 +12,7 @@ object DefaultValueSpec extends ZIOSpecDefault { def spec: Spec[TestEnvironment, Any] = suite("Custom Spec")( - customSuite, + customSuite ) @@ timeout(90.seconds) private val customSuite = suite("custom")( @@ -24,7 +24,7 @@ object DefaultValueSpec extends ZIOSpecDefault { ) ) - case class WithDefaultValue(orderId:Int, description: String = "desc") + case class WithDefaultValue(orderId: Int, description: String = "desc") object WithDefaultValue { implicit lazy val schema: Schema[WithDefaultValue] = DeriveSchema.gen[WithDefaultValue] diff --git a/zio-schema/jvm/src/main/scala/zio/schema/SchemaPlatformSpecific.scala b/zio-schema/jvm/src/main/scala/zio/schema/SchemaPlatformSpecific.scala index f256da2da..faef5f72d 100644 --- a/zio-schema/jvm/src/main/scala/zio/schema/SchemaPlatformSpecific.scala +++ b/zio-schema/jvm/src/main/scala/zio/schema/SchemaPlatformSpecific.scala @@ -6,7 +6,7 @@ trait SchemaPlatformSpecific { Schema[String].transformOrFail( string => try { - Right(new java.net.URL(string)) + Right(new java.net.URI(string).toURL) } catch { case _: Exception => Left(s"Invalid URL: $string") }, url => Right(url.toString) )