All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.dev.inmo.micro_utils.repos.MapperRepo.kt Maven / Gradle / Ivy

There is a newer version: 0.22.2
Show newest version
package dev.inmo.micro_utils.repos

import dev.inmo.micro_utils.common.*

@Suppress("UNCHECKED_CAST")
interface MapperRepo {
    val keyMapper: SimpleSuspendableMapper
        get() = simpleSuspendableMapper(
            { it.toInnerKey() },
            { it.toOutKey() }
        )
    val valueMapper: SimpleSuspendableMapper
        get() = simpleSuspendableMapper(
            { it.toInnerValue() },
            { it.toOutValue() }
        )

    suspend fun FromKey.toOutKey() = this as ToKey
    suspend fun FromValue.toOutValue() = this as ToValue

    suspend fun ToKey.toInnerKey() = this as FromKey
    suspend fun ToValue.toInnerValue() = this as FromValue

    companion object
}

class SimpleMapperRepo(
    private val keyFromToTo: suspend FromKey.() -> ToKey,
    private val valueFromToTo: suspend FromValue.() -> ToValue,
    private val keyToToFrom: suspend ToKey.() -> FromKey,
    private val valueToToFrom: suspend ToValue.() -> FromValue
) : MapperRepo {
    override suspend fun FromKey.toOutKey(): ToKey = keyFromToTo()
    override suspend fun FromValue.toOutValue(): ToValue = valueFromToTo()
    override suspend fun ToKey.toInnerKey(): FromKey = keyToToFrom()
    override suspend fun ToValue.toInnerValue(): FromValue = valueToToFrom()
}

operator fun  MapperRepo.Companion.invoke(
    keyFromToTo: suspend FromKey.() -> ToKey,
    valueFromToTo: suspend FromValue.() -> ToValue,
    keyToToFrom: suspend ToKey.() -> FromKey,
    valueToToFrom: suspend ToValue.() -> FromValue
) = SimpleMapperRepo(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom)

inline fun  mapper(
    noinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey },
    noinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue },
    noinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey },
    noinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue },
) = MapperRepo(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy