From 9ece160aa8133f402669c22efa3b1a6535305d47 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 25 Oct 2020 22:21:23 +0600 Subject: [PATCH] add extensions for onetomany functions --- CHANGELOG.md | 6 ++++ .../repos/OneToManyKeyValueRepo.kt | 32 +++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad4594b8140..c0c7930620e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.2.2 +* `Repos` + * `Common` + * Several new methods `ReadOneToManyKeyValueRepo#getAll` + * Several new method `WriteOneToManyKeyValueRepo#add` and several extensions + * Several new method `WriteOneToManyKeyValueRepo#remove` and several extensions + ## 0.2.1 * `Pagination` 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 e1313311baf..6fe71e2f445 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 @@ -25,11 +25,11 @@ interface ReadOneToManyKeyValueRepo : Repo { /** * WARNING!!! THIS METHOD PROBABLY IS NOT EFFICIENT, USE WITH CAUTION */ - suspend fun getAll(): Map> = mutableMapOf>().also { map -> + suspend fun getAll(reverseLists: Boolean = false): Map> = mutableMapOf>().also { map -> doWithPagination { keys(it).also { paginationResult -> paginationResult.results.forEach { k -> - map[k] = getAll(k) + map[k] = getAll(k, reverseLists) } }.nextPageIfNotEmpty() } @@ -62,4 +62,30 @@ interface WriteOneToManyKeyValueRepo : Repo { @Deprecated("Renamed", ReplaceWith("WriteOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo")) typealias OneToManyWriteKeyValueRepo = WriteOneToManyKeyValueRepo -interface OneToManyKeyValueRepo : ReadOneToManyKeyValueRepo, WriteOneToManyKeyValueRepo \ No newline at end of file +interface OneToManyKeyValueRepo : ReadOneToManyKeyValueRepo, WriteOneToManyKeyValueRepo + +suspend inline fun > REPO.add( + k: Key, + vararg v: Value +) = add(mapOf(k to v.toList())) + +suspend inline fun > REPO.add( + keysAndValues: List>> +) = add(keysAndValues.toMap()) + +suspend inline fun > REPO.add( + vararg keysAndValues: Pair> +) = add(keysAndValues.toMap()) + +suspend inline fun > REPO.remove( + k: Key, + vararg v: Value +) = remove(mapOf(k to v.toList())) + +suspend inline fun > REPO.remove( + keysAndValues: List>> +) = remove(keysAndValues.toMap()) + +suspend inline fun > REPO.remove( + vararg keysAndValues: Pair> +) = remove(keysAndValues.toMap())