Skip to content

Commit a48f408

Browse files
committed
expose serialization runtime in targets
fix IOOBE by replacing READ_ALL with decodeSequentially
1 parent 41e6002 commit a48f408

File tree

7 files changed

+29
-16
lines changed

7 files changed

+29
-16
lines changed

firebase-common/build.gradle.kts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ kotlin {
4747
jvmTarget = "1.8"
4848
}
4949
}
50+
val test by compilations.getting {
51+
kotlinOptions {
52+
jvmTarget = "1.8"
53+
}
54+
}
5055
}
5156

5257
tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>> {
@@ -60,24 +65,24 @@ kotlin {
6065
sourceSets {
6166
val commonMain by getting {
6267
dependencies {
63-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.20.0")
68+
api("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.20.0")
6469
}
6570
}
6671
val androidMain by getting {
6772
dependencies {
6873
api("com.google.firebase:firebase-common:19.2.0")
69-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0")
74+
api("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0")
7075
}
7176
}
7277
val jsMain by getting {
7378
dependencies {
7479
// implementation(npm("firebase", "6.2.3"))
75-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:0.20.0")
80+
api("org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:0.20.0")
7681
}
7782
}
7883
val jvmMain by getting {
7984
dependencies {
80-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0")
85+
api("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0")
8186
}
8287
kotlin.srcDir("src/androidMain/kotlin")
8388
}
@@ -89,7 +94,7 @@ kotlin {
8994
}
9095
val iosMain by getting {
9196
dependencies {
92-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.20.0")
97+
api("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.20.0")
9398
}
9499
}
95100
configure(listOf(iosArm64, iosX64)) {

firebase-common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@teamhubapp/firebase-common",
3-
"version": "0.1.0-beta14",
3+
"version": "0.1.0-beta",
44
"description": "Wrapper around firebase for usage in Kotlin Multiplatform projects",
55
"main": "firebase-common.js",
66
"scripts": {

firebase-common/src/androidTest/kotlin/dev/teamhub/firebase/EncodersTest.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class EncodersTest {
1212
@Test
1313
fun `encode a map`() {
1414
val encoded = encode<TestData>(TestData::class.serializer(), TestData(mapOf("key" to "value")))
15-
assertEquals(mapOf("key" to "value"), (encoded as Map<String, Any>)["map"])
15+
assertEquals(mapOf("map" to mapOf("key" to "value")), encoded)
1616
}
17-
}
17+
18+
@Test
19+
fun `decode a map`() {
20+
val decoded = decode<TestData>(TestData::class.serializer(), mapOf("map" to mapOf("key" to "value")))
21+
assertEquals(TestData(mapOf("key" to "value")), decoded)
22+
}}

firebase-common/src/commonMain/kotlin/dev/teamhub/firebase/decoders.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dev.teamhub.firebase
22

33
import kotlinx.serialization.*
4-
import kotlinx.serialization.CompositeDecoder.Companion.READ_ALL
54
import kotlinx.serialization.CompositeDecoder.Companion.READ_DONE
65
import kotlinx.serialization.builtins.nullable
76
import kotlinx.serialization.internal.UnitDescriptor
@@ -78,7 +77,9 @@ open class FirebaseCompositeDecoder constructor(
7877
override val context = EmptyModule
7978
override val updateMode = UpdateMode.OVERWRITE
8079

81-
override fun decodeElementIndex(desc: SerialDescriptor) = READ_ALL
80+
override fun decodeSequentially() = true
81+
82+
override fun decodeElementIndex(desc: SerialDescriptor): Int = throw NotImplementedError()
8283

8384
override fun decodeCollectionSize(desc: SerialDescriptor) = size
8485

firebase-common/src/commonMain/kotlin/dev/teamhub/firebase/serializers.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ class FirebaseMapSerializer : KSerializer<Map<String, Any?>> {
2525
override fun getElementIndex(name: String) = keys.indexOf(name)
2626
override fun getElementName(index: Int) = keys[index]
2727
override fun getElementAnnotations(index: Int) = emptyList<Annotation>()
28-
override fun getElementDescriptor(index: Int) = throw UnsupportedOperationException()
28+
override fun getElementDescriptor(index: Int) = throw NotImplementedError()
2929
override fun isElementOptional(index: Int) = false
3030
}
3131

32+
@Suppress("UNCHECKED_CAST")
3233
@ImplicitReflectionSerializer
3334
override fun serialize(encoder: Encoder, obj: Map<String, Any?>) {
3435
map = obj
3536
keys = obj.keys.toList()
3637
val collectionEncoder = encoder.beginCollection(descriptor, obj.size)
3738
keys.forEachIndexed { index, key ->
3839
val value = map.getValue(key)
39-
val serializer = value?.firebaseSerializer() ?: UnitSerializer().nullable as KSerializer<Any?>
40+
val serializer = (value?.firebaseSerializer() ?: UnitSerializer().nullable) as KSerializer<Any?>
4041
collectionEncoder.encodeNullableSerializableElement(
4142
serializer.descriptor, index, serializer, value
4243
)
@@ -65,16 +66,17 @@ class FirebaseListSerializer : KSerializer<Iterable<Any?>> {
6566
override fun getElementIndex(name: String) = throw NotImplementedError()
6667
override fun getElementName(index: Int) = throw NotImplementedError()
6768
override fun getElementAnnotations(index: Int) = emptyList<Annotation>()
68-
override fun getElementDescriptor(index: Int) = throw UnsupportedOperationException()
69+
override fun getElementDescriptor(index: Int) = throw NotImplementedError()
6970
override fun isElementOptional(index: Int) = false
7071
}
7172

7273
@Suppress("UNCHECKED_CAST")
74+
@ImplicitReflectionSerializer
7375
override fun serialize(encoder: Encoder, obj: Iterable<Any?>) {
7476
list = obj.toList()
7577
val collectionEncoder = encoder.beginCollection(descriptor, list.size)
7678
list.forEachIndexed { index, value ->
77-
val serializer = value?.firebaseSerializer() ?: UnitSerializer().nullable as KSerializer<Any>
79+
val serializer = (value?.firebaseSerializer() ?: UnitSerializer().nullable) as KSerializer<Any>
7880
collectionEncoder.encodeNullableSerializableElement(
7981
serializer.descriptor, index, serializer, value
8082
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlin.js.Json
88

99
@Suppress("UNCHECKED_CAST_TO_EXTERNAL_INTERFACE")
1010
actual fun FirebaseDecoder.structureDecoder(desc: SerialDescriptor, vararg typeParams: KSerializer<*>): CompositeDecoder = when(desc.kind as StructureKind) {
11-
StructureKind.CLASS -> (value as Json).let { json ->
11+
StructureKind.CLASS, StructureKind.OBJECT -> (value as Json).let { json ->
1212
FirebaseClassDecoder(js("Object").keys(value).length as Int, { json[it] != undefined }) {
1313
desc, index -> json[desc.getElementName(index)]
1414
}

firebase-common/src/jsMain/kotlin/dev/teamhub/firebase/_encoders.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ actual fun FirebaseEncoder.structureEncoder(desc: SerialDescriptor, vararg typeP
1010
StructureKind.LIST -> Array<Any?>(desc.elementsCount) { null }
1111
.also { value = it }
1212
.let { FirebaseCompositeEncoder(positiveInfinity) { _, index, value -> it[index] = value } }
13-
StructureKind.MAP, StructureKind.CLASS -> json()
13+
StructureKind.MAP, StructureKind.CLASS, StructureKind.OBJECT -> json()
1414
.also { value = it }
1515
.let { FirebaseCompositeEncoder(positiveInfinity) { _, index, value -> it[desc.getElementName(index)] = value } }
1616
}

0 commit comments

Comments
 (0)