Skip to content

Commit

Permalink
Fix deserializaing of VariableValue
Browse files Browse the repository at this point in the history
  • Loading branch information
uniumuniu committed Nov 9, 2023
1 parent 6ddf82c commit 7cd8733
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Evaluation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Fetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private inline fun fetch(
}
}
val responseBodyString = responseBody.string()
FeaturevisorInstance.companionLogger?.debug(responseBodyString)
val content = json.decodeFromString<DatafileContent>(responseBodyString)
completion(Result.success(content))
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/featurevisor/sdk/Instance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,6 +53,7 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {

init {
with(options) {
companionLogger = logger
if (onReady != null) {
emitter.addListener(event = READY, listener = onReady)
}
Expand Down
28 changes: 19 additions & 9 deletions src/main/kotlin/com/featurevisor/sdk/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ 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

typealias Context = Map<AttributeKey, AttributeValue>
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<String, VariableValue>

@Serializable
@Serializable(with = VariableValueSerializer::class)
sealed class VariableValue {
data class BooleanValue(val value: Boolean) : VariableValue()
data class StringValue(val value: String) : VariableValue()
Expand Down
40 changes: 40 additions & 0 deletions src/main/kotlin/com/featurevisor/sdk/serializers/Serializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -196,6 +197,45 @@ object ConditionValueSerializer : KSerializer<ConditionValue> {
}
}

@OptIn(InternalSerializationApi::class)
@Serializer(forClass = VariableValue::class)
object VariableValueSerializer : KSerializer<VariableValue> {
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
Expand Down

0 comments on commit 7cd8733

Please sign in to comment.