Skip to content

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

Merged
merged 15 commits into from
Jan 13, 2022
Merged

Conversation

corrado4eyes
Copy link

#35

@corrado4eyes corrado4eyes marked this pull request as draft January 7, 2022 17:18
@corrado4eyes corrado4eyes marked this pull request as ready for review January 10, 2022 09:03
Comment on lines 143 to 161
@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 }
}

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)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 125 to 139
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 }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as Android

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 102 to 126
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
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as update bellow.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 89 to 113
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
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for JS

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 129 to 146
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 }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for JS

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChristoferAlexander ChristoferAlexander changed the title Add set/update extended methods. Add set/update extended methods Jan 10, 2022
@corrado4eyes corrado4eyes merged commit b2463cc into prerelease Jan 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants