Skip to content

Commit be87ed7

Browse files
authored
Merge branch 'GitLiveApp:master' into brad/ios_constructors
2 parents 51b28e6 + 9e9d68b commit be87ed7

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

firebase-common/src/androidMain/kotlin/dev/gitlive/firebase/_decoders.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import kotlinx.serialization.descriptors.StructureKind
1111

1212
actual fun FirebaseDecoder.structureDecoder(descriptor: SerialDescriptor): CompositeDecoder = when(descriptor.kind) {
1313
StructureKind.CLASS, StructureKind.OBJECT, PolymorphicKind.SEALED -> (value as Map<*, *>).let { map ->
14-
FirebaseClassDecoder(map.size, { map.containsKey(it) }) { desc, index -> map[desc.getElementName(index)] }
14+
FirebaseClassDecoder(map.size, { map.containsKey(it) }) { desc, index ->
15+
val elementName = desc.getElementName(index)
16+
if (desc.kind is PolymorphicKind && elementName == "value") {
17+
map
18+
} else {
19+
map[desc.getElementName(index)]
20+
}
21+
}
1522
}
1623
StructureKind.LIST -> (value as List<*>).let {
1724
FirebaseCompositeDecoder(it.size) { _, index -> it[index] }

firebase-common/src/commonTest/kotlin/dev/gitlive/firebase/EncodersTest.kt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ sealed class TestSealed {
2525
data class ChildClass(val map: Map<String, String>, val bool: Boolean = false): TestSealed()
2626
}
2727

28+
@Serializable
29+
data class TestSealedList(val list: List<TestSealed>)
30+
2831
class EncodersTest {
2932
@Test
3033
fun encodeMap() {
@@ -74,4 +77,62 @@ class EncodersTest {
7477
val decoded = decode(TestSealed.serializer(), nativeMapOf("type" to "child", "map" to nativeMapOf("key" to "value"), "bool" to true))
7578
assertEquals(TestSealed.ChildClass(mapOf("key" to "value"), true), decoded)
7679
}
80+
81+
@Test
82+
fun encodeSealedClassList() {
83+
val toEncode = TestSealedList(
84+
list = listOf(
85+
TestSealed.ChildClass(
86+
map = mapOf("key" to "value"),
87+
bool = false
88+
)
89+
)
90+
)
91+
val encoded = encode<TestSealedList>(
92+
TestSealedList.serializer(),
93+
toEncode,
94+
shouldEncodeElementDefault = true
95+
)
96+
val expected = nativeMapOf(
97+
"list" to nativeListOf(
98+
nativeMapOf(
99+
"type" to "child",
100+
"map" to nativeMapOf(
101+
"key" to "value"
102+
),
103+
"bool" to false
104+
)
105+
)
106+
)
107+
nativeAssertEquals(expected, encoded)
108+
}
109+
110+
@Test
111+
fun decodeSealedClassList() {
112+
val toDecode = nativeMapOf(
113+
"list" to nativeListOf(
114+
nativeMapOf(
115+
"type" to "child",
116+
"map" to nativeMapOf(
117+
"key" to "value"
118+
),
119+
"bool" to false
120+
)
121+
)
122+
)
123+
val decoded = decode(
124+
TestSealedList.serializer(),
125+
toDecode
126+
)
127+
val expected = TestSealedList(
128+
list = listOf(
129+
TestSealed.ChildClass(
130+
map = mapOf("key" to "value"),
131+
bool = false
132+
)
133+
)
134+
)
135+
136+
assertEquals(expected, decoded)
137+
}
77138
}

firebase-common/src/iosMain/kotlin/dev/gitlive/firebase/_decoders.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import kotlinx.serialization.descriptors.StructureKind
1111

1212
actual fun FirebaseDecoder.structureDecoder(descriptor: SerialDescriptor): CompositeDecoder = when(descriptor.kind) {
1313
StructureKind.CLASS, StructureKind.OBJECT, PolymorphicKind.SEALED -> (value as Map<*, *>).let { map ->
14-
FirebaseClassDecoder(map.size, { map.containsKey(it) }) { desc, index -> map[desc.getElementName(index)] }
14+
FirebaseClassDecoder(map.size, { map.containsKey(it) }) { desc, index ->
15+
val elementName = desc.getElementName(index)
16+
if (desc.kind is PolymorphicKind && elementName == "value") {
17+
map
18+
} else {
19+
map[desc.getElementName(index)]
20+
}
21+
}
1522
}
1623
StructureKind.LIST -> (value as List<*>).let {
1724
FirebaseCompositeDecoder(it.size) { _, index -> it[index] }

firebase-common/src/jsMain/kotlin/dev/gitlive/firebase/_decoders.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ import kotlin.js.Json
1313
@Suppress("UNCHECKED_CAST_TO_EXTERNAL_INTERFACE")
1414
actual fun FirebaseDecoder.structureDecoder(descriptor: SerialDescriptor): CompositeDecoder = when(descriptor.kind) {
1515
StructureKind.CLASS, StructureKind.OBJECT, PolymorphicKind.SEALED -> (value as Json).let { json ->
16-
FirebaseClassDecoder(js("Object").keys(value).length as Int, { json[it] != undefined }) {
17-
desc, index -> json[desc.getElementName(index)]
16+
FirebaseClassDecoder(js("Object").keys(value).length as Int, { json[it] != undefined }) { desc, index ->
17+
val elementName = desc.getElementName(index)
18+
if (desc.kind is PolymorphicKind && elementName == "value") {
19+
json
20+
} else {
21+
json[desc.getElementName(index)]
22+
}
1823
}
1924
}
2025
StructureKind.LIST -> (value as Array<*>).let {

0 commit comments

Comments
 (0)