Skip to content
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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

Jojo4GH
Copy link
Contributor

@Jojo4GH Jojo4GH commented Sep 5, 2024

Example usage:

@Serializable(with = AnimalSerializer::class)
sealed interface Animal {
    @Serializable data class Cat(val isPurring: Boolean)
    @Serializable data class Dog(val isBarking: Boolean)
}

object AnimalSerializer : YamlContentPolymorphicSerializer<Animal>(Animal::class) {
    override fun selectDeserializer(node: YamlNode): DeserializationStrategy<Animal> = when {
        node.yamlMap.getKey("isPurring") != null -> Animal.Cat.serializer()
        node.yamlMap.getKey("isBarking") != null -> Animal.Dog.serializer()
        else -> error("Unknown animal")
    }
}

@Jojo4GH Jojo4GH marked this pull request as draft September 5, 2024 13:17
@Jojo4GH Jojo4GH marked this pull request as ready for review September 6, 2024 23:59
@Jojo4GH
Copy link
Contributor Author

Jojo4GH commented Sep 7, 2024

Optional improvement: Make the YamlNode API a bit more consumer friendly (especially for getting keys in YamlMap)

If this should be implemented in this PR, then I can do it

@charleskorn
Copy link
Owner

  • Fixed bug with YamlInput::node
  • Fixed bug with sealed polymorphism for scalars represented as inline value classes

Could you please elaborate on these? Perhaps they could be separate PRs to help keep this one smaller and easier to review.

Optional improvement: Make the YamlNode API a bit more consumer friendly (especially for getting keys in YamlMap)

If this should be implemented in this PR, then I can do it

Sounds like something for a separate PR.

@Jojo4GH
Copy link
Contributor Author

Jojo4GH commented Sep 8, 2024

Fixed bug with YamlInput::node

Previously the node property on the decoders was not traversed in deep YAML trees (e.g. with YamlMapLikeInputBase and YamlListInput). This behaviour is different from the JSON serialization library and makes it impossible to implement a content based polymorphic KSerializer in a meaningful way without knowing of the whole YAML tree. See also the DecodingFromYamlNodeSerializer in YamlReadingTest.kt for an affected case

Fixed bug with sealed polymorphism for scalars represented as inline value classes

Previously it was not possible to implement a content based polymorphic serializer in the case that one of the types involved is a scalar. Different from the JSON serialization library it was therefore impossible to implement something like:

myCats:
  myFirstCat: "Bella"
  mySecondCat:
    name: "Daisy"
    age: 2
typealias MyCats = Map<String, CatDetails>

@Serializable(with = MyContentBasedSerializer::class)
sealed interface CatDetails {

    @Serializable
    value class InlineName(val name: String) : CatDetails

    @Serializable
    data class More(
        val name: String,
        val age: String?
    ) : CatDetails
}

object MyContentBasedSerializer : YamlContentPolymorphicSerializer<CatDetails>(CatDetails::class) {
    override fun selectDeserializer(node: YamlNode) = when (node) {
        is YamlScalar -> CatDetails.InlineName.serializer()
        is YamlMap -> CatDetails.More.serializer()
        else -> error("Unsupported node type ${node::class.simpleName}")
    }
}

Optional improvement: [...]

Will do it sometime later in a separate PR.

@charleskorn charleskorn linked an issue Sep 15, 2024 that may be closed by this pull request
@charleskorn
Copy link
Owner

charleskorn commented Sep 15, 2024

Could you please split the two bug fixes out as their own separate PR? This is quite a big PR and so breaking it down into smaller pieces will make it easier for me to review.

@Jojo4GH
Copy link
Contributor Author

Jojo4GH commented Sep 16, 2024

Could you please split the two bug fixes out as their own separate PR? This is quite a big PR and so breaking it down into smaller pieces will make it easier for me to review.

This PR now depends on:

@charleskorn charleskorn changed the title Add YamlContentPolymorphicSerializer feat: add YamlContentPolymorphicSerializer Oct 30, 2024
@Jojo4GH
Copy link
Contributor Author

Jojo4GH commented Nov 1, 2024

Could you please split the two bug fixes out as their own separate PR? This is quite a big PR and so breaking it down into smaller pieces will make it easier for me to review.

This PR now depends on:

The necessary PRs are now merged so I merged main for easier review @charleskorn

@charleskorn charleskorn changed the title feat: add YamlContentPolymorphicSerializer feat: add YamlContentPolymorphicSerializer, similar to JsonContentPolymorphicSerializer Nov 3, 2024
@@ -115,6 +128,8 @@ public sealed class YamlInput(
private fun YamlMap.withoutKey(key: String): YamlMap {
return this.copy(entries = entries.filterKeys { it.content != key })
}

private val SerialDescriptor.isContentBasedPolymorphic get() = serialName.startsWith(YamlContentPolymorphicSerializer::class.simpleName!!)
Copy link
Owner

@charleskorn charleskorn Nov 3, 2024

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 by YamlContentPolymorphicSerializer, then checked for its presence here?

Copy link
Contributor Author

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 be public?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Content-based polymorphism
2 participants