Skip to content

Commit 496be9b

Browse files
committed
added test cases for polymorphic serialization
1 parent d10634a commit 496be9b

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<p align="center">
22
<a href="https://eignex.com/">
3-
<img src="https://raw.githubusercontent.com/Eignex/.github/refs/heads/main/profile/banner.svg" style="max-width: 100%; width: 22em" />
3+
<img src="https://raw.githubusercontent.com/Eignex/.github/refs/heads/main/profile/banner.svg" style="max-width: 100%; width: 22em" alt="Eignex">
44
</a>
55
</p>
66

@@ -85,7 +85,9 @@ payloads for Kotlin classes by moving structural metadata into a compact header.
8585
* VarInts: Int/Long fields can be optimized using @VarUInt or @VarInt (ZigZag)
8686
annotations.
8787
* Full Graph Support: Handles nested objects, lists, maps, and polymorphism
88-
recursively.
88+
recursively. While this is supported it will not produce as compact
89+
representations as flat structures that can pack all metadata into the same
90+
header.
8991

9092
### Field layout
9193

src/test/kotlin/com/eignex/kencode/PackedFormatTest.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,27 @@ class PackedFormatTest {
147147
),
148148
nestedLists = listOf(listOf(listOf(1, 2), listOf(3))),
149149
optionalDeepMap = mapOf(42 to null)
150-
)
150+
),
151+
152+
DeepBreadth(
153+
branchA = Level1(true, Level2("data-a", listOf(listOf(1)))),
154+
branchB = Level1(false, null),
155+
branchC = Level1(true, Level2("data-c", emptyList())),
156+
rootValue = 999
157+
),
158+
159+
PolymorphicContainer(
160+
main = PolymorphicBase.Action("Login", 1),
161+
history = listOf(
162+
PolymorphicBase.Notification("Welcome!", false),
163+
PolymorphicBase.Heartbeat,
164+
PolymorphicBase.Action("Update", 2),
165+
PolymorphicBase.Notification("Hello", true),
166+
)
167+
),
168+
169+
PolymorphicBase.Heartbeat,
170+
PolymorphicBase.Action("Standalone", 0)
151171
)
152172

153173
testData.forEach { value ->

src/test/kotlin/com/eignex/kencode/TestClasses.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,19 @@ data class MultiLevelCollections(
229229
val nestedLists: List<List<List<Int>>>,
230230
val optionalDeepMap: Map<Int, Map<String, Boolean>?>?
231231
)
232+
233+
@Serializable
234+
sealed class PolymorphicBase {
235+
@Serializable
236+
data class Action(val name: String, val priority: Int) : PolymorphicBase()
237+
@Serializable
238+
data class Notification(val message: String, val silent: Boolean) : PolymorphicBase()
239+
@Serializable
240+
object Heartbeat : PolymorphicBase()
241+
}
242+
243+
@Serializable
244+
data class PolymorphicContainer(
245+
val main: PolymorphicBase,
246+
val history: List<PolymorphicBase>
247+
)

0 commit comments

Comments
 (0)