Skip to content

added DocumentSnapshot.dataMap() (fixes #186) #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ actual class DocumentSnapshot(val android: com.google.firebase.firestore.Documen

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

actual fun dataMap(): Map<String, Any?> = android.data ?: mapOf()

actual inline fun <reified T> get(field: String) = decode<T>(value = android.get(field))

actual fun <T> get(field: String, strategy: DeserializationStrategy<T>) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ expect class DocumentSnapshot {
inline fun <reified T: Any> data(): T
fun <T> data(strategy: DeserializationStrategy<T>): T

fun dataMap(): Map<String, Any?>

val exists: Boolean
val id: String
val reference: DocumentReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ class FirebaseFirestoreTest {

}


@Test
fun testDocumentAutoId() = runTest {
val doc = Firebase.firestore
Expand All @@ -142,6 +141,23 @@ class FirebaseFirestoreTest {
assertEquals("AutoId", resultDoc.get("prop1"))
}

@Test
fun testDataMap() = runTest {
val doc = Firebase.firestore
.collection("testDataMap")
.document

doc.set(FirestoreTest.serializer(), FirestoreTest("dataMap", 123.45))

val resultDoc = Firebase.firestore
.collection("testDataMap")
.document(doc.id)
.get()

assertEquals(true, resultDoc.exists)
assertEquals(mapOf("prop1" to "dataMap", "time" to 123.45), resultDoc.dataMap())
}

private suspend fun setupFirestoreData() {
Firebase.firestore.collection("FirebaseFirestoreTest")
.document("one")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ actual class DocumentSnapshot(val ios: FIRDocumentSnapshot) {

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

actual fun dataMap(): Map<String, Any?> = ios.data()?.map { it.key.toString() to it.value }?.toMap() ?: mapOf()

actual inline fun <reified T> get(field: String) = decode<T>(value = ios.valueForField(field))

actual fun <T> get(field: String, strategy: DeserializationStrategy<T>) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ actual class DocumentSnapshot(val js: firebase.firestore.DocumentSnapshot) {
actual fun <T> data(strategy: DeserializationStrategy<T>): T =
rethrow { decode(strategy, js.data()) }

actual fun dataMap(): Map<String, Any?> = rethrow { mapOf(js.data().asDynamic()) }

actual inline fun <reified T> get(field: String) =
rethrow { decode<T>(value = js.get(field)) }

Expand Down Expand Up @@ -503,3 +505,13 @@ fun errorToException(e: dynamic) = (e?.code ?: e?.message ?: "")
}
}
}

// from: https://discuss.kotlinlang.org/t/how-to-access-native-js-object-as-a-map-string-any/509/8
fun entriesOf(jsObject: dynamic): List<Pair<String, Any?>> =
(js("Object.entries") as (dynamic) -> Array<Array<Any?>>)
.invoke(jsObject)
.map { entry -> entry[0] as String to entry[1] }

// from: https://discuss.kotlinlang.org/t/how-to-access-native-js-object-as-a-map-string-any/509/8
fun mapOf(jsObject: dynamic): Map<String, Any?> =
entriesOf(jsObject).toMap()
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ actual fun runTest(test: suspend () -> Unit) = GlobalScope
.promise {
try {
test()
} catch (e: dynamic) {
} catch (e: Throwable) {
e.log()
throw e
}
Expand Down