KsContextBinding

Describes how a single piece of per-call context is propagated across the wire. Implementations are referenced from KsContext.binding on a meta-annotation and are responsible for:

  • identifying the value on the wire (wireKey);

  • encoding / decoding the value for transports that carry it as a string (toWire / fromWire).

The binding is the CoroutineContext.Key for its element — a single source of truth for both the wire identity and the coroutine-context identity. The recommended shape is to declare the binding as a named companion object on the element type itself:

class AuthToken(val token: String) : CoroutineContext.Element {
override val key get() = Key

companion object Key : KsContextBinding<AuthToken> {
override val wireKey = "x-auth-token"
override fun toWire(value: AuthToken) = value.token
override fun fromWire(encoded: String) = AuthToken(encoded)
}
}

Reference the binding at the annotation site by its companion name:

@KsContext(binding = AuthToken.Key::class)

Handlers then read the propagated value with the standard coroutine-context lookup, naming the element type directly:

val token = coroutineContext[AuthToken]

The element type T must be a CoroutineContext.Element so it can sit directly in the running coroutine's context.

Implementations should be stateless and safe to reference from a KClass-literal in an annotation argument.

Properties

Link copied to clipboard
abstract val wireKey: String

Stable, transport-neutral identifier for this context entry. Transport layers use this string to name the field that carries the encoded value (e.g. as an HTTP header name, a JSON-RPC meta key, or a TLV tag in the packet protocol).

Functions

Link copied to clipboard
abstract fun fromWire(encoded: String): T

Decode a value of type T from its toWire string representation.

Link copied to clipboard
abstract fun toWire(value: T): String

Encode a value of type T for transports that carry it as a string.