-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'trunk' into 2757-parse-invalid-coupon-error
- Loading branch information
Showing
8 changed files
with
326 additions
and
10 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...ins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/StripProductMetaData.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,33 @@ | ||
package org.wordpress.android.fluxc.model | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.JsonArray | ||
import com.google.gson.JsonObject | ||
import org.wordpress.android.fluxc.model.WCProductModel.AddOnsMetadataKeys | ||
import org.wordpress.android.fluxc.model.WCProductModel.QuantityRulesMetadataKeys | ||
import org.wordpress.android.fluxc.utils.EMPTY_JSON_ARRAY | ||
import org.wordpress.android.fluxc.utils.isElementNullOrEmpty | ||
import javax.inject.Inject | ||
|
||
class StripProductMetaData @Inject internal constructor(private val gson: Gson) { | ||
operator fun invoke(metadata: String?): String { | ||
if (metadata.isNullOrEmpty()) return EMPTY_JSON_ARRAY | ||
|
||
return gson.fromJson(metadata, JsonArray::class.java) | ||
.mapNotNull { it as? JsonObject } | ||
.asSequence() | ||
.filter { jsonObject -> | ||
val isNullOrEmpty = jsonObject[WCMetaData.VALUE].isElementNullOrEmpty() | ||
jsonObject[WCMetaData.KEY]?.asString.orEmpty() in SUPPORTED_KEYS && isNullOrEmpty.not() | ||
}.toList() | ||
.takeIf { it.isNotEmpty() } | ||
?.let { gson.toJson(it) } ?: EMPTY_JSON_ARRAY | ||
} | ||
|
||
companion object { | ||
val SUPPORTED_KEYS: Set<String> = buildSet { | ||
add(AddOnsMetadataKeys.ADDONS_METADATA_KEY) | ||
addAll(QuantityRulesMetadataKeys.ALL_KEYS) | ||
} | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/utils/JsonUtils.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,17 @@ | ||
package org.wordpress.android.fluxc.utils | ||
|
||
import com.google.gson.JsonElement | ||
|
||
const val EMPTY_JSON_ARRAY = "[]" | ||
|
||
fun JsonElement?.isElementNullOrEmpty(): Boolean { | ||
return this?.let { | ||
when{ | ||
this.isJsonObject -> this.asJsonObject.size() == 0 | ||
this.isJsonArray -> this.asJsonArray.size()== 0 | ||
this.isJsonPrimitive && this.asJsonPrimitive.isString -> this.asString.isEmpty() | ||
this.isJsonNull -> true | ||
else -> false | ||
} | ||
} ?: true | ||
} |
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.