Skip to content

Commit d8403e8

Browse files
committed
Improve documentation
1 parent 17b7dfe commit d8403e8

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,55 @@ val kafkaConfig = KafkaTransportConfig(
226226
}
227227
```
228228

229+
## MessageRegistry & Message Serialization
230+
231+
The **MessageRegistry** is a core component of KCommand that provides a centralized mechanism for registering message types along with their corresponding serializers. This allows the system to automatically handle the (de)serialization of messages across different transports without requiring manual conversion for each message type.
232+
233+
### Key Features
234+
235+
- **Type Safety:** By registering each message type (typically using Kotlinx.serialization), you ensure that only known and supported message types are processed.
236+
- **Polymorphic Serialization:** When using sealed classes or polymorphic hierarchies, the MessageRegistry enables correct serialization/deserialization by associating a unique identifier (or type name) with each subtype.
237+
- **Centralized Configuration:** Instead of scattering serialization logic across your application, the MessageRegistry consolidates it, making it easier to update or extend the supported message types.
238+
239+
### How It Works
240+
241+
1. **Register Message Types:** The user defines and registers each message type with a unique identifier and its corresponding serializer.
242+
2. **Lookup at Runtime:** When a message is sent or received, the registry is used to look up the correct serializer based on the message’s type.
243+
3. **Integration with Transports:** Transports use the registry to convert messages to/from their serialized (typically JSON) form before sending to or after receiving from remote systems.
244+
245+
### Example
246+
247+
Below is an example of how to define a sealed class for messages and register its subtypes in the MessageRegistry:
248+
249+
```kotlin
250+
import kotlinx.serialization.Serializable
251+
import kotlinx.serialization.json.Json
252+
import io.github.theunic.kcommand.core.MessageRegistry
253+
254+
@Serializable
255+
sealed class MyMessage {
256+
@Serializable
257+
data class TextMessage(val text: String) : MyMessage()
258+
259+
@Serializable
260+
data class ImageMessage(val url: String) : MyMessage()
261+
}
262+
263+
// Create a MessageRegistry instance for MyMessage types
264+
val registry = MessageRegistry<MyMessage>().apply {
265+
// Register each subtype with a unique type identifier and its serializer.
266+
register(MyMessage.TextMessage::class, "text", MyMessage.TextMessage.serializer())
267+
register(MyMessage.ImageMessage::class, "image", MyMessage.ImageMessage.serializer())
268+
}
269+
270+
// Example: Serializing a message using the registry
271+
val json = Json { encodeDefaults = true }
272+
val textMsg = MyMessage.TextMessage("Hello, KCommand!")
273+
val (typeName, serializer) = registry.getTypeNameAndSerializer(textMsg::class)!!
274+
val payload = json.encodeToString(serializer, textMsg)
275+
println("Type: $typeName, Serialized Payload: $payload")
276+
```
277+
229278
## Contributing
230279

231280
Contributions of all sizes are welcome.

0 commit comments

Comments
 (0)