Skip to content

Commit

Permalink
Improves DSL for updating firestore references
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Sep 1, 2024
1 parent bf8680c commit 3fde0f5
Show file tree
Hide file tree
Showing 21 changed files with 318 additions and 327 deletions.
67 changes: 23 additions & 44 deletions firebase-firestore/api/android/firebase-firestore.api

Large diffs are not rendered by default.

67 changes: 23 additions & 44 deletions firebase-firestore/api/jvm/firebase-firestore.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dev.gitlive.firebase.firestore.internal

import com.google.android.gms.tasks.TaskExecutors
import com.google.firebase.firestore.MetadataChanges
import dev.gitlive.firebase.firestore.EncodedFieldPath
import dev.gitlive.firebase.firestore.NativeDocumentReferenceType
import dev.gitlive.firebase.firestore.NativeDocumentSnapshot
import dev.gitlive.firebase.firestore.Source
Expand Down Expand Up @@ -44,19 +43,12 @@ internal actual class NativeDocumentReference actual constructor(actual val nati
android.update(encodedData.android).await()
}

actual suspend fun updateEncodedFieldsAndValues(encodedFieldsAndValues: List<Pair<String, Any?>>) {
actual suspend fun updateEncoded(encodedFieldsAndValues: List<FieldAndValue>) {
encodedFieldsAndValues.takeUnless { encodedFieldsAndValues.isEmpty() }?.let {
android.update(encodedFieldsAndValues.toMap())
encodedFieldsAndValues.performUpdate(android::update, android::update)
}?.await()
}

actual suspend fun updateEncodedFieldPathsAndValues(encodedFieldsAndValues: List<Pair<EncodedFieldPath, Any?>>) {
encodedFieldsAndValues.takeUnless { encodedFieldsAndValues.isEmpty() }
?.performUpdate { field, value, moreFieldsAndValues ->
android.update(field, value, *moreFieldsAndValues)
}?.await()
}

actual suspend fun delete() {
android.delete().await()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.gitlive.firebase.firestore.internal

import dev.gitlive.firebase.firestore.DocumentReference
import dev.gitlive.firebase.firestore.EncodedFieldPath
import dev.gitlive.firebase.firestore.NativeTransaction
import dev.gitlive.firebase.firestore.android
import dev.gitlive.firebase.firestore.performUpdate
Expand All @@ -23,19 +22,17 @@ internal actual class NativeTransactionWrapper internal actual constructor(actua

actual fun updateEncoded(documentRef: DocumentReference, encodedData: EncodedObject) = native.update(documentRef.android, encodedData.android).let { this }

actual fun updateEncodedFieldsAndValues(
actual fun updateEncoded(
documentRef: DocumentReference,
encodedFieldsAndValues: List<Pair<String, Any?>>,
) = encodedFieldsAndValues.performUpdate { field, value, moreFieldsAndValues ->
native.update(documentRef.android, field, value, *moreFieldsAndValues)
}.let { this }

actual fun updateEncodedFieldPathsAndValues(
documentRef: DocumentReference,
encodedFieldsAndValues: List<Pair<EncodedFieldPath, Any?>>,
) = encodedFieldsAndValues.performUpdate { field, value, moreFieldsAndValues ->
native.update(documentRef.android, field, value, *moreFieldsAndValues)
}.let { this }
encodedFieldsAndValues: List<FieldAndValue>,
) = encodedFieldsAndValues.performUpdate(
updateAsField = { field, value, moreFieldsAndValues ->
native.update(documentRef.android, field, value, *moreFieldsAndValues)
},
updateAsFieldPath = { fieldPath, value, moreFieldsAndValues ->
native.update(documentRef.android, fieldPath, value, *moreFieldsAndValues)
},
).let { this }

actual fun delete(documentRef: DocumentReference) =
native.delete(documentRef.android).let { this }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.gitlive.firebase.firestore.internal

import dev.gitlive.firebase.firestore.DocumentReference
import dev.gitlive.firebase.firestore.EncodedFieldPath
import dev.gitlive.firebase.firestore.NativeWriteBatch
import dev.gitlive.firebase.firestore.android
import dev.gitlive.firebase.firestore.performUpdate
Expand All @@ -25,19 +24,17 @@ internal actual class NativeWriteBatchWrapper internal actual constructor(actual

actual fun updateEncoded(documentRef: DocumentReference, encodedData: EncodedObject) = native.update(documentRef.android, encodedData.android).let { this }

actual fun updateEncodedFieldsAndValues(
actual fun updateEncoded(
documentRef: DocumentReference,
encodedFieldsAndValues: List<Pair<String, Any?>>,
) = encodedFieldsAndValues.performUpdate { field, value, moreFieldsAndValues ->
native.update(documentRef.android, field, value, *moreFieldsAndValues)
}.let { this }

actual fun updateEncodedFieldPathsAndValues(
documentRef: DocumentReference,
encodedFieldsAndValues: List<Pair<EncodedFieldPath, Any?>>,
) = encodedFieldsAndValues.performUpdate { field, value, moreFieldsAndValues ->
native.update(documentRef.android, field, value, *moreFieldsAndValues)
}.let { this }
encodedFieldsAndValues: List<FieldAndValue>,
) = encodedFieldsAndValues.performUpdate(
updateAsField = { field, value, moreFieldsAndValues ->
native.update(documentRef.android, field, value, *moreFieldsAndValues)
},
updateAsFieldPath = { fieldPath, value, moreFieldsAndValues ->
native.update(documentRef.android, fieldPath, value, *moreFieldsAndValues)
},
).let { this }

actual fun delete(documentRef: DocumentReference) =
native.delete(documentRef.android).let { this }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.gitlive.firebase.firestore

import dev.gitlive.firebase.EncodeSettings
import kotlinx.serialization.SerializationStrategy

public class FieldValueBuilder internal constructor() {

internal val fieldValues: MutableList<Any> = mutableListOf()
public var buildSettings: EncodeSettings.Builder.() -> Unit = {
encodeDefaults = true
}

public inline fun <reified T> add(value: T) {
addEncoded(encode(value, buildSettings)!!)
}

public fun <T : Any> addWithStrategy(strategy: SerializationStrategy<T>, value: T) {
addEncoded(dev.gitlive.firebase.internal.encode(strategy, value, buildSettings)!!)
}

@PublishedApi
internal fun addEncoded(encodedValue: Any) {
fieldValues += encodedValue
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dev.gitlive.firebase.firestore

import dev.gitlive.firebase.EncodeSettings
import dev.gitlive.firebase.firestore.internal.FieldAndValue
import kotlinx.serialization.SerializationStrategy

public class FieldsAndValuesBuilder internal constructor() {

internal val fieldAndValues: MutableList<FieldAndValue> = mutableListOf()
public var buildSettings: EncodeSettings.Builder.() -> Unit = {
encodeDefaults = true
}

public inline infix fun <reified T> String.to(value: T) {
toEncoded(encode(value, buildSettings))
}

public inline infix fun <reified T> FieldPath.to(value: T) {
toEncoded(encode(value, buildSettings))
}

public fun <T : Any> String.to(strategy: SerializationStrategy<T>, value: T) {
toEncoded(dev.gitlive.firebase.internal.encode(strategy, value, buildSettings))
}

public fun <T : Any> FieldPath.to(strategy: SerializationStrategy<T>, value: T) {
toEncoded(dev.gitlive.firebase.internal.encode(strategy, value, buildSettings))
}

@PublishedApi
internal fun String.toEncoded(encodedValue: Any?) {
fieldAndValues += FieldAndValue.WithStringField(this, encodedValue)
}

@PublishedApi
internal fun FieldPath.toEncoded(encodedValue: Any?) {
fieldAndValues += FieldAndValue.WithFieldPath(this, encodedValue)
}
}
Loading

0 comments on commit 3fde0f5

Please sign in to comment.