Skip to content

Commit

Permalink
update String.toVariant payload handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Yiu authored and Tim Yiu committed Sep 22, 2023
1 parent 50685c6 commit 58be549
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
17 changes: 11 additions & 6 deletions sdk/src/main/java/com/amplitude/experiment/util/Variant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ internal fun String?.toVariant(): Variant? {
return if (this == null) {
null
} else {
try {
JSONObject(this).toVariant()
} catch (e: JSONException) {
Variant(key = this, value = this)
}
JSONObject(this).toVariant()
}
}

Expand All @@ -59,9 +55,18 @@ internal fun JSONObject?.toVariant(): Variant? {
}

val payload = when {
has("payload") -> get("payload")
has("payload") -> {
val payload = get("payload")
if (payload is JSONObject) {
payload.toMap()
} else {
payload
}
}

else -> null
}

var expKey = when {
has("expKey") -> getString("expKey")
else -> null
Expand Down
30 changes: 10 additions & 20 deletions sdk/src/test/java/com/amplitude/experiment/StorageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ import org.junit.Test

class TransformVariantFromStorageTest {

private val payload = "payload"

@Test
fun `v0 variant transformation`() {
Assert.assertEquals(
Variant(key = "on", value = "on"),
transformVariantFromStorage("on")
)
}

@Test
fun `v1 variant transformation`() {
val storedVariant = JSONObject(mapOf("value" to "on")).toString()
Expand All @@ -31,11 +21,11 @@ class TransformVariantFromStorageTest {
val storedVariant = JSONObject(
mapOf(
"value" to "on",
"payload" to payload
"payload" to mapOf("k" to "v")
)
).toString()
Assert.assertEquals(
Variant(key = "on", value = "on", payload = payload),
Variant(key = "on", value = "on", payload = mapOf("k" to "v")),
transformVariantFromStorage(storedVariant)
)
}
Expand All @@ -45,15 +35,15 @@ class TransformVariantFromStorageTest {
val storedVariant = JSONObject(
mapOf(
"value" to "on",
"payload" to payload,
"payload" to mapOf("k" to "v"),
"expKey" to "exp-1"
)
).toString()
Assert.assertEquals(
Variant(
key = "on",
value = "on",
payload = payload,
payload = mapOf("k" to "v"),
expKey = "exp-1",
metadata = mapOf("experimentKey" to "exp-1")
),
Expand Down Expand Up @@ -81,14 +71,14 @@ class TransformVariantFromStorageTest {
mapOf(
"key" to "treatment",
"value" to "on",
"payload" to payload
"payload" to mapOf("k" to "v")
)
).toString()
Assert.assertEquals(
Variant(
key = "treatment",
value = "on",
payload = payload
payload = mapOf("k" to "v")
),
transformVariantFromStorage(storedVariant)
)
Expand All @@ -100,15 +90,15 @@ class TransformVariantFromStorageTest {
mapOf(
"key" to "treatment",
"value" to "on",
"payload" to payload,
"payload" to mapOf("k" to "v"),
"expKey" to "exp-1"
)
).toString()
Assert.assertEquals(
Variant(
key = "treatment",
value = "on",
payload = payload,
payload = mapOf("k" to "v"),
expKey = "exp-1",
metadata = mapOf("experimentKey" to "exp-1")
),
Expand All @@ -122,15 +112,15 @@ class TransformVariantFromStorageTest {
mapOf(
"key" to "treatment",
"value" to "on",
"payload" to payload,
"payload" to mapOf("k" to "v"),
"metadata" to mapOf("experimentKey" to "exp-1")
)
).toString()
Assert.assertEquals(
Variant(
key = "treatment",
value = "on",
payload = payload,
payload = mapOf("k" to "v"),
expKey = "exp-1",
metadata = mapOf("experimentKey" to "exp-1")
),
Expand Down

0 comments on commit 58be549

Please sign in to comment.