Skip to content

Commit 0cddbe9

Browse files
committed
feat(sampleapp): add some UI
Signed-off-by: Nicklas Lundin <[email protected]>
1 parent 7319029 commit 0cddbe9

File tree

5 files changed

+340
-71
lines changed

5 files changed

+340
-71
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package dev.openfeature.sdk.sampleapp
2+
3+
import dev.openfeature.sdk.*
4+
import kotlinx.coroutines.delay
5+
6+
class ExampleProvider(override val hooks: List<Hook<*>> = listOf()) : FeatureProvider {
7+
8+
private var currentContext: EvaluationContext? = ImmutableContext()
9+
var delayTime = 1000L
10+
var returnDefaults = false
11+
val flags = mutableMapOf<String, Any>().apply {
12+
put("booleanFlag", true)
13+
put("stringFlag", "this is a string")
14+
put("intFlag", 1337)
15+
put("doubleFlag", 42.0)
16+
put(
17+
"objectFlag",
18+
Value.Structure(mapOf("key1" to Value.String("value"), "key2" to Value.Integer(10)))
19+
)
20+
}
21+
22+
override val metadata: ProviderMetadata
23+
get() = object : ProviderMetadata {
24+
override val name: String = "ExampleProvider"
25+
}
26+
27+
override suspend fun initialize(initialContext: EvaluationContext?) {
28+
currentContext = initialContext
29+
// Simulate a delay in the provider initialization
30+
delay(delayTime)
31+
}
32+
33+
override fun shutdown() {
34+
35+
}
36+
37+
override suspend fun onContextSet(
38+
oldContext: EvaluationContext?,
39+
newContext: EvaluationContext
40+
) {
41+
currentContext = newContext
42+
delay(delayTime)
43+
}
44+
45+
override fun getBooleanEvaluation(
46+
key: String,
47+
defaultValue: Boolean,
48+
context: EvaluationContext?
49+
): ProviderEvaluation<Boolean> = generateProviderEvaluation<Boolean>(defaultValue, key)
50+
51+
override fun getStringEvaluation(
52+
key: String,
53+
defaultValue: String,
54+
context: EvaluationContext?
55+
): ProviderEvaluation<String> = generateProviderEvaluation<String>(defaultValue, key)
56+
57+
override fun getIntegerEvaluation(
58+
key: String,
59+
defaultValue: Int,
60+
context: EvaluationContext?
61+
): ProviderEvaluation<Int> = generateProviderEvaluation<Int>(defaultValue, key)
62+
63+
override fun getDoubleEvaluation(
64+
key: String,
65+
defaultValue: Double,
66+
context: EvaluationContext?
67+
): ProviderEvaluation<Double> = generateProviderEvaluation<Double>(defaultValue, key)
68+
69+
override fun getObjectEvaluation(
70+
key: String,
71+
defaultValue: Value,
72+
context: EvaluationContext?
73+
): ProviderEvaluation<Value> = generateProviderEvaluation<Value>(defaultValue, key)
74+
75+
private inline fun <reified T> generateProviderEvaluation(
76+
defaultValue: T,
77+
key: String
78+
): ProviderEvaluation<T> {
79+
if (returnDefaults) {
80+
return ProviderEvaluation(defaultValue, null, reason = "returnDefaults")
81+
}
82+
return with(flags) {
83+
if (containsKey(key) && get(key) is T) {
84+
ProviderEvaluation(get(key) as T, "variant1", reason = "match")
85+
} else if (containsKey(key)) {
86+
ProviderEvaluation(defaultValue, null, reason = "invalid type")
87+
} else {
88+
ProviderEvaluation(defaultValue, null, reason = "notfound")
89+
}
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)