-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add YamlContentPolymorphicSerializer
, similar to JsonContentPolymorphicSerializer
#607
Merged
charleskorn
merged 10 commits into
charleskorn:main
from
Jojo4GH:content-polymorphic-serializer
Nov 14, 2024
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f2a231a
Fix Decoder not representing the current node correctly
Jojo4GH f01905c
Merge branch 'content-poly-bug2' into content-polymorphic-serializer
Jojo4GH 133edeb
Add YamlContentPolymorphicSerializer
Jojo4GH db09deb
Merge branch 'main' into content-polymorphic-serializer
Jojo4GH 91568e1
Shorten resulting path length in tests
Jojo4GH 521df2e
Fix ktlint
Jojo4GH e788b11
Fix spotless
Jojo4GH ae51605
Fix wasmJsBrowserTest
Jojo4GH 3f8e136
Rename to YamlContentPolymorphicSerializerTest.kt
Jojo4GH 126ef08
Added YamlContentPolymorphicSerializer.Marker annotation
Jojo4GH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/commonMain/kotlin/com/charleskorn/kaml/YamlContentPolymorphicSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
|
||
Copyright 2018-2023 Charles Korn. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
https://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
*/ | ||
|
||
package com.charleskorn.kaml | ||
|
||
import kotlinx.serialization.DeserializationStrategy | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.InternalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.descriptors.PolymorphicKind | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.serializerOrNull | ||
import kotlin.reflect.KClass | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
public abstract class YamlContentPolymorphicSerializer<T : Any>(private val baseClass: KClass<T>) : KSerializer<T> { | ||
@OptIn(InternalSerializationApi::class) | ||
override val descriptor: SerialDescriptor = buildSerialDescriptor( | ||
"${YamlContentPolymorphicSerializer::class.simpleName}<${baseClass.simpleName}>", | ||
PolymorphicKind.SEALED, | ||
) | ||
|
||
@OptIn(InternalSerializationApi::class) | ||
override fun serialize(encoder: Encoder, value: T) { | ||
val actualSerializer = encoder.serializersModule.getPolymorphic(baseClass, value) | ||
?: value::class.serializerOrNull() | ||
?: throwSubtypeNotRegistered(value::class, baseClass) | ||
@Suppress("UNCHECKED_CAST") | ||
(actualSerializer as KSerializer<T>).serialize(encoder, value) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): T { | ||
return decoder.decodeSerializableValue(selectDeserializer((decoder as YamlInput).node)) | ||
} | ||
|
||
public abstract fun selectDeserializer(node: YamlNode): DeserializationStrategy<T> | ||
|
||
private fun throwSubtypeNotRegistered(subClass: KClass<*>, baseClass: KClass<*>): Nothing { | ||
val subClassName = subClass.simpleName ?: "$subClass" | ||
throw SerializationException( | ||
""" | ||
Class '$subClassName' is not registered for polymorphic serialization in the scope of '${baseClass.simpleName}'. | ||
Mark the base class as 'sealed' or register the serializer explicitly. | ||
""".trimIndent(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
src/commonTest/kotlin/com/charleskorn/kaml/YamlContentPolymorphicSerializer.kt
Jojo4GH marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
|
||
Copyright 2018-2023 Charles Korn. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
https://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
*/ | ||
|
||
package com.charleskorn.kaml | ||
|
||
import com.charleskorn.kaml.testobjects.TestSealedStructure | ||
import com.charleskorn.kaml.testobjects.polymorphicModule | ||
import io.kotest.assertions.asClue | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.serialization.DeserializationStrategy | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.builtins.nullable | ||
|
||
class YamlContentPolymorphicSerializerTest : FlatFunSpec({ | ||
context("a YAML parser") { | ||
context("parsing polymorphic values with PolymorphismStyle.None") { | ||
val polymorphicYaml = Yaml( | ||
serializersModule = polymorphicModule, | ||
configuration = YamlConfiguration(polymorphismStyle = PolymorphismStyle.None), | ||
) | ||
|
||
context("given some input where the value should be a sealed class") { | ||
val input = """ | ||
value: "asdfg" | ||
""".trimIndent() | ||
|
||
val result = polymorphicYaml.decodeFromString(TestSealedStructureBasedOnContentSerializer, input) | ||
|
||
test("deserializes it to a Kotlin object") { | ||
result shouldBe TestSealedStructure.SimpleSealedString("asdfg") | ||
} | ||
} | ||
|
||
context("given some input where the value should be a sealed class (inline)") { | ||
val input = """ | ||
"abcdef" | ||
""".trimIndent() | ||
|
||
val result = polymorphicYaml.decodeFromString(TestSealedStructureBasedOnContentSerializer, input) | ||
|
||
test("deserializes it to a Kotlin object") { | ||
result shouldBe TestSealedStructure.InlineSealedString("abcdef") | ||
} | ||
} | ||
|
||
context("given some input missing without the serializer") { | ||
val input = """ | ||
value: "asdfg" | ||
""".trimIndent() | ||
|
||
test("throws an exception with the correct location information") { | ||
val exception = shouldThrow<IncorrectTypeException> { | ||
polymorphicYaml.decodeFromString(TestSealedStructure.serializer(), input) | ||
} | ||
|
||
exception.asClue { | ||
it.message shouldBe "Encountered a polymorphic map descriptor but PolymorphismStyle is 'None'" | ||
it.line shouldBe 1 | ||
it.column shouldBe 1 | ||
it.path shouldBe YamlPath.root | ||
} | ||
} | ||
} | ||
|
||
context("given some input representing a list of polymorphic objects") { | ||
val input = """ | ||
- value: null | ||
- value: -987 | ||
- value: 654 | ||
- "testing" | ||
- value: "tests" | ||
""".trimIndent() | ||
|
||
val result = polymorphicYaml.decodeFromString( | ||
ListSerializer(TestSealedStructureBasedOnContentSerializer), | ||
input, | ||
) | ||
|
||
test("deserializes it to a Kotlin object") { | ||
result shouldBe listOf( | ||
TestSealedStructure.SimpleSealedString(null), | ||
TestSealedStructure.SimpleSealedInt(-987), | ||
TestSealedStructure.SimpleSealedInt(654), | ||
TestSealedStructure.InlineSealedString("testing"), | ||
TestSealedStructure.SimpleSealedString("tests"), | ||
) | ||
} | ||
} | ||
|
||
context("given some input with a tag and a type property") { | ||
val input = """ | ||
!<sealedInt> | ||
kind: sealedString | ||
value: "asdfg" | ||
""".trimIndent() | ||
|
||
test("throws an exception with the correct location information") { | ||
val exception = shouldThrow<IncorrectTypeException> { | ||
polymorphicYaml.decodeFromString(TestSealedStructureBasedOnContentSerializer, input) | ||
} | ||
|
||
exception.asClue { | ||
it.message shouldBe "Encountered a tagged polymorphic descriptor but PolymorphismStyle is 'None'" | ||
it.line shouldBe 1 | ||
it.column shouldBe 1 | ||
it.path shouldBe YamlPath.root | ||
} | ||
} | ||
} | ||
} | ||
} | ||
context("a YAML serializer") { | ||
context("serializing polymorphic values with custom serializer") { | ||
val polymorphicYaml = Yaml( | ||
serializersModule = polymorphicModule, | ||
configuration = YamlConfiguration(polymorphismStyle = PolymorphismStyle.Tag), | ||
) | ||
|
||
context("serializing a sealed type") { | ||
val input = TestSealedStructure.SimpleSealedInt(5) | ||
val output = polymorphicYaml.encodeToString(TestSealedStructureBasedOnContentSerializer, input) | ||
val expectedYaml = """ | ||
value: 5 | ||
""".trimIndent() | ||
|
||
test("returns the value serialized in the expected YAML form") { | ||
output shouldBe expectedYaml | ||
} | ||
} | ||
|
||
context("serializing a list of polymorphic values") { | ||
val input = listOf( | ||
TestSealedStructure.SimpleSealedInt(5), | ||
TestSealedStructure.SimpleSealedString("some test"), | ||
TestSealedStructure.SimpleSealedInt(-20), | ||
TestSealedStructure.InlineSealedString("testing"), | ||
TestSealedStructure.SimpleSealedString(null), | ||
null, | ||
) | ||
|
||
val output = polymorphicYaml.encodeToString( | ||
ListSerializer(TestSealedStructureBasedOnContentSerializer.nullable), | ||
input, | ||
) | ||
|
||
val expectedYaml = """ | ||
- value: 5 | ||
- value: "some test" | ||
- value: -20 | ||
- "testing" | ||
- value: null | ||
- null | ||
""".trimIndent() | ||
|
||
test("returns the value serialized in the expected YAML form") { | ||
output shouldBe expectedYaml | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
object TestSealedStructureBasedOnContentSerializer : YamlContentPolymorphicSerializer<TestSealedStructure>( | ||
TestSealedStructure::class, | ||
) { | ||
override fun selectDeserializer(node: YamlNode): DeserializationStrategy<TestSealedStructure> = when (node) { | ||
is YamlScalar -> TestSealedStructure.InlineSealedString.serializer() | ||
is YamlMap -> when (val value: YamlNode? = node["value"]) { | ||
is YamlScalar -> when { | ||
value.content.toIntOrNull() == null -> TestSealedStructure.SimpleSealedString.serializer() | ||
else -> TestSealedStructure.SimpleSealedInt.serializer() | ||
} | ||
is YamlNull -> TestSealedStructure.SimpleSealedString.serializer() | ||
else -> throw SerializationException("Unsupported property type for TestSealedStructure.value: ${value?.let { it::class.simpleName}}") | ||
} | ||
else -> throw SerializationException("Unsupported node type for TestSealedStructure: ${node::class.simpleName}") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than checking the name like this, what if we introduced an annotation that is added to the
SerialDescriptor
created byYamlContentPolymorphicSerializer
, then checked for its presence here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The annotation is implemented as
internal
for now. Or should it bepublic
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
internal
seems fine to me, it's an implementation detail.