DispatchOn

@Target(allowedTargets = [AnnotationTarget.CLASS])
annotation class DispatchOn(val type: KClass<*>)

Specifies a custom discriminator type for a @ProtocolMessage sealed interface.

By default, @PacketType dispatch reads a single byte and matches its full value. @DispatchOn overrides this: the processor reads the specified type first, then dispatches on the property marked @DispatchValue within that type.

This enables protocols with bit-packed headers where the discriminator is not the full byte. The discriminator value is forwarded to sub-codecs via CodecContext, so variants can access fields like flags without re-reading.

@JvmInline
@ProtocolMessage
value class MqttFixedHeader(val raw: UByte) {
@DispatchValue
val packetType: Int get() = raw.toUInt().shr(4).toInt()
val flags: UByte get() = (raw.toUInt() and 0x0Fu).toUByte()
}

@DispatchOn(MqttFixedHeader::class)
sealed interface MqttControlPacket {
@PacketType(1) @ProtocolMessage data class Connect(...) : MqttControlPacket
@PacketType(3) @ProtocolMessage data class Publish(val header: MqttFixedHeader, ...) : MqttControlPacket
@PacketType(12) object PingRequest : MqttControlPacket
}

Parameters

type

A KClass referencing a @ProtocolMessage type (typically a value class) that contains a @DispatchValue-annotated property.

Properties

Link copied to clipboard
val type: KClass<*>