-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use different encodeSettings approach
- Loading branch information
Showing
10 changed files
with
237 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 27 additions & 25 deletions
52
firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/FieldValuesDSL.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,60 @@ | ||
package dev.gitlive.firebase.firestore | ||
|
||
import dev.gitlive.firebase.EncodeSettings | ||
import dev.gitlive.firebase.copyFrom | ||
import kotlinx.serialization.SerializationStrategy | ||
import kotlinx.serialization.modules.EmptySerializersModule | ||
import kotlinx.serialization.modules.SerializersModule | ||
|
||
/** | ||
* A builder for generating the field values of a [Query]. | ||
* The order of the field values must match the order by clauses of the [Query] | ||
*/ | ||
public class FieldValuesDSL internal constructor() { | ||
public class FieldValuesDSL internal constructor() : EncodeSettings.Builder { | ||
|
||
internal val fieldValues: MutableList<Any> = mutableListOf() | ||
override var encodeDefaults: Boolean = true | ||
override var serializersModule: SerializersModule = EmptySerializersModule() | ||
|
||
@PublishedApi | ||
internal var encodeNextWith: EncodeSettings.Builder.() -> Unit = { | ||
encodeDefaults = true | ||
} | ||
|
||
/** | ||
* Sets the [EncodeSettings.Builder] to apply to the next field values added. | ||
* Updating this value will only influence the encoding of field values not yet added to the update. | ||
* This allows for custom encoding per value, e.g. | ||
* | ||
* ``` | ||
* encodeNextWith { encodeDefaults = true } | ||
* add(ClassWithDefaults()) | ||
* encodeNextWith { encodeDefaults = false } | ||
* add(ClassWithDefaults()) | ||
* ``` | ||
*/ | ||
public fun encodeNextWith(builder: EncodeSettings.Builder.() -> Unit) { | ||
encodeNextWith = builder | ||
internal val fieldValuesToAdd: MutableList<() -> Any> = mutableListOf() | ||
internal val fieldValues = fieldValuesToAdd.map { valueToEncode -> | ||
valueToEncode.invoke() | ||
} | ||
|
||
/** | ||
* Adds a field value to the [Query] | ||
* The [value] will be encoded according to the [EncodeSettings] set by this builder. | ||
* @param T the type of the value to add | ||
* @param value the value [T] to add | ||
*/ | ||
public inline fun <reified T> add(value: T) { | ||
addEncoded(encode(value, encodeNextWith)!!) | ||
fieldValuesToAdd.add { | ||
encode(value, { copyFrom(this@FieldValuesDSL) })!! | ||
} | ||
} | ||
|
||
/** | ||
* Adds a field value to the [Query] | ||
* The [value] will be encoded according to the [EncodeSettings] set by this builder. | ||
* @param T the type of the value to add | ||
* @param strategy the [SerializationStrategy] to apply to the value | ||
* @param value the value [T] to add | ||
*/ | ||
public fun <T : Any> addWithStrategy(strategy: SerializationStrategy<T>, value: T) { | ||
addEncoded(dev.gitlive.firebase.internal.encode(strategy, value, encodeNextWith)!!) | ||
fieldValuesToAdd.add { | ||
dev.gitlive.firebase.internal.encode(strategy, value, { copyFrom(this@FieldValuesDSL) })!! | ||
} | ||
} | ||
|
||
@PublishedApi | ||
internal fun addEncoded(encodedValue: Any) { | ||
fieldValues += encodedValue | ||
/** | ||
* Provides an accessor for encoding values with [EncodeSettings] | ||
* @param dls the [FieldValuesDSL] to specify the [EncodeSettings] and values to add | ||
*/ | ||
public fun withEncodeSettings(dls: FieldValuesDSL.() -> Unit) { | ||
fieldValuesToAdd.addAll( | ||
FieldValuesDSL() | ||
.apply { copyFrom(this@FieldValuesDSL) } | ||
.apply(dls).fieldValuesToAdd, | ||
) | ||
} | ||
} |
Oops, something went wrong.