diff --git a/CHANGELOG.md b/CHANGELOG.md index 34ea789d010..aa1755adb01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.11.7 +* `Repos`: + * `Common`: + * Add mappers for `ReadCRUDRepo` + ## 0.11.6 * `FSM`: 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 new file mode 100644 index 00000000000..0f9e45e58c0 --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/CRUDMappers.kt @@ -0,0 +1,42 @@ +package dev.inmo.micro_utils.repos.mappers + +import dev.inmo.micro_utils.pagination.* +import dev.inmo.micro_utils.repos.* + +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 count(): Long = to.count() + + override suspend fun contains(id: FromId): Boolean = to.contains(id.toOutKey()) + + 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( + crossinline keyFromToTo: suspend FromKey.() -> ToKey = { this as ToKey }, + crossinline valueFromToTo: suspend FromValue.() -> ToValue = { this as ToValue }, + crossinline keyToToFrom: suspend ToKey.() -> FromKey = { this as FromKey }, + crossinline valueToToFrom: suspend ToValue.() -> FromValue = { this as FromValue }, +): ReadCRUDRepo = withMapper( + mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom) +)