Skip to content

Commit

Permalink
Decode polymorphic using elementName rather than index
Browse files Browse the repository at this point in the history
More in line with old code and probably more stable since we're dealing with maps
  • Loading branch information
Daeda88 committed Jan 29, 2024
1 parent 1995071 commit 87d4ae2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ actual fun getPolymorphicType(value: Any?, discriminator: String): String =
private fun FirebaseDecoder.decodeAsMap(isNestedPolymorphic: Boolean): CompositeDecoder = (value as? Map<*, *>).orEmpty().let { map ->
FirebaseClassDecoder(map.size, settings, { map.containsKey(it) }) { desc, index ->
if (isNestedPolymorphic) {
if (index == 0)
map[desc.getElementName(index)]
else {
if (desc.getElementName(index) == "value")
map
else {
map[desc.getElementName(index)]
}
} else {
map[desc.getElementName(index)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ data class GenericClass<T>(

@Serializable
abstract class AbstractClass {
abstract val value: String
abstract val abstractValue: String
}

@Serializable
@SerialName("implemented")
data class ImplementedClass(override val value: String, val otherValue: Boolean) : AbstractClass()
data class ImplementedClass(override val abstractValue: String, val otherValue: Boolean) : AbstractClass()

@Serializable
data class NestedClass(
Expand Down Expand Up @@ -170,7 +170,7 @@ class EncodersTest {
serializersModule = module
}

nativeAssertEquals(nativeMapOf("type" to "implemented", "value" to "value", "otherValue" to true), encoded)
nativeAssertEquals(nativeMapOf("type" to "implemented", "abstractValue" to "value", "otherValue" to true), encoded)

val decoded = decode(AbstractClass.serializer(), encoded) {
serializersModule = module
Expand All @@ -197,7 +197,7 @@ class EncodersTest {

val testDataEncoded = nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to null, "valueClass" to 42)
val sealedEncoded = nativeMapOf("type" to "test", "value" to "value")
val abstractEncoded = nativeMapOf("type" to "implemented", "value" to "value", "otherValue" to true)
val abstractEncoded = nativeMapOf("type" to "implemented", "abstractValue" to "value", "otherValue" to true)
nativeAssertEquals(
nativeMapOf(
"testData" to testDataEncoded,
Expand Down Expand Up @@ -321,16 +321,16 @@ class EncodersTest {

val reencoded = reencodeTransformation(
AbstractClass.serializer(),
nativeMapOf("type" to "implemented", "value" to "value", "otherValue" to true),
nativeMapOf("type" to "implemented", "abstractValue" to "value", "otherValue" to true),
builder = {
serializersModule = module
}
) {
assertEquals(ImplementedClass("value", true), it)
ImplementedClass("new-${it.value}", false)
ImplementedClass("new-${it.abstractValue}", false)
}

nativeAssertEquals(nativeMapOf("type" to "implemented", "value" to "new-value", "otherValue" to false), reencoded)
nativeAssertEquals(nativeMapOf("type" to "implemented", "abstractValue" to "new-value", "otherValue" to false), reencoded)
}

@Test
Expand Down Expand Up @@ -360,7 +360,7 @@ class EncodersTest {

val testDataEncoded = nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to null, "valueClass" to 42)
val sealedEncoded = nativeMapOf("type" to "test", "value" to "value")
val abstractEncoded = nativeMapOf("type" to "implemented", "value" to "value", "otherValue" to true)
val abstractEncoded = nativeMapOf("type" to "implemented", "abstractValue" to "value", "otherValue" to true)
nativeAssertEquals(
nativeMapOf(
"testData" to testDataEncoded,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ private fun FirebaseDecoder.decodeAsList(): CompositeDecoder = (value as? List<*
private fun FirebaseDecoder.decodeAsMap(isNestedPolymorphic: Boolean): CompositeDecoder = (value as? Map<*, *>).orEmpty().let { map ->
FirebaseClassDecoder(map.size, settings, { map.containsKey(it) }) { desc, index ->
if (isNestedPolymorphic) {
if (index == 0)
map[desc.getElementName(index)]
else {
if (desc.getElementName(index) == "value")
map
else {
map[desc.getElementName(index)]
}
} else {
map[desc.getElementName(index)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ private fun FirebaseDecoder.decodeAsList(): CompositeDecoder = (value as Array<*
private fun FirebaseDecoder.decodeAsMap(isNestedPolymorphic: Boolean): CompositeDecoder = (value as Json).let { json ->
FirebaseClassDecoder(js("Object").keys(value).length as Int, settings, { json[it] != undefined }) { desc, index ->
if (isNestedPolymorphic) {
if (index == 0) {
json[desc.getElementName(index)]
} else {
if (desc.getElementName(index) == "value") {
json
} else {
json[desc.getElementName(index)]
}
} else {
json[desc.getElementName(index)]
Expand Down

0 comments on commit 87d4ae2

Please sign in to comment.