I have various types of events
in my application that are represented by Data classes.
All events
must have an attribute called EventContent
that stores additional data about the event in String format. These events are saved to a database and EventContent
is serialized into a JSON via kotlin.x.serialization.
To enforce this, I have used an interface like this on a sample event called ShutdownEvent
:
interface Event {
var eventContent: String
}
data class ShutdownEvent(
override var eventContent: String
) : Event
Now imagine for the specific event called ShutdownEvent
, I know its EventContent
must have only 2 attributes: trigger
and location
, both of which are Strings. How can I enforce that the EventContent
follows this structure.
I was hoping for something like this:
data class ShutdownEvent(
@Serializable(with = ShutdownContentConverter::class) override var eventContent: ShutdownData,
) : Event
@Serializable
data class ShutdownData(
var trigger: String,
var location: String,
)
But this causes problems with my Event
interface implementation, because it expects the eventContent
attribute to be a String. Is there an easy way to make this work?
Edit:
I would appreciate an answer that allows me to process my events as follows:
fun process(event: Event) {
if (event is ShutdownEvent) {
// do something
}
}