Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit 5dd2a27

Browse files
Use WCMetaDataValue when adding metadata to a new product
1 parent f30d1b6 commit 5dd2a27

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/metadata/WCMetaData.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ data class WCMetaData(
2525
val displayValue: WCMetaDataValue? = null
2626
) {
2727
constructor(id: Long, key: String, value: String) : this(id, key,
28-
WCMetaDataValue.fromRawString(value)
28+
WCMetaDataValue(value)
2929
)
3030

3131
/**

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/metadata/WCMetaDataValue.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ sealed class WCMetaDataValue {
7878
}
7979

8080
companion object {
81+
operator fun invoke(value: String?): WCMetaDataValue {
82+
if (value == null) return StringValue(null)
83+
84+
return runCatching { JsonParser().parse(value) }
85+
.getOrElse { JsonPrimitive(value) }
86+
.let { fromJsonElement(it) }
87+
}
88+
operator fun invoke(value: Number): WCMetaDataValue = NumberValue(value)
89+
operator fun invoke(value: Boolean): WCMetaDataValue = BooleanValue(value)
90+
8191
internal fun fromJsonElement(element: JsonElement): WCMetaDataValue {
8292
return when {
8393
element.isJsonPrimitive -> {
@@ -95,10 +105,5 @@ sealed class WCMetaDataValue {
95105
else -> StringValue(element.toString())
96106
}
97107
}
98-
99-
fun fromRawString(value: String): WCMetaDataValue =
100-
runCatching { JsonParser().parse(value) }
101-
.getOrElse { JsonPrimitive(value) }
102-
.let { fromJsonElement(it) }
103108
}
104109
}

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/product/ProductRestClient.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.wordpress.android.fluxc.model.WCProductTagModel
2222
import org.wordpress.android.fluxc.model.WCProductVariationModel
2323
import org.wordpress.android.fluxc.model.metadata.MetadataChanges
2424
import org.wordpress.android.fluxc.model.metadata.WCMetaData
25+
import org.wordpress.android.fluxc.model.metadata.WCMetaDataValue
2526
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType
2627
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.PARSE_ERROR
2728
import org.wordpress.android.fluxc.network.rest.wpapi.WPAPINetworkError
@@ -1724,7 +1725,7 @@ class ProductRestClient @Inject constructor(
17241725
fun addProduct(
17251726
site: SiteModel,
17261727
productModel: WCProductModel,
1727-
metaData: Map<String, String>? = null
1728+
metaData: Map<String, WCMetaDataValue>? = null
17281729
) {
17291730
coroutineEngine.launch(AppLog.T.API, this, "addProduct") {
17301731
val url = WOOCOMMERCE.products.pathV3
@@ -1736,7 +1737,7 @@ class ProductRestClient @Inject constructor(
17361737
it.forEach { (key, value) ->
17371738
add(JsonObject().apply {
17381739
addProperty(WCMetaData.KEY, key)
1739-
addProperty(WCMetaData.VALUE, value)
1740+
add(WCMetaData.VALUE, value.jsonValue)
17401741
})
17411742
}
17421743
}

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/store/WCProductStore.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import org.wordpress.android.fluxc.model.WCProductVariationModel.ProductVariantO
2727
import org.wordpress.android.fluxc.model.addons.RemoteAddonDto
2828
import org.wordpress.android.fluxc.model.metadata.MetadataChanges
2929
import org.wordpress.android.fluxc.model.metadata.WCMetaData
30+
import org.wordpress.android.fluxc.model.metadata.WCMetaDataValue
3031
import org.wordpress.android.fluxc.model.metadata.get
3132
import org.wordpress.android.fluxc.network.BaseRequest.BaseNetworkError
3233
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType
@@ -305,7 +306,7 @@ class WCProductStore @Inject constructor(
305306
class AddProductPayload(
306307
var site: SiteModel,
307308
val product: WCProductModel,
308-
val metadata: Map<String, String>? = null
309+
val metadata: Map<String, WCMetaDataValue>? = null
309310
) : Payload<BaseNetworkError>()
310311

311312
class DeleteProductPayload(

plugins/woocommerce/src/test/java/org/wordpress/android/fluxc/model/WCMetaDataValueTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,37 @@ import org.wordpress.android.fluxc.model.metadata.WCMetaDataValue
66
class WCMetaDataValueTest {
77
@Test
88
fun `when given a plain string, should return a StringValue`() {
9-
val string = WCMetaDataValue.fromRawString("string")
9+
val string = WCMetaDataValue("string")
1010
assert(string is WCMetaDataValue.StringValue)
1111
}
1212

1313
@Test
1414
fun `when given a number, should return a NumberValue`() {
15-
val number = WCMetaDataValue.fromRawString("1")
15+
val number = WCMetaDataValue("1")
1616
assert(number is WCMetaDataValue.NumberValue)
1717
}
1818

1919
@Test
2020
fun `when given a float, should return a NumberValue`() {
21-
val number = WCMetaDataValue.fromRawString("1.5")
21+
val number = WCMetaDataValue("1.5")
2222
assert(number is WCMetaDataValue.NumberValue)
2323
}
2424

2525
@Test
2626
fun `when given a boolean, should return a BooleanValue`() {
27-
val boolean = WCMetaDataValue.fromRawString("true")
27+
val boolean = WCMetaDataValue("true")
2828
assert(boolean is WCMetaDataValue.BooleanValue)
2929
}
3030

3131
@Test
3232
fun `when given a JSON object, should return a JsonObjectValue`() {
33-
val jsonObject = WCMetaDataValue.fromRawString("""{"key":"value"}""")
33+
val jsonObject = WCMetaDataValue("""{"key":"value"}""")
3434
assert(jsonObject is WCMetaDataValue.JsonObjectValue)
3535
}
3636

3737
@Test
3838
fun `when given a JSON array, should return a JsonArrayValue`() {
39-
val jsonArray = WCMetaDataValue.fromRawString("""[{"key":"value"}]""")
39+
val jsonArray = WCMetaDataValue("""[{"key":"value"}]""")
4040
assert(jsonArray is WCMetaDataValue.JsonArrayValue)
4141
}
4242
}

0 commit comments

Comments
 (0)