Skip to content

Commit

Permalink
Make builders abstract to allow for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Sep 13, 2024
1 parent 5332e61 commit 4176fe6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 31 deletions.
11 changes: 6 additions & 5 deletions firebase-firestore/api/android/firebase-firestore.api
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public final class dev/gitlive/firebase/firestore/FieldValue$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class dev/gitlive/firebase/firestore/FieldValueBuilder {
public final fun addEncoded (Ljava/lang/Object;)V
public abstract class dev/gitlive/firebase/firestore/FieldValueBuilder {
public abstract fun addEncoded (Ljava/lang/Object;)V
public final fun addWithStrategy (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)V
public final fun getBuildSettings ()Lkotlin/jvm/functions/Function1;
public final fun setBuildSettings (Lkotlin/jvm/functions/Function1;)V
Expand All @@ -176,13 +176,14 @@ public final class dev/gitlive/firebase/firestore/FieldValueSerializer : kotlinx
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
}

public final class dev/gitlive/firebase/firestore/FieldsAndValuesBuilder {
public abstract class dev/gitlive/firebase/firestore/FieldsAndValuesBuilder {
public fun <init> ()V
public final fun getBuildSettings ()Lkotlin/jvm/functions/Function1;
public final fun setBuildSettings (Lkotlin/jvm/functions/Function1;)V
public final fun to (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)V
public final fun to (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)V
public final fun toEncoded (Ldev/gitlive/firebase/firestore/FieldPath;Ljava/lang/Object;)V
public final fun toEncoded (Ljava/lang/String;Ljava/lang/Object;)V
public abstract fun toEncoded (Ldev/gitlive/firebase/firestore/FieldPath;Ljava/lang/Object;)V
public abstract fun toEncoded (Ljava/lang/String;Ljava/lang/Object;)V
}

public abstract class dev/gitlive/firebase/firestore/Filter {
Expand Down
11 changes: 6 additions & 5 deletions firebase-firestore/api/jvm/firebase-firestore.api
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public final class dev/gitlive/firebase/firestore/FieldValue$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class dev/gitlive/firebase/firestore/FieldValueBuilder {
public final fun addEncoded (Ljava/lang/Object;)V
public abstract class dev/gitlive/firebase/firestore/FieldValueBuilder {
public abstract fun addEncoded (Ljava/lang/Object;)V
public final fun addWithStrategy (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)V
public final fun getBuildSettings ()Lkotlin/jvm/functions/Function1;
public final fun setBuildSettings (Lkotlin/jvm/functions/Function1;)V
Expand All @@ -176,13 +176,14 @@ public final class dev/gitlive/firebase/firestore/FieldValueSerializer : kotlinx
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
}

public final class dev/gitlive/firebase/firestore/FieldsAndValuesBuilder {
public abstract class dev/gitlive/firebase/firestore/FieldsAndValuesBuilder {
public fun <init> ()V
public final fun getBuildSettings ()Lkotlin/jvm/functions/Function1;
public final fun setBuildSettings (Lkotlin/jvm/functions/Function1;)V
public final fun to (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)V
public final fun to (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)V
public final fun toEncoded (Ldev/gitlive/firebase/firestore/FieldPath;Ljava/lang/Object;)V
public final fun toEncoded (Ljava/lang/String;Ljava/lang/Object;)V
public abstract fun toEncoded (Ldev/gitlive/firebase/firestore/FieldPath;Ljava/lang/Object;)V
public abstract fun toEncoded (Ljava/lang/String;Ljava/lang/Object;)V
}

public abstract class dev/gitlive/firebase/firestore/Filter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package dev.gitlive.firebase.firestore
import dev.gitlive.firebase.EncodeSettings
import kotlinx.serialization.SerializationStrategy

public class FieldValueBuilder internal constructor() {
public abstract class FieldValueBuilder internal constructor() {

internal val fieldValues: MutableList<Any> = mutableListOf()
public var buildSettings: EncodeSettings.Builder.() -> Unit = {
encodeDefaults = true
}
Expand All @@ -19,7 +18,5 @@ public class FieldValueBuilder internal constructor() {
}

@PublishedApi
internal fun addEncoded(encodedValue: Any) {
fieldValues += encodedValue
}
internal abstract fun addEncoded(encodedValue: Any)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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() {
public abstract class FieldsAndValuesBuilder {

internal val fieldAndValues: MutableList<FieldAndValue> = mutableListOf()
public var buildSettings: EncodeSettings.Builder.() -> Unit = {
encodeDefaults = true
}
Expand All @@ -28,12 +26,8 @@ public class FieldsAndValuesBuilder internal constructor() {
}

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

@PublishedApi
internal fun FieldPath.toEncoded(encodedValue: Any?) {
fieldAndValues += FieldAndValue.WithFieldPath(this, encodedValue)
}
internal abstract fun FieldPath.toEncoded(encodedValue: Any?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public data class Transaction internal constructor(internal val nativeWrapper: N
public fun update(
documentRef: DocumentReference,
fieldsAndValuesBuilder: FieldsAndValuesBuilder.() -> Unit,
): Transaction = Transaction(nativeWrapper.updateEncoded(documentRef, FieldsAndValuesBuilder().apply(fieldsAndValuesBuilder).fieldAndValues))
): Transaction = Transaction(nativeWrapper.updateEncoded(documentRef, dev.gitlive.firebase.firestore.internal.FieldsAndValuesBuilder().apply(fieldsAndValuesBuilder).fieldAndValues))

@PublishedApi
internal fun updateEncoded(documentRef: DocumentReference, encodedData: EncodedObject): Transaction = Transaction(nativeWrapper.updateEncoded(documentRef, encodedData))
Expand Down Expand Up @@ -263,7 +263,7 @@ public open class Query internal constructor(internal val nativeQuery: NativeQue
}
}

public fun startAfter(builder: FieldValueBuilder.() -> Unit): Query = Query(nativeQuery.startAfter(*FieldValueBuilder().apply(builder).fieldValues.toTypedArray()))
public fun startAfter(builder: FieldValueBuilder.() -> Unit): Query = Query(nativeQuery.startAfter(*dev.gitlive.firebase.firestore.internal.FieldValueBuilder().apply(builder).fieldValues.toTypedArray()))

public fun startAt(document: DocumentSnapshot): Query = Query(nativeQuery.startAt(document.native))
public fun startAt(vararg fieldValues: Any?): Query = startAt({}, *fieldValues)
Expand All @@ -275,7 +275,7 @@ public open class Query internal constructor(internal val nativeQuery: NativeQue
}
}

public fun startAt(builder: FieldValueBuilder.() -> Unit): Query = Query(nativeQuery.startAt(*FieldValueBuilder().apply(builder).fieldValues.toTypedArray()))
public fun startAt(builder: FieldValueBuilder.() -> Unit): Query = Query(nativeQuery.startAt(*dev.gitlive.firebase.firestore.internal.FieldValueBuilder().apply(builder).fieldValues.toTypedArray()))

public fun endBefore(document: DocumentSnapshot): Query = Query(nativeQuery.endBefore(document.native))
public fun endBefore(vararg fieldValues: Any?): Query = endBefore({}, *fieldValues)
Expand All @@ -287,7 +287,7 @@ public open class Query internal constructor(internal val nativeQuery: NativeQue
}
}

public fun endBefore(builder: FieldValueBuilder.() -> Unit): Query = Query(nativeQuery.endBefore(*FieldValueBuilder().apply(builder).fieldValues.toTypedArray()))
public fun endBefore(builder: FieldValueBuilder.() -> Unit): Query = Query(nativeQuery.endBefore(*dev.gitlive.firebase.firestore.internal.FieldValueBuilder().apply(builder).fieldValues.toTypedArray()))

public fun endAt(document: DocumentSnapshot): Query = Query(nativeQuery.endAt(document.native))
public fun endAt(vararg fieldValues: Any?): Query = endAt({}, *fieldValues)
Expand All @@ -299,7 +299,7 @@ public open class Query internal constructor(internal val nativeQuery: NativeQue
}
}

public fun endAt(builder: FieldValueBuilder.() -> Unit): Query = Query(nativeQuery.endAt(*FieldValueBuilder().apply(builder).fieldValues.toTypedArray()))
public fun endAt(builder: FieldValueBuilder.() -> Unit): Query = Query(nativeQuery.endAt(*dev.gitlive.firebase.firestore.internal.FieldValueBuilder().apply(builder).fieldValues.toTypedArray()))
}

@Deprecated("Deprecated in favor of using a [FilterBuilder]", replaceWith = ReplaceWith("where { field equalTo equalTo }", "dev.gitlive.firebase.firestore"))
Expand Down Expand Up @@ -461,7 +461,7 @@ public data class WriteBatch internal constructor(internal val nativeWrapper: Na
): WriteBatch = WriteBatch(
nativeWrapper.updateEncoded(
documentRef,
FieldsAndValuesBuilder().apply(fieldAndValuesBuilder).fieldAndValues,
dev.gitlive.firebase.firestore.internal.FieldsAndValuesBuilder().apply(fieldAndValuesBuilder).fieldAndValues,
),
)

Expand Down Expand Up @@ -633,7 +633,7 @@ public data class DocumentReference internal constructor(internal val native: Na
public suspend fun update(
fieldsAndValuesBuilder: FieldsAndValuesBuilder.() -> Unit,
) {
native.updateEncoded(FieldsAndValuesBuilder().apply(fieldsAndValuesBuilder).fieldAndValues)
native.updateEncoded(dev.gitlive.firebase.firestore.internal.FieldsAndValuesBuilder().apply(fieldsAndValuesBuilder).fieldAndValues)
}

public suspend fun delete() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.gitlive.firebase.firestore.internal

import kotlin.experimental.ExperimentalObjCName
import kotlin.native.ObjCName

@OptIn(ExperimentalObjCName::class)
@ObjCName("FieldValueBuilderImpl")
internal class FieldValueBuilder : dev.gitlive.firebase.firestore.FieldValueBuilder() {

internal val fieldValues: MutableList<Any> = mutableListOf()

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

import dev.gitlive.firebase.firestore.FieldPath
import dev.gitlive.firebase.firestore.FieldsAndValuesBuilder
import kotlin.experimental.ExperimentalObjCName
import kotlin.native.ObjCName

@OptIn(ExperimentalObjCName::class)
@ObjCName("FieldsAndValuesBuilderImpl")
internal class FieldsAndValuesBuilder : FieldsAndValuesBuilder() {

internal val fieldAndValues: MutableList<FieldAndValue> = mutableListOf()

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

override fun FieldPath.toEncoded(encodedValue: Any?) {
fieldAndValues += FieldAndValue.WithFieldPath(this, encodedValue)
}
}

0 comments on commit 4176fe6

Please sign in to comment.