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
Types
Key for retrieving FlippingExecutionContext from a CoroutineContext.
Properties
Functions
Retrieves a required parameter from the context, throwing if not present.
Merges this context with another, with the other context's values taking precedence. Does not modify either original context.
Combines this context with another, returning a new context containing all entries from both.
Creates a new context with an additional parameter. Does not modify the original context.
Creates a new context with additional parameters. Does not modify the original context.