-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Instance exstension functions
- Loading branch information
Showing
14 changed files
with
676 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
src/main/kotlin/com/featurevisor/sdk/Instance+Activation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.