From dd2ce34a9f981575da0d559509848094a8169800 Mon Sep 17 00:00:00 2001 From: augustnagro Date: Mon, 24 Apr 2023 11:16:37 -0700 Subject: [PATCH 1/3] Always put a '@type' key in ADT conversion This simplifies the conversion code and developer expectations --- .../nativeconverter/NativeConverter.scala | 55 +------------------ js/src/test/scala/JsonTests.scala | 8 +-- 2 files changed, 5 insertions(+), 58 deletions(-) diff --git a/js/src/main/scala/org/getshaka/nativeconverter/NativeConverter.scala b/js/src/main/scala/org/getshaka/nativeconverter/NativeConverter.scala index cf182ac..66dc802 100644 --- a/js/src/main/scala/org/getshaka/nativeconverter/NativeConverter.scala +++ b/js/src/main/scala/org/getshaka/nativeconverter/NativeConverter.scala @@ -541,7 +541,7 @@ object NativeConverter: val resArr = Array.ofDim[Any](constValue[Tuple.Size[Mets]]) nativeToProduct[A, Mets, Mels](p, resArr, ps, jsDict) - case s: Mirror.SumOf[A] => sumConverter[A, Mets](s) + case s: Mirror.SumOf[A] => adtSumConverter[A](s) private inline def productToNative[Mets, Mels]( p: Product, @@ -583,59 +583,6 @@ object NativeConverter: resArr(i) = nc.fromNative(ps.atKey(key, elementJs)) nativeToProduct[A, metsTail, melsTail](mirror, resArr, ps, jsDict, i + 1) - private inline def sumConverter[A, Mets]( - m: Mirror.SumOf[A] - ): NativeConverter[A] = - inline erasedValue[Mets] match - case _: (met *: metsTail) => - inline if isSingleton[met] then sumConverter[A, metsTail](m) - else adtSumConverter(m) - - case _: EmptyTuple => simpleSumConverter(m) - - /** A singleton is a Product with no parameter elements - */ - private inline def isSingleton[T]: Boolean = summonFrom[T] { - case product: Mirror.ProductOf[T] => - inline erasedValue[product.MirroredElemTypes] match - case _: EmptyTuple => true - case _ => false - case _ => false - } - - private inline def simpleSumConverter[A]( - m: Mirror.SumOf[A] - ): NativeConverter[A] = - type Mets = m.MirroredElemTypes - type Mels = m.MirroredElemLabels - type Label = m.MirroredLabel - - new NativeConverter[A]: - extension (a: A) def toNative: js.Any = simpleSumToNative[Mels](m.ordinal(a)) - def fromNative(ps: ParseState): A = - val name = ps.json.asInstanceOf[Any] match - case s: String => s - case _ => ps.fail("String") - simpleSumFromNative[A, Label, Mets, Mels](ps, name) - - private inline def simpleSumToNative[Mels](n: Int, i: Int = 0): js.Any = - inline erasedValue[Mels] match - case _: EmptyTuple => // can never reach - case _: (mel *: melsTail) => - if i == n then constValue[mel].asInstanceOf[js.Any] - else simpleSumToNative[melsTail](n, i + 1) - - private inline def simpleSumFromNative[A, Label, Mets, Mels]( - ps: ParseState, - name: String - ): A = - inline (erasedValue[Mets], erasedValue[Mels]) match - case _: (EmptyTuple, EmptyTuple) => - ps.fail(s"a member of Sum type ${constValue[Label]}") - case _: (met *: metsTail, mel *: melsTail) => - if constValue[mel] == name then summonInline[Mirror.ProductOf[met & A]].fromProduct(EmptyTuple) - else simpleSumFromNative[A, Label, metsTail, melsTail](ps, name) - private inline def adtSumConverter[A]( m: Mirror.SumOf[A] ): NativeConverter[A] = diff --git a/js/src/test/scala/JsonTests.scala b/js/src/test/scala/JsonTests.scala index 5c9ccbf..7f545dc 100644 --- a/js/src/test/scala/JsonTests.scala +++ b/js/src/test/scala/JsonTests.scala @@ -145,11 +145,11 @@ class JsonTests: @Test def jsonSimpleEnumTest: Unit = - assertEquals(""" "Adams" """.trim, JSON.stringify(Founder.Adams.toNative)) - assertEquals(Founder.Washington, """ "Washington" """.fromJson[Founder]) + assertEquals(""" {"@type":"Adams"} """.trim, JSON.stringify(Founder.Adams.toNative)) + assertEquals(Founder.Washington, """ {"@type":"Washington"} """.fromJson[Founder]) - assertEquals(""" "Blue" """.trim, JSON.stringify(Color.Blue.toNative)) - assertEquals(0x00ff00, """ "Green" """.fromJson[Color].rgb) + assertEquals(""" {"@type":"Blue"} """.trim, JSON.stringify(Color.Blue.toNative)) + assertEquals(0x00ff00, """ {"@type":"Green"} """.fromJson[Color].rgb) assertEquals(""" {"@type":"Nn"} """.trim, JSON.stringify(Opt.Nn.toNative)) try From b2533966af2b0b87b51176f88ff923285a8fc5af Mon Sep 17 00:00:00 2001 From: augustnagro Date: Sun, 28 May 2023 20:00:12 -0700 Subject: [PATCH 2/3] upgrade to scala 3.3.0 --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index c0b8381..68bf210 100644 --- a/build.sbt +++ b/build.sbt @@ -9,7 +9,7 @@ lazy val root = crossProject(JVMPlatform, JSPlatform) name := "native-converter", version := "0.8.1-SNAPSHOT", versionScheme := Some("early-semver"), - scalaVersion := "3.2.2", + scalaVersion := "3.3.0", // publishing settings homepage := Some(url("https://github.com/getshaka-org/native-converter")), From 10a14e0686d24b2a3dd1868ea93cc8c43422300e Mon Sep 17 00:00:00 2001 From: augustnagro Date: Sun, 28 May 2023 21:20:06 -0700 Subject: [PATCH 3/3] v0.9.0 --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 68bf210..a2d7d97 100644 --- a/build.sbt +++ b/build.sbt @@ -7,7 +7,7 @@ lazy val root = crossProject(JVMPlatform, JSPlatform) .settings( organization := "org.getshaka", name := "native-converter", - version := "0.8.1-SNAPSHOT", + version := "0.9.0", versionScheme := Some("early-semver"), scalaVersion := "3.3.0",