Skip to content

Commit

Permalink
refactor json utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejo committed Jun 20, 2023
1 parent db3112a commit a4e1737
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,26 @@ package org.wordpress.android.fluxc.model

import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonElement
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.JsonStringUtils
import org.wordpress.android.fluxc.utils.isJsonEmptyElement
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 JsonStringUtils.EMPTY.ARRAY
if (metadata.isNullOrEmpty()) return EMPTY_JSON_ARRAY

return gson.fromJson(metadata, JsonArray::class.java)
.mapNotNull { it as? JsonObject }
.asSequence()
.filter { jsonObject ->
val isNullOrEmpty = isElementNullOrEmpty(jsonObject[WCMetaData.VALUE])
val isNullOrEmpty = jsonObject[WCMetaData.VALUE].isElementNullOrEmpty()
jsonObject[WCMetaData.KEY]?.asString.orEmpty() in SUPPORTED_KEYS && isNullOrEmpty.not()
}.toList()
.takeIf { it.isNotEmpty() }
?.let { gson.toJson(it) } ?: JsonStringUtils.EMPTY.ARRAY
}

private fun isElementNullOrEmpty(jsonElement: JsonElement?): Boolean {
return jsonElement?.let {
if (it.isJsonNull) return@let true

val valueString = gson.toJson(jsonElement)
valueString.isJsonEmptyElement()
} ?: true
?.let { gson.toJson(it) } ?: EMPTY_JSON_ARRAY
}

companion object {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.wordpress.android.fluxc.utils

import com.google.gson.JsonElement

const val EMPTY_JSON_ARRAY = "[]"

fun JsonElement?.isElementNullOrEmpty(): Boolean {
return this?.let {
if (it.isJsonNull) return@let true
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.assertj.core.api.Assertions
import org.junit.Test
import org.wordpress.android.fluxc.model.StripProductMetaData
import org.wordpress.android.fluxc.model.WCMetaData
import org.wordpress.android.fluxc.utils.JsonStringUtils
import org.wordpress.android.fluxc.utils.EMPTY_JSON_ARRAY

class StripProductMetaDataTest {
private val gson = Gson()
Expand All @@ -34,7 +34,7 @@ class StripProductMetaDataTest {
val metadata = getOneItemMetadata(supportedKey, value)
val result = sut.invoke(metadata)

Assertions.assertThat(result).isEqualTo(JsonStringUtils.EMPTY.ARRAY)
Assertions.assertThat(result).isEqualTo(EMPTY_JSON_ARRAY)
}

@Test
Expand All @@ -45,7 +45,7 @@ class StripProductMetaDataTest {
val metadata = getOneItemMetadata(supportedKey, value)
val result = sut.invoke(metadata)

Assertions.assertThat(result).isEqualTo(JsonStringUtils.EMPTY.ARRAY)
Assertions.assertThat(result).isEqualTo(EMPTY_JSON_ARRAY)
}

@Test
Expand All @@ -66,7 +66,7 @@ class StripProductMetaDataTest {
@Test
fun `when metadata is null, then empty array is return`() {
val result = sut.invoke(null)
Assertions.assertThat(result).isEqualTo(JsonStringUtils.EMPTY.ARRAY)
Assertions.assertThat(result).isEqualTo(EMPTY_JSON_ARRAY)
}

private fun getOneItemMetadata(itemKey: String, itemValue: String?): String {
Expand Down

0 comments on commit a4e1737

Please sign in to comment.