Skip to content

Commit

Permalink
refactor: changing vars to vals (#65)
Browse files Browse the repository at this point in the history
Signed-off-by: Nicklas Lundin <[email protected]>
  • Loading branch information
nicklasl authored Nov 7, 2023
1 parent 95e910a commit cdcb2df
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import dev.openfeature.sdk.exceptions.ErrorCode

data class FlagEvaluationDetails<T>(
val flagKey: String,
override var value: T,
override var variant: String? = null,
override var reason: String? = null,
override var errorCode: ErrorCode? = null,
override var errorMessage: String? = null
override val value: T,
override val variant: String? = null,
override val reason: String? = null,
override val errorCode: ErrorCode? = null,
override val errorMessage: String? = null
) : BaseEvaluation<T> {
companion object
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.openfeature.sdk

data class FlagEvaluationOptions(
var hooks: List<Hook<*>> = listOf(),
var hookHints: Map<String, Any> = mapOf()
val hooks: List<Hook<*>> = listOf(),
val hookHints: Map<String, Any> = mapOf()
)
10 changes: 5 additions & 5 deletions OpenFeature/src/main/java/dev/openfeature/sdk/HookContext.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dev.openfeature.sdk

data class HookContext<T>(
var flagKey: String,
val flagKey: String,
val type: FlagValueType,
var defaultValue: T,
var ctx: EvaluationContext?,
var clientMetadata: ClientMetadata?,
var providerMetadata: ProviderMetadata
val defaultValue: T,
val ctx: EvaluationContext?,
val clientMetadata: ClientMetadata?,
val providerMetadata: ProviderMetadata
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dev.openfeature.sdk

class ImmutableContext
(private var targetingKey: String = "", attributes: Map<String, Value> = mapOf()) : EvaluationContext {
private var structure: ImmutableStructure = ImmutableStructure(attributes)
private val structure: ImmutableStructure = ImmutableStructure(attributes)
override fun getTargetingKey(): String {
return targetingKey
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.openfeature.sdk

class ImmutableStructure(private var attributes: Map<String, Value> = mapOf()) : Structure {
class ImmutableStructure(private val attributes: Map<String, Value> = mapOf()) : Structure {
override fun keySet(): Set<String> {
return attributes.keys
}
Expand Down
7 changes: 3 additions & 4 deletions OpenFeature/src/main/java/dev/openfeature/sdk/NoOpProvider.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.openfeature.sdk

class NoOpProvider : FeatureProvider {
override var metadata: ProviderMetadata = NoOpProviderMetadata("No-op provider")
class NoOpProvider(override val hooks: List<Hook<*>> = listOf()) : FeatureProvider {
override val metadata: ProviderMetadata = NoOpProviderMetadata("No-op provider")
override fun initialize(initialContext: EvaluationContext?) {
// no-op
}
Expand All @@ -17,7 +17,6 @@ class NoOpProvider : FeatureProvider {
// no-op
}

override var hooks: List<Hook<*>> = listOf()
override fun getBooleanEvaluation(
key: String,
defaultValue: Boolean,
Expand Down Expand Up @@ -58,5 +57,5 @@ class NoOpProvider : FeatureProvider {
return ProviderEvaluation(defaultValue, "Passed in default", Reason.DEFAULT.toString())
}

data class NoOpProviderMetadata(override var name: String?) : ProviderMetadata
data class NoOpProviderMetadata(override val name: String?) : ProviderMetadata
}
42 changes: 28 additions & 14 deletions OpenFeature/src/main/java/dev/openfeature/sdk/OpenFeatureClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import dev.openfeature.sdk.exceptions.ErrorCode
import dev.openfeature.sdk.exceptions.OpenFeatureError
import dev.openfeature.sdk.exceptions.OpenFeatureError.GeneralError

private val typeMatchingException = GeneralError("Unable to match default value type with flag value type")
private val typeMatchingException =
GeneralError("Unable to match default value type with flag value type")

class OpenFeatureClient(
private val openFeatureAPI: OpenFeatureAPI,
Expand All @@ -18,7 +19,7 @@ class OpenFeatureClient(
override val hooks: MutableList<Hook<*>> = mutableListOf()
) : Client {
override val metadata: ClientMetadata = Metadata(name)
private var hookSupport = HookSupport()
private val hookSupport = HookSupport()
override fun addHooks(hooks: List<Hook<*>>) {
this.hooks += hooks
}
Expand Down Expand Up @@ -168,7 +169,8 @@ class OpenFeatureClient(
val hints = options.hookHints
var details = FlagEvaluationDetails(key, defaultValue)
val provider = openFeatureAPI.getProvider() ?: NoOpProvider()
val mergedHooks: List<Hook<*>> = provider.hooks + options.hooks + hooks + openFeatureAPI.hooks
val mergedHooks: List<Hook<*>> =
provider.hooks + options.hooks + hooks + openFeatureAPI.hooks
val context = openFeatureAPI.getEvaluationContext()
val hookCtx: HookContext<T> = HookContext(
key,
Expand All @@ -190,14 +192,17 @@ class OpenFeatureClient(
details = FlagEvaluationDetails.from(providerEval, key)
hookSupport.afterHooks(flagValueType, hookCtx, details, mergedHooks, hints)
} catch (error: Exception) {
if (error is OpenFeatureError) {
details.errorCode = error.errorCode()
val errorCode = if (error is OpenFeatureError) {
error.errorCode()
} else {
details.errorCode = ErrorCode.GENERAL
ErrorCode.GENERAL
}

details.errorMessage = error.message
details.reason = Reason.ERROR.toString()
details = details.copy(
errorMessage = error.message,
reason = Reason.ERROR.toString(),
errorCode = errorCode
)

hookSupport.errorHooks(flagValueType, hookCtx, error, mergedHooks, hints)
}
Expand All @@ -216,31 +221,40 @@ class OpenFeatureClient(
return when (flagValueType) {
BOOLEAN -> {
val defaultBoolean = defaultValue as? Boolean ?: throw typeMatchingException
val eval: ProviderEvaluation<Boolean> = provider.getBooleanEvaluation(key, defaultBoolean, context)
val eval: ProviderEvaluation<Boolean> =
provider.getBooleanEvaluation(key, defaultBoolean, context)
eval as? ProviderEvaluation<V> ?: throw typeMatchingException
}

STRING -> {
val defaultString = defaultValue as? String ?: throw typeMatchingException
val eval: ProviderEvaluation<String> = provider.getStringEvaluation(key, defaultString, context)
val eval: ProviderEvaluation<String> =
provider.getStringEvaluation(key, defaultString, context)
eval as? ProviderEvaluation<V> ?: throw typeMatchingException
}

INTEGER -> {
val defaultInteger = defaultValue as? Int ?: throw typeMatchingException
val eval: ProviderEvaluation<Int> = provider.getIntegerEvaluation(key, defaultInteger, context)
val eval: ProviderEvaluation<Int> =
provider.getIntegerEvaluation(key, defaultInteger, context)
eval as? ProviderEvaluation<V> ?: throw typeMatchingException
}

DOUBLE -> {
val defaultDouble = defaultValue as? Double ?: throw typeMatchingException
val eval: ProviderEvaluation<Double> = provider.getDoubleEvaluation(key, defaultDouble, context)
val eval: ProviderEvaluation<Double> =
provider.getDoubleEvaluation(key, defaultDouble, context)
eval as? ProviderEvaluation<V> ?: throw typeMatchingException
}

OBJECT -> {
val defaultObject = defaultValue as? Value ?: throw typeMatchingException
val eval: ProviderEvaluation<Value> = provider.getObjectEvaluation(key, defaultObject, context)
val eval: ProviderEvaluation<Value> =
provider.getObjectEvaluation(key, defaultObject, context)
eval as? ProviderEvaluation<V> ?: throw typeMatchingException
}
}
}

data class Metadata(override var name: String?) : ClientMetadata
data class Metadata(override val name: String?) : ClientMetadata
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package dev.openfeature.sdk
import dev.openfeature.sdk.exceptions.ErrorCode

data class ProviderEvaluation<T>(
var value: T,
var variant: String? = null,
var reason: String? = null,
var errorCode: ErrorCode? = null,
var errorMessage: String? = null
val value: T,
val variant: String? = null,
val reason: String? = null,
val errorCode: ErrorCode? = null,
val errorMessage: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ class HookSpecTests {

@Test
fun testHookEvaluationOrder() = runTest {
val provider = NoOpProvider()
val evalOrder: MutableList<String> = mutableListOf()
val addEval: (String) -> Unit = { eval: String -> evalOrder += eval }

provider.hooks = listOf(GenericSpyHookMock("provider", addEval))
val provider = NoOpProvider(
hooks = listOf(GenericSpyHookMock("provider", addEval))
)

OpenFeatureAPI.setProvider(provider)
OpenFeatureAPI.addHooks(listOf(GenericSpyHookMock("api", addEval)))
val client = OpenFeatureAPI.getClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ class AlwaysBrokenProvider(override var hooks: List<Hook<*>> = listOf(), overrid
throw FlagNotFoundError(key)
}

class AlwaysBrokenProviderMetadata(override var name: String? = "test") : ProviderMetadata
class AlwaysBrokenProviderMetadata(override val name: String? = "test") : ProviderMetadata
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import dev.openfeature.sdk.ProviderEvaluation
import dev.openfeature.sdk.ProviderMetadata
import dev.openfeature.sdk.Value

class DoSomethingProvider(override val hooks: List<Hook<*>> = listOf(), override val metadata: ProviderMetadata = DoSomethingProviderMetadata()) : FeatureProvider {
class DoSomethingProvider(
override val hooks: List<Hook<*>> = listOf(),
override val metadata: ProviderMetadata = DoSomethingProviderMetadata()
) : FeatureProvider {
override fun initialize(initialContext: EvaluationContext?) {
// no-op
}
Expand Down Expand Up @@ -62,5 +65,5 @@ class DoSomethingProvider(override val hooks: List<Hook<*>> = listOf(), override
): ProviderEvaluation<Value> {
return ProviderEvaluation(Value.Null)
}
class DoSomethingProviderMetadata(override var name: String? = "something") : ProviderMetadata
class DoSomethingProviderMetadata(override val name: String? = "something") : ProviderMetadata
}

0 comments on commit cdcb2df

Please sign in to comment.