From 7cd87334bf018ac4b2e1716b51a394a83222865e Mon Sep 17 00:00:00 2001 From: uniumuniu Date: Thu, 9 Nov 2023 12:14:52 +0100 Subject: [PATCH] Fix deserializaing of VariableValue --- .../featurevisor/sdk/Instance+Evaluation.kt | 4 +- .../com/featurevisor/sdk/Instance+Fetch.kt | 1 + .../kotlin/com/featurevisor/sdk/Instance.kt | 3 ++ src/main/kotlin/com/featurevisor/sdk/Types.kt | 28 ++++++++----- .../sdk/serializers/Serializers.kt | 40 +++++++++++++++++++ 5 files changed, 65 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/featurevisor/sdk/Instance+Evaluation.kt b/src/main/kotlin/com/featurevisor/sdk/Instance+Evaluation.kt index 4f80a9d..0a0a187 100644 --- a/src/main/kotlin/com/featurevisor/sdk/Instance+Evaluation.kt +++ b/src/main/kotlin/com/featurevisor/sdk/Instance+Evaluation.kt @@ -615,9 +615,9 @@ private fun FeaturevisorInstance.getBucketKey(feature: Feature, context: Context bucketKey.add(AttributeValue.StringValue(featureKey)) - val result = bucketKey.map { + val result = bucketKey.joinToString(separator = bucketKeySeparator) { it.toString() - }.joinToString(separator = bucketKeySeparator) + } configureBucketKey?.let { configureBucketKey -> return configureBucketKey(feature, context, result) diff --git a/src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt b/src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt index baec776..9a03844 100644 --- a/src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt +++ b/src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt @@ -56,6 +56,7 @@ private inline fun fetch( } } val responseBodyString = responseBody.string() + FeaturevisorInstance.companionLogger?.debug(responseBodyString) val content = json.decodeFromString(responseBodyString) completion(Result.success(content)) } else { diff --git a/src/main/kotlin/com/featurevisor/sdk/Instance.kt b/src/main/kotlin/com/featurevisor/sdk/Instance.kt index c0ef784..2c5461e 100644 --- a/src/main/kotlin/com/featurevisor/sdk/Instance.kt +++ b/src/main/kotlin/com/featurevisor/sdk/Instance.kt @@ -23,6 +23,8 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) { fun createInstance(options: InstanceOptions): FeaturevisorInstance { return FeaturevisorInstance(options) } + + var companionLogger: Logger? = null } private val on: (EventName, Listener) -> Unit @@ -51,6 +53,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) { init { with(options) { + companionLogger = logger if (onReady != null) { emitter.addListener(event = READY, listener = onReady) } diff --git a/src/main/kotlin/com/featurevisor/sdk/Types.kt b/src/main/kotlin/com/featurevisor/sdk/Types.kt index b5306a6..e4454eb 100644 --- a/src/main/kotlin/com/featurevisor/sdk/Types.kt +++ b/src/main/kotlin/com/featurevisor/sdk/Types.kt @@ -4,6 +4,8 @@ import com.featurevisor.sdk.serializers.BucketBySerializer import com.featurevisor.sdk.serializers.ConditionSerializer import com.featurevisor.sdk.serializers.ConditionValueSerializer import com.featurevisor.sdk.serializers.GroupSegmentSerializer +import com.featurevisor.sdk.serializers.VariableValueSerializer +import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import java.time.LocalDate @@ -11,19 +13,27 @@ typealias Context = Map typealias VariationValue = String typealias VariableKey = String -enum class VariableType(val value: String) { - BOOLEAN("boolean"), - STRING("string"), - INTEGER("integer"), - DOUBLE("double"), - ARRAY("array"), - OBJECT("object"), - JSON("json"); +@Serializable +enum class VariableType { + @SerialName("boolean") + BOOLEAN, + @SerialName("string") + STRING, + @SerialName("integer") + INTEGER, + @SerialName("double") + DOUBLE, + @SerialName("array") + ARRAY, + @SerialName("object") + OBJECT, + @SerialName("json") + JSON } typealias VariableObjectValue = Map -@Serializable +@Serializable(with = VariableValueSerializer::class) sealed class VariableValue { data class BooleanValue(val value: Boolean) : VariableValue() data class StringValue(val value: String) : VariableValue() diff --git a/src/main/kotlin/com/featurevisor/sdk/serializers/Serializers.kt b/src/main/kotlin/com/featurevisor/sdk/serializers/Serializers.kt index c0fa2e9..ce33726 100644 --- a/src/main/kotlin/com/featurevisor/sdk/serializers/Serializers.kt +++ b/src/main/kotlin/com/featurevisor/sdk/serializers/Serializers.kt @@ -5,6 +5,7 @@ import com.featurevisor.sdk.Condition import com.featurevisor.sdk.ConditionValue import com.featurevisor.sdk.GroupSegment import com.featurevisor.sdk.Operator +import com.featurevisor.sdk.VariableValue import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.KSerializer import kotlinx.serialization.SerializationException @@ -196,6 +197,45 @@ object ConditionValueSerializer : KSerializer { } } +@OptIn(InternalSerializationApi::class) +@Serializer(forClass = VariableValue::class) +object VariableValueSerializer : KSerializer { + override val descriptor: SerialDescriptor = + buildSerialDescriptor("package.VariableValue", PolymorphicKind.SEALED) + + override fun deserialize(decoder: Decoder): VariableValue { + val input = decoder as? JsonDecoder + ?: throw SerializationException("This class can be decoded only by Json format") + return when (val tree = input.decodeJsonElement()) { + is JsonPrimitive -> { + tree.intOrNull?.let { + VariableValue.IntValue(it) + } ?: tree.booleanOrNull?.let { + VariableValue.BooleanValue(it) + } ?: tree.doubleOrNull?.let { + VariableValue.DoubleValue(it) + } ?: tree.content.let { + VariableValue.StringValue(it) + // TODO: +// VariableValue.DateTimeValue + } + } + + is JsonArray -> { + VariableValue.ArrayValue(tree.jsonArray.map { jsonElement -> jsonElement.jsonPrimitive.content }) + } + + is JsonObject -> { + throw NotImplementedError("VariableValue does not support JsonObject") + } + } + } + + override fun serialize(encoder: Encoder, value: VariableValue) { + // TODO: Later if needed + } +} + private fun mapOperator(value: String): Operator { return when (value.trim()) { "equals" -> Operator.EQUALS