From 193b47fa78ed0f3aa323294bd1dad4926bb12b98 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sat, 16 Sep 2023 14:17:53 +0200 Subject: [PATCH 1/2] de-duplicate nullable/non-nullable SerialDescriptors after extraction --- .../core/SerializerDescriptorsExtractor.kt | 9 +-- .../SerializerDescriptorsExtractorTest.kt | 59 ++++++++++++++----- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractor.kt b/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractor.kt index d5b01c6b..32db4d08 100644 --- a/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractor.kt +++ b/modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractor.kt @@ -2,12 +2,7 @@ package dev.adamko.kxstsgen.core import dev.adamko.kxstsgen.core.util.MutableMapWithDefaultPut import kotlinx.serialization.KSerializer -import kotlinx.serialization.descriptors.PolymorphicKind -import kotlinx.serialization.descriptors.PrimitiveKind -import kotlinx.serialization.descriptors.SerialDescriptor -import kotlinx.serialization.descriptors.SerialKind -import kotlinx.serialization.descriptors.StructureKind -import kotlinx.serialization.descriptors.elementDescriptors +import kotlinx.serialization.descriptors.* /** @@ -26,6 +21,8 @@ fun interface SerializerDescriptorsExtractor { serializer: KSerializer<*> ): Set { return extractDescriptors(serializer.descriptor) + .distinctBy { it.nullable } + .toSet() } diff --git a/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt b/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt index 4c1dd3b5..8fd07691 100644 --- a/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt +++ b/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt @@ -5,6 +5,7 @@ import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder import kotlinx.serialization.Serializable import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.descriptors.SerialDescriptor class SerializerDescriptorsExtractorTest : FunSpec({ @@ -18,14 +19,7 @@ class SerializerDescriptorsExtractorTest : FunSpec({ val actual = SerializerDescriptorsExtractor.Default(Example1.Parent.serializer()) - withClue( - """ - expected: ${expected.map { it.serialName }.sorted().joinToString()} - actual: ${actual.map { it.serialName }.sorted().joinToString()} - """.trimIndent() - ) { - actual shouldContainExactlyInAnyOrder expected - } + actual shouldHaveDescriptors expected } test("Example2: given parent class, expect subclass property descriptor extracted") { @@ -38,17 +32,36 @@ class SerializerDescriptorsExtractorTest : FunSpec({ val actual = SerializerDescriptorsExtractor.Default(Example2.Parent.serializer()) - withClue( - """ + actual shouldHaveDescriptors expected + } + + test("Example3: expect nullable/non-nullable SerialDescriptors be de-duplicated") { + + val expected = listOf( + Example3.SealedType.serializer().descriptor, + Example3.TypeHolder.serializer().descriptor, + String.serializer().descriptor, + ) + + val actual = SerializerDescriptorsExtractor.Default(Example3.TypeHolder.serializer()) + + actual shouldHaveDescriptors expected + } +}) { + companion object { + private infix fun Collection.shouldHaveDescriptors(expected: Collection) { + val actual = this + withClue( + """ expected: ${expected.map { it.serialName }.sorted().joinToString()} actual: ${actual.map { it.serialName }.sorted().joinToString()} """.trimIndent() - ) { - actual shouldContainExactlyInAnyOrder expected + ) { + actual shouldContainExactlyInAnyOrder expected + } } } - -}) +} @Suppress("unused") @@ -78,3 +91,21 @@ private object Example2 { @Serializable class SubClass1(val n: Nested) : SealedSub() } + +@Suppress("unused") +private object Example3 { + + @Serializable + sealed class SealedType { + @Serializable + class A(val a: String) : SealedType() + @Serializable + class B(val b: String) : SealedType() + } + + @Serializable + class TypeHolder( + val required: SealedType, + val optional: SealedType?, + ) +} From 25013feb00834a51b69b5ed11ec2b2816bf6ac4d Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sun, 17 Sep 2023 10:23:39 +0200 Subject: [PATCH 2/2] simplify test example --- .../SerializerDescriptorsExtractorTest.kt | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt b/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt index 8fd07691..83e7ab77 100644 --- a/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt +++ b/modules/kxs-ts-gen-core/src/commonTest/kotlin/dev/adamko/kxstsgen/core/SerializerDescriptorsExtractorTest.kt @@ -19,7 +19,7 @@ class SerializerDescriptorsExtractorTest : FunSpec({ val actual = SerializerDescriptorsExtractor.Default(Example1.Parent.serializer()) - actual shouldHaveDescriptors expected + actual shouldContainDescriptors expected } test("Example2: given parent class, expect subclass property descriptor extracted") { @@ -32,30 +32,30 @@ class SerializerDescriptorsExtractorTest : FunSpec({ val actual = SerializerDescriptorsExtractor.Default(Example2.Parent.serializer()) - actual shouldHaveDescriptors expected + actual shouldContainDescriptors expected } test("Example3: expect nullable/non-nullable SerialDescriptors be de-duplicated") { val expected = listOf( - Example3.SealedType.serializer().descriptor, + Example3.SomeType.serializer().descriptor, Example3.TypeHolder.serializer().descriptor, String.serializer().descriptor, ) val actual = SerializerDescriptorsExtractor.Default(Example3.TypeHolder.serializer()) - actual shouldHaveDescriptors expected + actual shouldContainDescriptors expected } }) { companion object { - private infix fun Collection.shouldHaveDescriptors(expected: Collection) { + private infix fun Collection.shouldContainDescriptors(expected: Collection) { val actual = this withClue( """ - expected: ${expected.map { it.serialName }.sorted().joinToString()} - actual: ${actual.map { it.serialName }.sorted().joinToString()} - """.trimIndent() + expected: ${expected.map { it.serialName }.sorted().joinToString()} + actual: ${actual.map { it.serialName }.sorted().joinToString()} + """.trimIndent() ) { actual shouldContainExactlyInAnyOrder expected } @@ -92,20 +92,16 @@ private object Example2 { class SubClass1(val n: Nested) : SealedSub() } + @Suppress("unused") private object Example3 { @Serializable - sealed class SealedType { - @Serializable - class A(val a: String) : SealedType() - @Serializable - class B(val b: String) : SealedType() - } + class SomeType(val a: String) @Serializable class TypeHolder( - val required: SealedType, - val optional: SealedType?, + val required: SomeType, + val optional: SomeType?, ) }