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

Add decoding from YamlNode #369

Conversation

dellisd
Copy link
Contributor

@dellisd dellisd commented Jan 13, 2023

This adds a method on YamlInput that allows for decoding a serializable value from a YamlNode.

The use case for this kind of functionality is the ability to manually interpret the structure of a YAML document and then delegate the deserialization of YamlNodes to other serializer implementations.

For example, in the test case I added we have this document:

keyA:
    host: A
keyB:
    host: B

which is a map of Database objects. Perhaps we want to decode this as a List<Database> instead of a Map<String, Database> (because reasons) and this enables us to write a custom serializer that looks at each YamlNode entry on the YamlMap and then delegates the deserialization of each Database object to the Database serializer rather than having to manually decode the rest of the Yaml structure.

@charleskorn
Copy link
Owner

Thanks for the contribution @dellisd.

I'm wondering if this would be better suited as a method on Yaml instead of on YamlInput, as the method doesn't strictly require a YamlInput instance. I'm imagining a method like decodeFromNode(node, serializer). What do you think?

Also, could you please add a test case for the scenario where the serializer is decoding something that isn't the root of the document? There might be an edge case there that I'd like to check.

@dellisd
Copy link
Contributor Author

dellisd commented Jan 15, 2023

I agree adding a method to Yaml makes sense, but I think the YamlInput class will also need to be modified to expose the current Yaml instance so that custom settings are preserved and new Yaml instances don't need to be allocated during deserialization. This is similar to what is done with the JsonDecoder implementation.

I can make these changes, and I'll add that test case.

Copy link
Owner

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, just have a couple of questions...

@@ -32,49 +32,50 @@ import kotlinx.serialization.modules.SerializersModule
@OptIn(ExperimentalSerializationApi::class)
public sealed class YamlInput(
public val node: YamlNode,
public val yaml: Yaml,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this yaml: Yaml property required? Is this so that custom deserializers can access it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct. I explained above that the Json implementations in kotlinx.serialization expose the Json instances in the same way.

@charleskorn charleskorn merged commit bd2b8d4 into charleskorn:main Jan 21, 2023
@charleskorn
Copy link
Owner

Thanks @dellisd!

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.

2 participants