This repository has been archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
[Woo] Extract subscriptions outside of WCProductModel table #3106
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
202e100
Move logic of JSON conversion to make it reusable
hichamboushaba 11e901f
Update product store to allow passing metadata separately
hichamboushaba 6e1d36a
Remove the metadata column from WCProductModel
hichamboushaba bb0f237
Create a migration to drop the DB column
hichamboushaba 31b0bc0
Add documentation for the new arguments
hichamboushaba b2002e5
Fix unit tests
hichamboushaba f30d1b6
Introduce MetadataChanges class to improve usage in other stores
hichamboushaba 5dd2a27
Use WCMetaDataValue when adding metadata to a new product
hichamboushaba 58e794d
Fix typo
hichamboushaba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 41 additions & 0 deletions
41
...woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/metadata/MetadataChanges.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.wordpress.android.fluxc.model.metadata | ||
|
||
import com.google.gson.JsonArray | ||
import com.google.gson.JsonNull | ||
import com.google.gson.JsonObject | ||
|
||
data class MetadataChanges( | ||
val insertedMetadata: List<WCMetaData> = emptyList(), | ||
val updatedMetadata: List<WCMetaData> = emptyList(), | ||
val deletedMetadataIds: List<Long> = emptyList(), | ||
) { | ||
init { | ||
// The ID of inserted metadata is ignored, so to ensure that there is no data loss here, | ||
// we require that all inserted metadata have an ID of 0. | ||
require(insertedMetadata.all { it.id == 0L }) { | ||
"Inserted metadata must have an ID of 0" | ||
} | ||
} | ||
|
||
internal fun toJsonArray() = JsonArray().apply { | ||
insertedMetadata.forEach { | ||
add( | ||
JsonObject().apply { | ||
addProperty(WCMetaData.KEY, it.key) | ||
add(WCMetaData.VALUE, it.value.jsonValue) | ||
} | ||
) | ||
} | ||
updatedMetadata.forEach { | ||
add(it.toJson()) | ||
} | ||
deletedMetadataIds.forEach { | ||
add( | ||
JsonObject().apply { | ||
addProperty(WCMetaData.ID, it) | ||
add(WCMetaData.VALUE, JsonNull.INSTANCE) | ||
} | ||
) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,16 @@ sealed class WCMetaDataValue { | |
} | ||
|
||
companion object { | ||
operator fun invoke(value: String?): WCMetaDataValue { | ||
if (value == null) return StringValue(null) | ||
|
||
return runCatching { JsonParser().parse(value) } | ||
.getOrElse { JsonPrimitive(value) } | ||
.let { fromJsonElement(it) } | ||
} | ||
operator fun invoke(value: Number): WCMetaDataValue = NumberValue(value) | ||
operator fun invoke(value: Boolean): WCMetaDataValue = BooleanValue(value) | ||
Comment on lines
+81
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a nicer way to instantiate the values instead of the |
||
|
||
internal fun fromJsonElement(element: JsonElement): WCMetaDataValue { | ||
return when { | ||
element.isJsonPrimitive -> { | ||
|
@@ -95,10 +105,5 @@ sealed class WCMetaDataValue { | |
else -> StringValue(element.toString()) | ||
} | ||
} | ||
|
||
fun fromRawString(value: String): WCMetaDataValue = | ||
runCatching { JsonParser().parse(value) } | ||
.getOrElse { JsonPrimitive(value) } | ||
.let { fromJsonElement(it) } | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I extracted this bit to a separate class, this class can now be used in other places when needed (now it's used in WCProductStore, but can be used for orders as well), while the
UpdateMetadataRequest
will still be used for the MetadataStore operations.My goal of extracting this is to not have to deal with unneeded information for the other usages (I mean
parentItemId
andparentItemType
).