Skip to content

Commit

Permalink
update String.toFlag
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 d4289b8 commit 528dcaf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
14 changes: 3 additions & 11 deletions sdk/src/main/java/com/amplitude/experiment/storage/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package com.amplitude.experiment.storage

import com.amplitude.experiment.Variant
import com.amplitude.experiment.evaluation.EvaluationFlag
import com.amplitude.experiment.evaluation.json
import com.amplitude.experiment.util.toFlag
import com.amplitude.experiment.util.toVariant
import com.amplitude.experiment.util.toJson
import kotlinx.serialization.decodeFromString
import org.json.JSONObject

internal class LoadStoreCache<V : Any>(
private val namespace: String,
Expand Down Expand Up @@ -42,11 +39,6 @@ internal class LoadStoreCache<V : Any>(

fun load() = synchronized(cache) {
val rawValues = storage.get(namespace)
if (rawValues == null) {
clear()
return
}

val values = rawValues.mapNotNull { entry ->
try {
val value = transformer.invoke(entry.value)
Expand Down Expand Up @@ -100,11 +92,11 @@ internal fun transformVariantFromStorage(storageValue: String): Variant? {
return storageValue.toVariant()
}

private fun transformFlagFromStorage(storageValue: String): EvaluationFlag? {
return json.decodeFromString<JSONObject>(storageValue).toFlag()
internal fun transformFlagFromStorage(storageValue: String): EvaluationFlag? {
return storageValue.toFlag()
}

private fun transformStringFromV(value: Any): String? {
internal fun transformStringFromV(value: Any): String? {
return when (value) {
is Variant -> value.toJson()
is EvaluationFlag -> value.toJson()
Expand Down
9 changes: 8 additions & 1 deletion sdk/src/main/java/com/amplitude/experiment/util/Flag.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.amplitude.experiment.util

import com.amplitude.experiment.Variant
import com.amplitude.experiment.evaluation.EvaluationFlag
import com.amplitude.experiment.evaluation.EvaluationSegment
import com.amplitude.experiment.evaluation.EvaluationVariant
import org.json.JSONException
import org.json.JSONObject

internal fun String?.toFlag(): EvaluationFlag? {
return if (this == null) {
return null
} else {
JSONObject(this).toFlag()
}
}

internal fun EvaluationFlag.toJson(): String {
val jsonObject = JSONObject()
try {
Expand Down

0 comments on commit 528dcaf

Please sign in to comment.