From 5a5519c7cb871c37b99760ed6a33d49a8f5d49d6 Mon Sep 17 00:00:00 2001 From: 000Sanya <000sanya.000sanya@gmail.com> Date: Thu, 24 Sep 2020 12:27:16 +1000 Subject: [PATCH] implemented coroutines and exposed utils --- coroutines/build.gradle | 16 +++++++++++ .../micro_utils/coroutines/HandleSafely.kt | 23 ++++++++++++++++ exposed/build.gradle | 16 +++++++++++ .../micro_utils/exposed/QueryExtensions.kt | 27 +++++++++++++++++++ ktor/client/build.gradle | 1 + .../ktor/client/FlowsWebsockets.kt | 1 + ktor/common/build.gradle | 1 + ktor/server/build.gradle | 1 + .../micro_utils/ktor/server/FlowsWebsocket.kt | 1 + repos/exposed/build.gradle | 5 ++++ .../exposed/AbstractExposedReadCRUDRepo.kt | 8 +++--- .../exposed/AbstractExposedWriteCRUDRepo.kt | 4 +-- .../keyvalue/AbstractExposedKeyValueRepo.kt | 2 +- .../AbstractExposedReadKeyValueRepo.kt | 8 +++--- .../AbstractOneToManyExposedKeyValueRepo.kt | 4 +-- ...bstractOneToManyExposedReadKeyValueRepo.kt | 8 +++--- settings.gradle | 2 ++ 17 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 coroutines/build.gradle create mode 100644 coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt create mode 100644 exposed/build.gradle create mode 100644 exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/exposed/QueryExtensions.kt diff --git a/coroutines/build.gradle b/coroutines/build.gradle new file mode 100644 index 00000000000..7616133d4f6 --- /dev/null +++ b/coroutines/build.gradle @@ -0,0 +1,16 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" +} + +apply from: "$mppProjectWithSerializationPresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version" + } + } + } +} \ No newline at end of file diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt new file mode 100644 index 00000000000..c58ba10c3d3 --- /dev/null +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/HandleSafely.kt @@ -0,0 +1,23 @@ +package dev.inmo.micro_utils.coroutines + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.supervisorScope + + +typealias ExceptionHandler = suspend (Throwable) -> T +/** + * It will run [block] inside of [supervisorScope] to avoid problems with catching of exceptions + * + * @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this + * exception will be available for catching + */ +suspend inline fun safely( + noinline onException: ExceptionHandler = { throw it }, + noinline block: suspend CoroutineScope.() -> T +): T { + return try { + supervisorScope(block) + } catch (e: Throwable) { + onException(e) + } +} diff --git a/exposed/build.gradle b/exposed/build.gradle new file mode 100644 index 00000000000..6899f4079ce --- /dev/null +++ b/exposed/build.gradle @@ -0,0 +1,16 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" +} + +apply from: "$mppJavaProjectPresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + + } + } + } +} diff --git a/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/exposed/QueryExtensions.kt b/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/exposed/QueryExtensions.kt new file mode 100644 index 00000000000..c5b9cc02ca3 --- /dev/null +++ b/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/exposed/QueryExtensions.kt @@ -0,0 +1,27 @@ +package dev.inmo.micro_utils.exposed + +import dev.inmo.micro_utils.pagination.* +import org.jetbrains.exposed.sql.* + +fun Query.paginate(with: Pagination, orderBy: Pair, SortOrder>? = null) = limit( + with.size, + (if (orderBy ?.second == SortOrder.DESC) { + with.lastIndex + } else { + with.firstIndex + }).toLong() +).let { + if (orderBy != null) { + it.orderBy( + orderBy.first, + orderBy.second + ) + } else { + it + } +} + +fun Query.paginate(with: Pagination, orderBy: Expression<*>?, reversed: Boolean = false) = paginate( + with, + orderBy ?.let { it to if (reversed) SortOrder.DESC else SortOrder.ASC } +) diff --git a/ktor/client/build.gradle b/ktor/client/build.gradle index bcdcdfd4e4c..50f1eaaabfd 100644 --- a/ktor/client/build.gradle +++ b/ktor/client/build.gradle @@ -10,6 +10,7 @@ kotlin { commonMain { dependencies { api internalProject("micro_utils.ktor.common") + api internalProject("micro_utils.coroutines") api "io.ktor:ktor-client:$ktor_version" } } diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt index b9000c27e07..12772d08d9c 100644 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/FlowsWebsockets.kt @@ -1,5 +1,6 @@ package dev.inmo.micro_utils.ktor.client +import dev.inmo.micro_utils.coroutines.safely import dev.inmo.micro_utils.ktor.common.asCorrectWebSocketUrl import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import io.ktor.client.HttpClient diff --git a/ktor/common/build.gradle b/ktor/common/build.gradle index f8c12008e68..0d6bb922b88 100644 --- a/ktor/common/build.gradle +++ b/ktor/common/build.gradle @@ -9,6 +9,7 @@ kotlin { sourceSets { commonMain { dependencies { + api "org.jetbrains.kotlinx:kotlinx-serialization-cbor:$kotlin_serialisation_core_version" api "com.soywiz.korlibs.klock:klock:$klockVersion" } } diff --git a/ktor/server/build.gradle b/ktor/server/build.gradle index f21e34a994b..b6df33972d7 100644 --- a/ktor/server/build.gradle +++ b/ktor/server/build.gradle @@ -10,6 +10,7 @@ kotlin { commonMain { dependencies { api internalProject("micro_utils.ktor.common") + api internalProject("micro_utils.coroutines") } } diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt index 7ae3de5d4ad..c591eaf8655 100644 --- a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/FlowsWebsocket.kt @@ -1,5 +1,6 @@ package dev.inmo.micro_utils.ktor.server +import dev.inmo.micro_utils.coroutines.safely import dev.inmo.micro_utils.ktor.common.CorrectCloseException import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import io.ktor.http.cio.websocket.* diff --git a/repos/exposed/build.gradle b/repos/exposed/build.gradle index 15db9cd680d..b4a3d77c1f4 100644 --- a/repos/exposed/build.gradle +++ b/repos/exposed/build.gradle @@ -12,5 +12,10 @@ kotlin { api internalProject("micro_utils.repos.common") } } + jvmMain { + dependencies { + api internalProject("micro_utils.exposed") + } + } } } diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedReadCRUDRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedReadCRUDRepo.kt index 2662389b3b4..5ebd0b33847 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedReadCRUDRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedReadCRUDRepo.kt @@ -1,8 +1,10 @@ package dev.inmo.micro_utils.repos.exposed -import com.insanusmokrassar.postssystem.exposed.commons.paginate -import com.insanusmokrassar.postssystem.utils.common.pagination.* -import com.insanusmokrassar.postssystem.utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.exposed.paginate +import dev.inmo.micro_utils.pagination.Pagination +import dev.inmo.micro_utils.pagination.PaginationResult +import dev.inmo.micro_utils.pagination.createPaginationResult +import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedWriteCRUDRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedWriteCRUDRepo.kt index 4e0a0ff9bc7..d422f177725 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedWriteCRUDRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedWriteCRUDRepo.kt @@ -1,7 +1,7 @@ package dev.inmo.micro_utils.repos.exposed -import com.insanusmokrassar.postssystem.utils.repos.UpdatedValuePair -import com.insanusmokrassar.postssystem.utils.repos.WriteStandardCRUDRepo +import dev.inmo.micro_utils.repos.UpdatedValuePair +import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.asFlow diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt index 3cefadb086f..56fa9f88147 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt @@ -1,6 +1,6 @@ package dev.inmo.micro_utils.repos.exposed.keyvalue -import com.insanusmokrassar.postssystem.utils.repos.StandardKeyValueRepo +import dev.inmo.micro_utils.repos.StandardKeyValueRepo import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt index d8d96986060..6bf88b7cdfa 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt @@ -1,8 +1,10 @@ package dev.inmo.micro_utils.repos.exposed.keyvalue -import com.insanusmokrassar.postssystem.exposed.commons.paginate -import com.insanusmokrassar.postssystem.utils.common.pagination.* -import com.insanusmokrassar.postssystem.utils.repos.StandardReadKeyValueRepo +import dev.inmo.micro_utils.exposed.paginate +import dev.inmo.micro_utils.pagination.Pagination +import dev.inmo.micro_utils.pagination.PaginationResult +import dev.inmo.micro_utils.pagination.createPaginationResult +import dev.inmo.micro_utils.repos.StandardReadKeyValueRepo import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedKeyValueRepo.kt index 013d3edfc52..a6f1127e90d 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedKeyValueRepo.kt @@ -1,8 +1,6 @@ package dev.inmo.micro_utils.repos.exposed.onetomany -import com.insanusmokrassar.budgetmanager.core.utils.repo.onetomany.AbstractOneToManyExposedReadKeyValueRepo -import com.insanusmokrassar.budgetmanager.core.utils.repo.onetomany.ColumnAllocator -import com.insanusmokrassar.postssystem.utils.repos.OneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedReadKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedReadKeyValueRepo.kt index 90434afbf10..07ae9574e3c 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedReadKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedReadKeyValueRepo.kt @@ -1,8 +1,10 @@ package dev.inmo.micro_utils.repos.exposed.onetomany -import com.insanusmokrassar.postssystem.exposed.commons.paginate -import com.insanusmokrassar.postssystem.utils.common.pagination.* -import com.insanusmokrassar.postssystem.utils.repos.OneToManyReadKeyValueRepo +import dev.inmo.micro_utils.exposed.paginate +import dev.inmo.micro_utils.pagination.Pagination +import dev.inmo.micro_utils.pagination.PaginationResult +import dev.inmo.micro_utils.pagination.createPaginationResult +import dev.inmo.micro_utils.repos.OneToManyReadKeyValueRepo import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction diff --git a/settings.gradle b/settings.gradle index 1c87c1f50d7..8b76a53dfb9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -13,6 +13,8 @@ String[] includes = [ ":ktor:server", ":ktor:common", ":ktor:client", + ":coroutines", + ":exposed" ]