diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Mapper.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Mapper.kt index 5fcee5a2749..3494ebfff1b 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Mapper.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Mapper.kt @@ -1,23 +1,24 @@ package dev.inmo.micro_utils.common +import kotlin.jvm.JvmName + interface SimpleMapper { - 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 SimpleMapper.convert(from: T2) = convertToT1(from) +@JvmName("convertFromT1") +fun SimpleMapper.convert(from: T1) = convertToT2(from) + class SimpleMapperImpl( private val t1: (T2) -> T1, private val t2: (T1) -> T2, ) : SimpleMapper { - 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 simpleMapper( ) = SimpleMapperImpl(t1, t2) interface SimpleSuspendableMapper { - 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 SimpleSuspendableMapper.convert(from: T2) = convertToT1(from) +@JvmName("convertFromT1") +suspend fun SimpleSuspendableMapper.convert(from: T1) = convertToT2(from) + class SimpleSuspendableMapperImpl( private val t1: suspend (T2) -> T1, private val t2: suspend (T1) -> T2, ) : SimpleSuspendableMapper { - 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") diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/CRUDMappers.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/CRUDMappers.kt index 82cb94d72b8..40543da0eba 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/CRUDMappers.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/CRUDMappers.kt @@ -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> ): List = 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): List = to.create( values.map { - it.convert() + convert(it) } ).map { it.toInnerValue()