Skip to content

Commit 6d44c70

Browse files
authored
Merge branch 'master' into updates
2 parents 1788270 + 5b54af7 commit 6d44c70

File tree

6 files changed

+36
-2
lines changed
  • firebase-firestore/src

6 files changed

+36
-2
lines changed

firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

+2
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ actual class DocumentSnapshot(val android: com.google.firebase.firestore.Documen
413413

414414
actual fun <T> data(strategy: DeserializationStrategy<T>) = decode(strategy, android.data)
415415

416+
actual fun dataMap(): Map<String, Any?> = android.data ?: emptyMap()
417+
416418
actual inline fun <reified T> get(field: String) = decode<T>(value = android.get(field))
417419

418420
actual fun <T> get(field: String, strategy: DeserializationStrategy<T>) =

firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

+2
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ expect class DocumentSnapshot {
196196
inline fun <reified T: Any> data(): T
197197
fun <T> data(strategy: DeserializationStrategy<T>): T
198198

199+
fun dataMap(): Map<String, Any?>
200+
199201
val exists: Boolean
200202
val id: String
201203
val reference: DocumentReference

firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt

+17-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ class FirebaseFirestoreTest {
124124

125125
}
126126

127-
128127
@Test
129128
fun testDocumentAutoId() = runTest {
130129
val doc = Firebase.firestore
@@ -142,6 +141,23 @@ class FirebaseFirestoreTest {
142141
assertEquals("AutoId", resultDoc.get("prop1"))
143142
}
144143

144+
@Test
145+
fun testDataMap() = runTest {
146+
val doc = Firebase.firestore
147+
.collection("testDataMap")
148+
.document
149+
150+
doc.set(FirestoreTest.serializer(), FirestoreTest("dataMap", 123.45))
151+
152+
val resultDoc = Firebase.firestore
153+
.collection("testDataMap")
154+
.document(doc.id)
155+
.get()
156+
157+
assertEquals(true, resultDoc.exists)
158+
assertEquals(mapOf("prop1" to "dataMap", "time" to 123.45), resultDoc.dataMap())
159+
}
160+
145161
private suspend fun setupFirestoreData() {
146162
Firebase.firestore.collection("FirebaseFirestoreTest")
147163
.document("one")

firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

+2
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ actual class DocumentSnapshot(val ios: FIRDocumentSnapshot) {
381381

382382
actual fun <T> data(strategy: DeserializationStrategy<T>) = decode(strategy, ios.data())
383383

384+
actual fun dataMap(): Map<String, Any?> = ios.data()?.map { it.key.toString() to it.value }?.toMap() ?: emptyMap()
385+
384386
actual inline fun <reified T> get(field: String) = decode<T>(value = ios.valueForField(field))
385387

386388
actual fun <T> get(field: String, strategy: DeserializationStrategy<T>) =

firebase-firestore/src/jsMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

+12
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ actual class DocumentSnapshot(val js: firebase.firestore.DocumentSnapshot) {
393393
actual fun <T> data(strategy: DeserializationStrategy<T>): T =
394394
rethrow { decode(strategy, js.data()) }
395395

396+
actual fun dataMap(): Map<String, Any?> = rethrow { mapOf(js.data().asDynamic()) }
397+
396398
actual inline fun <reified T> get(field: String) =
397399
rethrow { decode<T>(value = js.get(field)) }
398400

@@ -503,3 +505,13 @@ fun errorToException(e: dynamic) = (e?.code ?: e?.message ?: "")
503505
}
504506
}
505507
}
508+
509+
// from: https://discuss.kotlinlang.org/t/how-to-access-native-js-object-as-a-map-string-any/509/8
510+
fun entriesOf(jsObject: dynamic): List<Pair<String, Any?>> =
511+
(js("Object.entries") as (dynamic) -> Array<Array<Any?>>)
512+
.invoke(jsObject)
513+
.map { entry -> entry[0] as String to entry[1] }
514+
515+
// from: https://discuss.kotlinlang.org/t/how-to-access-native-js-object-as-a-map-string-any/509/8
516+
fun mapOf(jsObject: dynamic): Map<String, Any?> =
517+
entriesOf(jsObject).toMap()

firebase-firestore/src/jsTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ actual fun runTest(test: suspend () -> Unit) = GlobalScope
1515
.promise {
1616
try {
1717
test()
18-
} catch (e: dynamic) {
18+
} catch (e: Throwable) {
1919
e.log()
2020
throw e
2121
}

0 commit comments

Comments
 (0)