-
Notifications
You must be signed in to change notification settings - Fork 2
Add set/update extended methods #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@JvmName("updateFieldsExtended") | ||
actual inline fun <reified T> update( | ||
documentRef: DocumentReference, | ||
strategy: SerializationStrategy<T>, | ||
data: T, | ||
encodeDefaults: Boolean, | ||
vararg fieldsAndValues: Pair<String, Any?> | ||
): WriteBatch { | ||
val serializedItem = encode(data, encodeDefaults) as Map<String, Any> | ||
|
||
val serializedFieldAndValues = fieldsAndValues.takeUnless { fieldsAndValues.isEmpty() } | ||
?.map { (field, value) -> field to encode(value, encodeDefaults) }?.toMap() | ||
|
||
val result = (serializedFieldAndValues?.let { | ||
serializedItem.plus(it) | ||
} ?: serializedItem) | ||
|
||
return android.update(documentRef.android, result).let { this } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Android specific impl of new added method, basically it combines the mapping of data and fieldsAndValues into one update query. Use case example is an update on a data class A with fields A.field1 and A.field2 where field1 value needs to be updated and field2 value needs to be deleted. In that case new value for field1 is included on the "data" and field2 in the fieldsAndValues. (Due to limitation of FieldValue.delete() or FieldValue.serverTimestamp() that can only by included in fieldsAndValues and not in the data class itself)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actual inline fun <reified T> update( | ||
documentRef: DocumentReference, | ||
strategy: SerializationStrategy<T>, | ||
data: T, | ||
encodeDefaults: Boolean, | ||
vararg fieldsAndValues: Pair<String, Any?> | ||
): WriteBatch { | ||
val serializedItem = encode(strategy, data, encodeDefaults) as Map<Any?, *> | ||
val serializedFieldAndValues = fieldsAndValues.associate { (field, value) -> field to encode(value, encodeDefaults) } | ||
|
||
val result = serializedFieldAndValues?.let { | ||
serializedItem.plus(it) | ||
} ?: serializedItem | ||
return ios.updateData(result, documentRef.ios).let { this } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as Android
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actual fun <T> set( | ||
documentRef: DocumentReference, | ||
strategy: SerializationStrategy<T>, | ||
data: T, | ||
encodeDefaults: Boolean, | ||
merge: Boolean, | ||
vararg fieldsAndValues: Pair<String, Any?> | ||
): WriteBatch { | ||
val serializedItem = encode(strategy, data, encodeDefaults) as Map<String, Any>? | ||
val serializedFieldAndValues = fieldsAndValues.takeUnless { fieldsAndValues.isEmpty() } | ||
?.map { (field, value) -> field to encode(value, encodeDefaults) }?.toMap() | ||
|
||
val result = serializedItem?.let { item -> | ||
serializedFieldAndValues?.let { fieldsAndValues -> | ||
item + fieldsAndValues | ||
} | ||
} as Any? ?: return this | ||
|
||
if (merge) { | ||
android.set(documentRef.android, result, SetOptions.merge()) | ||
} else { | ||
android.set(documentRef.android, result) | ||
} | ||
return this | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as update bellow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt
Outdated
Show resolved
Hide resolved
actual fun <T> set( | ||
documentRef: DocumentReference, | ||
strategy: SerializationStrategy<T>, | ||
data: T, | ||
encodeDefaults: Boolean, | ||
merge: Boolean, | ||
vararg fieldsAndValues: Pair<String, Any?> | ||
): WriteBatch { | ||
val serializedItem = encode(strategy, data, encodeDefaults) as Map<String, Any>? | ||
val serializedFieldAndValues = fieldsAndValues.takeUnless { fieldsAndValues.isEmpty() } | ||
?.map { (field, value) -> field to encode(value, encodeDefaults) }?.toMap() | ||
|
||
val result = serializedItem?.let { item -> | ||
serializedFieldAndValues?.let { fieldsAndValues -> | ||
item + fieldsAndValues | ||
} | ||
} as Any? ?: return this | ||
|
||
if (merge) { | ||
js.set(documentRef.js, result, json("merge" to merge)) | ||
} else { | ||
js.set(documentRef.js, result) | ||
} | ||
return this | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for JS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actual inline fun <reified T> update( | ||
documentRef: DocumentReference, | ||
strategy: SerializationStrategy<T>, | ||
data: T, | ||
encodeDefaults: Boolean, | ||
vararg fieldsAndValues: Pair<String, Any?> | ||
): WriteBatch { | ||
val serializedItem = encode(data, encodeDefaults) as Map<String, Any> | ||
|
||
val serializedFieldAndValues = fieldsAndValues.takeUnless { fieldsAndValues.isEmpty() } | ||
?.map { (field, value) -> field to encode(value, encodeDefaults) }?.toMap() | ||
|
||
val result = (serializedFieldAndValues?.let { | ||
serializedItem.plus(it) | ||
} ?: serializedItem) | ||
|
||
return js.update(documentRef.js, result).let { this } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for JS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
firebase-firestore/src/iosMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt
Show resolved
Hide resolved
…estore/firestore.kt
…ebase-kotlin-sdk into 35-add-batch-methods
…tlin-sdk into 35-add-batch-methods
#35