Skip to content

Commit 887a3cf

Browse files
committed
feat: Setting personalApiKey turns local eval on by default
1 parent 518dc86 commit 887a3cf

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

posthog-server/src/main/java/com/posthog/server/PostHogConfig.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public open class PostHogConfig constructor(
227227
private var featureFlagCacheSize: Int = DEFAULT_FEATURE_FLAG_CACHE_SIZE
228228
private var featureFlagCacheMaxAgeMs: Int = DEFAULT_FEATURE_FLAG_CACHE_MAX_AGE_MS
229229
private var featureFlagCalledCacheSize: Int = DEFAULT_FEATURE_FLAG_CALLED_CACHE_SIZE
230-
private var localEvaluation: Boolean = false
230+
private var localEvaluation: Boolean? = null
231231
private var personalApiKey: String? = null
232232
private var pollIntervalSeconds: Int = DEFAULT_POLL_INTERVAL_SECONDS
233233

@@ -265,7 +265,13 @@ public open class PostHogConfig constructor(
265265

266266
public fun localEvaluation(localEvaluation: Boolean): Builder = apply { this.localEvaluation = localEvaluation }
267267

268-
public fun personalApiKey(personalApiKey: String?): Builder = apply { this.personalApiKey = personalApiKey }
268+
public fun personalApiKey(personalApiKey: String?): Builder =
269+
apply {
270+
this.personalApiKey = personalApiKey
271+
if (localEvaluation == null) {
272+
this.localEvaluation = personalApiKey != null
273+
}
274+
}
269275

270276
public fun pollIntervalSeconds(pollIntervalSeconds: Int): Builder = apply { this.pollIntervalSeconds = pollIntervalSeconds }
271277

@@ -287,7 +293,7 @@ public open class PostHogConfig constructor(
287293
featureFlagCacheSize = featureFlagCacheSize,
288294
featureFlagCacheMaxAgeMs = featureFlagCacheMaxAgeMs,
289295
featureFlagCalledCacheSize = featureFlagCalledCacheSize,
290-
localEvaluation = localEvaluation,
296+
localEvaluation = localEvaluation ?: false,
291297
personalApiKey = personalApiKey,
292298
pollIntervalSeconds = pollIntervalSeconds,
293299
)

posthog-server/src/test/java/com/posthog/server/PostHogConfigTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,4 +476,38 @@ internal class PostHogConfigTest {
476476
assertEquals(coreConfig1.apiKey, coreConfig2.apiKey)
477477
assertEquals(coreConfig1.host, coreConfig2.host)
478478
}
479+
480+
@Test
481+
fun `builder personalApiKey enables localEvaluation when not explicitly set`() {
482+
val config =
483+
PostHogConfig.builder(TEST_API_KEY)
484+
.personalApiKey("test-personal-api-key")
485+
.build()
486+
487+
assertEquals("test-personal-api-key", config.personalApiKey)
488+
assertEquals(true, config.localEvaluation)
489+
}
490+
491+
@Test
492+
fun `builder personalApiKey with null does not enable localEvaluation when not explicitly set`() {
493+
val config =
494+
PostHogConfig.builder(TEST_API_KEY)
495+
.personalApiKey(null)
496+
.build()
497+
498+
assertNull(config.personalApiKey)
499+
assertEquals(false, config.localEvaluation)
500+
}
501+
502+
@Test
503+
fun `builder personalApiKey does not override explicit localEvaluation false`() {
504+
val config =
505+
PostHogConfig.builder(TEST_API_KEY)
506+
.localEvaluation(false)
507+
.personalApiKey("test-personal-api-key")
508+
.build()
509+
510+
assertEquals("test-personal-api-key", config.personalApiKey)
511+
assertEquals(false, config.localEvaluation)
512+
}
479513
}

0 commit comments

Comments
 (0)