FlippingExecutionContext

A type-safe execution context for feature flipping strategies.

This context holds key-value pairs that can be used during FlippingStrategy evaluation. Values are stored with runtime type checking, ensuring type safety when retrieving values.

Implements CoroutineContext.Element to enable automatic propagation through suspend function calls without ThreadLocal. Use withFlippingContext to set the context for a coroutine scope, and currentFlippingContext to retrieve it.

Example usage:

// Creating and using context directly
val context = FlippingExecutionContext(ContextKeys.USER_ID to 123, ContextKeys.USER_NAME to "Alice")
val id: Int? = context[ContextKeys.USER_ID] // Returns 123
val name: String? = context[ContextKeys.USER_NAME] // Returns "Alice"

// Using with coroutines (implicit context propagation)
withFlippingContext(FlippingExecutionContext(ContextKeys.USER_ID to "user-123")) {
ff4k.check("my-feature") // context automatically available
}

// Immutable modification (creates new instance)
val newContext = context.withParameter(ContextKeys.REGION, "EU")

Constructors

Link copied to clipboard
constructor(values: Map<String, Any> = emptyMap())
constructor(vararg pairs: Pair<String, Any>)

Creates a context with initial key-value pairs.

Types

Link copied to clipboard

Properties

Link copied to clipboard

Indicates whether the context is empty.

Link copied to clipboard
open override val key: CoroutineContext.Key<*>

Functions

Link copied to clipboard
operator fun contains(key: String): Boolean

Checks if the context contains a value for the given key.

Link copied to clipboard
open override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R
Link copied to clipboard
inline operator fun <T> get(key: String, required: Boolean = false): T?

Retrieves a value from the context with runtime type checking.

open operator override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E?
Link copied to clipboard

Retrieves a required parameter from the context, throwing if not present.

Link copied to clipboard

Merges this context with another, with the other context's values taking precedence. Does not modify either original context.

Link copied to clipboard
open override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext
Link copied to clipboard

Combines this context with another, returning a new context containing all entries from both.

open operator fun plus(context: CoroutineContext): CoroutineContext
Link copied to clipboard

Creates a new context with an additional parameter. Does not modify the original context.

Link copied to clipboard

Creates a new context with additional parameters. Does not modify the original context.