Skip to content

Commit

Permalink
Use WCMetaDataValue when adding metadata to a new product
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Oct 9, 2024
1 parent f30d1b6 commit 5dd2a27
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data class WCMetaData(
val displayValue: WCMetaDataValue? = null
) {
constructor(id: Long, key: String, value: String) : this(id, key,
WCMetaDataValue.fromRawString(value)
WCMetaDataValue(value)
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

internal fun fromJsonElement(element: JsonElement): WCMetaDataValue {
return when {
element.isJsonPrimitive -> {
Expand All @@ -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) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.wordpress.android.fluxc.model.WCProductTagModel
import org.wordpress.android.fluxc.model.WCProductVariationModel
import org.wordpress.android.fluxc.model.metadata.MetadataChanges
import org.wordpress.android.fluxc.model.metadata.WCMetaData
import org.wordpress.android.fluxc.model.metadata.WCMetaDataValue
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.PARSE_ERROR
import org.wordpress.android.fluxc.network.rest.wpapi.WPAPINetworkError
Expand Down Expand Up @@ -1724,7 +1725,7 @@ class ProductRestClient @Inject constructor(
fun addProduct(
site: SiteModel,
productModel: WCProductModel,
metaData: Map<String, String>? = null
metaData: Map<String, WCMetaDataValue>? = null
) {
coroutineEngine.launch(AppLog.T.API, this, "addProduct") {
val url = WOOCOMMERCE.products.pathV3
Expand All @@ -1736,7 +1737,7 @@ class ProductRestClient @Inject constructor(
it.forEach { (key, value) ->
add(JsonObject().apply {
addProperty(WCMetaData.KEY, key)
addProperty(WCMetaData.VALUE, value)
add(WCMetaData.VALUE, value.jsonValue)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.wordpress.android.fluxc.model.WCProductVariationModel.ProductVariantO
import org.wordpress.android.fluxc.model.addons.RemoteAddonDto
import org.wordpress.android.fluxc.model.metadata.MetadataChanges
import org.wordpress.android.fluxc.model.metadata.WCMetaData
import org.wordpress.android.fluxc.model.metadata.WCMetaDataValue
import org.wordpress.android.fluxc.model.metadata.get
import org.wordpress.android.fluxc.network.BaseRequest.BaseNetworkError
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType
Expand Down Expand Up @@ -305,7 +306,7 @@ class WCProductStore @Inject constructor(
class AddProductPayload(
var site: SiteModel,
val product: WCProductModel,
val metadata: Map<String, String>? = null
val metadata: Map<String, WCMetaDataValue>? = null
) : Payload<BaseNetworkError>()

class DeleteProductPayload(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@ import org.wordpress.android.fluxc.model.metadata.WCMetaDataValue
class WCMetaDataValueTest {
@Test
fun `when given a plain string, should return a StringValue`() {
val string = WCMetaDataValue.fromRawString("string")
val string = WCMetaDataValue("string")
assert(string is WCMetaDataValue.StringValue)
}

@Test
fun `when given a number, should return a NumberValue`() {
val number = WCMetaDataValue.fromRawString("1")
val number = WCMetaDataValue("1")
assert(number is WCMetaDataValue.NumberValue)
}

@Test
fun `when given a float, should return a NumberValue`() {
val number = WCMetaDataValue.fromRawString("1.5")
val number = WCMetaDataValue("1.5")
assert(number is WCMetaDataValue.NumberValue)
}

@Test
fun `when given a boolean, should return a BooleanValue`() {
val boolean = WCMetaDataValue.fromRawString("true")
val boolean = WCMetaDataValue("true")
assert(boolean is WCMetaDataValue.BooleanValue)
}

@Test
fun `when given a JSON object, should return a JsonObjectValue`() {
val jsonObject = WCMetaDataValue.fromRawString("""{"key":"value"}""")
val jsonObject = WCMetaDataValue("""{"key":"value"}""")
assert(jsonObject is WCMetaDataValue.JsonObjectValue)
}

@Test
fun `when given a JSON array, should return a JsonArrayValue`() {
val jsonArray = WCMetaDataValue.fromRawString("""[{"key":"value"}]""")
val jsonArray = WCMetaDataValue("""[{"key":"value"}]""")
assert(jsonArray is WCMetaDataValue.JsonArrayValue)
}
}

0 comments on commit 5dd2a27

Please sign in to comment.