Add feature flags
This commit is contained in:
parent
0c1921539f
commit
cfee354370
10 changed files with 198 additions and 0 deletions
8
core/featureflags/build.gradle.kts
Normal file
8
core/featureflags/build.gradle.kts
Normal file
|
@ -0,0 +1,8 @@
|
|||
plugins {
|
||||
id(ThunderbirdPlugins.Library.jvm)
|
||||
alias(libs.plugins.android.lint)
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation(projects.core.testing)
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package app.k9mail.core.featureflag
|
||||
|
||||
data class FeatureFlag(
|
||||
val key: FeatureFlagKey,
|
||||
val enabled: Boolean = false,
|
||||
)
|
|
@ -0,0 +1,5 @@
|
|||
package app.k9mail.core.featureflag
|
||||
|
||||
fun interface FeatureFlagFactory {
|
||||
fun createFeatureCatalog(): List<FeatureFlag>
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package app.k9mail.core.featureflag
|
||||
|
||||
@JvmInline
|
||||
value class FeatureFlagKey(val key: String)
|
|
@ -0,0 +1,5 @@
|
|||
package app.k9mail.core.featureflag
|
||||
|
||||
fun interface FeatureFlagProvider {
|
||||
fun provide(key: FeatureFlagKey): FeatureFlagResult
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package app.k9mail.core.featureflag
|
||||
|
||||
sealed interface FeatureFlagResult {
|
||||
object Enabled : FeatureFlagResult
|
||||
object Disabled : FeatureFlagResult
|
||||
object Unavailable : FeatureFlagResult
|
||||
|
||||
fun onEnabled(action: () -> Unit): FeatureFlagResult {
|
||||
if (this is Enabled) {
|
||||
action()
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
fun onDisabled(action: () -> Unit): FeatureFlagResult {
|
||||
if (this is Disabled) {
|
||||
action()
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
fun onUnavailable(action: () -> Unit): FeatureFlagResult {
|
||||
if (this is Unavailable) {
|
||||
action()
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package app.k9mail.core.featureflag
|
||||
|
||||
class InMemoryFeatureFlagProvider(
|
||||
featureFlagFactory: FeatureFlagFactory,
|
||||
) : FeatureFlagProvider {
|
||||
|
||||
private val features: Map<FeatureFlagKey, FeatureFlag> =
|
||||
featureFlagFactory.createFeatureCatalog().associateBy { it.key }
|
||||
|
||||
override fun provide(key: FeatureFlagKey): FeatureFlagResult {
|
||||
return when (features[key]?.enabled) {
|
||||
null -> FeatureFlagResult.Unavailable
|
||||
true -> FeatureFlagResult.Enabled
|
||||
false -> FeatureFlagResult.Disabled
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package app.k9mail.core.featureflags
|
||||
|
||||
import app.k9mail.core.featureflag.FeatureFlagResult
|
||||
import assertk.assertThat
|
||||
import assertk.assertions.isEqualTo
|
||||
import org.junit.Test
|
||||
|
||||
class FeatureFlagResultTest {
|
||||
|
||||
@Test
|
||||
fun `should only call onEnabled when enabled`() {
|
||||
val testSubject = FeatureFlagResult.Enabled
|
||||
|
||||
var result = ""
|
||||
|
||||
testSubject.onEnabled {
|
||||
result = "enabled"
|
||||
}.onDisabled {
|
||||
result = "disabled"
|
||||
}.onUnavailable {
|
||||
result = "unavailable"
|
||||
}
|
||||
|
||||
assertThat(result).isEqualTo("enabled")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should only call onDisabled when disabled`() {
|
||||
val testSubject = FeatureFlagResult.Disabled
|
||||
|
||||
var result = ""
|
||||
|
||||
testSubject.onEnabled {
|
||||
result = "enabled"
|
||||
}.onDisabled {
|
||||
result = "disabled"
|
||||
}.onUnavailable {
|
||||
result = "unavailable"
|
||||
}
|
||||
|
||||
assertThat(result).isEqualTo("disabled")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should only call onUnavailable when unavailable`() {
|
||||
val testSubject = FeatureFlagResult.Unavailable
|
||||
|
||||
var result = ""
|
||||
|
||||
testSubject.onEnabled {
|
||||
result = "enabled"
|
||||
}.onDisabled {
|
||||
result = "disabled"
|
||||
}.onUnavailable {
|
||||
result = "unavailable"
|
||||
}
|
||||
|
||||
assertThat(result).isEqualTo("unavailable")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package app.k9mail.core.featureflags
|
||||
|
||||
import app.k9mail.core.featureflag.FeatureFlag
|
||||
import app.k9mail.core.featureflag.FeatureFlagKey
|
||||
import app.k9mail.core.featureflag.FeatureFlagResult
|
||||
import app.k9mail.core.featureflag.InMemoryFeatureFlagProvider
|
||||
import assertk.assertThat
|
||||
import assertk.assertions.isInstanceOf
|
||||
import org.junit.Test
|
||||
|
||||
class InMemoryFeatureFlagProviderTest {
|
||||
|
||||
@Test
|
||||
fun `should return FeatureFlagResult#Enabled when feature is enabled`() {
|
||||
val feature1Key = FeatureFlagKey("feature1")
|
||||
val featureFlagProvider = InMemoryFeatureFlagProvider(
|
||||
featureFlagFactory = {
|
||||
listOf(
|
||||
FeatureFlag(key = feature1Key, enabled = true),
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
val result = featureFlagProvider.provide(feature1Key)
|
||||
|
||||
assertThat(result).isInstanceOf<FeatureFlagResult.Enabled>()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should return FeatureFlagResult#Disabled when feature is disabled`() {
|
||||
val feature1Key = FeatureFlagKey("feature1")
|
||||
val featureFlagProvider = InMemoryFeatureFlagProvider(
|
||||
featureFlagFactory = {
|
||||
listOf(
|
||||
FeatureFlag(key = feature1Key, enabled = false),
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
val result = featureFlagProvider.provide(feature1Key)
|
||||
|
||||
assertThat(result).isInstanceOf<FeatureFlagResult.Disabled>()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should return FeatureFlagResult#Unavailable when feature is not found`() {
|
||||
val feature1Key = FeatureFlagKey("feature1")
|
||||
val feature2Key = FeatureFlagKey("feature2")
|
||||
val featureFlagProvider = InMemoryFeatureFlagProvider(
|
||||
featureFlagFactory = {
|
||||
listOf(
|
||||
FeatureFlag(key = feature1Key, enabled = false),
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
val result = featureFlagProvider.provide(feature2Key)
|
||||
|
||||
assertThat(result).isInstanceOf<FeatureFlagResult.Unavailable>()
|
||||
}
|
||||
}
|
|
@ -52,6 +52,7 @@ include(
|
|||
|
||||
include(
|
||||
":core:common",
|
||||
":core:featureflags",
|
||||
":core:testing",
|
||||
":core:android:common",
|
||||
":core:android:testing",
|
||||
|
|
Loading…
Reference in a new issue