entities.EntityResolver.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of baku Show documentation
Show all versions of baku Show documentation
helps you focus your REST API back-end on the business logic
package com.github.fluidsonic.baku
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.flatMap
import kotlin.reflect.KClass
internal class EntityResolver(
private val resolvers: Map, suspend Transaction.(ids: Set) -> ReceiveChannel>
) {
suspend fun resolve(ids: Set, transaction: Transaction) =
ids
.groupBy { it.factory }
.map { (factory, ids) ->
resolvers[factory.idClass]
?.let { resolve -> transaction.resolve(ids.toSet()) }
?: GlobalScope.emptyReceiveChannel()
}
.toChannel()
.flatMap { it }
}