-
Notifications
You must be signed in to change notification settings - Fork 18
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
DOCSP-36217: polymorphic serialization #163
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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
22 changes: 22 additions & 0 deletions
22
source/examples/generated/KotlinXSerializationTest.snippet.polymorphic-dataclasses.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,22 @@ | ||
@Serializable | ||
sealed interface Person { | ||
val name: String | ||
} | ||
|
||
@Serializable | ||
data class Student( | ||
@Contextual | ||
@SerialName("_id") | ||
val id: ObjectId, | ||
override val name: String, | ||
val grade: Int, | ||
) : Person | ||
|
||
@Serializable | ||
data class Teacher( | ||
@Contextual | ||
@SerialName("_id") | ||
val id: ObjectId, | ||
override val name: String, | ||
val department: String, | ||
) : Person |
24 changes: 24 additions & 0 deletions
24
source/examples/generated/KotlinXSerializationTest.snippet.polymorphic-example.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,24 @@ | ||
val collection = database.getCollection<Person>("school") | ||
|
||
val teacherDoc = Teacher(ObjectId(), "Vivian Lee", "History") | ||
val studentDoc = Student(ObjectId(), "Kate Parker", 10) | ||
|
||
collection.insertOne(teacherDoc) | ||
collection.insertOne(studentDoc) | ||
|
||
println("Retrieving by using data classes") | ||
collection.withDocumentClass<Teacher>() | ||
.find(Filters.exists("department")) | ||
.first().also { println(it) } | ||
|
||
collection.withDocumentClass<Student>() | ||
.find(Filters.exists("grade")) | ||
.first().also { println(it) } | ||
|
||
println("\nRetrieving by using Person interface") | ||
val resultsFlow = collection.withDocumentClass<Person>().find() | ||
resultsFlow.collect { println(it) } | ||
|
||
println("\nRetrieving as Document type") | ||
val resultsDocFlow = collection.withDocumentClass<Document>().find() | ||
resultsDocFlow.collect { println(it) } |
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
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 | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,7 +9,7 @@ Kotlin Serialization | |||||||||||||||||||||||||
:values: reference | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. meta:: | ||||||||||||||||||||||||||
:keywords: code example, data model, conversion | ||||||||||||||||||||||||||
:keywords: code example, data model, conversion, polymorphism | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. contents:: On this page | ||||||||||||||||||||||||||
:local: | ||||||||||||||||||||||||||
|
@@ -98,6 +98,8 @@ dependencies to your project by using the :guilabel:`Gradle` and | |||||||||||||||||||||||||
<version>{+full-version+}</version> | ||||||||||||||||||||||||||
</dependency> | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. _kotlin-data-class-annotation: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Annotate Data Classes | ||||||||||||||||||||||||||
--------------------- | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
@@ -246,3 +248,56 @@ see the following API documentation: | |||||||||||||||||||||||||
- `KotlinSerializerCodec.create() <{+api+}/apidocs/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-kotlin-serializer-codec/-companion/create.html>`__ | ||||||||||||||||||||||||||
- `BsonConfiguration <{+api+}/apidocs/bson-kotlinx/bson-kotlinx/org.bson.codecs.kotlinx/-bson-configuration/index.html>`__ | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. _kotlin-polymorphic: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Polymorphic Serialization | ||||||||||||||||||||||||||
------------------------- | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
The {+driver-short+} natively supports serialization and deserialization | ||||||||||||||||||||||||||
of polymorphic classes. When you mark a sealed interface and data | ||||||||||||||||||||||||||
classes that inherit that interface with the ``@Serializable`` | ||||||||||||||||||||||||||
annotation, the driver uses a ``KSerializer`` implementation to handle | ||||||||||||||||||||||||||
conversion of your types to and from BSON. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
When you insert an instance of a polymorphic data class into MongoDB, | ||||||||||||||||||||||||||
the driver adds the field ``_t``, the | ||||||||||||||||||||||||||
discriminator field. The value of this field is the data class name. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Polymorphic Data Classes Example | ||||||||||||||||||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
In this example, you create an interface and two data classes that | ||||||||||||||||||||||||||
inherit that interface. In the data classes, the ``id`` field is marked | ||||||||||||||||||||||||||
with the annotations described in the | ||||||||||||||||||||||||||
:ref:`kotlin-data-class-annotation` section: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. literalinclude:: /examples/generated/KotlinXSerializationTest.snippet.polymorphic-dataclasses.kt | ||||||||||||||||||||||||||
:language: kotlin | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Then, you can perform operations with data classes as usual. In the | ||||||||||||||||||||||||||
following example, you parametrize the collection with the ``Person`` | ||||||||||||||||||||||||||
interface, but you can perform operations with the polymorphic classes | ||||||||||||||||||||||||||
``Teacher`` and ``Student``. When you retrieve documents, the driver | ||||||||||||||||||||||||||
automatically detects the type based on the discriminator value and | ||||||||||||||||||||||||||
deserializes them accordingly. | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (these are based on the way we seem to typically introduce examples, let me know if I'm on the wrong track here).
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. io-code-block:: | ||||||||||||||||||||||||||
:copyable: true | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. input:: /examples/generated/KotlinXSerializationTest.snippet.polymorphic-example.kt | ||||||||||||||||||||||||||
:language: kotlin | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. output:: | ||||||||||||||||||||||||||
:language: console | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Retrieving by using data classes | ||||||||||||||||||||||||||
Teacher(id=..., name=Vivian Lee, department=History) | ||||||||||||||||||||||||||
Student(id=..., name=Kate Parker, grade=10) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Retrieving by using Person interface | ||||||||||||||||||||||||||
Teacher(id=..., name=Vivian Lee, department=History) | ||||||||||||||||||||||||||
Student(id=..., name=Kate Parker, grade=10) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Retrieving as Document type | ||||||||||||||||||||||||||
Document{{_id=..., _t=Teacher, name=Vivian Lee, department=History}} | ||||||||||||||||||||||||||
Document{{_id=..., _t=Student, name=Kate Parker, grade=10}} |
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
Oops, something went wrong.
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.
Nit