Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isFeatureEnabled now returns true if multivariant flag #42

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- isFeatureEnabled now returns true if multivariant flag ([#42](https://github.com/PostHog/posthog-android/pull/42))

## 3.0.0-alpha.6 - 2023-10-06

- Upsert flags when loading feature flags with computed errors ([#38](https://github.com/PostHog/posthog-android/pull/38))
Expand Down
2 changes: 1 addition & 1 deletion posthog/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ configure<JavaPluginExtension> {

buildConfig {
useKotlinOutput()
packageName("com.posthog.internal")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packageName("com.posthog")
buildConfigField("String", "VERSION_NAME", "\"${project.version}\"")
}

Expand Down
1 change: 0 additions & 1 deletion posthog/src/main/java/com/posthog/PostHogConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.posthog

import com.posthog.internal.BuildConfig
import com.posthog.internal.PostHogContext
import com.posthog.internal.PostHogLogger
import com.posthog.internal.PostHogNetworkStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ internal class PostHogFeatureFlags(
synchronized(featureFlagsLock) {
if (response.errorsWhileComputingFlags) {
// if not all flags were computed, we upsert flags instead of replacing them
this.featureFlags = (this.featureFlags ?: mapOf()) + (response.featureFlags ?: mapOf())
this.featureFlagPayloads = (this.featureFlagPayloads ?: mapOf()) + (response.featureFlagPayloads ?: mapOf())
this.featureFlags =
(this.featureFlags ?: mapOf()) + (response.featureFlags ?: mapOf())
this.featureFlagPayloads = (
this.featureFlagPayloads
?: mapOf()
) + (response.featureFlagPayloads ?: mapOf())
} else {
this.featureFlags = response.featureFlags
this.featureFlagPayloads = response.featureFlagPayloads
Expand Down Expand Up @@ -87,8 +91,13 @@ internal class PostHogFeatureFlags(
value = featureFlags?.get(key)
}

return if (value is Boolean) {
value
return if (value != null) {
if (value is Boolean) {
value
} else {
// if its multivariant flag, its enabled by default
true
}
} else {
defaultValue
}
Expand Down
1 change: 0 additions & 1 deletion posthog/src/test/java/com/posthog/PostHogConfigTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.posthog

import com.posthog.internal.BuildConfig
import com.posthog.internal.PostHogPrintLogger
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.posthog.internal

import com.posthog.BuildConfig
import com.posthog.PostHogConfig
import com.posthog.apiKey
import com.posthog.generateEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.io.File
import java.util.concurrent.Executors
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue

Expand Down Expand Up @@ -168,4 +169,27 @@ internal class PostHogFeatureFlagsTest {
assertTrue(sut.getFeatureFlagPayload("thePayload", defaultValue = false) as Boolean)
assertTrue(sut.getFeatureFlagPayload("foo", defaultValue = false) as Boolean)
}

@Test
fun `returns flag enabled if multivariant`() {
val file = File("src/test/resources/json/basic-decide-with-non-active-flags.json")

val http = mockHttp(
response =
MockResponse()
.setBody(file.readText()),
)
val url = http.url("/")

val sut = getSut(host = url.toString())

sut.loadFeatureFlags("my_identify", "anonId", emptyMap(), null)

executor.shutdownAndAwaitTermination()

assertTrue(sut.isFeatureEnabled("4535-funnel-bar-viz", defaultValue = false))
assertFalse(sut.isFeatureEnabled("IAmInactive", defaultValue = true))
assertTrue(sut.isFeatureEnabled("splashScreenName", defaultValue = false))
assertTrue(sut.isFeatureEnabled("IDontExist", defaultValue = true))
}
}