From e20929aec44e6cf535ba56833708175e2a7a1968 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 11 Nov 2020 12:03:14 +0600 Subject: [PATCH] mappers workaround --- CHANGELOG.md | 4 ++++ .../dev/inmo/micro_utils/repos/MapperRepo.kt | 14 +++++++++++++- .../repos/mappers/KeyValueMappers.kt | 17 ++++++++++++++++- .../repos/mappers/OneToManyKeyValueMappers.kt | 15 +++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 907add6213a..45dcb68db3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ * `Versions`: * `Coroutines`: `1.4.1` -> `1.4.2` +* `Repos`: + * `Common`: + * New inline function `mapper` for simplier creating of `MapperRepo` objects + * Extensions `withMapper` for keyvalue repos and onetomany repos ## 0.3.1 diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapperRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapperRepo.kt index 334077a007f..3d25d74ee2a 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapperRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapperRepo.kt @@ -6,4 +6,16 @@ interface MapperRepo { suspend fun ToKey.toInnerKey() = this as FromKey suspend fun ToValue.toInnerValue() = this as FromValue -} \ No newline at end of file +} + +inline fun mapper( + 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 }, +) = object : 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() +} diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt index 75d4f860079..cf727a024ec 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt @@ -51,7 +51,12 @@ open class MapperReadStandardKeyValueRepo( override suspend fun count(): Long = to.count() } -open class MapperWriteStandardKeyValueRepo( +@Suppress("NOTHING_TO_INLINE") +inline fun ReadStandardKeyValueRepo.withMapper( + mapper: MapperRepo +): ReadStandardKeyValueRepo = MapperReadStandardKeyValueRepo(this, mapper) + + open class MapperWriteStandardKeyValueRepo( private val to: WriteStandardKeyValueRepo, mapper: MapperRepo ) : WriteStandardKeyValueRepo, MapperRepo by mapper { @@ -75,6 +80,11 @@ open class MapperWriteStandardKeyValueRepo( ) } +@Suppress("NOTHING_TO_INLINE") +inline fun WriteStandardKeyValueRepo.withMapper( + mapper: MapperRepo +): WriteStandardKeyValueRepo = MapperWriteStandardKeyValueRepo(this, mapper) + open class MapperStandardKeyValueRepo( private val to: StandardKeyValueRepo, mapper: MapperRepo @@ -82,3 +92,8 @@ open class MapperStandardKeyValueRepo( MapperRepo by mapper, ReadStandardKeyValueRepo by MapperReadStandardKeyValueRepo(to, mapper), WriteStandardKeyValueRepo by MapperWriteStandardKeyValueRepo(to, mapper) + +@Suppress("NOTHING_TO_INLINE") +inline fun StandardKeyValueRepo.withMapper( + mapper: MapperRepo +): StandardKeyValueRepo = MapperStandardKeyValueRepo(this, mapper) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt index 37c1602b203..b3fbddd9db7 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt @@ -49,6 +49,11 @@ open class MapperReadOneToManyKeyValueRepo( override suspend fun count(k: FromKey): Long = to.count(k.toOutKey()) } +@Suppress("NOTHING_TO_INLINE") +inline fun ReadOneToManyKeyValueRepo.withMapper( + mapper: MapperRepo +): ReadOneToManyKeyValueRepo = MapperReadOneToManyKeyValueRepo(this, mapper) + open class MapperWriteOneToManyKeyValueRepo( private val to: WriteOneToManyKeyValueRepo, mapper: MapperRepo @@ -78,6 +83,11 @@ open class MapperWriteOneToManyKeyValueRepo( override suspend fun clear(k: FromKey) = to.clear(k.toOutKey()) } +@Suppress("NOTHING_TO_INLINE") +inline fun WriteOneToManyKeyValueRepo.withMapper( + mapper: MapperRepo +): WriteOneToManyKeyValueRepo = MapperWriteOneToManyKeyValueRepo(this, mapper) + open class MapperOneToManyKeyValueRepo( private val to: OneToManyKeyValueRepo, mapper: MapperRepo @@ -85,3 +95,8 @@ open class MapperOneToManyKeyValueRepo( MapperRepo by mapper, ReadOneToManyKeyValueRepo by MapperReadOneToManyKeyValueRepo(to, mapper), WriteOneToManyKeyValueRepo by MapperWriteOneToManyKeyValueRepo(to, mapper) + +@Suppress("NOTHING_TO_INLINE") +inline fun OneToManyKeyValueRepo.withMapper( + mapper: MapperRepo +): OneToManyKeyValueRepo = MapperOneToManyKeyValueRepo(this, mapper)