Skip to content

Commit 6a08e16

Browse files
committed
Done on Android
1 parent 039f5ed commit 6a08e16

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ package dev.gitlive.firebase
77
import kotlinx.serialization.encoding.CompositeDecoder
88
import kotlinx.serialization.KSerializer
99
import kotlinx.serialization.SerializationException
10+
import kotlinx.serialization.descriptors.PolymorphicKind
1011
import kotlinx.serialization.descriptors.SerialDescriptor
1112
import kotlinx.serialization.descriptors.StructureKind
1213

13-
actual fun FirebaseDecoder.structureDecoder(descriptor: SerialDescriptor, decodeDouble: (value: Any?) -> Double?): CompositeDecoder = when(descriptor.kind as StructureKind) {
14-
StructureKind.CLASS, StructureKind.OBJECT -> (value as Map<*, *>).let { map ->
14+
actual fun FirebaseDecoder.structureDecoder(descriptor: SerialDescriptor, decodeDouble: (value: Any?) -> Double?): CompositeDecoder =
15+
when(descriptor.kind) {
16+
StructureKind.CLASS, StructureKind.OBJECT, PolymorphicKind.SEALED -> (value as Map<*, *>).let { map ->
1517
FirebaseClassDecoder(decodeDouble, map.size, { map.containsKey(it) }) { desc, index -> map[desc.getElementName(index)] }
1618
}
17-
StructureKind.LIST -> (value as List<*>).let {
19+
StructureKind.LIST, PolymorphicKind.SEALED-> (value as List<*>).let {
1820
FirebaseCompositeDecoder(decodeDouble, it.size) { _, index -> it[index] }
1921
}
2022
StructureKind.MAP -> (value as Map<*, *>).entries.toList().let {
2123
FirebaseCompositeDecoder(decodeDouble, it.size) { _, index -> it[index/2].run { if(index % 2 == 0) key else value } }
2224
}
25+
else -> TODO("The firebase-kotlin-sdk does not support $descriptor for serialization yet")
2326
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44

55
package dev.gitlive.firebase
66

7+
import kotlinx.serialization.KSerializer
8+
import kotlinx.serialization.descriptors.PolymorphicKind
79
import kotlinx.serialization.encoding.CompositeEncoder
810
import kotlinx.serialization.descriptors.SerialDescriptor
911
import kotlinx.serialization.descriptors.StructureKind
1012
import kotlin.collections.set
1113

12-
actual fun FirebaseEncoder.structureEncoder(descriptor: SerialDescriptor): CompositeEncoder = when(descriptor.kind as StructureKind) {
13-
StructureKind.LIST -> mutableListOf<Any?>()
14-
.also { value = it }
15-
.let { FirebaseCompositeEncoder(shouldEncodeElementDefault, positiveInfinity) { _, index, value -> it.add(index, value) } }
16-
StructureKind.MAP -> mutableListOf<Any?>()
17-
.let { FirebaseCompositeEncoder(shouldEncodeElementDefault, positiveInfinity, { value = it.chunked(2).associate { (k, v) -> k to v } }) { _, _, value -> it.add(value) } }
18-
StructureKind.CLASS, StructureKind.OBJECT -> mutableMapOf<Any?, Any?>()
19-
.also { value = it }
20-
.let { FirebaseCompositeEncoder(shouldEncodeElementDefault, positiveInfinity) { _, index, value -> it[descriptor.getElementName(index)] = value } }
21-
}
14+
actual fun FirebaseEncoder.structureEncoder(descriptor: SerialDescriptor): CompositeEncoder =
15+
when(descriptor.kind) {
16+
StructureKind.LIST -> mutableListOf<Any?>()
17+
.also { value = it }
18+
.let { FirebaseCompositeEncoder(shouldEncodeElementDefault, positiveInfinity) { _, index, value -> it.add(index, value) } }
19+
StructureKind.MAP -> mutableListOf<Any?>()
20+
.let { FirebaseCompositeEncoder(shouldEncodeElementDefault, positiveInfinity, { value = it.chunked(2).associate { (k, v) -> k to v } }) { _, _, value -> it.add(value) } }
21+
StructureKind.CLASS, StructureKind.OBJECT, PolymorphicKind.SEALED -> mutableMapOf<Any?, Any?>()
22+
.also { value = it }
23+
.let { FirebaseCompositeEncoder(shouldEncodeElementDefault, positiveInfinity) { _, index, value -> it[descriptor.getElementName(index)] = value } }
24+
else -> TODO("The firebase-kotlin-sdk does not support $descriptor for serialization yet")
25+
}

0 commit comments

Comments
 (0)