Eugene Freeman
04/24/2021, 11:30 AMinterface DynamoAttrs<T> {
fun serialize(): Map<String, AttributeValue>
}
interface DynamoEntity<T : DynamoAttrs<T>> {
fun deserialize(attrs: Map<String, AttributeValue>): T
}
data class Event(
val id: Long,
val message: String,
) : DynamoAttrs<Event> {
override fun serialize(): Map<String, AttributeValue> = ...
companion object : DynamoEntity<Event> {
override fun deserialize(attrs: Map<String, AttributeValue>): Event = ...
}
}
data class QueueItem<T : DynamoAttrs<T>>(
val kind: QueueItemKind,
val payload: T,
val processed: Boolean = false
) : DynamoAttrs<QueueItem<T>> {
override fun serialize(): Map<String, AttributeValue> = mapOf(
"kind" to AttributeValue.builder().s(this.kind.name).build(),
"payload" to AttributeValue.builder().m(this.payload.serialize()).build(),
"processed" to AttributeValue.builder().bool(this.processed).build()
)
companion object : DynamoEntity<QueueItem<Any>> { --- how to declre sygnature properly to get it implementing DynamoEntity interface?
fun deserialize(attrs: Map<String, AttributeValue>): QueueItem<E> {
val kind = QueueItemKind.valueOf(
attrs["kind"]?.s() ?: throw IllegalArgumentException(
"Failed to parse queue item kind. Attrs: $attrs"
)
)
if (kind == QueueItemKind.ENTITY) {
QueueItem(
kind,
Entity.deserialize(
attrs["payload"]?.m() ?: throw IllegalArgumentException(
"Failed to deserialize attributes to Entity. Attrs: $attrs"
)
),
attrs["processed"]?.bool() ?: throw IllegalArgumentException(
"Failed to read 'processed' flag. Attrs: $attrs"
)
)
}
throw IllegalStateException("Unexpected queue item kind $kind. Attrs: $attrs")
}
}
}
enum class QueueItemKind {
ENTITY
}
is it possible?