Skip to content

Commit

Permalink
Use different encodeSettings approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Nov 14, 2024
1 parent 545eea2 commit 59fb0fd
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 171 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ documentRef.updateFields {
// Set the value of otherField to "1" using a custom Serializer
"otherField".to(IntAsStringSerializer(), 1)

// Overwrite build settings. All fields added after this will have these build settings applied
encodeNextWith {
// Overwrite build settings. All fields added within this block will have these build settings applied
withEncodeSettings {
encodeDefaults = true
serializersModule = module
"city" to abstractCity
}
"city" to abstractCity
}
```

Expand All @@ -214,12 +214,12 @@ query.orderBy("field", "otherField", "city").startAtFieldValues { // similar syn
// Starts at "1" for the otherField value
add(1, IntAsStringSerializer())

// Overwrite build settings. All field values added after this will have these build settings applied
encodeNextWith {
// Overwrite build settings. All field values added within this block will have these build settings applied
withEncodeSettings {
encodeDefaults = true
serializersModule = module
add(abstractCity)
}
add(abstractCity)
}
```

Expand Down
5 changes: 5 additions & 0 deletions firebase-common/api/android/firebase-common.api
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public abstract interface class dev/gitlive/firebase/EncodeDecodeSettings {
public abstract interface class dev/gitlive/firebase/EncodeDecodeSettingsBuilder : dev/gitlive/firebase/DecodeSettings$Builder, dev/gitlive/firebase/EncodeSettings$Builder {
}

public final class dev/gitlive/firebase/EncodeDecodeSettingsKt {
public static final fun copyFrom (Ldev/gitlive/firebase/DecodeSettings$Builder;Ldev/gitlive/firebase/DecodeSettings$Builder;)V
public static final fun copyFrom (Ldev/gitlive/firebase/EncodeSettings$Builder;Ldev/gitlive/firebase/EncodeSettings$Builder;)V
}

public abstract interface class dev/gitlive/firebase/EncodeSettings : dev/gitlive/firebase/EncodeDecodeSettings {
public abstract fun getEncodeDefaults ()Z
}
Expand Down
5 changes: 5 additions & 0 deletions firebase-common/api/jvm/firebase-common.api
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public abstract interface class dev/gitlive/firebase/EncodeDecodeSettings {
public abstract interface class dev/gitlive/firebase/EncodeDecodeSettingsBuilder : dev/gitlive/firebase/DecodeSettings$Builder, dev/gitlive/firebase/EncodeSettings$Builder {
}

public final class dev/gitlive/firebase/EncodeDecodeSettingsKt {
public static final fun copyFrom (Ldev/gitlive/firebase/DecodeSettings$Builder;Ldev/gitlive/firebase/DecodeSettings$Builder;)V
public static final fun copyFrom (Ldev/gitlive/firebase/EncodeSettings$Builder;Ldev/gitlive/firebase/EncodeSettings$Builder;)V
}

public abstract interface class dev/gitlive/firebase/EncodeSettings : dev/gitlive/firebase/EncodeDecodeSettings {
public abstract fun getEncodeDefaults ()Z
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public interface EncodeSettings : EncodeDecodeSettings {
}
}

public fun EncodeSettings.Builder.copyFrom(other: EncodeSettings.Builder) {
encodeDefaults = other.encodeDefaults
serializersModule = other.serializersModule
}

/**
* [EncodeDecodeSettings] used when decoding an object
* @param serializersModule the [SerializersModule] to use for deserialization. This allows for polymorphic serialization on runtime
Expand All @@ -38,6 +43,10 @@ public interface DecodeSettings : EncodeDecodeSettings {
}
}

public fun DecodeSettings.Builder.copyFrom(other: DecodeSettings.Builder) {
serializersModule = other.serializersModule
}

public interface EncodeDecodeSettingsBuilder :
EncodeSettings.Builder,
DecodeSettings.Builder
42 changes: 25 additions & 17 deletions firebase-firestore/api/android/firebase-firestore.api
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,27 @@ 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/FieldValuesDSL {
public final fun addEncoded (Ljava/lang/Object;)V
public final class dev/gitlive/firebase/firestore/FieldValuesDSL : dev/gitlive/firebase/EncodeSettings$Builder {
public final fun addWithStrategy (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)V
public final fun encodeNextWith (Lkotlin/jvm/functions/Function1;)V
public final fun getEncodeNextWith ()Lkotlin/jvm/functions/Function1;
public final fun setEncodeNextWith (Lkotlin/jvm/functions/Function1;)V
}

public final class dev/gitlive/firebase/firestore/FieldsAndValuesUpdateDSL {
public final fun encodeNextWith (Lkotlin/jvm/functions/Function1;)V
public final fun getEncodeNextWith ()Lkotlin/jvm/functions/Function1;
public final fun setEncodeNextWith (Lkotlin/jvm/functions/Function1;)V
public fun getEncodeDefaults ()Z
public final fun getFieldValuesToAdd ()Ljava/util/List;
public fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
public fun setEncodeDefaults (Z)V
public fun setSerializersModule (Lkotlinx/serialization/modules/SerializersModule;)V
public final fun withEncodeSettings (Lkotlin/jvm/functions/Function1;)V
}

public final class dev/gitlive/firebase/firestore/FieldsAndValuesUpdateDSL : dev/gitlive/firebase/EncodeSettings$Builder {
public fun getEncodeDefaults ()Z
public final fun getFieldAndValueToAdd ()Ljava/util/List;
public fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
public fun setEncodeDefaults (Z)V
public fun setSerializersModule (Lkotlinx/serialization/modules/SerializersModule;)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 final fun toEncoded (Ldev/gitlive/firebase/firestore/FieldPath;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/internal/FieldAndValue$WithFieldPath;
public final fun toEncoded (Ljava/lang/String;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/internal/FieldAndValue$WithStringField;
public final fun withEncodeSettings (Lkotlin/jvm/functions/Function1;)V
}

public abstract class dev/gitlive/firebase/firestore/Filter {
Expand Down Expand Up @@ -232,18 +237,19 @@ public abstract class dev/gitlive/firebase/firestore/Filter$WithConstraint : dev
public abstract fun getConstraint ()Ldev/gitlive/firebase/firestore/WhereConstraint;
}

public final class dev/gitlive/firebase/firestore/FilterBuilder {
public final class dev/gitlive/firebase/firestore/FilterBuilder : dev/gitlive/firebase/EncodeSettings$Builder {
public final fun all ([Ldev/gitlive/firebase/firestore/Filter;)Ldev/gitlive/firebase/firestore/Filter;
public final fun and (Ldev/gitlive/firebase/firestore/Filter;Ldev/gitlive/firebase/firestore/Filter;)Ldev/gitlive/firebase/firestore/Filter$And;
public final fun any ([Ldev/gitlive/firebase/firestore/Filter;)Ldev/gitlive/firebase/firestore/Filter;
public final fun contains (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun contains (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun containsAny (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/util/List;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun containsAny (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/util/List;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun encodeNextWith (Lkotlin/jvm/functions/Function1;)V
public final fun equalTo (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun equalTo (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun getEncodeNextWith ()Lkotlin/jvm/functions/Function1;
public fun getEncodeDefaults ()Z
public final fun getEncodeSettingsBuilder ()Lkotlin/jvm/functions/Function1;
public fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
public final fun greaterThan (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun greaterThan (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun greaterThanOrEqualTo (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
Expand All @@ -263,7 +269,9 @@ public final class dev/gitlive/firebase/firestore/FilterBuilder {
public final fun notInArray (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/util/List;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun notInArray (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/util/List;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun or (Ldev/gitlive/firebase/firestore/Filter;Ldev/gitlive/firebase/firestore/Filter;)Ldev/gitlive/firebase/firestore/Filter$Or;
public final fun setEncodeNextWith (Lkotlin/jvm/functions/Function1;)V
public fun setEncodeDefaults (Z)V
public fun setSerializersModule (Lkotlinx/serialization/modules/SerializersModule;)V
public final fun withEncoder (Lkotlin/jvm/functions/Function1;)Ldev/gitlive/firebase/firestore/Filter;
}

public final class dev/gitlive/firebase/firestore/FirebaseFirestore {
Expand Down
42 changes: 25 additions & 17 deletions firebase-firestore/api/jvm/firebase-firestore.api
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,27 @@ 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/FieldValuesDSL {
public final fun addEncoded (Ljava/lang/Object;)V
public final class dev/gitlive/firebase/firestore/FieldValuesDSL : dev/gitlive/firebase/EncodeSettings$Builder {
public final fun addWithStrategy (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)V
public final fun encodeNextWith (Lkotlin/jvm/functions/Function1;)V
public final fun getEncodeNextWith ()Lkotlin/jvm/functions/Function1;
public final fun setEncodeNextWith (Lkotlin/jvm/functions/Function1;)V
}

public final class dev/gitlive/firebase/firestore/FieldsAndValuesUpdateDSL {
public final fun encodeNextWith (Lkotlin/jvm/functions/Function1;)V
public final fun getEncodeNextWith ()Lkotlin/jvm/functions/Function1;
public final fun setEncodeNextWith (Lkotlin/jvm/functions/Function1;)V
public fun getEncodeDefaults ()Z
public final fun getFieldValuesToAdd ()Ljava/util/List;
public fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
public fun setEncodeDefaults (Z)V
public fun setSerializersModule (Lkotlinx/serialization/modules/SerializersModule;)V
public final fun withEncodeSettings (Lkotlin/jvm/functions/Function1;)V
}

public final class dev/gitlive/firebase/firestore/FieldsAndValuesUpdateDSL : dev/gitlive/firebase/EncodeSettings$Builder {
public fun getEncodeDefaults ()Z
public final fun getFieldAndValueToAdd ()Ljava/util/List;
public fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
public fun setEncodeDefaults (Z)V
public fun setSerializersModule (Lkotlinx/serialization/modules/SerializersModule;)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 final fun toEncoded (Ldev/gitlive/firebase/firestore/FieldPath;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/internal/FieldAndValue$WithFieldPath;
public final fun toEncoded (Ljava/lang/String;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/internal/FieldAndValue$WithStringField;
public final fun withEncodeSettings (Lkotlin/jvm/functions/Function1;)V
}

public abstract class dev/gitlive/firebase/firestore/Filter {
Expand Down Expand Up @@ -232,18 +237,19 @@ public abstract class dev/gitlive/firebase/firestore/Filter$WithConstraint : dev
public abstract fun getConstraint ()Ldev/gitlive/firebase/firestore/WhereConstraint;
}

public final class dev/gitlive/firebase/firestore/FilterBuilder {
public final class dev/gitlive/firebase/firestore/FilterBuilder : dev/gitlive/firebase/EncodeSettings$Builder {
public final fun all ([Ldev/gitlive/firebase/firestore/Filter;)Ldev/gitlive/firebase/firestore/Filter;
public final fun and (Ldev/gitlive/firebase/firestore/Filter;Ldev/gitlive/firebase/firestore/Filter;)Ldev/gitlive/firebase/firestore/Filter$And;
public final fun any ([Ldev/gitlive/firebase/firestore/Filter;)Ldev/gitlive/firebase/firestore/Filter;
public final fun contains (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun contains (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun containsAny (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/util/List;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun containsAny (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/util/List;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun encodeNextWith (Lkotlin/jvm/functions/Function1;)V
public final fun equalTo (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun equalTo (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun getEncodeNextWith ()Lkotlin/jvm/functions/Function1;
public fun getEncodeDefaults ()Z
public final fun getEncodeSettingsBuilder ()Lkotlin/jvm/functions/Function1;
public fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
public final fun greaterThan (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun greaterThan (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun greaterThanOrEqualTo (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
Expand All @@ -263,7 +269,9 @@ public final class dev/gitlive/firebase/firestore/FilterBuilder {
public final fun notInArray (Ldev/gitlive/firebase/firestore/FieldPath;Lkotlinx/serialization/SerializationStrategy;Ljava/util/List;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun notInArray (Ljava/lang/String;Lkotlinx/serialization/SerializationStrategy;Ljava/util/List;)Ldev/gitlive/firebase/firestore/Filter$WithConstraint;
public final fun or (Ldev/gitlive/firebase/firestore/Filter;Ldev/gitlive/firebase/firestore/Filter;)Ldev/gitlive/firebase/firestore/Filter$Or;
public final fun setEncodeNextWith (Lkotlin/jvm/functions/Function1;)V
public fun setEncodeDefaults (Z)V
public fun setSerializersModule (Lkotlinx/serialization/modules/SerializersModule;)V
public final fun withEncoder (Lkotlin/jvm/functions/Function1;)Ldev/gitlive/firebase/firestore/Filter;
}

public final class dev/gitlive/firebase/firestore/FirebaseFirestore {
Expand Down
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,
)
}
}
Loading

0 comments on commit 59fb0fd

Please sign in to comment.