Skip to content

Commit

Permalink
Track for JS
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Jan 3, 2024
1 parent 582532d commit fae5299
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@file:JvmName("android")
package dev.gitlive.firebase.firestore

import com.google.android.gms.tasks.Task
import com.google.firebase.firestore.*
import dev.gitlive.firebase.*
import kotlinx.coroutines.runBlocking
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.gitlive.firebase.firestore

import dev.gitlive.firebase.firebaseSerializer
import dev.gitlive.firebase.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

package dev.gitlive.firebase.firestore

import dev.gitlive.firebase.EncodeSettings
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.FirebaseOptions
import dev.gitlive.firebase.apps
import dev.gitlive.firebase.decode
import dev.gitlive.firebase.initialize
import dev.gitlive.firebase.runBlockingTest
import dev.gitlive.firebase.runTest
import dev.gitlive.firebase.withSerializer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -728,7 +731,7 @@ class FirebaseFirestoreTest {
fun encodeDocumentReference() = runTest {
val doc = firestore.document("a/b")
val item = TestDataWithDocumentReference("123", doc, doc)
val encoded = encodedAsMap(encode(item, shouldEncodeElementDefault = false))
val encoded = encodedAsMap(encode(item, EncodeSettings(shouldEncodeElementDefault = false)))
assertEquals("123", encoded["uid"])
assertEquals(doc.nativeValue, encoded["reference"])
assertEquals(doc.nativeValue, encoded["optionalReference"])
Expand All @@ -737,7 +740,7 @@ class FirebaseFirestoreTest {
@Test
fun encodeNullDocumentReference() = runTest {
val item = TestDataWithOptionalDocumentReference(null)
val encoded = encodedAsMap(encode(item, shouldEncodeElementDefault = false))
val encoded = encodedAsMap(encode(item, EncodeSettings(shouldEncodeElementDefault = false)))
assertNull(encoded["optionalReference"])
}

Expand Down Expand Up @@ -791,7 +794,6 @@ class FirebaseFirestoreTest {
assertNull(deletedList)
}

private suspend fun setupFirestoreData() {
@Test
fun testQueryEqualTo() = runTest {
setupFirestoreData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,13 @@ actual class DocumentReference actual constructor(internal actual val nativeValu
}
}

actual typealias NativeQuery = JsQuery
actual data class NativeQuery(val js: JsQuery)

actual open class Query internal actual constructor(nativeQuery: NativeQuery) {

open val js: JsQuery = nativeQuery
constructor(js: JsQuery) : this(NativeQuery(js))

open val js: JsQuery = nativeQuery.js

actual suspend fun get() = rethrow { QuerySnapshot(getDocs(js).await()) }

Expand Down Expand Up @@ -342,7 +344,7 @@ actual open class Query internal actual constructor(nativeQuery: NativeQuery) {
}
}

actual class CollectionReference(override val js: JsCollectionReference) : BaseCollectionReference(js) {
actual class CollectionReference(override val js: JsCollectionReference) : BaseCollectionReference(NativeQuery(js)) {

actual val path: String
get() = rethrow { js.path }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.gitlive.firebase.firestore

import kotlinx.serialization.Serializable

/** Represents a platform specific Firebase FieldValue. */
typealias NativeFieldValue = com.google.firebase.firestore.FieldValue

/** Represents a Firebase FieldValue. */
@Serializable(with = FieldValueSerializer::class)
actual class FieldValue internal actual constructor(internal actual val nativeValue: Any) {
init {
require(nativeValue is NativeFieldValue)
}
override fun equals(other: Any?): Boolean =
this === other || other is FieldValue && nativeValue == other.nativeValue
override fun hashCode(): Int = nativeValue.hashCode()
override fun toString(): String = nativeValue.toString()

actual companion object {
actual val serverTimestamp: FieldValue get() = FieldValue(NativeFieldValue.serverTimestamp())
actual val delete: FieldValue get() = FieldValue(NativeFieldValue.delete())
actual fun increment(value: Int): FieldValue = FieldValue(NativeFieldValue.increment(value.toDouble()))
actual fun arrayUnion(vararg elements: Any): FieldValue = FieldValue(NativeFieldValue.arrayUnion(*elements))
actual fun arrayRemove(vararg elements: Any): FieldValue = FieldValue(NativeFieldValue.arrayRemove(*elements))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.gitlive.firebase.firestore

@PublishedApi
internal actual fun isSpecialValue(value: Any) = when(value) {
is NativeFieldValue,
is NativeGeoPoint,
is NativeTimestamp,
is NativeDocumentReference -> true
else -> false
}
Loading

0 comments on commit fae5299

Please sign in to comment.