This commit is contained in:
InsanusMokrassar 2022-06-28 01:06:46 +06:00
parent b40c093917
commit 12a7e3c4af
2 changed files with 24 additions and 25 deletions

View File

@ -1,23 +1,24 @@
package dev.inmo.micro_utils.common
import kotlin.jvm.JvmName
interface SimpleMapper<T1, T2> {
fun t1(from: T2): T1
fun t2(from: T1): T2
operator fun T2.invoke(): T1 = t1(this)
operator fun T1.invoke(): T2 = t2(this)
fun T2.convert(): T1 = t1(this)
fun T1.convert(): T2 = t2(this)
fun convertToT1(from: T2): T1
fun convertToT2(from: T1): T2
}
@JvmName("convertFromT2")
fun <T1, T2> SimpleMapper<T1, T2>.convert(from: T2) = convertToT1(from)
@JvmName("convertFromT1")
fun <T1, T2> SimpleMapper<T1, T2>.convert(from: T1) = convertToT2(from)
class SimpleMapperImpl<T1, T2>(
private val t1: (T2) -> T1,
private val t2: (T1) -> T2,
) : SimpleMapper<T1, T2> {
override fun t1(from: T2): T1 = t1.invoke(from)
override fun convertToT1(from: T2): T1 = t1.invoke(from)
override fun t2(from: T1): T2 = t2.invoke(from)
override fun convertToT2(from: T1): T2 = t2.invoke(from)
}
@Suppress("NOTHING_TO_INLINE")
@ -27,23 +28,22 @@ inline fun <T1, T2> simpleMapper(
) = SimpleMapperImpl(t1, t2)
interface SimpleSuspendableMapper<T1, T2> {
suspend fun t1(from: T2): T1
suspend fun t2(from: T1): T2
suspend operator fun T2.invoke(): T1 = t1(this)
suspend operator fun T1.invoke(): T2 = t2(this)
suspend fun T2.convert(): T1 = t1(this)
suspend fun T1.convert(): T2 = t2(this)
suspend fun convertToT1(from: T2): T1
suspend fun convertToT2(from: T1): T2
}
@JvmName("convertFromT2")
suspend fun <T1, T2> SimpleSuspendableMapper<T1, T2>.convert(from: T2) = convertToT1(from)
@JvmName("convertFromT1")
suspend fun <T1, T2> SimpleSuspendableMapper<T1, T2>.convert(from: T1) = convertToT2(from)
class SimpleSuspendableMapperImpl<T1, T2>(
private val t1: suspend (T2) -> T1,
private val t2: suspend (T1) -> T2,
) : SimpleSuspendableMapper<T1, T2> {
override suspend fun t1(from: T2): T1 = t1.invoke(from)
override suspend fun convertToT1(from: T2): T1 = t1.invoke(from)
override suspend fun t2(from: T1): T2 = t2.invoke(from)
override suspend fun convertToT2(from: T1): T2 = t2.invoke(from)
}
@Suppress("NOTHING_TO_INLINE")

View File

@ -1,7 +1,6 @@
package dev.inmo.micro_utils.repos.mappers
import dev.inmo.micro_utils.common.SimpleSuspendableMapper
import dev.inmo.micro_utils.common.simpleSuspendableMapper
import dev.inmo.micro_utils.common.*
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.flow.Flow
@ -62,18 +61,18 @@ open class MapperWriteCRUDRepo<FromId, FromRegistered, FromInput, ToId, ToRegist
values: List<UpdatedValuePair<FromId, FromInput>>
): List<FromRegistered> = to.update(
values.map {
it.first.toOutKey() to it.second.convert()
it.first.toOutKey() to convert(it.second)
}
).map { it.toInnerValue() }
override suspend fun update(
id: FromId,
value: FromInput
): FromRegistered? = to.update(id.toOutKey(), value.convert()) ?.toInnerValue()
): FromRegistered? = to.update(id.toOutKey(), convert(value)) ?.toInnerValue()
override suspend fun create(values: List<FromInput>): List<FromRegistered> = to.create(
values.map {
it.convert()
convert(it)
}
).map {
it.toInnerValue()