File tree 6 files changed +36
-2
lines changed
androidMain/kotlin/dev/gitlive/firebase/firestore
commonMain/kotlin/dev/gitlive/firebase/firestore
commonTest/kotlin/dev/gitlive/firebase/firestore
iosMain/kotlin/dev/gitlive/firebase/firestore
jsMain/kotlin/dev/gitlive/firebase/firestore
jsTest/kotlin/dev/gitlive/firebase/firestore
6 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -413,6 +413,8 @@ actual class DocumentSnapshot(val android: com.google.firebase.firestore.Documen
413
413
414
414
actual fun <T > data (strategy : DeserializationStrategy <T >) = decode(strategy, android.data)
415
415
416
+ actual fun dataMap (): Map <String , Any ?> = android.data ? : emptyMap()
417
+
416
418
actual inline fun <reified T > get (field : String ) = decode<T >(value = android.get(field))
417
419
418
420
actual fun <T > get (field : String , strategy : DeserializationStrategy <T >) =
Original file line number Diff line number Diff line change @@ -196,6 +196,8 @@ expect class DocumentSnapshot {
196
196
inline fun <reified T : Any > data (): T
197
197
fun <T > data (strategy : DeserializationStrategy <T >): T
198
198
199
+ fun dataMap (): Map <String , Any ?>
200
+
199
201
val exists: Boolean
200
202
val id: String
201
203
val reference: DocumentReference
Original file line number Diff line number Diff line change @@ -124,7 +124,6 @@ class FirebaseFirestoreTest {
124
124
125
125
}
126
126
127
-
128
127
@Test
129
128
fun testDocumentAutoId () = runTest {
130
129
val doc = Firebase .firestore
@@ -142,6 +141,23 @@ class FirebaseFirestoreTest {
142
141
assertEquals(" AutoId" , resultDoc.get(" prop1" ))
143
142
}
144
143
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
+
145
161
private suspend fun setupFirestoreData () {
146
162
Firebase .firestore.collection(" FirebaseFirestoreTest" )
147
163
.document(" one" )
Original file line number Diff line number Diff line change @@ -381,6 +381,8 @@ actual class DocumentSnapshot(val ios: FIRDocumentSnapshot) {
381
381
382
382
actual fun <T > data (strategy : DeserializationStrategy <T >) = decode(strategy, ios.data())
383
383
384
+ actual fun dataMap (): Map <String , Any ?> = ios.data()?.map { it.key.toString() to it.value }?.toMap() ? : emptyMap()
385
+
384
386
actual inline fun <reified T > get (field : String ) = decode<T >(value = ios.valueForField(field))
385
387
386
388
actual fun <T > get (field : String , strategy : DeserializationStrategy <T >) =
Original file line number Diff line number Diff line change @@ -393,6 +393,8 @@ actual class DocumentSnapshot(val js: firebase.firestore.DocumentSnapshot) {
393
393
actual fun <T > data (strategy : DeserializationStrategy <T >): T =
394
394
rethrow { decode(strategy, js.data()) }
395
395
396
+ actual fun dataMap (): Map <String , Any ?> = rethrow { mapOf (js.data().asDynamic()) }
397
+
396
398
actual inline fun <reified T > get (field : String ) =
397
399
rethrow { decode<T >(value = js.get(field)) }
398
400
@@ -503,3 +505,13 @@ fun errorToException(e: dynamic) = (e?.code ?: e?.message ?: "")
503
505
}
504
506
}
505
507
}
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()
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ actual fun runTest(test: suspend () -> Unit) = GlobalScope
15
15
.promise {
16
16
try {
17
17
test()
18
- } catch (e: dynamic ) {
18
+ } catch (e: Throwable ) {
19
19
e.log()
20
20
throw e
21
21
}
You can’t perform that action at this time.
0 commit comments