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

commonMain.dev.inmo.micro_utils.repos.mappers.CRUDMappers.kt Maven / Gradle / Ivy

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

import dev.inmo.micro_utils.common.*
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

open class MapperReadCRUDRepo(
    private val to: ReadCRUDRepo,
    mapper: MapperRepo
) : ReadCRUDRepo, MapperRepo by mapper {
    override suspend fun getByPagination(
        pagination: Pagination
    ): PaginationResult = to.getByPagination(
        pagination
    ).let {
        it.changeResultsUnchecked(
            it.results.map { it.toInnerValue() }
        )
    }

    override suspend fun getIdsByPagination(pagination: Pagination): PaginationResult = to.getIdsByPagination(
        pagination
    ).let {
        it.changeResultsUnchecked(
            it.results.map { it.toInnerKey() }
        )
    }

    override suspend fun count(): Long = to.count()

    override suspend fun contains(id: FromId): Boolean = to.contains(id.toOutKey())

    override suspend fun getAll(): Map = to.getAll().asSequence().associate { (k, v) ->
        k.toInnerKey() to v.toInnerValue()
    }

    override suspend fun getById(id: FromId): FromRegistered? = to.getById(
        id.toOutKey()
    ) ?.toInnerValue()
}

@Suppress("NOTHING_TO_INLINE")
inline fun  ReadCRUDRepo.withMapper(
    mapper: MapperRepo
): ReadCRUDRepo = MapperReadCRUDRepo(this, mapper)

@Suppress("NOTHING_TO_INLINE")
inline fun  ReadCRUDRepo.withMapper(
    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 },
): ReadCRUDRepo = withMapper(
    mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom)
)

open class MapperWriteCRUDRepo(
    private val to: WriteCRUDRepo,
    mapper: MapperRepo,
    inputMapper: SimpleSuspendableMapper
) : WriteCRUDRepo,
    MapperRepo by mapper,
    SimpleSuspendableMapper by inputMapper {
    override val newObjectsFlow: Flow = to.newObjectsFlow.map { it.toInnerValue() }
    override val updatedObjectsFlow: Flow = to.updatedObjectsFlow.map { it.toInnerValue() }
    override val deletedObjectsIdsFlow: Flow = to.deletedObjectsIdsFlow.map { it.toInnerKey() }

    override suspend fun deleteById(ids: List) = to.deleteById(ids.map { it.toOutKey() })

    override suspend fun update(
        values: List>
    ): List = to.update(
        values.map {
            it.first.toOutKey() to convert(it.second)
        }
    ).map { it.toInnerValue() }

    override suspend fun update(
        id: FromId,
        value: FromInput
    ): FromRegistered? = to.update(id.toOutKey(), convert(value)) ?.toInnerValue()

    override suspend fun create(values: List): List = to.create(
        values.map {
            convert(it)
        }
    ).map {
        it.toInnerValue()
    }

}

@Suppress("NOTHING_TO_INLINE")
inline fun  WriteCRUDRepo.withMapper(
    mapper: MapperRepo,
    simpleSuspendableMapper: SimpleSuspendableMapper
): WriteCRUDRepo = MapperWriteCRUDRepo(this, mapper, simpleSuspendableMapper)

@Suppress("NOTHING_TO_INLINE")
inline fun  WriteCRUDRepo.withMapper(
    noinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey },
    noinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue },
    noinline inputFromToTo: suspend FromInput.() -> ToInput = { this as ToInput },
    noinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey },
    noinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue },
    noinline inputToToFrom: suspend ToInput.() -> FromInput = { this as FromInput },
): WriteCRUDRepo = withMapper(
    mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom),
    simpleSuspendableMapper({ inputToToFrom(it) }, { inputFromToTo(it) })
)

@Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE")
open class MapperCRUDRepo(
    private val to: CRUDRepo,
    private val mapper: MapperRepo,
    private val inputMapper: SimpleSuspendableMapper
) : CRUDRepo,
    MapperRepo by mapper,
    ReadCRUDRepo by MapperReadCRUDRepo(to, mapper),
    WriteCRUDRepo by MapperWriteCRUDRepo(to, mapper, inputMapper)


@Suppress("NOTHING_TO_INLINE")
inline fun  CRUDRepo.withMapper(
    mapper: MapperRepo,
    simpleSuspendableMapper: SimpleSuspendableMapper
): CRUDRepo = MapperCRUDRepo(this, mapper, simpleSuspendableMapper)

@Suppress("NOTHING_TO_INLINE")
inline fun  CRUDRepo.withMapper(
    noinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey },
    noinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue },
    noinline inputFromToTo: suspend FromInput.() -> ToInput = { this as ToInput },
    noinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey },
    noinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue },
    noinline inputToToFrom: suspend ToInput.() -> FromInput = { this as FromInput },
): CRUDRepo = withMapper(
    mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom),
    simpleSuspendableMapper({ inputToToFrom(it) }, { inputFromToTo(it) })
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy