From dd2fc5a86f725e2401b813ff2c1f40c13b95fb68 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 18 Nov 2020 20:07:54 +0600 Subject: [PATCH 1/2] start 0.4.2 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- .../repos/OneToManyKeyValueRepo.kt | 21 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf47f5471f1..d17a93eb1ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.4.2 + ## 0.4.1 * `Repos`: diff --git a/gradle.properties b/gradle.properties index c793f689a86..f338ad654d4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -41,5 +41,5 @@ dokka_version=1.4.0 # Project data group=dev.inmo -version=0.4.1 -android_code_version=5 +version=0.4.2 +android_code_version=6 diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt index 4572bce20dc..12137c06c74 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt @@ -47,6 +47,11 @@ interface WriteOneToManyKeyValueRepo : Repo { suspend fun remove(toRemove: Map>) suspend fun clear(k: Key) + + suspend fun set(toSet: Map>) { + toSet.keys.forEach { key -> clear(key) } + add(toSet) + } } suspend inline fun > REPO.add( @@ -65,6 +70,22 @@ suspend inline fun WriteOneToManyKeyValueRepo.add( k: Key, vararg v: Value ) = add(k, v.toList()) +suspend inline fun > REPO.set( + keysAndValues: List>> +) = set(keysAndValues.toMap()) + +suspend inline fun > REPO.set( + vararg keysAndValues: Pair> +) = set(keysAndValues.toMap()) + +suspend inline fun WriteOneToManyKeyValueRepo.set( + k: Key, v: List +) = set(mapOf(k to v)) + +suspend inline fun WriteOneToManyKeyValueRepo.set( + k: Key, vararg v: Value +) = set(k, v.toList()) + interface OneToManyKeyValueRepo : ReadOneToManyKeyValueRepo, WriteOneToManyKeyValueRepo suspend inline fun WriteOneToManyKeyValueRepo.remove( From fcacdcd5449df3b2477b6c7bf810edacd457d688 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 18 Nov 2020 20:27:29 +0600 Subject: [PATCH 2/2] set method in realizations --- CHANGELOG.md | 3 +++ .../repos/mappers/OneToManyKeyValueMappers.kt | 6 +++++ .../repos/onetomany/OneToManyAndroidRepo.kt | 23 +++++++++++++++++++ .../KtorWriteOneToManyKeyValueRepo.kt | 13 +++++++++-- .../common/one_to_many/OneToManyRoutes.kt | 3 ++- ...nfigureOneToManyWriteKeyValueRepoRoutes.kt | 9 ++++++++ 6 files changed, 54 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d17a93eb1ff..285b42aeb6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.4.2 +* `Repos`: + * Add `WriteOneToManyKeyValueRepo#set` function and extensions + ## 0.4.1 * `Repos`: 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 228b52134cc..dd777d15088 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 @@ -107,6 +107,12 @@ open class MapperWriteOneToManyKeyValueRepo( }.toMap() ) + override suspend fun set(toSet: Map>) { + to.set( + toSet.map { (k, vs) -> k.toOutKey() to vs.map { v -> v.toOutValue() } }.toMap() + ) + } + override suspend fun clear(k: FromKey) = to.clear(k.toOutKey()) } diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt index 48afbe42a8f..ffd6c20a45e 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/onetomany/OneToManyAndroidRepo.kt @@ -87,6 +87,29 @@ class OneToManyAndroidRepo( } } + override suspend fun set(toSet: Map>) { + val (clearedKeys, inserted) = helper.writableTransaction { + toSet.mapNotNull { (k, _) -> + if (delete(tableName, "$idColumnName=?", arrayOf(k.asId())) > 0) { + k + } else { + null + } + } to toSet.flatMap { (k, values) -> + values.map { v -> + insert( + tableName, + null, + contentValuesOf(idColumnName to k.asId(), valueColumnName to v.asValue()) + ) + k to v + } + } + } + clearedKeys.forEach { _onDataCleared.emit(it) } + inserted.forEach { newPair -> _onNewValue.emit(newPair) } + } + override suspend fun contains(k: Key): Boolean = helper.readableTransaction { select(tableName, selection = "$idColumnName=?", selectionArgs = arrayOf(k.asId()), limit = FirstPagePagination(1).limitClause()).use { it.count > 0 diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt index a231918430f..0da3137ff1f 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt @@ -42,12 +42,12 @@ class KtorWriteOneToManyKeyValueRepo ( override suspend fun add(toAdd: Map>) = client.unipost( buildStandardUrl( baseUrl, - clearRoute, + addRoute, ), BodyPair(keyValueMapSerializer, toAdd), Unit.serializer(), ) - override suspend fun clear(k: Key) = client.unipost( + override suspend fun clear(k: Key) = client.unipost( buildStandardUrl( baseUrl, clearRoute, @@ -55,4 +55,13 @@ class KtorWriteOneToManyKeyValueRepo ( BodyPair(keySerializer, k), Unit.serializer(), ) + + override suspend fun set(toSet: Map>) = client.unipost( + buildStandardUrl( + baseUrl, + setRoute, + ), + BodyPair(keyValueMapSerializer, toSet), + Unit.serializer(), + ) } \ No newline at end of file diff --git a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/one_to_many/OneToManyRoutes.kt b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/one_to_many/OneToManyRoutes.kt index 637edd9bda1..c1a2df1eac1 100644 --- a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/one_to_many/OneToManyRoutes.kt +++ b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/one_to_many/OneToManyRoutes.kt @@ -13,4 +13,5 @@ const val onDataClearedRoute = "onDataCleared" const val addRoute = "add" const val removeRoute = "remove" -const val clearRoute = "clear" \ No newline at end of file +const val clearRoute = "clear" +const val setRoute = "set" \ No newline at end of file diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt index fec8fa9d416..aeb8656a1eb 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt @@ -61,4 +61,13 @@ fun Route.configureOneToManyWriteKeyValueRepoRoutes( originalRepo.clear(key), ) } + + post(setRoute) { + val obj = call.uniload(keyValueMapSerializer) + + call.unianswer( + Unit.serializer(), + originalRepo.set(obj) + ) + } } \ No newline at end of file