Skip to content

Commit

Permalink
Introduce Instance exstension functions
Browse files Browse the repository at this point in the history
  • Loading branch information
uniumuniu committed Oct 20, 2023
1 parent ea136a6 commit 2f8de11
Show file tree
Hide file tree
Showing 14 changed files with 676 additions and 301 deletions.
12 changes: 12 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ dependencies {
implementation("net.swiftzer.semver:semver:1.3.0")
implementation("com.goncalossilva:murmurhash:0.4.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
implementation("com.squareup.okhttp3:okhttp:4.11.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
}

// Apply a specific Java toolchain to ease working on different environments.
Expand All @@ -80,3 +82,13 @@ tasks.named<Test>("test") {
showStandardStreams = true
}
}

// TODO: Remove excludes when tests are fixed
sourceSets {
test {
kotlin {
exclude("com/featurevisor/sdk/InstanceTest.kt")
exclude("com/featurevisor/sdk/EmitterTest.kt")
}
}
}
8 changes: 4 additions & 4 deletions src/main/kotlin/com/featurevisor/sdk/Emitter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.featurevisor.sdk
import com.featurevisor.types.EventName

class Emitter {
private val listeners = mutableMapOf<EventName, () -> Unit>()
private val listeners = mutableMapOf<EventName, (Array<out Any>) -> Unit>()

fun addListener(event: EventName, listener: () -> Unit) {
fun addListener(event: EventName, listener: (Array<out Any>) -> Unit) {
listeners.putIfAbsent(event, listener)
}

Expand All @@ -17,7 +17,7 @@ class Emitter {
listeners.clear()
}

fun emit(event: EventName) {
listeners.getOrDefault(event, null)?.invoke()
fun emit(event: EventName, vararg args: Any) {
listeners.getOrDefault(event, null)?.invoke(args)
}
}
19 changes: 17 additions & 2 deletions src/main/kotlin/com/featurevisor/sdk/FeaturevisorError.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package com.featurevisor.sdk

sealed class FeaturevisorError(message: String, result: String? = null) : Throwable(message = message) {
sealed class FeaturevisorError(message: String) : Throwable(message = message) {

/// Thrown when attempting to init Featurevisor instance without passing datafile and datafileUrl.
/// At least one of them is required to init the SDK correctly
/// - Parameter string: The invalid URL string.
object MissingDatafileOptions : FeaturevisorError("Missing data file options")
class FetchingDataFileFailed(result: String) : FeaturevisorError("Fetching data file failed", result)

class FetchingDataFileFailed(val result: String) : FeaturevisorError("Fetching data file failed")

/// Thrown when receiving unparseable Datafile JSON responses.
/// - Parameters:
/// - data: The data being parsed.
/// - errorMessage: The message from the error which occured during parsing.
class UnparsableJson(val data: String?, errorMessage: String) : FeaturevisorError(errorMessage)

/// Thrown when attempting to construct an invalid URL.
/// - Parameter string: The invalid URL string.
class InvalidUrl(val url: String?) : FeaturevisorError("Invalid URL")
}
33 changes: 33 additions & 0 deletions src/main/kotlin/com/featurevisor/sdk/Instance+Activation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.featurevisor.sdk

import com.featurevisor.types.AttributeValue
import com.featurevisor.types.Context
import com.featurevisor.types.EventName
import com.featurevisor.types.FeatureKey
import com.featurevisor.types.VariationValue

fun FeaturevisorInstance.activate(featureKey: FeatureKey, context: Context = emptyMap()): VariationValue? {
val evaluation = evaluateVariation(featureKey, context)
val variationValue = evaluation.variation?.value ?: evaluation.variationValue ?: return null
val finalContext = interceptContext?.invoke(context) ?: context
val captureContext = mutableMapOf<String, AttributeValue>()
val attributesForCapturing = datafileReader.getAllAttributes()
.filter { it.capture == true }

attributesForCapturing.forEach { attribute ->
if (finalContext[attribute.key] != null) {
captureContext[attribute.key] = context[attribute.key]!!
}
}

emitter.emit(
EventName.ACTIVATION,
featureKey,
variationValue,
finalContext,
captureContext,
evaluation
)

return variationValue
}
Loading

0 comments on commit 2f8de11

Please sign in to comment.