From 0e4a63057f5463280598e72fbb81290ca1f2117c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 2 Jun 2022 23:50:06 +0600 Subject: [PATCH 01/14] start 0.11.0 --- gradle.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index 188d5773257..4d9254e829a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,5 +14,5 @@ crypto_js_version=4.1.1 # Project data group=dev.inmo -version=0.10.8 -android_code_version=123 +version=0.11.0 +android_code_version=124 From 8eed435302a397b159700ffd837e8f0232c19cf9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Jun 2022 12:22:51 +0600 Subject: [PATCH 02/14] start improvements in ktor parts --- CHANGELOG.md | 2 + .../ktor/client/NewFlowsWebsockets.kt | 54 +++++++++++ .../ktor/server/NewFlowsWebsocket.kt | 30 ++++++ .../client/crud/KtorReadStandardCrudRepo.kt | 1 + .../crud/KtorReadStandardCrudRepoClient.kt | 80 ++++++++++++++++ .../ktor/client/crud/KtorStandardCrudRepo.kt | 1 + .../client/crud/KtorStandardCrudRepoClient.kt | 93 +++++++++++++++++++ .../client/crud/KtorWriteStandardCrudRepo.kt | 1 + .../crud/KtorWriteStandardCrudRepoClient.kt | 51 ++++++++++ .../crud/NewKtorReadStandardCrudRepo.kt | 71 ++++++++++++++ .../server/crud/NewKtorStandardCrudRepo.kt | 34 +++++++ .../crud/NewKtorWriteStandardCrudRepo.kt | 43 +++++++++ 12 files changed, 461 insertions(+) create mode 100644 ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/NewFlowsWebsockets.kt create mode 100644 ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/NewFlowsWebsocket.kt create mode 100644 repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt create mode 100644 repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt create mode 100644 repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt create mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt create mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorStandardCrudRepo.kt create mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 1716489a86a..c9a0763bafc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.11.0 + ## 0.10.8 * `Common` diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/NewFlowsWebsockets.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/NewFlowsWebsockets.kt new file mode 100644 index 00000000000..ba84f813bac --- /dev/null +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/NewFlowsWebsockets.kt @@ -0,0 +1,54 @@ +package dev.inmo.micro_utils.ktor.client + +import dev.inmo.micro_utils.coroutines.runCatchingSafely +import dev.inmo.micro_utils.coroutines.safely +import dev.inmo.micro_utils.ktor.common.* +import io.ktor.client.HttpClient +import io.ktor.client.plugins.pluginOrNull +import io.ktor.client.plugins.websocket.* +import io.ktor.client.request.HttpRequestBuilder +import io.ktor.websocket.Frame +import io.ktor.websocket.readBytes +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.channelFlow +import kotlinx.coroutines.isActive +import kotlinx.serialization.DeserializationStrategy + +/** + * @param checkReconnection This lambda will be called when it is required to reconnect to websocket to establish + * connection. Must return true in case if must be reconnected. By default always reconnecting + */ +inline fun HttpClient.createStandardWebsocketFlow( + url: String, + crossinline checkReconnection: suspend (Throwable?) -> Boolean = { true }, + noinline requestBuilder: HttpRequestBuilder.() -> Unit = {} +): Flow { + pluginOrNull(WebSockets) ?: error("Plugin $WebSockets must be installed for using createStandardWebsocketFlow") + + val correctedUrl = url.asCorrectWebSocketUrl + + return channelFlow { + do { + val reconnect = runCatchingSafely { + ws(correctedUrl, requestBuilder) { + for (received in incoming) { + sendSerialized(received.data) + } + } + checkReconnection(null) + }.getOrElse { e -> + checkReconnection(e).also { + if (!it) { + close(e) + } + } + } + } while (reconnect && isActive) + + if (isActive) { + safely { + close() + } + } + } +} diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/NewFlowsWebsocket.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/NewFlowsWebsocket.kt new file mode 100644 index 00000000000..ae9108f9235 --- /dev/null +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/NewFlowsWebsocket.kt @@ -0,0 +1,30 @@ +package dev.inmo.micro_utils.ktor.server + +import dev.inmo.micro_utils.coroutines.safely +import dev.inmo.micro_utils.ktor.common.* +import io.ktor.http.URLProtocol +import io.ktor.server.application.install +import io.ktor.server.application.pluginOrNull +import io.ktor.server.routing.Route +import io.ktor.server.routing.application +import io.ktor.server.websocket.* +import io.ktor.websocket.send +import kotlinx.coroutines.flow.Flow +import kotlinx.serialization.SerializationStrategy + +inline fun Route.includeWebsocketHandling( + suburl: String, + flow: Flow, + protocol: URLProtocol? = null +) { + application.apply { + pluginOrNull(WebSockets) ?: install(WebSockets) + } + webSocket(suburl, protocol ?.name) { + safely { + flow.collect { + sendSerialized(it) + } + } + } +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt index 470249b0039..70bfce26391 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt @@ -9,6 +9,7 @@ import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer +@Deprecated("Use KtorReadStandardCrudRepoClient instead") class KtorReadStandardCrudRepo ( private val baseUrl: String, private val unifiedRequester: UnifiedRequester, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt new file mode 100644 index 00000000000..e7f00f3a40e --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt @@ -0,0 +1,80 @@ +package dev.inmo.micro_utils.repos.ktor.client.crud + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.pagination.* +import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ktor.common.crud.* +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.get +import io.ktor.http.HttpStatusCode +import io.ktor.util.reflect.TypeInfo +import io.ktor.util.reflect.typeInfo +import kotlinx.serialization.* + +class KtorReadStandardCrudRepoClient ( + private val baseUrl: String, + private val httpClient: HttpClient, + private val objectType: TypeInfo, + private val idSerializer: suspend (IdType) -> String +) : ReadStandardCRUDRepo { + override suspend fun getByPagination(pagination: Pagination): PaginationResult = httpClient.get( + buildStandardUrl(baseUrl, getByPaginationRouting, pagination.asUrlQueryParts) + ).body() + + override suspend fun getById(id: IdType): ObjectType? = httpClient.get( + buildStandardUrl( + baseUrl, + getByIdRouting, + mapOf( + "id" to idSerializer(id) + ) + ) + ).takeIf { it.status != HttpStatusCode.NoContent } ?.body(objectType) + + override suspend fun contains(id: IdType): Boolean = httpClient.get( + buildStandardUrl( + baseUrl, + containsRouting, + mapOf( + "id" to idSerializer(id) + ) + ) + ).body() + + override suspend fun count(): Long = httpClient.get( + buildStandardUrl( + baseUrl, + countRouting + ) + ).body() +} + +inline fun KtorReadStandardCrudRepoClient( + baseUrl: String, + httpClient: HttpClient, + noinline idSerializer: suspend (IdType) -> String +) = KtorReadStandardCrudRepoClient( + baseUrl, + httpClient, + typeInfo(), + idSerializer +) + +inline fun KtorReadStandardCrudRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: StringFormat +) = KtorReadStandardCrudRepoClient(baseUrl, httpClient) { + serialFormat.encodeToString(idsSerializer, it) +} + +inline fun KtorReadStandardCrudRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: BinaryFormat +) = KtorReadStandardCrudRepoClient(baseUrl, httpClient) { + serialFormat.encodeHex(idsSerializer, it) +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt index 19561cd92eb..1f0b31fafd4 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt @@ -7,6 +7,7 @@ import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer +@Deprecated("Use KtorStandardCrudRepoClient instead") class KtorStandardCrudRepo ( baseUrl: String, baseSubpart: String, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt new file mode 100644 index 00000000000..aa6bc86600e --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt @@ -0,0 +1,93 @@ +package dev.inmo.micro_utils.repos.ktor.client.crud + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.repos.* +import io.ktor.client.HttpClient +import io.ktor.util.reflect.TypeInfo +import io.ktor.util.reflect.typeInfo +import kotlinx.serialization.* + +class KtorStandardCrudRepoClient ( + baseUrl: String, + httpClient: HttpClient, + objectTypeInfo: TypeInfo, + idSerializer: suspend (IdType) -> String +) : StandardCRUDRepo, + ReadStandardCRUDRepo by KtorReadStandardCrudRepoClient( + baseUrl, + httpClient, + objectTypeInfo, + idSerializer + ), + WriteStandardCRUDRepo by KtorWriteStandardCrudRepoClient( + baseUrl, + httpClient + ) { + constructor( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + objectTypeInfo: TypeInfo, + idSerializer: suspend (IdType) -> String + ) : this( + buildStandardUrl(baseUrl, subpart), httpClient, objectTypeInfo, idSerializer + ) +} + + +inline fun KtorStandardCrudRepoClient( + baseUrl: String, + httpClient: HttpClient, + noinline idSerializer: suspend (IdType) -> String +) = KtorStandardCrudRepoClient( + baseUrl, + httpClient, + typeInfo(), + idSerializer +) + +inline fun KtorStandardCrudRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: StringFormat +) = KtorStandardCrudRepoClient(baseUrl, httpClient) { + serialFormat.encodeToString(idsSerializer, it) +} + +inline fun KtorStandardCrudRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: BinaryFormat +) = KtorStandardCrudRepoClient(baseUrl, httpClient) { + serialFormat.encodeHex(idsSerializer, it) +} + + +inline fun KtorStandardCrudRepoClient( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + noinline idSerializer: suspend (IdType) -> String +) = KtorStandardCrudRepoClient( + buildStandardUrl(baseUrl, subpart), + httpClient, + idSerializer +) + +inline fun KtorStandardCrudRepoClient( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: StringFormat +) = KtorStandardCrudRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat) + +inline fun KtorStandardCrudRepoClient( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: BinaryFormat +) = KtorStandardCrudRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt index 71bc3823d92..2013041d432 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt @@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.Flow import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* +@Deprecated("Use KtorWriteStandardCrudRepoClient instead") class KtorWriteStandardCrudRepo ( private val baseUrl: String, private val unifiedRequester: UnifiedRequester, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt new file mode 100644 index 00000000000..b0f9e27a9f0 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt @@ -0,0 +1,51 @@ +package dev.inmo.micro_utils.repos.ktor.client.crud + +import dev.inmo.micro_utils.ktor.client.* +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.repos.UpdatedValuePair +import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo +import dev.inmo.micro_utils.repos.ktor.common.crud.* +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.* +import io.ktor.util.reflect.TypeInfo +import io.ktor.util.reflect.typeInfo +import kotlinx.coroutines.flow.Flow +import kotlinx.serialization.KSerializer +import kotlinx.serialization.builtins.* + +class KtorWriteStandardCrudRepoClient ( + private val baseUrl: String, + private val httpClient: HttpClient +) : WriteStandardCRUDRepo { + + override val newObjectsFlow: Flow = httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, newObjectsFlowRouting), + ) + override val updatedObjectsFlow: Flow = httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, updatedObjectsFlowRouting) + ) + override val deletedObjectsIdsFlow: Flow = httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting) + ) + + + override suspend fun create(values: List): List = httpClient.post { + url(buildStandardUrl(baseUrl, createRouting)) + setBody(values) + }.body() + + override suspend fun update(values: List>): List = httpClient.post { + url(buildStandardUrl(baseUrl, updateManyRouting)) + setBody(values) + }.body() + + override suspend fun update(id: IdType, value: InputValue): ObjectType? = update(listOf(id to value)).firstOrNull() + + override suspend fun deleteById(ids: List) { + httpClient.post { + url(buildStandardUrl(baseUrl, deleteByIdRouting)) + setBody(ids) + } + } +} diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt new file mode 100644 index 00000000000..7ac033b7006 --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt @@ -0,0 +1,71 @@ +package dev.inmo.micro_utils.repos.ktor.server.crud + +import dev.inmo.micro_utils.ktor.common.decodeHex +import dev.inmo.micro_utils.ktor.server.* +import dev.inmo.micro_utils.pagination.extractPagination +import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ktor.common.crud.* +import io.ktor.http.ContentType +import io.ktor.http.HttpStatusCode +import io.ktor.server.application.call +import io.ktor.server.response.respond +import io.ktor.server.routing.Route +import io.ktor.server.routing.get +import kotlinx.serialization.* + +inline fun Route.configureReadStandardCrudRepoRoutes( + originalRepo: ReadStandardCRUDRepo, + noinline idDeserializer: suspend (String) -> IdType +) { + get(getByPaginationRouting) { + val pagination = call.request.queryParameters.extractPagination + + call.respond(originalRepo.getByPagination(pagination)) + } + + get(getByIdRouting) { + val id = idDeserializer( + call.getQueryParameterOrSendError("id") ?: return@get + ) + + val result = originalRepo.getById(id) + + if (result == null) { + call.respond(HttpStatusCode.NoContent) + } else { + call.respond(result) + } + } + + get(containsRouting) { + val id = idDeserializer( + call.getQueryParameterOrSendError("id") ?: return@get + ) + + call.respond( + originalRepo.contains(id) + ) + } + + get(countRouting) { + call.respond( + originalRepo.count() + ) + } +} + +inline fun Route.configureReadStandardCrudRepoRoutes( + originalRepo: ReadStandardCRUDRepo, + idsSerializer: KSerializer, + serialFormat: StringFormat +) = configureReadStandardCrudRepoRoutes(originalRepo) { + serialFormat.decodeFromString(idsSerializer, it) +} + +inline fun Route.configureReadStandardCrudRepoRoutes( + originalRepo: ReadStandardCRUDRepo, + idsSerializer: KSerializer, + serialFormat: BinaryFormat +) = configureReadStandardCrudRepoRoutes(originalRepo) { + serialFormat.decodeHex(idsSerializer, it) +} diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorStandardCrudRepo.kt new file mode 100644 index 00000000000..30aa02b89da --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorStandardCrudRepo.kt @@ -0,0 +1,34 @@ +package dev.inmo.micro_utils.repos.ktor.server.crud + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.ktor.server.UnifiedRouter +import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType +import dev.inmo.micro_utils.repos.StandardCRUDRepo +import io.ktor.http.ContentType +import io.ktor.server.routing.Route +import io.ktor.server.routing.route +import kotlinx.serialization.* + +inline fun Route.configureStandardCrudRepoRoutes( + originalRepo: StandardCRUDRepo, + noinline idDeserializer: suspend (String) -> IdType +) { + configureReadStandardCrudRepoRoutes(originalRepo, idDeserializer) + configureWriteStandardCrudRepoRoutes(originalRepo) +} + +inline fun Route.configureStandardCrudRepoRoutes( + originalRepo: StandardCRUDRepo, + idsSerializer: KSerializer, + serialFormat: StringFormat +) = configureStandardCrudRepoRoutes(originalRepo) { + serialFormat.decodeFromString(idsSerializer, it) +} + +inline fun Route.configureStandardCrudRepoRoutes( + originalRepo: StandardCRUDRepo, + idsSerializer: KSerializer, + serialFormat: BinaryFormat +) = configureStandardCrudRepoRoutes(originalRepo) { + serialFormat.decodeHex(idsSerializer, it) +} diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt new file mode 100644 index 00000000000..ae205e27f5d --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt @@ -0,0 +1,43 @@ +package dev.inmo.micro_utils.repos.ktor.server.crud + +import dev.inmo.micro_utils.ktor.server.* +import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo +import dev.inmo.micro_utils.repos.ktor.common.crud.* +import io.ktor.server.application.call +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Route +import io.ktor.server.routing.post + +inline fun Route.configureWriteStandardCrudRepoRoutes( + originalRepo: WriteStandardCRUDRepo +) { + includeWebsocketHandling( + newObjectsFlowRouting, + originalRepo.newObjectsFlow, + ) + includeWebsocketHandling( + updatedObjectsFlowRouting, + originalRepo.updatedObjectsFlow + ) + includeWebsocketHandling( + deletedObjectsIdsFlowRouting, + originalRepo.deletedObjectsIdsFlow + ) + + post(createRouting) { + call.respond( + originalRepo.create( + call.receive() + ) + ) + } + + post(updateManyRouting) { + call.respond(originalRepo.update(call.receive())) + } + + post(deleteByIdRouting) { + call.respond(originalRepo.deleteById(call.receive())) + } +} From bcb0e42fa21d3c7f2fa9d457b4264b9945d1d43a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Jun 2022 17:52:58 +0600 Subject: [PATCH 03/14] add new started clients for crud --- .../ktor/client/NewFlowsWebsockets.kt | 5 +- .../micro_utils/repos/StandartCRUDRepo.kt | 9 +- .../dev/inmo/micro_utils/repos/MapCRUDRepo.kt | 17 ++- .../crud/KtorReadStandardCrudRepoClient.kt | 31 ++++-- .../client/crud/KtorStandardCrudRepoClient.kt | 105 +++++++++++------- .../crud/KtorWriteStandardCrudRepoClient.kt | 88 ++++++++++----- 6 files changed, 174 insertions(+), 81 deletions(-) diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/NewFlowsWebsockets.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/NewFlowsWebsockets.kt index ba84f813bac..4ee959eceb3 100644 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/NewFlowsWebsockets.kt +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/NewFlowsWebsockets.kt @@ -9,6 +9,7 @@ import io.ktor.client.plugins.websocket.* import io.ktor.client.request.HttpRequestBuilder import io.ktor.websocket.Frame import io.ktor.websocket.readBytes +import io.ktor.websocket.serialization.sendSerializedBase import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.channelFlow import kotlinx.coroutines.isActive @@ -18,9 +19,9 @@ import kotlinx.serialization.DeserializationStrategy * @param checkReconnection This lambda will be called when it is required to reconnect to websocket to establish * connection. Must return true in case if must be reconnected. By default always reconnecting */ -inline fun HttpClient.createStandardWebsocketFlow( +inline fun HttpClient.createStandardWebsocketFlow( url: String, - crossinline checkReconnection: suspend (Throwable?) -> Boolean = { true }, + noinline checkReconnection: suspend (Throwable?) -> Boolean = { true }, noinline requestBuilder: HttpRequestBuilder.() -> Unit = {} ): Flow { pluginOrNull(WebSockets) ?: error("Plugin $WebSockets must be installed for using createStandardWebsocketFlow") diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt index df64fc47de7..33911b8d8ed 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt @@ -42,4 +42,11 @@ suspend fun WriteStandardCRUDRepo : ReadStandardCRUDRepo, WriteStandardCRUDRepo -typealias CRUDRepo = StandardCRUDRepo \ No newline at end of file +typealias CRUDRepo = StandardCRUDRepo + +class DelegateBasedStandardCRUDRepo( + readDelegate: ReadStandardCRUDRepo, + writeDelegate: WriteStandardCRUDRepo +) : StandardCRUDRepo, + ReadStandardCRUDRepo by readDelegate, + WriteStandardCRUDRepo by writeDelegate diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt index 1d605a4c77b..e040c478112 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt @@ -74,19 +74,24 @@ abstract class MapCRUDRepo( fun MapCRUDRepo( map: MutableMap, - updateCallback: suspend (newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, - createCallback: suspend (newValue: InputValueType) -> Pair + updateCallback: suspend MutableMap.(newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, + createCallback: suspend MutableMap.(newValue: InputValueType) -> Pair ) = object : MapCRUDRepo(map) { override suspend fun updateObject( newValue: InputValueType, id: IdType, old: ObjectType - ): ObjectType = updateCallback(newValue, id, old) + ): ObjectType = map.updateCallback(newValue, id, old) - override suspend fun createObject(newValue: InputValueType): Pair = createCallback(newValue) + override suspend fun createObject(newValue: InputValueType): Pair = map.createCallback(newValue) } +fun MapCRUDRepo( + updateCallback: suspend MutableMap.(newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, + createCallback: suspend MutableMap.(newValue: InputValueType) -> Pair +) = MapCRUDRepo(mutableMapOf(), updateCallback, createCallback) + fun MutableMap.asCrudRepo( - updateCallback: suspend (newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, - createCallback: suspend (newValue: InputValueType) -> Pair + updateCallback: suspend MutableMap.(newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, + createCallback: suspend MutableMap.(newValue: InputValueType) -> Pair ) = MapCRUDRepo(this, updateCallback, createCallback) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt index e7f00f3a40e..ce280f2c8bf 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt @@ -7,7 +7,7 @@ import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.request.get -import io.ktor.http.HttpStatusCode +import io.ktor.http.* import io.ktor.util.reflect.TypeInfo import io.ktor.util.reflect.typeInfo import kotlinx.serialization.* @@ -16,11 +16,14 @@ class KtorReadStandardCrudRepoClient ( private val baseUrl: String, private val httpClient: HttpClient, private val objectType: TypeInfo, + private val contentType: ContentType, private val idSerializer: suspend (IdType) -> String ) : ReadStandardCRUDRepo { override suspend fun getByPagination(pagination: Pagination): PaginationResult = httpClient.get( buildStandardUrl(baseUrl, getByPaginationRouting, pagination.asUrlQueryParts) - ).body() + ) { + contentType(contentType) + }.body() override suspend fun getById(id: IdType): ObjectType? = httpClient.get( buildStandardUrl( @@ -30,7 +33,9 @@ class KtorReadStandardCrudRepoClient ( "id" to idSerializer(id) ) ) - ).takeIf { it.status != HttpStatusCode.NoContent } ?.body(objectType) + ) { + contentType(contentType) + }.takeIf { it.status != HttpStatusCode.NoContent } ?.body(objectType) override suspend fun contains(id: IdType): Boolean = httpClient.get( buildStandardUrl( @@ -40,24 +45,30 @@ class KtorReadStandardCrudRepoClient ( "id" to idSerializer(id) ) ) - ).body() + ) { + contentType(contentType) + }.body() override suspend fun count(): Long = httpClient.get( buildStandardUrl( baseUrl, countRouting ) - ).body() + ) { + contentType(contentType) + }.body() } inline fun KtorReadStandardCrudRepoClient( baseUrl: String, httpClient: HttpClient, + contentType: ContentType, noinline idSerializer: suspend (IdType) -> String ) = KtorReadStandardCrudRepoClient( baseUrl, httpClient, typeInfo(), + contentType, idSerializer ) @@ -65,8 +76,9 @@ inline fun KtorReadStandardCrudRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, - serialFormat: StringFormat -) = KtorReadStandardCrudRepoClient(baseUrl, httpClient) { + serialFormat: StringFormat, + contentType: ContentType, +) = KtorReadStandardCrudRepoClient(baseUrl, httpClient, contentType) { serialFormat.encodeToString(idsSerializer, it) } @@ -74,7 +86,8 @@ inline fun KtorReadStandardCrudRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, - serialFormat: BinaryFormat -) = KtorReadStandardCrudRepoClient(baseUrl, httpClient) { + serialFormat: BinaryFormat, + contentType: ContentType, +) = KtorReadStandardCrudRepoClient(baseUrl, httpClient, contentType) { serialFormat.encodeHex(idsSerializer, it) } diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt index aa6bc86600e..d32a072f26c 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt @@ -3,91 +3,120 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient +import io.ktor.client.utils.EmptyContent.contentType +import io.ktor.http.ContentType import io.ktor.util.reflect.TypeInfo import io.ktor.util.reflect.typeInfo import kotlinx.serialization.* class KtorStandardCrudRepoClient ( - baseUrl: String, - httpClient: HttpClient, - objectTypeInfo: TypeInfo, - idSerializer: suspend (IdType) -> String -) : StandardCRUDRepo, - ReadStandardCRUDRepo by KtorReadStandardCrudRepoClient( - baseUrl, - httpClient, - objectTypeInfo, - idSerializer - ), - WriteStandardCRUDRepo by KtorWriteStandardCrudRepoClient( - baseUrl, - httpClient - ) { - constructor( - baseUrl: String, - subpart: String, - httpClient: HttpClient, - objectTypeInfo: TypeInfo, - idSerializer: suspend (IdType) -> String - ) : this( - buildStandardUrl(baseUrl, subpart), httpClient, objectTypeInfo, idSerializer - ) + readDelegate: ReadStandardCRUDRepo, + writeDelegate: WriteStandardCRUDRepo +) : StandardCRUDRepo by DelegateBasedStandardCRUDRepo( + readDelegate, + writeDelegate +) { + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + objectTypeInfo: TypeInfo, + contentType: ContentType, + noinline idSerializer: suspend (IdType) -> String + ) = KtorStandardCrudRepoClient( + KtorReadStandardCrudRepoClient( + baseUrl, + httpClient, + objectTypeInfo, + contentType, + idSerializer + ), + KtorWriteStandardCrudRepoClient( + baseUrl, + httpClient, + contentType + ) + ) + + inline operator fun invoke( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + objectTypeInfo: TypeInfo, + contentType: ContentType, + noinline idSerializer: suspend (IdType) -> String + ) = KtorStandardCrudRepoClient( + buildStandardUrl(baseUrl, subpart), + httpClient, + objectTypeInfo, + contentType, + idSerializer + ) + } } -inline fun KtorStandardCrudRepoClient( +inline fun KtorStandardCrudRepoClient( baseUrl: String, httpClient: HttpClient, + contentType: ContentType, noinline idSerializer: suspend (IdType) -> String ) = KtorStandardCrudRepoClient( baseUrl, httpClient, typeInfo(), + contentType, idSerializer ) -inline fun KtorStandardCrudRepoClient( +inline fun KtorStandardCrudRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, - serialFormat: StringFormat -) = KtorStandardCrudRepoClient(baseUrl, httpClient) { + serialFormat: StringFormat, + contentType: ContentType, +) = KtorStandardCrudRepoClient(baseUrl, httpClient, contentType) { serialFormat.encodeToString(idsSerializer, it) } -inline fun KtorStandardCrudRepoClient( +inline fun KtorStandardCrudRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, - serialFormat: BinaryFormat -) = KtorStandardCrudRepoClient(baseUrl, httpClient) { + serialFormat: BinaryFormat, + contentType: ContentType, +) = KtorStandardCrudRepoClient(baseUrl, httpClient, contentType) { serialFormat.encodeHex(idsSerializer, it) } -inline fun KtorStandardCrudRepoClient( +inline fun KtorStandardCrudRepoClient( baseUrl: String, subpart: String, httpClient: HttpClient, + contentType: ContentType, noinline idSerializer: suspend (IdType) -> String ) = KtorStandardCrudRepoClient( buildStandardUrl(baseUrl, subpart), httpClient, + contentType, idSerializer ) -inline fun KtorStandardCrudRepoClient( +inline fun KtorStandardCrudRepoClient( baseUrl: String, subpart: String, httpClient: HttpClient, idsSerializer: KSerializer, - serialFormat: StringFormat -) = KtorStandardCrudRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat) + serialFormat: StringFormat, + contentType: ContentType, +) = KtorStandardCrudRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat, contentType) -inline fun KtorStandardCrudRepoClient( +inline fun KtorStandardCrudRepoClient( baseUrl: String, subpart: String, httpClient: HttpClient, idsSerializer: KSerializer, - serialFormat: BinaryFormat -) = KtorStandardCrudRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat) + serialFormat: BinaryFormat, + contentType: ContentType, +) = KtorStandardCrudRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat, contentType) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt index b0f9e27a9f0..0c5e6673b12 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt @@ -8,6 +8,9 @@ import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.request.* +import io.ktor.client.statement.HttpResponse +import io.ktor.http.ContentType +import io.ktor.http.contentType import io.ktor.util.reflect.TypeInfo import io.ktor.util.reflect.typeInfo import kotlinx.coroutines.flow.Flow @@ -16,36 +19,71 @@ import kotlinx.serialization.builtins.* class KtorWriteStandardCrudRepoClient ( private val baseUrl: String, - private val httpClient: HttpClient + private val httpClient: HttpClient, + override val newObjectsFlow: Flow, + override val updatedObjectsFlow: Flow, + override val deletedObjectsIdsFlow: Flow, + private val createSetup: suspend HttpRequestBuilder.(List) -> Unit, + private val updateSetup: suspend HttpRequestBuilder.(List>) -> Unit, + private val deleteByIdSetup: suspend HttpRequestBuilder.(List) -> Unit, + private val createBodyGetter: suspend HttpResponse.() -> List, + private val updateBodyGetter: suspend HttpResponse.() -> List ) : WriteStandardCRUDRepo { + override suspend fun create(values: List): List = httpClient.post( + buildStandardUrl(baseUrl, createRouting) + ) { + createSetup(values) + }.createBodyGetter() - override val newObjectsFlow: Flow = httpClient.createStandardWebsocketFlow( - buildStandardUrl(baseUrl, newObjectsFlowRouting), - ) - override val updatedObjectsFlow: Flow = httpClient.createStandardWebsocketFlow( - buildStandardUrl(baseUrl, updatedObjectsFlowRouting) - ) - override val deletedObjectsIdsFlow: Flow = httpClient.createStandardWebsocketFlow( - buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting) - ) - - - override suspend fun create(values: List): List = httpClient.post { - url(buildStandardUrl(baseUrl, createRouting)) - setBody(values) - }.body() - - override suspend fun update(values: List>): List = httpClient.post { - url(buildStandardUrl(baseUrl, updateManyRouting)) - setBody(values) - }.body() + override suspend fun update( + values: List> + ): List = httpClient.post( + buildStandardUrl(baseUrl, updateManyRouting) + ) { + updateSetup(values) + }.updateBodyGetter() override suspend fun update(id: IdType, value: InputValue): ObjectType? = update(listOf(id to value)).firstOrNull() override suspend fun deleteById(ids: List) { - httpClient.post { - url(buildStandardUrl(baseUrl, deleteByIdRouting)) - setBody(ids) - } + httpClient.post( + buildStandardUrl(baseUrl, deleteByIdRouting) + ) { + deleteByIdSetup(ids) + }.status + } + + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType + ) = KtorWriteStandardCrudRepoClient( + baseUrl, + httpClient, + httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, newObjectsFlowRouting), + ), + httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, updatedObjectsFlowRouting), + ), + httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, deletedObjectsIdsFlowRouting), + ), + { + contentType(contentType) + setBody(it) + }, + { + contentType(contentType) + setBody(it) + }, + { + contentType(contentType) + setBody(it) + }, + { body() }, + { body() } + ) } } From eaa143f7d7609611c7ea511b34e4e4f5295f799a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Jun 2022 22:33:26 +0600 Subject: [PATCH 04/14] kv rework, fixes in map keyvalue repo, tests --- gradle/libs.versions.toml | 7 + .../micro_utils/repos/StandartKeyValueRepo.kt | 7 + .../inmo/micro_utils/repos/MapKeyValueRepo.kt | 10 +- .../client/crud/KtorReadStandardCrudRepo.kt | 9 +- .../crud/KtorReadStandardCrudRepoClient.kt | 5 +- .../client/crud/KtorStandardCrudRepoClient.kt | 4 +- .../KtorReadStandardKeyValueRepoClient.kt | 144 ++++++++++++++++++ .../KtorStandardKeyValueRepoClient.kt | 85 +++++++++++ .../KtorWriteStandardKeyValueRepoClient.kt | 83 ++++++++++ repos/ktor/common/build.gradle | 17 +++ .../repos/ktor/common/ParametersNames.kt | 3 +- .../common/src/jvmTest/kotlin/CRUDTests.kt | 89 +++++++++++ .../common/src/jvmTest/kotlin/ComplexData.kt | 10 ++ .../ktor/common/src/jvmTest/kotlin/KVTests.kt | 139 +++++++++++++++++ .../common/src/jvmTest/kotlin/SimpleData.kt | 7 + .../crud/NewKtorReadStandardCrudRepo.kt | 5 +- .../crud/NewKtorWriteStandardCrudRepo.kt | 6 +- .../key_value/NewKtorStandartKeyValueRepo.kt | 58 +++++++ .../NewKtorStandartReadKeyValueRepo.kt | 104 +++++++++++++ .../NewKtorStandartWriteKeyValueRepo.kt | 45 ++++++ 20 files changed, 814 insertions(+), 23 deletions(-) create mode 100644 repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt create mode 100644 repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandardKeyValueRepoClient.kt create mode 100644 repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt create mode 100644 repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt create mode 100644 repos/ktor/common/src/jvmTest/kotlin/ComplexData.kt create mode 100644 repos/ktor/common/src/jvmTest/kotlin/KVTests.kt create mode 100644 repos/ktor/common/src/jvmTest/kotlin/SimpleData.kt create mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartKeyValueRepo.kt create mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt create mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 145cf936447..12aa141beb1 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -37,17 +37,24 @@ kt-serialization-cbor = { module = "org.jetbrains.kotlinx:kotlinx-serialization- kt-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kt-coroutines" } kt-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kt-coroutines" } +kt-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kt-coroutines" } ktor-io = { module = "io.ktor:ktor-io", version.ref = "ktor" } +ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" } ktor-client = { module = "io.ktor:ktor-client-core", version.ref = "ktor" } +ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" } ktor-client-java = { module = "io.ktor:ktor-client-java", version.ref = "ktor" } +ktor-client-websockets = { module = "io.ktor:ktor-client-websockets", version.ref = "ktor" } +ktor-client-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" } +ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" } ktor-server = { module = "io.ktor:ktor-server", version.ref = "ktor" } ktor-server-cio = { module = "io.ktor:ktor-server-cio", version.ref = "ktor" } ktor-server-host-common = { module = "io.ktor:ktor-server-host-common", version.ref = "ktor" } ktor-websockets = { module = "io.ktor:ktor-websockets", version.ref = "ktor" } ktor-server-websockets = { module = "io.ktor:ktor-server-websockets", version.ref = "ktor" } ktor-server-statusPages = { module = "io.ktor:ktor-server-status-pages", version.ref = "ktor" } +ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" } klock = { module = "com.soywiz.korlibs.klock:klock", version.ref = "klock" } diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt index 90ad3a0b92f..b224d8caebf 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt @@ -50,3 +50,10 @@ interface StandardKeyValueRepo : ReadStandardKeyValueRepo = StandardKeyValueRepo + +class DelegateBasedStandardKeyValueRepo( + readDelegate: ReadStandardKeyValueRepo, + writeDelegate: WriteStandardKeyValueRepo +) : StandardKeyValueRepo, + ReadStandardKeyValueRepo by readDelegate, + WriteStandardKeyValueRepo by writeDelegate diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt index 3d929737a12..7e87197ac8d 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt @@ -78,11 +78,11 @@ class WriteMapKeyValueRepo( } override suspend fun unsetWithValues(toUnset: List) { - map.forEach { - if (it.value in toUnset) { - map.remove(it.key) - _onValueRemoved.emit(it.key) - } + map.mapNotNull { (k, v) -> + k.takeIf { v in toUnset } + }.forEach { + map.remove(it) + _onValueRemoved.emit(it) } } } diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt index 70bfce26391..80955437308 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt @@ -5,6 +5,7 @@ import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* +import dev.inmo.micro_utils.repos.ktor.common.idParameterName import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer @@ -39,9 +40,7 @@ class KtorReadStandardCrudRepo ( buildStandardUrl( baseUrl, getByIdRouting, - mapOf( - "id" to unifiedRequester.encodeUrlQueryValue(idsSerializer, id) - ) + idParameterName to unifiedRequester.encodeUrlQueryValue(idsSerializer, id) ), objectsSerializerNullable ) @@ -50,9 +49,7 @@ class KtorReadStandardCrudRepo ( buildStandardUrl( baseUrl, containsRouting, - mapOf( - "id" to unifiedRequester.encodeUrlQueryValue(idsSerializer, id) - ) + idParameterName to unifiedRequester.encodeUrlQueryValue(idsSerializer, id) ), Boolean.serializer() ) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt index ce280f2c8bf..82de6de4b5c 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt @@ -4,6 +4,7 @@ import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* +import dev.inmo.micro_utils.repos.ktor.common.idParameterName import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.request.get @@ -30,7 +31,7 @@ class KtorReadStandardCrudRepoClient ( baseUrl, getByIdRouting, mapOf( - "id" to idSerializer(id) + idParameterName to idSerializer(id) ) ) ) { @@ -42,7 +43,7 @@ class KtorReadStandardCrudRepoClient ( baseUrl, containsRouting, mapOf( - "id" to idSerializer(id) + idParameterName to idSerializer(id) ) ) ) { diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt index d32a072f26c..87d79182c6d 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt @@ -17,7 +17,7 @@ class KtorStandardCrudRepoClient ( writeDelegate ) { companion object { - inline operator fun invoke( + inline operator fun invoke( baseUrl: String, httpClient: HttpClient, objectTypeInfo: TypeInfo, @@ -38,7 +38,7 @@ class KtorStandardCrudRepoClient ( ) ) - inline operator fun invoke( + inline operator fun invoke( baseUrl: String, subpart: String, httpClient: HttpClient, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt new file mode 100644 index 00000000000..e22a426dca2 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt @@ -0,0 +1,144 @@ +package dev.inmo.micro_utils.repos.ktor.client.key_value + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.pagination.* +import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.* +import dev.inmo.micro_utils.repos.ktor.common.crud.* +import dev.inmo.micro_utils.repos.ktor.common.key_value.* +import dev.inmo.micro_utils.repos.ktor.common.one_to_many.containsByKeyRoute +import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.get +import io.ktor.http.* +import io.ktor.util.reflect.TypeInfo +import io.ktor.util.reflect.typeInfo +import kotlinx.serialization.* + +class KtorReadStandardKeyValueRepoClient( + private val baseUrl: String, + private val httpClient: HttpClient, + private val contentType: ContentType, + private val objectType: TypeInfo, + private val paginationResultObjectsTypeInfo: TypeInfo, + private val paginationResultIdsTypeInfo: TypeInfo, + private val idSerializer: suspend (Key) -> String, + private val valueSerializer: suspend (Value) -> String +) : ReadStandardKeyValueRepo { + override suspend fun get(k: Key): Value? = httpClient.get( + buildStandardUrl( + baseUrl, + getRoute, + mapOf( + idParameterName to idSerializer(k) + ) + ) + ) { + contentType(contentType) + }.takeIf { it.status != HttpStatusCode.NoContent } ?.body(objectType) + + override suspend fun contains(key: Key): Boolean = httpClient.get( + buildStandardUrl( + baseUrl, + containsRoute, + idParameterName to idSerializer(key) + ) + ) { + contentType(contentType) + }.body() + + override suspend fun values( + pagination: Pagination, + reversed: Boolean + ): PaginationResult = httpClient.get( + buildStandardUrl(baseUrl, valuesRoute, pagination.asUrlQueryParts + (reversedParameterName to reversed.toString())) + ) { + contentType(contentType) + }.body(paginationResultObjectsTypeInfo) + + override suspend fun keys( + pagination: Pagination, + reversed: Boolean + ): PaginationResult = httpClient.get( + buildStandardUrl(baseUrl, keysRoute, pagination.asUrlQueryParts + (reversedParameterName to reversed.toString())) + ) { + contentType(contentType) + }.body(paginationResultIdsTypeInfo) + + override suspend fun keys( + v: Value, + pagination: Pagination, + reversed: Boolean + ): PaginationResult = httpClient.get( + buildStandardUrl( + baseUrl, + keysRoute, + pagination.asUrlQueryParts + (reversedParameterName to reversed.toString()) + (valueParameterName to valueSerializer(v)) + ) + ) { + contentType(contentType) + }.body(paginationResultIdsTypeInfo) + + override suspend fun count(): Long = httpClient.get( + buildStandardUrl( + baseUrl, + countRouting + ) + ) { + contentType(contentType) + }.body() +} + +inline fun KtorReadStandardKeyValueRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + noinline idSerializer: suspend (Key) -> String, + noinline valueSerializer: suspend (Value) -> String +) = KtorReadStandardKeyValueRepoClient( + baseUrl, + httpClient, + contentType, + typeInfo(), + typeInfo>(), + typeInfo>(), + idSerializer, + valueSerializer +) + +inline fun KtorReadStandardKeyValueRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: StringFormat, + contentType: ContentType, +) = KtorReadStandardKeyValueRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeToString(idsSerializer, it).encodeURLQueryComponent() + } +) { + serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() +} + +inline fun KtorReadStandardKeyValueRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + valuesSerializer: KSerializer, + serialFormat: BinaryFormat, + contentType: ContentType, +) = KtorReadStandardKeyValueRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeHex(idsSerializer, it) + } +) { + serialFormat.encodeHex(valuesSerializer, it) +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandardKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandardKeyValueRepoClient.kt new file mode 100644 index 00000000000..92f60bf7053 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandardKeyValueRepoClient.kt @@ -0,0 +1,85 @@ +package dev.inmo.micro_utils.repos.ktor.client.key_value + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.repos.* +import io.ktor.client.HttpClient +import io.ktor.http.ContentType +import io.ktor.http.encodeURLQueryComponent +import kotlinx.serialization.* + +class KtorStandardKeyValueRepoClient ( + readDelegate: ReadStandardKeyValueRepo, + writeDelegate: WriteStandardKeyValueRepo +) : StandardKeyValueRepo by DelegateBasedStandardKeyValueRepo( + readDelegate, + writeDelegate +) { + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + noinline idSerializer: suspend (Key) -> String, + noinline valueSerializer: suspend (Value) -> String + ) = KtorStandardKeyValueRepoClient( + KtorReadStandardKeyValueRepoClient( + baseUrl, httpClient, contentType, idSerializer, valueSerializer + ), + KtorWriteStandardKeyValueRepoClient( + baseUrl, + httpClient, + contentType + ) + ) + inline operator fun invoke( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + contentType: ContentType, + noinline idSerializer: suspend (Key) -> String, + noinline valueSerializer: suspend (Value) -> String + ) = KtorStandardKeyValueRepoClient( + buildStandardUrl(baseUrl, subpart), + httpClient, + contentType, + idSerializer, + valueSerializer + ) + } +} + +inline fun KtorStandardKeyValueRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + idSerializer: SerializationStrategy, + valueSerializer: SerializationStrategy, + serialFormat: StringFormat, +) = KtorStandardKeyValueRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeToString(idSerializer, it).encodeURLQueryComponent() + } +) { + serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() +} + +inline fun KtorStandardKeyValueRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + idSerializer: SerializationStrategy, + valueSerializer: SerializationStrategy, + serialFormat: BinaryFormat, +) = KtorStandardKeyValueRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeHex(idSerializer, it) + } +) { + serialFormat.encodeHex(valueSerializer, it) +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt new file mode 100644 index 00000000000..65cceca45e9 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt @@ -0,0 +1,83 @@ +package dev.inmo.micro_utils.repos.ktor.client.key_value + +import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.pagination.* +import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.* +import dev.inmo.micro_utils.repos.ktor.common.crud.* +import dev.inmo.micro_utils.repos.ktor.common.key_value.* +import io.ktor.client.HttpClient +import io.ktor.client.request.get +import io.ktor.client.request.post +import io.ktor.http.* +import io.ktor.util.InternalAPI +import io.ktor.util.reflect.TypeInfo +import io.ktor.util.reflect.typeInfo +import kotlinx.coroutines.flow.Flow +import kotlinx.serialization.* + +class KtorWriteStandardKeyValueRepoClient( + private val baseUrl: String, + private val httpClient: HttpClient, + private val contentType: ContentType, + override val onNewValue: Flow>, + override val onValueRemoved: Flow, + private val idsListTypeInfo: TypeInfo, + private val objectsListTypeInfo: TypeInfo, + private val idsToObjectsMapTypeInfo: TypeInfo +) : WriteStandardKeyValueRepo { + @OptIn(InternalAPI::class) + override suspend fun unsetWithValues(toUnset: List) { + httpClient.post( + buildStandardUrl(baseUrl, unsetWithValuesRoute) + ) { + body = toUnset + bodyType = objectsListTypeInfo + contentType(contentType) + }.status + } + + @OptIn(InternalAPI::class) + override suspend fun unset(toUnset: List) { + httpClient.post( + buildStandardUrl(baseUrl, unsetRoute) + ) { + body = toUnset + bodyType = idsListTypeInfo + contentType(contentType) + }.status + } + + @OptIn(InternalAPI::class) + override suspend fun set(toSet: Map) { + httpClient.post( + buildStandardUrl(baseUrl, setRoute) + ) { + body = toSet + bodyType = idsToObjectsMapTypeInfo + contentType(contentType) + }.status + } + + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType + ) = KtorWriteStandardKeyValueRepoClient( + baseUrl, + httpClient, + contentType, + httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, onNewValueRoute), + ), + httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, onValueRemovedRoute), + ), + typeInfo>(), + typeInfo>(), + typeInfo>() + ) + } +} diff --git a/repos/ktor/common/build.gradle b/repos/ktor/common/build.gradle index 1881ecbcc12..79002ed147b 100644 --- a/repos/ktor/common/build.gradle +++ b/repos/ktor/common/build.gradle @@ -13,5 +13,22 @@ kotlin { api internalProject("micro_utils.repos.common") } } + jvmTest { + dependencies { + implementation internalProject("micro_utils.repos.common") + implementation internalProject("micro_utils.repos.ktor.client") + implementation internalProject("micro_utils.repos.ktor.server") + implementation internalProject("micro_utils.repos.inmemory") + implementation libs.kt.coroutines.test + + implementation libs.ktor.server.cio + implementation libs.ktor.client.cio + implementation libs.ktor.server.content.negotiation + implementation libs.ktor.serialization.kotlinx.json + implementation libs.ktor.client.content.negotiation + implementation libs.ktor.client.logging + implementation libs.ktor.client.websockets + } + } } } diff --git a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/ParametersNames.kt b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/ParametersNames.kt index 85bece83603..68022ef6200 100644 --- a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/ParametersNames.kt +++ b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/ParametersNames.kt @@ -1,5 +1,6 @@ package dev.inmo.micro_utils.repos.ktor.common +const val idParameterName = "id" const val keyParameterName = "key" const val valueParameterName = "value" -const val reversedParameterName = "reversed" \ No newline at end of file +const val reversedParameterName = "reversed" diff --git a/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt b/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt new file mode 100644 index 00000000000..9c17c341e0f --- /dev/null +++ b/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt @@ -0,0 +1,89 @@ +import dev.inmo.micro_utils.repos.* +import dev.inmo.micro_utils.repos.ktor.client.crud.KtorStandardCrudRepoClient +import dev.inmo.micro_utils.repos.ktor.server.crud.configureStandardCrudRepoRoutes +import io.ktor.client.HttpClient +import io.ktor.client.plugins.logging.Logging +import io.ktor.http.ContentType +import io.ktor.serialization.kotlinx.KotlinxWebsocketSerializationConverter +import io.ktor.serialization.kotlinx.json.json +import io.ktor.server.application.install +import io.ktor.server.cio.CIO +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import io.ktor.server.routing.routing +import io.ktor.server.websocket.WebSockets +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import kotlinx.serialization.json.Json +import kotlin.test.Test +import kotlin.test.assertEquals + +class CRUDTests { + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun testCRUDFunctions() { + runTest() { + val map = mutableMapOf() + val repo = MapCRUDRepo( + map, + { newValue, id, oldValue -> + oldValue.copy(title = newValue.title) + } + ) { + size to ComplexData(size, title = it.title) + } + val server = io.ktor.server.engine.embeddedServer( + CIO, + 23456, + "127.0.0.1" + ) { + install(ContentNegotiation) { + json() + } + install(WebSockets) { + contentConverter = KotlinxWebsocketSerializationConverter(Json) + } + routing { + configureStandardCrudRepoRoutes( + repo + ) { + it.toInt() + } + } + }.start(false) + val client = HttpClient { + install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) { + json() + } + install(Logging) + install(io.ktor.client.plugins.websocket.WebSockets) { + contentConverter = KotlinxWebsocketSerializationConverter(Json) + } + } + val crudClient = KtorStandardCrudRepoClient( + "http://127.0.0.1:23456", + client, + ContentType.Application.Json + ) { + it.toString() + } + + val created = crudClient.create(SimpleData("Example")).single() + assertEquals(map.size, 1) + assertEquals(map.size.toLong(), crudClient.count()) + assertEquals(1, crudClient.count()) + assertEquals(map.getValue(map.keys.first()), created) + + val updated = crudClient.update(created.id, SimpleData("Example2")) + assertEquals(map.size, 1) + assertEquals(map.size.toLong(), crudClient.count()) + assertEquals(1, crudClient.count()) + assertEquals(map.getValue(map.keys.first()), updated) + + crudClient.deleteById(created.id) + assertEquals(map.size, 0) + assertEquals(map.size.toLong(), crudClient.count()) + assertEquals(0, crudClient.count()) + server.stop() + } + } +} diff --git a/repos/ktor/common/src/jvmTest/kotlin/ComplexData.kt b/repos/ktor/common/src/jvmTest/kotlin/ComplexData.kt new file mode 100644 index 00000000000..ad202ee46b3 --- /dev/null +++ b/repos/ktor/common/src/jvmTest/kotlin/ComplexData.kt @@ -0,0 +1,10 @@ +import com.benasher44.uuid.uuid4 +import kotlinx.serialization.Serializable + +@Serializable +data class ComplexData( + val id: Int, + val simple: SimpleData = SimpleData(), + val simples: List = (0 until 100).map { SimpleData(("$it")) }, + val title: String = uuid4().toString() +) diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt new file mode 100644 index 00000000000..e9071246e86 --- /dev/null +++ b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt @@ -0,0 +1,139 @@ +import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination +import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging +import dev.inmo.micro_utils.repos.* +import dev.inmo.micro_utils.repos.ktor.client.key_value.KtorStandardKeyValueRepoClient +import dev.inmo.micro_utils.repos.ktor.server.key_value.configureStandardKeyValueRepoRoutes +import io.ktor.client.HttpClient +import io.ktor.client.plugins.logging.Logging +import io.ktor.http.ContentType +import io.ktor.serialization.kotlinx.KotlinxWebsocketSerializationConverter +import io.ktor.serialization.kotlinx.json.json +import io.ktor.server.application.install +import io.ktor.server.cio.CIO +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import io.ktor.server.routing.routing +import io.ktor.server.websocket.WebSockets +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.json.Json +import kotlin.test.* + +class KVTests { + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun testCRUDFunctions() { + runTest() { + val map = mutableMapOf() + val repo = MapKeyValueRepo(map) + val server = io.ktor.server.engine.embeddedServer( + CIO, + 23456, + "127.0.0.1" + ) { + install(ContentNegotiation) { + json() + } + install(WebSockets) { + contentConverter = KotlinxWebsocketSerializationConverter(Json) + } + routing { + configureStandardKeyValueRepoRoutes( + repo, + Int.serializer(), + ComplexData.serializer(), + Json {} + ) + } + }.start(false) + val client = HttpClient { + install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) { + json() + } + install(Logging) + install(io.ktor.client.plugins.websocket.WebSockets) { + contentConverter = KotlinxWebsocketSerializationConverter(Json) + } + } + val crudClient = KtorStandardKeyValueRepoClient( + "http://127.0.0.1:23456", + client, + ContentType.Application.Json, + Int.serializer(), + ComplexData.serializer(), + Json + ) + + val dataInOneKey = ComplexData(1, title = "Example1") + val dataInMultipleKeys = ComplexData(2, title = "Example2") + val repeatCount = 3 + + val dataList = listOf( + 1 to dataInOneKey + ) + (0 until repeatCount).map { + (it + 2) to dataInMultipleKeys + } + + dataList.forEachIndexed { i, (id, data) -> + crudClient.set(id, data) + assertEquals(map.size, i + 1) + assertEquals(map.size.toLong(), crudClient.count()) + assertEquals(i + 1L, crudClient.count()) + dataList.take(i + 1).forEach { (id, data) -> + assertEquals(data, map[id]) + assertEquals(data, crudClient.get(id)) + assertEquals(map[id], crudClient.get(id)) + } + } + + dataList.forEach { (id, data) -> + assertTrue(crudClient.contains(id)) + assertEquals(data, crudClient.get(id)) + } + + assertEquals( + dataList.mapNotNull { if (it.second == dataInMultipleKeys) it.first else null }, + getAllWithNextPaging(firstPageWithOneElementPagination) { + crudClient.keys(dataInMultipleKeys, it) + } + ) + + assertEquals( + dataList.mapNotNull { if (it.second == dataInOneKey) it.first else null }, + getAllWithNextPaging(firstPageWithOneElementPagination) { + crudClient.keys(dataInOneKey, it) + } + ) + + assertEquals( + dataList.map { it.first }, + getAllWithNextPaging(firstPageWithOneElementPagination) { + crudClient.keys(it) + } + ) + + assertEquals( + dataList.map { it.second }, + getAllWithNextPaging(firstPageWithOneElementPagination) { + crudClient.values(it) + } + ) + + assertEquals(dataList.size.toLong(), crudClient.count()) + + crudClient.unsetWithValues(dataInMultipleKeys) + assertEquals( + dataList.filter { it.second == dataInOneKey }.size.toLong(), + crudClient.count() + ) + + crudClient.unset(dataList.first { it.second == dataInOneKey }.first) + assertEquals( + 0, + crudClient.count() + ) + + server.stop() + } + } +} diff --git a/repos/ktor/common/src/jvmTest/kotlin/SimpleData.kt b/repos/ktor/common/src/jvmTest/kotlin/SimpleData.kt new file mode 100644 index 00000000000..a078cfe0cf1 --- /dev/null +++ b/repos/ktor/common/src/jvmTest/kotlin/SimpleData.kt @@ -0,0 +1,7 @@ +import com.benasher44.uuid.uuid4 +import kotlinx.serialization.Serializable + +@Serializable +data class SimpleData( + val title: String = uuid4().toString() +) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt index 7ac033b7006..e8ee34dd409 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt @@ -5,6 +5,7 @@ import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* +import dev.inmo.micro_utils.repos.ktor.common.idParameterName import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode import io.ktor.server.application.call @@ -25,7 +26,7 @@ inline fun Route.configureReadStandardCrudR get(getByIdRouting) { val id = idDeserializer( - call.getQueryParameterOrSendError("id") ?: return@get + call.getQueryParameterOrSendError(idParameterName) ?: return@get ) val result = originalRepo.getById(id) @@ -39,7 +40,7 @@ inline fun Route.configureReadStandardCrudR get(containsRouting) { val id = idDeserializer( - call.getQueryParameterOrSendError("id") ?: return@get + call.getQueryParameterOrSendError(idParameterName) ?: return@get ) call.respond( diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt index ae205e27f5d..3dbe3c78188 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt @@ -26,11 +26,7 @@ inline fun Route.configureStandardKeyValueRepoRoutes ( + originalRepo: StandardKeyValueRepo, + noinline idDeserializer: suspend (String) -> Key, + noinline valueDeserializer: suspend (String) -> Value +) { + configureReadStandardKeyValueRepoRoutes(originalRepo, idDeserializer, valueDeserializer) + configureWriteStandardKeyValueRepoRoutes(originalRepo) +} + +inline fun Route.configureStandardKeyValueRepoRoutes( + originalRepo: StandardKeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: StringFormat +) = configureStandardKeyValueRepoRoutes( + originalRepo, + { + serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) + }, + { + serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent()) + } +) + +inline fun Route.configureStandardKeyValueRepoRoutes( + originalRepo: StandardKeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: BinaryFormat +) = configureStandardKeyValueRepoRoutes( + originalRepo, + { + serialFormat.decodeHex(idsSerializer, it) + }, + { + serialFormat.decodeHex(valueSerializer, it) + } +) + diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt new file mode 100644 index 00000000000..8ade9432458 --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt @@ -0,0 +1,104 @@ +package dev.inmo.micro_utils.repos.ktor.server.key_value + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.ktor.server.* +import dev.inmo.micro_utils.pagination.PaginationResult +import dev.inmo.micro_utils.pagination.extractPagination +import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.idParameterName +import dev.inmo.micro_utils.repos.ktor.common.key_value.* +import dev.inmo.micro_utils.repos.ktor.common.valueParameterName +import io.ktor.http.* +import io.ktor.server.application.call +import io.ktor.server.response.respond +import io.ktor.server.response.responseType +import io.ktor.server.routing.Route +import io.ktor.server.routing.get +import io.ktor.util.InternalAPI +import io.ktor.util.reflect.typeInfo +import kotlinx.serialization.* +import kotlinx.serialization.builtins.serializer + +@OptIn(InternalAPI::class) +inline fun Route.configureReadStandardKeyValueRepoRoutes ( + originalRepo: ReadStandardKeyValueRepo, + noinline idDeserializer: suspend (String) -> Key, + noinline valueDeserializer: suspend (String) -> Value +) { + get(getRoute) { + val key = idDeserializer( + call.getQueryParameterOrSendError(idParameterName) ?: return@get + ) + + originalRepo.get(key) ?.let { + call.respond(it) + } ?: call.respond(HttpStatusCode.NoContent) + } + + val paginationWithValuesTypeInfo = typeInfo>() + get(valuesRoute) { + val pagination = call.request.queryParameters.extractPagination + val reversed = call.getQueryParameter(reversedParameterName) ?.toBoolean() ?: false + + call.response.responseType = paginationWithValuesTypeInfo + call.response.pipeline.execute(call, originalRepo.values(pagination, reversed) as Any) + } + + get(keysRoute) { + val pagination = call.request.queryParameters.extractPagination + val reversed = call.getQueryParameterOrSendError(reversedParameterName) ?.toBoolean() ?: false + val value = call.getQueryParameter(valueParameterName) ?.let { + valueDeserializer(it) + } + + call.respond( + value ?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed) + ) + } + + get(containsRoute) { + val key = idDeserializer( + call.getQueryParameterOrSendError(idParameterName) ?: return@get + ) + + call.respond( + originalRepo.contains(key) + ) + } + + get(countRoute) { + call.respond(originalRepo.count()) + } +} + +inline fun Route.configureReadStandardKeyValueRepoRoutes( + originalRepo: ReadStandardKeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: StringFormat +) = configureReadStandardKeyValueRepoRoutes( + originalRepo, + { + serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) + }, + { + serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent()) + } +) + +inline fun Route.configureReadStandardKeyValueRepoRoutes( + originalRepo: ReadStandardKeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: BinaryFormat +) = configureReadStandardKeyValueRepoRoutes( + originalRepo, + { + serialFormat.decodeHex(idsSerializer, it) + }, + { + serialFormat.decodeHex(valueSerializer, it) + } +) + diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt new file mode 100644 index 00000000000..bac70871d05 --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt @@ -0,0 +1,45 @@ +package dev.inmo.micro_utils.repos.ktor.server.key_value + +import dev.inmo.micro_utils.ktor.server.* +import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.key_value.* +import io.ktor.http.HttpStatusCode +import io.ktor.server.application.call +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Route +import io.ktor.server.routing.post +import io.ktor.util.reflect.typeInfo + +inline fun Route.configureWriteStandardKeyValueRepoRoutes ( + originalRepo: WriteStandardKeyValueRepo +) { + includeWebsocketHandling( + onNewValueRoute, + originalRepo.onNewValue + ) + + includeWebsocketHandling( + onValueRemovedRoute, + originalRepo.onValueRemoved + ) + + val mapType = typeInfo>() + val listKeysType = typeInfo>() + val listValuesType = typeInfo>() + + post(setRoute) { + originalRepo.set(call.receive(mapType)) + call.respond(HttpStatusCode.OK) + } + + post(unsetRoute) { + originalRepo.unset(call.receive(listKeysType)) + call.respond(HttpStatusCode.OK) + } + + post(unsetWithValuesRoute) { + originalRepo.unsetWithValues(call.receive(listValuesType)) + call.respond(HttpStatusCode.OK) + } +} From 78903cd4eb691cf47cf44bc3401458bfda7603c5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Jun 2022 00:22:36 +0600 Subject: [PATCH 05/14] complete improvements in repos ktor parts --- CHANGELOG.md | 6 + .../ktor/client/HttpStatusCodeAsThrowable.kt | 15 ++ .../ApplicationCallRespondWithTypeInfo.kt | 15 ++ .../repos/OneToManyKeyValueRepo.kt | 7 + .../repos/MapOneToManyKeyValueRepo.kt | 4 + .../client/crud/KtorReadStandardCrudRepo.kt | 1 + .../crud/KtorReadStandardCrudRepoClient.kt | 1 + .../crud/KtorWriteStandardCrudRepoClient.kt | 6 +- .../key_value/KtorReadStandardKeyValueRepo.kt | 6 +- .../KtorReadStandardKeyValueRepoClient.kt | 5 +- .../KtorWriteStandardKeyValueRepoClient.kt | 12 +- .../KtorReadStandardKeyValuesRepoClient.kt | 159 ++++++++++++++++++ .../KtorStandardKeyValuesRepoClient.kt | 89 ++++++++++ .../KtorWriteStandardKeyValuesRepoClient.kt | 106 ++++++++++++ .../repos/ktor/common/ContainsRoute.kt | 3 + .../repos/ktor/common/CountRoute.kt | 3 + .../repos/ktor/common/CountRouting.kt | 3 + .../repos/ktor/common/crud/CrudReadRoutes.kt | 1 - .../ktor/common/key_value/KeyValueRoutes.kt | 6 +- .../common/one_to_many/OneToManyRoutes.kt | 2 +- .../common/src/jvmTest/kotlin/CRUDTests.kt | 2 +- .../ktor/common/src/jvmTest/kotlin/KVTests.kt | 4 +- .../common/src/jvmTest/kotlin/KVsTests.kt | 141 ++++++++++++++++ .../server/crud/KtorReadStandardCrudRepo.kt | 1 + .../crud/NewKtorReadStandardCrudRepo.kt | 1 + .../key_value/KtorStandartReadKeyValueRepo.kt | 6 +- .../NewKtorStandartReadKeyValueRepo.kt | 26 +-- .../NewKtorStandartKeyValuesRepo.kt | 47 ++++++ .../NewKtorStandartReadKeyValuesRepo.kt | 107 ++++++++++++ .../NewKtorStandartWriteKeyValuesRepo.kt | 58 +++++++ 30 files changed, 805 insertions(+), 38 deletions(-) create mode 100644 ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/HttpStatusCodeAsThrowable.kt create mode 100644 ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ApplicationCallRespondWithTypeInfo.kt create mode 100644 repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadStandardKeyValuesRepoClient.kt create mode 100644 repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorStandardKeyValuesRepoClient.kt create mode 100644 repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteStandardKeyValuesRepoClient.kt create mode 100644 repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/ContainsRoute.kt create mode 100644 repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/CountRoute.kt create mode 100644 repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/CountRouting.kt create mode 100644 repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt create mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartKeyValuesRepo.kt create mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartReadKeyValuesRepo.kt create mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartWriteKeyValuesRepo.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index c9a0763bafc..33eb21d63d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.11.0 +* `Ktor` + * +* `Repos` + * `Ktor`: + * Fully rewritten work with all declared repositories + ## 0.10.8 * `Common` diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/HttpStatusCodeAsThrowable.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/HttpStatusCodeAsThrowable.kt new file mode 100644 index 00000000000..8f4205c0caf --- /dev/null +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/HttpStatusCodeAsThrowable.kt @@ -0,0 +1,15 @@ +package dev.inmo.micro_utils.ktor.client + +import io.ktor.client.plugins.ClientRequestException +import io.ktor.client.statement.HttpResponse +import io.ktor.http.isSuccess + +inline fun HttpResponse.throwOnUnsuccess( + unsuccessMessage: () -> String +) { + if (status.isSuccess()) { + return + } + + throw ClientRequestException(this, unsuccessMessage()) +} diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ApplicationCallRespondWithTypeInfo.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ApplicationCallRespondWithTypeInfo.kt new file mode 100644 index 00000000000..207dde73a63 --- /dev/null +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ApplicationCallRespondWithTypeInfo.kt @@ -0,0 +1,15 @@ +package dev.inmo.micro_utils.ktor.server + +import io.ktor.server.application.ApplicationCall +import io.ktor.server.response.responseType +import io.ktor.util.InternalAPI +import io.ktor.util.reflect.TypeInfo + +@InternalAPI +suspend fun ApplicationCall.respond( + message: T, + typeInfo: TypeInfo +) { + response.responseType = typeInfo + response.pipeline.execute(this, message as Any) +} 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 df5bafa86d5..ce9836c59cd 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 @@ -104,6 +104,13 @@ interface OneToManyKeyValueRepo : ReadOneToManyKeyValueRepo = OneToManyKeyValueRepo +class DelegateBasedOneToManyKeyValueRepo( + readDelegate: ReadOneToManyKeyValueRepo, + writeDelegate: WriteOneToManyKeyValueRepo +) : OneToManyKeyValueRepo, + ReadOneToManyKeyValueRepo by readDelegate, + WriteOneToManyKeyValueRepo by writeDelegate + suspend inline fun WriteOneToManyKeyValueRepo.remove( keysAndValues: List>> ) = remove(keysAndValues.toMap()) diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt index 08ebbb45f90..be4dbfd7ab0 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt @@ -80,6 +80,10 @@ class MapWriteOneToManyKeyValueRepo( _onValueRemoved.emit(k to v) } } + if (map[k] ?.isEmpty() == true) { + map.remove(k) + _onDataCleared.emit(k) + } } } diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt index 80955437308..dfb7ff57df9 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt @@ -4,6 +4,7 @@ import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ktor.common.countRouting import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.idParameterName import io.ktor.client.HttpClient diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt index 82de6de4b5c..088cea5e31c 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt @@ -3,6 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ktor.common.countRouting import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.idParameterName import io.ktor.client.HttpClient diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt index 0c5e6673b12..6caf6969b83 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt @@ -11,11 +11,7 @@ import io.ktor.client.request.* import io.ktor.client.statement.HttpResponse import io.ktor.http.ContentType import io.ktor.http.contentType -import io.ktor.util.reflect.TypeInfo -import io.ktor.util.reflect.typeInfo import kotlinx.coroutines.flow.Flow -import kotlinx.serialization.KSerializer -import kotlinx.serialization.builtins.* class KtorWriteStandardCrudRepoClient ( private val baseUrl: String, @@ -50,7 +46,7 @@ class KtorWriteStandardCrudRepoClient ( buildStandardUrl(baseUrl, deleteByIdRouting) ) { deleteByIdSetup(ids) - }.status + }.throwOnUnsuccess { "Unable to delete $ids" } } companion object { diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt index 55bdf8f0ad8..2359c8d2c24 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt @@ -4,8 +4,12 @@ import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.* +import dev.inmo.micro_utils.repos.ktor.common.containsRoute +import dev.inmo.micro_utils.repos.ktor.common.countRoute import dev.inmo.micro_utils.repos.ktor.common.key_value.* -import dev.inmo.micro_utils.repos.ktor.common.valueParameterName +import dev.inmo.micro_utils.repos.ktor.common.key_value.keyParameterName +import dev.inmo.micro_utils.repos.ktor.common.key_value.reversedParameterName import io.ktor.client.HttpClient import kotlinx.serialization.* import kotlinx.serialization.builtins.serializer diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt index e22a426dca2..792d6b0d0d9 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt @@ -4,9 +4,8 @@ import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.* -import dev.inmo.micro_utils.repos.ktor.common.crud.* +import dev.inmo.micro_utils.repos.ktor.common.containsRoute import dev.inmo.micro_utils.repos.ktor.common.key_value.* -import dev.inmo.micro_utils.repos.ktor.common.one_to_many.containsByKeyRoute import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import io.ktor.client.HttpClient import io.ktor.client.call.body @@ -83,7 +82,7 @@ class KtorReadStandardKeyValueRepoClient( override suspend fun count(): Long = httpClient.get( buildStandardUrl( baseUrl, - countRouting + dev.inmo.micro_utils.repos.ktor.common.countRoute ) ) { contentType(contentType) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt index 65cceca45e9..d9abaf672ce 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt @@ -1,21 +1,17 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow +import dev.inmo.micro_utils.ktor.client.throwOnUnsuccess import dev.inmo.micro_utils.ktor.common.* -import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo -import dev.inmo.micro_utils.repos.ktor.common.* -import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.client.HttpClient -import io.ktor.client.request.get import io.ktor.client.request.post import io.ktor.http.* import io.ktor.util.InternalAPI import io.ktor.util.reflect.TypeInfo import io.ktor.util.reflect.typeInfo import kotlinx.coroutines.flow.Flow -import kotlinx.serialization.* class KtorWriteStandardKeyValueRepoClient( private val baseUrl: String, @@ -35,7 +31,7 @@ class KtorWriteStandardKeyValueRepoClient( body = toUnset bodyType = objectsListTypeInfo contentType(contentType) - }.status + }.throwOnUnsuccess { "Unable to unset data with values $toUnset" } } @OptIn(InternalAPI::class) @@ -46,7 +42,7 @@ class KtorWriteStandardKeyValueRepoClient( body = toUnset bodyType = idsListTypeInfo contentType(contentType) - }.status + }.throwOnUnsuccess { "Unable to unset $toUnset" } } @OptIn(InternalAPI::class) @@ -57,7 +53,7 @@ class KtorWriteStandardKeyValueRepoClient( body = toSet bodyType = idsToObjectsMapTypeInfo contentType(contentType) - }.status + }.throwOnUnsuccess { "Unable to set $toSet" } } companion object { diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadStandardKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadStandardKeyValuesRepoClient.kt new file mode 100644 index 00000000000..f05478c1ea7 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadStandardKeyValuesRepoClient.kt @@ -0,0 +1,159 @@ +package dev.inmo.micro_utils.repos.ktor.client.one_to_many + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.pagination.* +import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.* +import dev.inmo.micro_utils.repos.ktor.common.crud.* +import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* +import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.request.get +import io.ktor.http.* +import io.ktor.util.reflect.TypeInfo +import io.ktor.util.reflect.typeInfo +import kotlinx.serialization.* + +class KtorReadStandardKeyValuesRepoClient( + private val baseUrl: String, + private val httpClient: HttpClient, + private val contentType: ContentType, + private val paginationResultValuesTypeInfo: TypeInfo, + private val paginationResultKeysTypeInfo: TypeInfo, + private val keySerializer: suspend (Key) -> String, + private val valueSerializer: suspend (Value) -> String +) : ReadOneToManyKeyValueRepo { + override suspend fun get( + k: Key, + pagination: Pagination, + reversed: Boolean + ): PaginationResult = httpClient.get( + buildStandardUrl( + baseUrl, + getRoute, + pagination.asUrlQueryParts + (reversedParameterName to reversed.toString()) + (keyParameterName to keySerializer(k)) + ) + ) { + contentType(contentType) + }.body(paginationResultValuesTypeInfo) + + override suspend fun keys( + pagination: Pagination, + reversed: Boolean + ): PaginationResult = httpClient.get( + buildStandardUrl( + baseUrl, + keysRoute, + pagination.asUrlQueryParts + (reversedParameterName to reversed.toString()) + ) + ) { + contentType(contentType) + }.body(paginationResultKeysTypeInfo) + + override suspend fun keys( + v: Value, + pagination: Pagination, + reversed: Boolean + ): PaginationResult = httpClient.get( + buildStandardUrl( + baseUrl, + keysRoute, + pagination.asUrlQueryParts + (reversedParameterName to reversed.toString()) + (valueParameterName to valueSerializer(v)) + ) + ) { + contentType(contentType) + }.body(paginationResultKeysTypeInfo) + + override suspend fun contains(k: Key): Boolean = httpClient.get( + buildStandardUrl( + baseUrl, + containsRoute, + keyParameterName to keySerializer(k) + ) + ) { + contentType(contentType) + }.body() + + override suspend fun contains(k: Key, v: Value): Boolean = httpClient.get( + buildStandardUrl( + baseUrl, + containsRoute, + keyParameterName to keySerializer(k), + valueParameterName to valueSerializer(v) + ) + ) { + contentType(contentType) + }.body() + + override suspend fun count(): Long = httpClient.get( + buildStandardUrl( + baseUrl, + countRouting + ) + ) { + contentType(contentType) + }.body() + + override suspend fun count(k: Key): Long = httpClient.get( + buildStandardUrl( + baseUrl, + countRouting, + keyParameterName to keySerializer(k), + ) + ) { + contentType(contentType) + }.body() +} + +inline fun KtorReadStandardKeyValuesRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + noinline keySerializer: suspend (Key) -> String, + noinline valueSerializer: suspend (Value) -> String +) = KtorReadStandardKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + typeInfo>(), + typeInfo>(), + keySerializer, + valueSerializer +) + +inline fun KtorReadStandardKeyValuesRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: StringFormat, + contentType: ContentType, +) = KtorReadStandardKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeToString(idsSerializer, it).encodeURLQueryComponent() + } +) { + serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() +} + +inline fun KtorReadStandardKeyValuesRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + valuesSerializer: KSerializer, + serialFormat: BinaryFormat, + contentType: ContentType, +) = KtorReadStandardKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeHex(idsSerializer, it) + } +) { + serialFormat.encodeHex(valuesSerializer, it) +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorStandardKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorStandardKeyValuesRepoClient.kt new file mode 100644 index 00000000000..6f1e2f43087 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorStandardKeyValuesRepoClient.kt @@ -0,0 +1,89 @@ +package dev.inmo.micro_utils.repos.ktor.client.one_to_many + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.repos.* +import io.ktor.client.HttpClient +import io.ktor.http.ContentType +import io.ktor.http.encodeURLQueryComponent +import kotlinx.serialization.* + +class KtorStandardKeyValuesRepoClient ( + readDelegate: ReadOneToManyKeyValueRepo, + writeDelegate: WriteOneToManyKeyValueRepo +) : OneToManyKeyValueRepo by DelegateBasedOneToManyKeyValueRepo( + readDelegate, + writeDelegate +) { + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + noinline keySerializer: suspend (Key) -> String, + noinline valueSerializer: suspend (Value) -> String + ) = KtorStandardKeyValuesRepoClient( + KtorReadStandardKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + keySerializer, + valueSerializer + ), + KtorWriteStandardKeyValuesRepoClient( + baseUrl, + httpClient, + contentType + ) + ) + inline operator fun invoke( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + contentType: ContentType, + noinline keySerializer: suspend (Key) -> String, + noinline valueSerializer: suspend (Value) -> String + ) = KtorStandardKeyValuesRepoClient( + buildStandardUrl(baseUrl, subpart), + httpClient, + contentType, + keySerializer, + valueSerializer + ) + } +} + +inline fun KtorStandardKeyValuesRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + keySerializer: SerializationStrategy, + valueSerializer: SerializationStrategy, + serialFormat: StringFormat, +) = KtorStandardKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeToString(keySerializer, it).encodeURLQueryComponent() + } +) { + serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() +} + +inline fun KtorStandardKeyValuesRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + keySerializer: SerializationStrategy, + valueSerializer: SerializationStrategy, + serialFormat: BinaryFormat, +) = KtorStandardKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeHex(keySerializer, it) + } +) { + serialFormat.encodeHex(valueSerializer, it) +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteStandardKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteStandardKeyValuesRepoClient.kt new file mode 100644 index 00000000000..1f77a79f57f --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteStandardKeyValuesRepoClient.kt @@ -0,0 +1,106 @@ +package dev.inmo.micro_utils.repos.ktor.client.one_to_many + +import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow +import dev.inmo.micro_utils.ktor.client.throwOnUnsuccess +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* +import io.ktor.client.HttpClient +import io.ktor.client.request.post +import io.ktor.http.* +import io.ktor.util.InternalAPI +import io.ktor.util.reflect.TypeInfo +import io.ktor.util.reflect.typeInfo +import kotlinx.coroutines.flow.Flow + +class KtorWriteStandardKeyValuesRepoClient( + private val baseUrl: String, + private val httpClient: HttpClient, + private val contentType: ContentType, + override val onNewValue: Flow>, + override val onValueRemoved: Flow>, + override val onDataCleared: Flow, + private val keyTypeInfo: TypeInfo, + private val valueTypeInfo: TypeInfo, + private val keyToValuesMapTypeInfo: TypeInfo +) : WriteOneToManyKeyValueRepo { + + @OptIn(InternalAPI::class) + override suspend fun add(toAdd: Map>) { + httpClient.post( + buildStandardUrl(baseUrl, addRoute) + ) { + body = toAdd + bodyType = keyToValuesMapTypeInfo + contentType(contentType) + }.throwOnUnsuccess { "Unable to add $toAdd" } + } + + @OptIn(InternalAPI::class) + override suspend fun remove(toRemove: Map>) { + httpClient.post( + buildStandardUrl(baseUrl, removeRoute) + ) { + body = toRemove + bodyType = keyToValuesMapTypeInfo + contentType(contentType) + }.throwOnUnsuccess { "Unable to remove $toRemove" } + } + + @OptIn(InternalAPI::class) + override suspend fun clear(k: Key) { + httpClient.post( + buildStandardUrl(baseUrl, clearRoute) + ) { + body = k + bodyType = keyTypeInfo + contentType(contentType) + }.throwOnUnsuccess { "Unable to clear data with key $k" } + } + + @OptIn(InternalAPI::class) + override suspend fun clearWithValue(v: Value) { + httpClient.post( + buildStandardUrl(baseUrl, clearWithValueRoute) + ) { + body = v + bodyType = valueTypeInfo + contentType(contentType) + }.throwOnUnsuccess { "Unable to clear data with value $v" } + } + + @OptIn(InternalAPI::class) + override suspend fun set(toSet: Map>) { + httpClient.post( + buildStandardUrl(baseUrl, setRoute) + ) { + body = toSet + bodyType = keyToValuesMapTypeInfo + contentType(contentType) + }.throwOnUnsuccess { "Unable to set data $toSet" } + } + + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType + ) = KtorWriteStandardKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, onNewValueRoute), + ), + httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, onValueRemovedRoute), + ), + httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, onDataClearedRoute), + ), + typeInfo(), + typeInfo(), + typeInfo>>() + ) + } +} diff --git a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/ContainsRoute.kt b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/ContainsRoute.kt new file mode 100644 index 00000000000..988d02c30ee --- /dev/null +++ b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/ContainsRoute.kt @@ -0,0 +1,3 @@ +package dev.inmo.micro_utils.repos.ktor.common + +const val containsRoute = "contains" diff --git a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/CountRoute.kt b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/CountRoute.kt new file mode 100644 index 00000000000..c1811cba14e --- /dev/null +++ b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/CountRoute.kt @@ -0,0 +1,3 @@ +package dev.inmo.micro_utils.repos.ktor.common + +const val countRoute = "count" diff --git a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/CountRouting.kt b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/CountRouting.kt new file mode 100644 index 00000000000..5803193e10f --- /dev/null +++ b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/CountRouting.kt @@ -0,0 +1,3 @@ +package dev.inmo.micro_utils.repos.ktor.common + +const val countRouting = "count" diff --git a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/crud/CrudReadRoutes.kt b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/crud/CrudReadRoutes.kt index 00348cc409d..dc44163c26e 100644 --- a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/crud/CrudReadRoutes.kt +++ b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/crud/CrudReadRoutes.kt @@ -3,4 +3,3 @@ package dev.inmo.micro_utils.repos.ktor.common.crud const val getByPaginationRouting = "getByPagination" const val getByIdRouting = "getById" const val containsRouting = "contains" -const val countRouting = "count" diff --git a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/key_value/KeyValueRoutes.kt b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/key_value/KeyValueRoutes.kt index 860429cf28c..61f14d6c4bf 100644 --- a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/key_value/KeyValueRoutes.kt +++ b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/key_value/KeyValueRoutes.kt @@ -3,11 +3,11 @@ package dev.inmo.micro_utils.repos.ktor.common.key_value const val getRoute = "get" const val valuesRoute = "values" const val keysRoute = "keys" -const val containsRoute = "contains" -const val countRoute = "count" +const val containsRoute = dev.inmo.micro_utils.repos.ktor.common.containsRoute +const val countRoute = dev.inmo.micro_utils.repos.ktor.common.countRoute const val onNewValueRoute = "onNewValue" const val onValueRemovedRoute = "onValueRemoved" const val setRoute = "set" const val unsetRoute = "unset" -const val unsetWithValuesRoute = "unsetWithValues" \ No newline at end of file +const val unsetWithValuesRoute = "unsetWithValues" 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 b10a45476ff..3bd568bb3a4 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 @@ -5,7 +5,7 @@ const val keysRoute = "keys" const val containsByKeyRoute = "containsByKey" const val containsByKeyValueRoute = "containsByKeyValue" const val countByKeyRoute = "countByKey" -const val countRoute = "count" +const val countRoute = dev.inmo.micro_utils.repos.ktor.common.countRoute const val onNewValueRoute = "onNewValue" const val onValueRemovedRoute = "onValueRemoved" diff --git a/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt b/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt index 9c17c341e0f..4ac4993dffc 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt @@ -21,7 +21,7 @@ class CRUDTests { @OptIn(ExperimentalCoroutinesApi::class) @Test fun testCRUDFunctions() { - runTest() { + runTest { val map = mutableMapOf() val repo = MapCRUDRepo( map, diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt index e9071246e86..c3ddf467325 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt @@ -22,8 +22,8 @@ import kotlin.test.* class KVTests { @OptIn(ExperimentalCoroutinesApi::class) @Test - fun testCRUDFunctions() { - runTest() { + fun testKVFunctions() { + runTest { val map = mutableMapOf() val repo = MapKeyValueRepo(map) val server = io.ktor.server.engine.embeddedServer( diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt new file mode 100644 index 00000000000..36363da5c60 --- /dev/null +++ b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt @@ -0,0 +1,141 @@ +import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination +import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging +import dev.inmo.micro_utils.repos.* +import dev.inmo.micro_utils.repos.ktor.client.key_value.KtorStandardKeyValueRepoClient +import dev.inmo.micro_utils.repos.ktor.client.one_to_many.KtorStandardKeyValuesRepoClient +import dev.inmo.micro_utils.repos.ktor.server.one_to_many.configureStandardKeyValuesRepoRoutes +import io.ktor.client.HttpClient +import io.ktor.client.plugins.logging.Logging +import io.ktor.http.ContentType +import io.ktor.serialization.kotlinx.KotlinxWebsocketSerializationConverter +import io.ktor.serialization.kotlinx.json.json +import io.ktor.server.application.install +import io.ktor.server.cio.CIO +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import io.ktor.server.routing.routing +import io.ktor.server.websocket.WebSockets +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.json.Json +import kotlin.test.* + +class KVsTests { + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun testKVsFunctions() { + runTest { + val map = mutableMapOf>() + val repo = MapOneToManyKeyValueRepo(map) + val server = io.ktor.server.engine.embeddedServer( + CIO, + 23456, + "127.0.0.1" + ) { + install(ContentNegotiation) { + json() + } + install(WebSockets) { + contentConverter = KotlinxWebsocketSerializationConverter(Json) + } + routing { + configureStandardKeyValuesRepoRoutes( + repo, + Int.serializer(), + ComplexData.serializer(), + Json {} + ) + } + }.start(false) + val client = HttpClient { + install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) { + json() + } + install(Logging) + install(io.ktor.client.plugins.websocket.WebSockets) { + contentConverter = KotlinxWebsocketSerializationConverter(Json) + } + } + val crudClient = KtorStandardKeyValuesRepoClient( + "http://127.0.0.1:23456", + client, + ContentType.Application.Json, + Int.serializer(), + ComplexData.serializer(), + Json + ) + + val dataInOneKey = ComplexData(1, title = "Example1") + val dataInMultipleKeys = ComplexData(2, title = "Example2") + val repeatCount = 3 + + val dataList = listOf( + 1 to listOf(dataInOneKey) + ) + (0 until repeatCount).map { + (it + 2) to listOf(dataInMultipleKeys) + } + + dataList.forEachIndexed { i, (id, data) -> + crudClient.set(id, data) + assertEquals(i + 1, map.size) + assertEquals(map.size.toLong(), crudClient.count()) + assertEquals(i + 1L, crudClient.count()) + dataList.take(i + 1).forEach { (id, data) -> + assertContentEquals(data, map[id]) + assertContentEquals(data, crudClient.getAll(id)) + assertContentEquals(map[id], crudClient.getAll(id)) + } + } + + dataList.forEach { (key, data) -> + assertTrue(crudClient.contains(key)) + assertContentEquals(data, crudClient.getAll(key)) + } + + assertEquals( + dataList.mapNotNull { if (it.second.contains(dataInMultipleKeys)) it.first else null }, + getAllWithNextPaging(firstPageWithOneElementPagination) { + crudClient.keys(dataInMultipleKeys, it) + } + ) + + assertEquals( + dataList.mapNotNull { if (it.second.contains(dataInOneKey)) it.first else null }, + getAllWithNextPaging(firstPageWithOneElementPagination) { + crudClient.keys(dataInOneKey, it) + } + ) + + assertEquals( + dataList.map { it.first }, + getAllWithNextPaging(firstPageWithOneElementPagination) { + crudClient.keys(it) + } + ) + + assertEquals( + dataList.map { it.first }, + getAllWithNextPaging(firstPageWithOneElementPagination) { + crudClient.keys(it) + } + ) + + assertEquals(dataList.size.toLong(), crudClient.count()) + + crudClient.remove(dataList.filter { it.second.contains(dataInMultipleKeys) }) + println(map) + assertEquals( + dataList.filter { it.second.contains(dataInOneKey) }.size.toLong(), + crudClient.count() + ) + + crudClient.remove(dataList.filter { it.second.contains(dataInOneKey) }) + assertEquals( + 0, + crudClient.count() + ) + + server.stop() + } + } +} diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt index 75053535d03..e5bdbaa5b84 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt @@ -6,6 +6,7 @@ import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ktor.common.countRouting import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.http.ContentType import io.ktor.server.application.call diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt index e8ee34dd409..53354208e16 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt @@ -4,6 +4,7 @@ import dev.inmo.micro_utils.ktor.common.decodeHex import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ktor.common.countRouting import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.idParameterName import io.ktor.http.ContentType diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt index 843b935fe18..a24b402cb3a 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt @@ -6,8 +6,12 @@ import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.* +import dev.inmo.micro_utils.repos.ktor.common.containsRoute +import dev.inmo.micro_utils.repos.ktor.common.countRoute import dev.inmo.micro_utils.repos.ktor.common.key_value.* -import dev.inmo.micro_utils.repos.ktor.common.valueParameterName +import dev.inmo.micro_utils.repos.ktor.common.key_value.keyParameterName +import dev.inmo.micro_utils.repos.ktor.common.key_value.reversedParameterName import io.ktor.http.ContentType import io.ktor.server.application.call import io.ktor.server.routing.Route diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt index 8ade9432458..a0f55ecc488 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt @@ -4,21 +4,20 @@ import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination -import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo -import dev.inmo.micro_utils.repos.ktor.common.idParameterName +import dev.inmo.micro_utils.repos.ktor.common.* +import dev.inmo.micro_utils.repos.ktor.common.containsRoute +import dev.inmo.micro_utils.repos.ktor.common.countRoute import dev.inmo.micro_utils.repos.ktor.common.key_value.* -import dev.inmo.micro_utils.repos.ktor.common.valueParameterName +import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import io.ktor.http.* import io.ktor.server.application.call import io.ktor.server.response.respond -import io.ktor.server.response.responseType import io.ktor.server.routing.Route import io.ktor.server.routing.get import io.ktor.util.InternalAPI import io.ktor.util.reflect.typeInfo import kotlinx.serialization.* -import kotlinx.serialization.builtins.serializer @OptIn(InternalAPI::class) inline fun Route.configureReadStandardKeyValueRepoRoutes ( @@ -26,6 +25,9 @@ inline fun Route.configureReadStandardKeyValueRepoR noinline idDeserializer: suspend (String) -> Key, noinline valueDeserializer: suspend (String) -> Value ) { + val paginationWithValuesTypeInfo = typeInfo>() + val paginationWithKeysTypeInfo = typeInfo>() + get(getRoute) { val key = idDeserializer( call.getQueryParameterOrSendError(idParameterName) ?: return@get @@ -36,13 +38,14 @@ inline fun Route.configureReadStandardKeyValueRepoR } ?: call.respond(HttpStatusCode.NoContent) } - val paginationWithValuesTypeInfo = typeInfo>() get(valuesRoute) { val pagination = call.request.queryParameters.extractPagination val reversed = call.getQueryParameter(reversedParameterName) ?.toBoolean() ?: false - call.response.responseType = paginationWithValuesTypeInfo - call.response.pipeline.execute(call, originalRepo.values(pagination, reversed) as Any) + call.respond( + originalRepo.values(pagination, reversed), + paginationWithValuesTypeInfo + ) } get(keysRoute) { @@ -53,7 +56,8 @@ inline fun Route.configureReadStandardKeyValueRepoR } call.respond( - value ?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed) + value ?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed), + paginationWithKeysTypeInfo ) } @@ -62,9 +66,7 @@ inline fun Route.configureReadStandardKeyValueRepoR call.getQueryParameterOrSendError(idParameterName) ?: return@get ) - call.respond( - originalRepo.contains(key) - ) + call.respond(originalRepo.contains(key)) } get(countRoute) { diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartKeyValuesRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartKeyValuesRepo.kt new file mode 100644 index 00000000000..625ddc25529 --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartKeyValuesRepo.kt @@ -0,0 +1,47 @@ +package dev.inmo.micro_utils.repos.ktor.server.one_to_many + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.repos.* +import io.ktor.http.* +import io.ktor.server.routing.Route +import kotlinx.serialization.* + +inline fun Route.configureStandardKeyValuesRepoRoutes ( + originalRepo: OneToManyKeyValueRepo, + noinline idDeserializer: suspend (String) -> Key, + noinline valueDeserializer: suspend (String) -> Value +) { + configureReadStandardKeyValuesRepoRoutes(originalRepo, idDeserializer, valueDeserializer) + configureWriteStandardKeyValuesRepoRoutes(originalRepo) +} + +inline fun Route.configureStandardKeyValuesRepoRoutes( + originalRepo: OneToManyKeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: StringFormat +) = configureStandardKeyValuesRepoRoutes( + originalRepo, + { + serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) + }, + { + serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent()) + } +) + +inline fun Route.configureStandardKeyValuesRepoRoutes( + originalRepo: OneToManyKeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: BinaryFormat +) = configureStandardKeyValuesRepoRoutes( + originalRepo, + { + serialFormat.decodeHex(idsSerializer, it) + }, + { + serialFormat.decodeHex(valueSerializer, it) + } +) + diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartReadKeyValuesRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartReadKeyValuesRepo.kt new file mode 100644 index 00000000000..c8f5c0b7b7c --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartReadKeyValuesRepo.kt @@ -0,0 +1,107 @@ +package dev.inmo.micro_utils.repos.ktor.server.one_to_many + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.ktor.server.* +import dev.inmo.micro_utils.pagination.PaginationResult +import dev.inmo.micro_utils.pagination.extractPagination +import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.* +import dev.inmo.micro_utils.repos.ktor.common.containsRoute +import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* +import io.ktor.http.* +import io.ktor.server.application.call +import io.ktor.server.response.respond +import io.ktor.server.routing.Route +import io.ktor.server.routing.get +import io.ktor.util.InternalAPI +import io.ktor.util.reflect.typeInfo +import kotlinx.serialization.* + +@OptIn(InternalAPI::class) +inline fun Route.configureReadStandardKeyValuesRepoRoutes ( + originalRepo: ReadOneToManyKeyValueRepo, + noinline idDeserializer: suspend (String) -> Key, + noinline valueDeserializer: suspend (String) -> Value +) { + val paginationWithValuesTypeInfo = typeInfo>() + val paginationWithKeysTypeInfo = typeInfo>() + + get(getRoute) { + val key = idDeserializer( + call.getQueryParameterOrSendError(keyParameterName) ?: return@get + ) + val pagination = call.request.queryParameters.extractPagination + val reversed = call.getQueryParameter(reversedParameterName) ?.toBoolean() ?: false + + call.respond( + originalRepo.get(key, pagination, reversed), + paginationWithValuesTypeInfo + ) + } + + get(keysRoute) { + val pagination = call.request.queryParameters.extractPagination + val reversed = call.getQueryParameterOrSendError(reversedParameterName) ?.toBoolean() ?: false + val value = call.getQueryParameter(valueParameterName) ?.let { + valueDeserializer(it) + } + + call.respond( + value ?.let { originalRepo.keys(value, pagination, reversed) } ?: originalRepo.keys(pagination, reversed), + paginationWithKeysTypeInfo + ) + } + + get(containsRoute) { + val key = idDeserializer( + call.getQueryParameterOrSendError(keyParameterName) ?: return@get + ) + val value = call.getQueryParameter(valueParameterName) ?.let { + valueDeserializer(it) + } + + call.respond( + value ?.let { originalRepo.contains(key, value) } ?: originalRepo.contains(key) + ) + } + + get(dev.inmo.micro_utils.repos.ktor.common.countRoute) { + val id = call.getQueryParameter(keyParameterName) ?.let { + idDeserializer(it) + } + call.respond( + id ?.let { originalRepo.count(it) } ?: originalRepo.count() + ) + } +} + +inline fun Route.configureReadStandardKeyValuesRepoRoutes( + originalRepo: ReadOneToManyKeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: StringFormat +) = configureReadStandardKeyValuesRepoRoutes( + originalRepo, + { + serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) + }, + { + serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent()) + } +) + +inline fun Route.configureReadStandardKeyValuesRepoRoutes( + originalRepo: ReadOneToManyKeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: BinaryFormat +) = configureReadStandardKeyValuesRepoRoutes( + originalRepo, + { + serialFormat.decodeHex(idsSerializer, it) + }, + { + serialFormat.decodeHex(valueSerializer, it) + } +) + diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartWriteKeyValuesRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartWriteKeyValuesRepo.kt new file mode 100644 index 00000000000..a33e9062a72 --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartWriteKeyValuesRepo.kt @@ -0,0 +1,58 @@ +package dev.inmo.micro_utils.repos.ktor.server.one_to_many + +import dev.inmo.micro_utils.ktor.server.* +import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* +import io.ktor.http.HttpStatusCode +import io.ktor.server.application.call +import io.ktor.server.request.receive +import io.ktor.server.response.respond +import io.ktor.server.routing.Route +import io.ktor.server.routing.post +import io.ktor.util.reflect.typeInfo + +inline fun Route.configureWriteStandardKeyValuesRepoRoutes ( + originalRepo: WriteOneToManyKeyValueRepo +) { + includeWebsocketHandling( + onNewValueRoute, + originalRepo.onNewValue + ) + + includeWebsocketHandling( + onValueRemovedRoute, + originalRepo.onValueRemoved + ) + + includeWebsocketHandling( + onDataClearedRoute, + originalRepo.onDataCleared + ) + + val mapType = typeInfo>>() + + post(addRoute) { + originalRepo.add(call.receive(mapType)) + call.respond(HttpStatusCode.OK) + } + + post(setRoute) { + originalRepo.set(call.receive(mapType)) + call.respond(HttpStatusCode.OK) + } + + post(removeRoute) { + originalRepo.remove(call.receive(mapType)) + call.respond(HttpStatusCode.OK) + } + + post(clearRoute) { + originalRepo.clear(call.receive()) + call.respond(HttpStatusCode.OK) + } + + post(clearWithValueRoute) { + originalRepo.clearWithValue(call.receive()) + call.respond(HttpStatusCode.OK) + } +} From 18593c530be6730993767c6866bd3381f2b96473 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Jun 2022 14:42:16 +0600 Subject: [PATCH 06/14] renames in key value repos --- .../repos/cache/KeyValueCacheRepo.kt | 2 - .../micro_utils/repos/StandartKeyValueRepo.kt | 32 ++++++------ .../repos/mappers/KeyValueMappers.kt | 52 +++++++++++-------- .../KeyValuePaginationExtensions.kt | 4 +- .../KeyValueBasedVersionsRepoProxy.kt | 4 +- ...ardKeyValueRepo.kt => FileKeyValueRepo.kt} | 25 ++++++--- .../repos/keyvalue/KeyValueStore.kt | 6 +-- ...redPreferencesStandardVersionsRepoProxy.kt | 12 ++--- .../exposed/keyvalue/ExposedKeyValueRepo.kt | 6 +-- .../keyvalue/ExposedReadKeyValueRepo.kt | 4 +- .../inmo/micro_utils/repos/MapKeyValueRepo.kt | 12 ++--- ...epoClient.kt => KtorKeyValueRepoClient.kt} | 24 ++++----- ...lient.kt => KtorReadKeyValueRepoClient.kt} | 18 +++---- .../key_value/KtorReadStandardKeyValueRepo.kt | 5 +- .../key_value/KtorStandartKeyValueRepo.kt | 9 ++-- ...ient.kt => KtorWriteKeyValueRepoClient.kt} | 8 +-- .../KtorWriteStandardKeyValueRepo.kt | 5 +- .../ktor/common/src/jvmTest/kotlin/KVTests.kt | 8 +-- .../common/src/jvmTest/kotlin/KVsTests.kt | 1 - .../key_value/KtorStandartKeyValueRepo.kt | 12 ++--- .../key_value/KtorStandartReadKeyValueRepo.kt | 6 +-- .../KtorStandartWriteKeyValueRepo.kt | 10 ++-- .../key_value/NewKtorStandartKeyValueRepo.kt | 33 ++++-------- .../NewKtorStandartReadKeyValueRepo.kt | 18 +++---- .../NewKtorStandartWriteKeyValueRepo.kt | 6 +-- 25 files changed, 161 insertions(+), 161 deletions(-) rename repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/{FileStandardKeyValueRepo.kt => FileKeyValueRepo.kt} (87%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/{KtorStandardKeyValueRepoClient.kt => KtorKeyValueRepoClient.kt} (76%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/{KtorReadStandardKeyValueRepoClient.kt => KtorReadKeyValueRepoClient.kt} (88%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/{KtorWriteStandardKeyValueRepoClient.kt => KtorWriteKeyValueRepoClient.kt} (92%) diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt index ad1e24ab696..79ae1392d1b 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt @@ -4,8 +4,6 @@ import dev.inmo.micro_utils.repos.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.* -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.withLock open class ReadKeyValueCacheRepo( protected val parentRepo: ReadKeyValueRepo, diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt index b224d8caebf..8ffe9094719 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt @@ -4,7 +4,7 @@ import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.doAllWithCurrentPaging import kotlinx.coroutines.flow.Flow -interface ReadStandardKeyValueRepo : Repo { +interface ReadKeyValueRepo : Repo { suspend fun get(k: Key): Value? suspend fun values(pagination: Pagination, reversed: Boolean = false): PaginationResult suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult @@ -12,9 +12,9 @@ interface ReadStandardKeyValueRepo : Repo { suspend fun contains(key: Key): Boolean suspend fun count(): Long } -typealias ReadKeyValueRepo = ReadStandardKeyValueRepo +typealias ReadStandardKeyValueRepo = ReadKeyValueRepo -interface WriteStandardKeyValueRepo : Repo { +interface WriteKeyValueRepo : Repo { val onNewValue: Flow> val onValueRemoved: Flow @@ -22,25 +22,25 @@ interface WriteStandardKeyValueRepo : Repo { suspend fun unset(toUnset: List) suspend fun unsetWithValues(toUnset: List) } -typealias WriteKeyValueRepo = WriteStandardKeyValueRepo +typealias WriteStandardKeyValueRepo = WriteKeyValueRepo -suspend inline fun WriteStandardKeyValueRepo.set( +suspend inline fun WriteKeyValueRepo.set( vararg toSet: Pair ) = set(toSet.toMap()) -suspend inline fun WriteStandardKeyValueRepo.set( +suspend inline fun WriteKeyValueRepo.set( k: Key, v: Value ) = set(k to v) -suspend inline fun WriteStandardKeyValueRepo.unset( +suspend inline fun WriteKeyValueRepo.unset( vararg k: Key ) = unset(k.toList()) -suspend inline fun WriteStandardKeyValueRepo.unsetWithValues( +suspend inline fun WriteKeyValueRepo.unsetWithValues( vararg v: Value ) = unsetWithValues(v.toList()) -interface StandardKeyValueRepo : ReadStandardKeyValueRepo, WriteStandardKeyValueRepo { +interface KeyValueRepo : ReadKeyValueRepo, WriteKeyValueRepo { override suspend fun unsetWithValues(toUnset: List) = toUnset.forEach { v -> doAllWithCurrentPaging { keys(v, it).also { @@ -49,11 +49,11 @@ interface StandardKeyValueRepo : ReadStandardKeyValueRepo = StandardKeyValueRepo +typealias StandardKeyValueRepo = KeyValueRepo -class DelegateBasedStandardKeyValueRepo( - readDelegate: ReadStandardKeyValueRepo, - writeDelegate: WriteStandardKeyValueRepo -) : StandardKeyValueRepo, - ReadStandardKeyValueRepo by readDelegate, - WriteStandardKeyValueRepo by writeDelegate +class DelegateBasedKeyValueRepo( + readDelegate: ReadKeyValueRepo, + writeDelegate: WriteKeyValueRepo +) : KeyValueRepo, + ReadKeyValueRepo by readDelegate, + WriteKeyValueRepo by writeDelegate 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 3644852d713..23955920fe8 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 @@ -6,10 +6,12 @@ import dev.inmo.micro_utils.repos.* import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map -open class MapperReadStandardKeyValueRepo( - private val to: ReadStandardKeyValueRepo, +@Deprecated("Renamed", ReplaceWith("MapperReadKeyValueRepo", "dev.inmo.micro_utils.repos.mappers.MapperReadKeyValueRepo")) +typealias MapperReadStandardKeyValueRepo = MapperReadKeyValueRepo +open class MapperReadKeyValueRepo( + private val to: ReadKeyValueRepo, mapper: MapperRepo -) : ReadStandardKeyValueRepo, MapperRepo by mapper { +) : ReadKeyValueRepo, MapperRepo by mapper { override suspend fun get(k: FromKey): FromValue? = to.get( k.toOutKey() ) ?.toInnerValue() @@ -69,24 +71,26 @@ open class MapperReadStandardKeyValueRepo( } @Suppress("NOTHING_TO_INLINE") -inline fun ReadStandardKeyValueRepo.withMapper( +inline fun ReadKeyValueRepo.withMapper( mapper: MapperRepo -): ReadStandardKeyValueRepo = MapperReadStandardKeyValueRepo(this, mapper) +): ReadKeyValueRepo = MapperReadKeyValueRepo(this, mapper) @Suppress("NOTHING_TO_INLINE") -inline fun ReadStandardKeyValueRepo.withMapper( +inline fun ReadKeyValueRepo.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 }, -): ReadStandardKeyValueRepo = withMapper( +): ReadKeyValueRepo = withMapper( mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom) ) -open class MapperWriteStandardKeyValueRepo( - private val to: WriteStandardKeyValueRepo, +@Deprecated("Renamed", ReplaceWith("MapperWriteKeyValueRepo", "dev.inmo.micro_utils.repos.mappers.MapperWriteKeyValueRepo")) +typealias MapperWriteStandardKeyValueRepo = MapperWriteKeyValueRepo +open class MapperWriteKeyValueRepo( + private val to: WriteKeyValueRepo, mapper: MapperRepo -) : WriteStandardKeyValueRepo, MapperRepo by mapper { +) : WriteKeyValueRepo, MapperRepo by mapper { override val onNewValue: Flow> = to.onNewValue.map { (k, v) -> k.toInnerKey() to v.toInnerValue() } @@ -112,40 +116,42 @@ open class MapperWriteStandardKeyValueRepo( } @Suppress("NOTHING_TO_INLINE") -inline fun WriteStandardKeyValueRepo.withMapper( +inline fun WriteKeyValueRepo.withMapper( mapper: MapperRepo -): WriteStandardKeyValueRepo = MapperWriteStandardKeyValueRepo(this, mapper) +): WriteKeyValueRepo = MapperWriteKeyValueRepo(this, mapper) @Suppress("NOTHING_TO_INLINE") -inline fun WriteStandardKeyValueRepo.withMapper( +inline fun WriteKeyValueRepo.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 }, -): WriteStandardKeyValueRepo = withMapper( +): WriteKeyValueRepo = withMapper( mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom) ) +@Deprecated("Renamed", ReplaceWith("MapperKeyValueRepo", "dev.inmo.micro_utils.repos.mappers.MapperKeyValueRepo")) +typealias MapperStandardKeyValueRepo = MapperKeyValueRepo @Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE") -open class MapperStandardKeyValueRepo( - private val to: StandardKeyValueRepo, +open class MapperKeyValueRepo( + private val to: KeyValueRepo, private val mapper: MapperRepo -) : StandardKeyValueRepo, +) : KeyValueRepo, MapperRepo by mapper, - ReadStandardKeyValueRepo by MapperReadStandardKeyValueRepo(to, mapper), - WriteStandardKeyValueRepo by MapperWriteStandardKeyValueRepo(to, mapper) + ReadKeyValueRepo by MapperReadKeyValueRepo(to, mapper), + WriteKeyValueRepo by MapperWriteKeyValueRepo(to, mapper) @Suppress("NOTHING_TO_INLINE") -inline fun StandardKeyValueRepo.withMapper( +inline fun KeyValueRepo.withMapper( mapper: MapperRepo -): StandardKeyValueRepo = MapperStandardKeyValueRepo(this, mapper) +): KeyValueRepo = MapperKeyValueRepo(this, mapper) @Suppress("NOTHING_TO_INLINE") -inline fun StandardKeyValueRepo.withMapper( +inline fun KeyValueRepo.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 }, -): StandardKeyValueRepo = withMapper( +): KeyValueRepo = withMapper( mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom) ) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt index abf462ff4e6..14fe3068416 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt @@ -2,9 +2,9 @@ package dev.inmo.micro_utils.repos.pagination import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging -import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValueRepo -suspend inline fun > REPO.getAll( +suspend inline fun > REPO.getAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult ): List> = getAllWithNextPaging { diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/versions/KeyValueBasedVersionsRepoProxy.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/versions/KeyValueBasedVersionsRepoProxy.kt index 5d7e3435670..1e467685b6e 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/versions/KeyValueBasedVersionsRepoProxy.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/versions/KeyValueBasedVersionsRepoProxy.kt @@ -1,10 +1,10 @@ package dev.inmo.micro_utils.repos.versions -import dev.inmo.micro_utils.repos.StandardKeyValueRepo +import dev.inmo.micro_utils.repos.KeyValueRepo import dev.inmo.micro_utils.repos.set class KeyValueBasedVersionsRepoProxy( - private val keyValueStore: StandardKeyValueRepo, + private val keyValueStore: KeyValueRepo, override val database: T ) : StandardVersionsRepoProxy { override suspend fun getTableVersion(tableName: String): Int? = keyValueStore.get(tableName) diff --git a/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileStandardKeyValueRepo.kt b/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt similarity index 87% rename from repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileStandardKeyValueRepo.kt rename to repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt index 5e93cfc10e6..2b9d9163ff7 100644 --- a/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileStandardKeyValueRepo.kt +++ b/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt @@ -13,9 +13,12 @@ import java.nio.file.StandardWatchEventKinds.* private inline val String.isAbsolute get() = startsWith(File.separator) -class FileReadStandardKeyValueRepo( +@Deprecated("Renamed", ReplaceWith("FileReadKeyValueRepo", "dev.inmo.micro_utils.repos.FileReadKeyValueRepo")) +typealias FileReadStandardKeyValueRepo = FileReadKeyValueRepo + +class FileReadKeyValueRepo( private val folder: File -) : ReadStandardKeyValueRepo { +) : ReadKeyValueRepo { init { folder.mkdirs() } @@ -79,14 +82,17 @@ class FileReadStandardKeyValueRepo( override suspend fun count(): Long = folder.list() ?.size ?.toLong() ?: 0L } +@Deprecated("Renamed", ReplaceWith("FileWriteKeyValueRepo", "dev.inmo.micro_utils.repos.FileWriteKeyValueRepo")) +typealias FileWriteStandardKeyValueRepo = FileWriteKeyValueRepo + /** * Files watching will not correctly works on Android with version of API lower than API 26 */ @Warning("Files watching will not correctly works on Android Platform with version of API lower than API 26") -class FileWriteStandardKeyValueRepo( +class FileWriteKeyValueRepo( private val folder: File, filesChangedProcessingScope: CoroutineScope? = null -) : WriteStandardKeyValueRepo { +) : WriteKeyValueRepo { private val _onNewValue = MutableSharedFlow>() override val onNewValue: Flow> = _onNewValue.asSharedFlow() private val _onValueRemoved = MutableSharedFlow() @@ -174,12 +180,15 @@ class FileWriteStandardKeyValueRepo( } } +@Deprecated("Renamed", ReplaceWith("FileKeyValueRepo", "dev.inmo.micro_utils.repos.FileKeyValueRepo")) +typealias FileStandardKeyValueRepo = FileKeyValueRepo + @Warning("Files watching will not correctly works on Android Platform with version of API lower than API 26") @Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE") -class FileStandardKeyValueRepo( +class FileKeyValueRepo( folder: File, filesChangedProcessingScope: CoroutineScope? = null -) : StandardKeyValueRepo, - WriteStandardKeyValueRepo by FileWriteStandardKeyValueRepo(folder, filesChangedProcessingScope), - ReadStandardKeyValueRepo by FileReadStandardKeyValueRepo(folder) { +) : KeyValueRepo, + WriteKeyValueRepo by FileWriteKeyValueRepo(folder, filesChangedProcessingScope), + ReadKeyValueRepo by FileReadKeyValueRepo(folder) { } diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt index 440490f51ea..dbddcbf52cb 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt @@ -7,7 +7,7 @@ import dev.inmo.micro_utils.pagination.Pagination import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.reverse -import dev.inmo.micro_utils.repos.StandardKeyValueRepo +import dev.inmo.micro_utils.repos.KeyValueRepo import kotlinx.coroutines.flow.* private val cache = HashMap>() @@ -15,7 +15,7 @@ private val cache = HashMap>() fun Context.keyValueStore( name: String = "default", cacheValues: Boolean = false -): StandardKeyValueRepo { +): KeyValueRepo { @Suppress("UNCHECKED_CAST") return cache.getOrPut(name) { KeyValueStore(this, name, cacheValues) @@ -26,7 +26,7 @@ class KeyValueStore internal constructor ( c: Context, preferencesName: String, useCache: Boolean = false -) : SharedPreferences.OnSharedPreferenceChangeListener, StandardKeyValueRepo { +) : SharedPreferences.OnSharedPreferenceChangeListener, KeyValueRepo { private val sharedPreferences = c.getSharedPreferences(preferencesName, Context.MODE_PRIVATE) private val cachedData = if (useCache) { diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/versions/AndroidSharedPreferencesStandardVersionsRepoProxy.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/versions/AndroidSharedPreferencesStandardVersionsRepoProxy.kt index 1e9f1e1c1a4..6dfd471311b 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/versions/AndroidSharedPreferencesStandardVersionsRepoProxy.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/versions/AndroidSharedPreferencesStandardVersionsRepoProxy.kt @@ -4,13 +4,11 @@ package dev.inmo.micro_utils.repos.versions import android.content.Context import android.database.sqlite.SQLiteOpenHelper -import androidx.core.content.contentValuesOf import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.keyvalue.keyValueStore -import kotlinx.coroutines.runBlocking /** - * Will create [VersionsRepo] based on [T], but versions will be stored in [StandardKeyValueRepo] + * Will create [VersionsRepo] based on [T], but versions will be stored in [KeyValueRepo] * * @receiver Will be used to create [KeyValueBasedVersionsRepoProxy] via [keyValueStore] and pass it to [StandardVersionsRepo] * @@ -26,9 +24,9 @@ inline fun Context.versionsKeyValueRepo( ) ) /** - * Will create [VersionsRepo] based on [SQLiteOpenHelper], but versions will be stored in [StandardKeyValueRepo] + * Will create [VersionsRepo] based on [SQLiteOpenHelper], but versions will be stored in [KeyValueRepo] * - * @receiver Will be used to create [StandardKeyValueRepo] via [keyValueStore] and pass it to [StandardVersionsRepo] + * @receiver Will be used to create [KeyValueRepo] via [keyValueStore] and pass it to [StandardVersionsRepo] * * @see [keyValueStore] */ @@ -37,9 +35,9 @@ inline fun Context.versionsKeyValueRepoForSQL( ) = versionsKeyValueRepo(database) /** - * Will create [VersionsRepo] based on [SQLiteOpenHelper], but versions will be stored in [StandardKeyValueRepo] + * Will create [VersionsRepo] based on [SQLiteOpenHelper], but versions will be stored in [KeyValueRepo] * - * @param context Will be used to create [StandardKeyValueRepo] via [keyValueStore] and pass it to [StandardVersionsRepo] + * @param context Will be used to create [KeyValueRepo] via [keyValueStore] and pass it to [StandardVersionsRepo] * * @see [keyValueStore] */ diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt index ee50149845a..8d3588d842f 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt @@ -1,9 +1,7 @@ package dev.inmo.micro_utils.repos.exposed.keyvalue -import dev.inmo.micro_utils.repos.StandardKeyValueRepo +import dev.inmo.micro_utils.repos.KeyValueRepo import dev.inmo.micro_utils.repos.exposed.ColumnAllocator -import dev.inmo.micro_utils.repos.exposed.initTable -import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.* import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction @@ -13,7 +11,7 @@ open class ExposedKeyValueRepo( keyColumnAllocator: ColumnAllocator, valueColumnAllocator: ColumnAllocator, tableName: String? = null -) : StandardKeyValueRepo, ExposedReadKeyValueRepo( +) : KeyValueRepo, ExposedReadKeyValueRepo( database, keyColumnAllocator, valueColumnAllocator, diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt index ecf87dc312e..7fa979d4a26 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt @@ -1,7 +1,7 @@ package dev.inmo.micro_utils.repos.exposed.keyvalue import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.exposed.* import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction @@ -11,7 +11,7 @@ open class ExposedReadKeyValueRepo( keyColumnAllocator: ColumnAllocator, valueColumnAllocator: ColumnAllocator, tableName: String? = null -) : ReadStandardKeyValueRepo, ExposedRepo, Table(tableName ?: "") { +) : ReadKeyValueRepo, ExposedRepo, Table(tableName ?: "") { val keyColumn: Column = keyColumnAllocator() val valueColumn: Column = valueColumnAllocator() override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn) diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt index 7e87197ac8d..17c21d33379 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow class ReadMapKeyValueRepo( protected val map: Map = emptyMap() -) : ReadStandardKeyValueRepo { +) : ReadKeyValueRepo { override suspend fun get(k: Key): Value? = map[k] override suspend fun values( @@ -58,7 +58,7 @@ class ReadMapKeyValueRepo( class WriteMapKeyValueRepo( private val map: MutableMap = mutableMapOf() -) : WriteStandardKeyValueRepo { +) : WriteKeyValueRepo { private val _onNewValue: MutableSharedFlow> = MutableSharedFlow() override val onNewValue: Flow> get() = _onNewValue @@ -89,8 +89,8 @@ class WriteMapKeyValueRepo( class MapKeyValueRepo( private val map: MutableMap = mutableMapOf() -) : StandardKeyValueRepo, - ReadStandardKeyValueRepo by ReadMapKeyValueRepo(map), - WriteStandardKeyValueRepo by WriteMapKeyValueRepo(map) +) : KeyValueRepo, + ReadKeyValueRepo by ReadMapKeyValueRepo(map), + WriteKeyValueRepo by WriteMapKeyValueRepo(map) -fun MutableMap.asKeyValueRepo(): StandardKeyValueRepo = MapKeyValueRepo(this) +fun MutableMap.asKeyValueRepo(): KeyValueRepo = MapKeyValueRepo(this) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandardKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorKeyValueRepoClient.kt similarity index 76% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandardKeyValueRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorKeyValueRepoClient.kt index 92f60bf7053..dc7d6f0b0b7 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandardKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorKeyValueRepoClient.kt @@ -7,10 +7,10 @@ import io.ktor.http.ContentType import io.ktor.http.encodeURLQueryComponent import kotlinx.serialization.* -class KtorStandardKeyValueRepoClient ( - readDelegate: ReadStandardKeyValueRepo, - writeDelegate: WriteStandardKeyValueRepo -) : StandardKeyValueRepo by DelegateBasedStandardKeyValueRepo( +class KtorKeyValueRepoClient ( + readDelegate: ReadKeyValueRepo, + writeDelegate: WriteKeyValueRepo +) : KeyValueRepo by DelegateBasedKeyValueRepo( readDelegate, writeDelegate ) { @@ -21,11 +21,11 @@ class KtorStandardKeyValueRepoClient ( contentType: ContentType, noinline idSerializer: suspend (Key) -> String, noinline valueSerializer: suspend (Value) -> String - ) = KtorStandardKeyValueRepoClient( - KtorReadStandardKeyValueRepoClient( + ) = KtorKeyValueRepoClient( + KtorReadKeyValueRepoClient( baseUrl, httpClient, contentType, idSerializer, valueSerializer ), - KtorWriteStandardKeyValueRepoClient( + KtorWriteKeyValueRepoClient( baseUrl, httpClient, contentType @@ -38,7 +38,7 @@ class KtorStandardKeyValueRepoClient ( contentType: ContentType, noinline idSerializer: suspend (Key) -> String, noinline valueSerializer: suspend (Value) -> String - ) = KtorStandardKeyValueRepoClient( + ) = KtorKeyValueRepoClient( buildStandardUrl(baseUrl, subpart), httpClient, contentType, @@ -48,14 +48,14 @@ class KtorStandardKeyValueRepoClient ( } } -inline fun KtorStandardKeyValueRepoClient( +inline fun KtorKeyValueRepoClient( baseUrl: String, httpClient: HttpClient, contentType: ContentType, idSerializer: SerializationStrategy, valueSerializer: SerializationStrategy, serialFormat: StringFormat, -) = KtorStandardKeyValueRepoClient( +) = KtorKeyValueRepoClient( baseUrl, httpClient, contentType, @@ -66,14 +66,14 @@ inline fun KtorStandardKeyValueRepoClient( serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() } -inline fun KtorStandardKeyValueRepoClient( +inline fun KtorKeyValueRepoClient( baseUrl: String, httpClient: HttpClient, contentType: ContentType, idSerializer: SerializationStrategy, valueSerializer: SerializationStrategy, serialFormat: BinaryFormat, -) = KtorStandardKeyValueRepoClient( +) = KtorKeyValueRepoClient( baseUrl, httpClient, contentType, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt similarity index 88% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt index 792d6b0d0d9..ded3486007d 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt @@ -2,7 +2,7 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.* import dev.inmo.micro_utils.repos.ktor.common.containsRoute import dev.inmo.micro_utils.repos.ktor.common.key_value.* @@ -15,7 +15,7 @@ import io.ktor.util.reflect.TypeInfo import io.ktor.util.reflect.typeInfo import kotlinx.serialization.* -class KtorReadStandardKeyValueRepoClient( +class KtorReadKeyValueRepoClient( private val baseUrl: String, private val httpClient: HttpClient, private val contentType: ContentType, @@ -24,7 +24,7 @@ class KtorReadStandardKeyValueRepoClient( private val paginationResultIdsTypeInfo: TypeInfo, private val idSerializer: suspend (Key) -> String, private val valueSerializer: suspend (Value) -> String -) : ReadStandardKeyValueRepo { +) : ReadKeyValueRepo { override suspend fun get(k: Key): Value? = httpClient.get( buildStandardUrl( baseUrl, @@ -89,13 +89,13 @@ class KtorReadStandardKeyValueRepoClient( }.body() } -inline fun KtorReadStandardKeyValueRepoClient( +inline fun KtorReadKeyValueRepoClient( baseUrl: String, httpClient: HttpClient, contentType: ContentType, noinline idSerializer: suspend (Key) -> String, noinline valueSerializer: suspend (Value) -> String -) = KtorReadStandardKeyValueRepoClient( +) = KtorReadKeyValueRepoClient( baseUrl, httpClient, contentType, @@ -106,14 +106,14 @@ inline fun KtorReadStandardKeyValueRepoClient( valueSerializer ) -inline fun KtorReadStandardKeyValueRepoClient( +inline fun KtorReadKeyValueRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, valueSerializer: KSerializer, serialFormat: StringFormat, contentType: ContentType, -) = KtorReadStandardKeyValueRepoClient( +) = KtorReadKeyValueRepoClient( baseUrl, httpClient, contentType, @@ -124,14 +124,14 @@ inline fun KtorReadStandardKeyValueRepoClient( serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() } -inline fun KtorReadStandardKeyValueRepoClient( +inline fun KtorReadKeyValueRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, valuesSerializer: KSerializer, serialFormat: BinaryFormat, contentType: ContentType, -) = KtorReadStandardKeyValueRepoClient( +) = KtorReadKeyValueRepoClient( baseUrl, httpClient, contentType, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt index 2359c8d2c24..cf9500652e1 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.* import dev.inmo.micro_utils.repos.ktor.common.containsRoute import dev.inmo.micro_utils.repos.ktor.common.countRoute @@ -14,13 +14,14 @@ import io.ktor.client.HttpClient import kotlinx.serialization.* import kotlinx.serialization.builtins.serializer +@Deprecated("Replaced with KtorReadKeyValueRepoClient") class KtorReadStandardKeyValueRepo ( private val baseUrl: String, private val unifiedRequester: UnifiedRequester, private val keySerializer: KSerializer, private val valueSerializer: KSerializer, private val valueNullableSerializer: KSerializer -) : ReadStandardKeyValueRepo { +) : ReadKeyValueRepo { constructor( baseUrl: String, client: HttpClient, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt index 0dc7952970b..2ce6b5d6b1e 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt @@ -7,6 +7,7 @@ import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient import kotlinx.serialization.* +@Deprecated("Replaced with KtorKeyValueRepoClient") @Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE") class KtorStandartKeyValueRepo ( baseUrl: String, @@ -15,15 +16,15 @@ class KtorStandartKeyValueRepo ( keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer -) : StandardKeyValueRepo, - ReadStandardKeyValueRepo by KtorReadStandardKeyValueRepo( +) : KeyValueRepo, + ReadKeyValueRepo by KtorReadStandardKeyValueRepo( "$baseUrl/$baseSubpart", unifiedRequester, keySerializer, valueSerializer, valueNullableSerializer ), - WriteStandardKeyValueRepo by KtorWriteStandardKeyValueRepo( + WriteKeyValueRepo by KtorWriteStandardKeyValueRepo( "$baseUrl/$baseSubpart", unifiedRequester, keySerializer, @@ -38,4 +39,4 @@ class KtorStandartKeyValueRepo ( valueNullableSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this(baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer, valueNullableSerializer) -} \ No newline at end of file +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteKeyValueRepoClient.kt similarity index 92% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteKeyValueRepoClient.kt index d9abaf672ce..398e94de811 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteKeyValueRepoClient.kt @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow import dev.inmo.micro_utils.ktor.client.throwOnUnsuccess import dev.inmo.micro_utils.ktor.common.* -import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo +import dev.inmo.micro_utils.repos.WriteKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.client.HttpClient import io.ktor.client.request.post @@ -13,7 +13,7 @@ import io.ktor.util.reflect.TypeInfo import io.ktor.util.reflect.typeInfo import kotlinx.coroutines.flow.Flow -class KtorWriteStandardKeyValueRepoClient( +class KtorWriteKeyValueRepoClient( private val baseUrl: String, private val httpClient: HttpClient, private val contentType: ContentType, @@ -22,7 +22,7 @@ class KtorWriteStandardKeyValueRepoClient( private val idsListTypeInfo: TypeInfo, private val objectsListTypeInfo: TypeInfo, private val idsToObjectsMapTypeInfo: TypeInfo -) : WriteStandardKeyValueRepo { +) : WriteKeyValueRepo { @OptIn(InternalAPI::class) override suspend fun unsetWithValues(toUnset: List) { httpClient.post( @@ -61,7 +61,7 @@ class KtorWriteStandardKeyValueRepoClient( baseUrl: String, httpClient: HttpClient, contentType: ContentType - ) = KtorWriteStandardKeyValueRepoClient( + ) = KtorWriteKeyValueRepoClient( baseUrl, httpClient, contentType, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt index a7c1e898efb..4b51836ab57 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt @@ -2,19 +2,20 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.* -import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo +import dev.inmo.micro_utils.repos.WriteKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.client.HttpClient import kotlinx.coroutines.flow.Flow import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* +@Deprecated("Replaced with KtorWriteKeyValueRepoClient") class KtorWriteStandardKeyValueRepo ( private var baseUrl: String, private var unifiedRequester: UnifiedRequester, private var keySerializer: KSerializer, private var valueSerializer: KSerializer, -) : WriteStandardKeyValueRepo { +) : WriteKeyValueRepo { private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer) private val keysListSerializer = ListSerializer(keySerializer) private val valuesListSerializer = ListSerializer(valueSerializer) diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt index c3ddf467325..8ffb2185f3e 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt @@ -1,8 +1,8 @@ import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.* -import dev.inmo.micro_utils.repos.ktor.client.key_value.KtorStandardKeyValueRepoClient -import dev.inmo.micro_utils.repos.ktor.server.key_value.configureStandardKeyValueRepoRoutes +import dev.inmo.micro_utils.repos.ktor.client.key_value.KtorKeyValueRepoClient +import dev.inmo.micro_utils.repos.ktor.server.key_value.configureKeyValueRepoRoutes import io.ktor.client.HttpClient import io.ktor.client.plugins.logging.Logging import io.ktor.http.ContentType @@ -38,7 +38,7 @@ class KVTests { contentConverter = KotlinxWebsocketSerializationConverter(Json) } routing { - configureStandardKeyValueRepoRoutes( + configureKeyValueRepoRoutes( repo, Int.serializer(), ComplexData.serializer(), @@ -55,7 +55,7 @@ class KVTests { contentConverter = KotlinxWebsocketSerializationConverter(Json) } } - val crudClient = KtorStandardKeyValueRepoClient( + val crudClient = KtorKeyValueRepoClient( "http://127.0.0.1:23456", client, ContentType.Application.Json, diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt index 36363da5c60..20a8427e3e2 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt @@ -1,7 +1,6 @@ import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.* -import dev.inmo.micro_utils.repos.ktor.client.key_value.KtorStandardKeyValueRepoClient import dev.inmo.micro_utils.repos.ktor.client.one_to_many.KtorStandardKeyValuesRepoClient import dev.inmo.micro_utils.repos.ktor.server.one_to_many.configureStandardKeyValuesRepoRoutes import io.ktor.client.HttpClient diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt index 0ed4f5d1a68..daec736b46c 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt @@ -4,15 +4,15 @@ import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.UnifiedRouter import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType -import dev.inmo.micro_utils.repos.StandardKeyValueRepo +import dev.inmo.micro_utils.repos.KeyValueRepo import io.ktor.http.ContentType import io.ktor.server.routing.Route import io.ktor.server.routing.route import kotlinx.serialization.KSerializer -fun Route.configureStandardKeyValueRepoRoutes( +fun Route.configureKeyValueRepoRoutes( baseSubpart: String, - originalRepo: StandardKeyValueRepo, + originalRepo: KeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer, @@ -26,7 +26,7 @@ fun Route.configureStandardKeyValueRepoRoutes( valueNullableSerializer, unifiedRouter ) - configureWriteStandardKeyValueRepoRoutes( + configureWriteValueRepoRoutes( originalRepo, keySerializer, valueSerializer, @@ -37,10 +37,10 @@ fun Route.configureStandardKeyValueRepoRoutes( fun Route.configureStandartKeyValueRepoRoutes( baseSubpart: String, - originalRepo: StandardKeyValueRepo, + originalRepo: KeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, serialFormatContentType: ContentType = standardKtorSerialFormatContentType -) = configureStandardKeyValueRepoRoutes(baseSubpart, originalRepo, keySerializer, valueSerializer, valueNullableSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) +) = configureKeyValueRepoRoutes(baseSubpart, originalRepo, keySerializer, valueSerializer, valueNullableSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt index a24b402cb3a..59b54d9b00a 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt @@ -5,7 +5,7 @@ import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination -import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.* import dev.inmo.micro_utils.repos.ktor.common.containsRoute import dev.inmo.micro_utils.repos.ktor.common.countRoute @@ -20,7 +20,7 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer fun Route.configureReadStandartKeyValueRepoRoutes ( - originalRepo: ReadStandardKeyValueRepo, + originalRepo: ReadKeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer, @@ -96,7 +96,7 @@ fun Route.configureReadStandartKeyValueRepoRoutes ( } inline fun Route.configureReadStandartKeyValueRepoRoutes ( - originalRepo: ReadStandardKeyValueRepo, + originalRepo: ReadKeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer, diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt index 85179e81771..accc61d9f6c 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.server.key_value import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* -import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo +import dev.inmo.micro_utils.repos.WriteKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.http.ContentType import io.ktor.server.routing.Route @@ -11,8 +11,8 @@ import io.ktor.server.routing.post import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* -fun Route.configureWriteStandardKeyValueRepoRoutes ( - originalRepo: WriteStandardKeyValueRepo, +fun Route.configureWriteValueRepoRoutes ( + originalRepo: WriteKeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, unifiedRouter: UnifiedRouter @@ -62,9 +62,9 @@ fun Route.configureWriteStandardKeyValueRepoRoutes ( } fun Route.configureWriteStandartKeyValueRepoRoutes ( - originalRepo: WriteStandardKeyValueRepo, + originalRepo: WriteKeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, serialFormatContentType: ContentType = standardKtorSerialFormatContentType -) = configureWriteStandardKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) +) = configureWriteValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartKeyValueRepo.kt index 0748fd96cbf..b62db0700ff 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartKeyValueRepo.kt @@ -1,37 +1,26 @@ package dev.inmo.micro_utils.repos.ktor.server.key_value import dev.inmo.micro_utils.ktor.common.* -import dev.inmo.micro_utils.ktor.server.* -import dev.inmo.micro_utils.pagination.PaginationResult -import dev.inmo.micro_utils.pagination.extractPagination -import dev.inmo.micro_utils.repos.StandardCRUDRepo -import dev.inmo.micro_utils.repos.StandardKeyValueRepo -import dev.inmo.micro_utils.repos.ktor.common.idParameterName -import dev.inmo.micro_utils.repos.ktor.common.key_value.* -import dev.inmo.micro_utils.repos.ktor.common.valueParameterName +import dev.inmo.micro_utils.repos.KeyValueRepo import io.ktor.http.* -import io.ktor.server.application.call -import io.ktor.server.response.respond import io.ktor.server.routing.Route -import io.ktor.server.routing.get import kotlinx.serialization.* -import kotlinx.serialization.builtins.serializer -inline fun Route.configureStandardKeyValueRepoRoutes ( - originalRepo: StandardKeyValueRepo, +inline fun Route.configureKeyValueRepoRoutes ( + originalRepo: KeyValueRepo, noinline idDeserializer: suspend (String) -> Key, noinline valueDeserializer: suspend (String) -> Value ) { - configureReadStandardKeyValueRepoRoutes(originalRepo, idDeserializer, valueDeserializer) - configureWriteStandardKeyValueRepoRoutes(originalRepo) + configureReadKeyValueRepoRoutes(originalRepo, idDeserializer, valueDeserializer) + configureWriteValueRepoRoutes(originalRepo) } -inline fun Route.configureStandardKeyValueRepoRoutes( - originalRepo: StandardKeyValueRepo, +inline fun Route.configureKeyValueRepoRoutes( + originalRepo: KeyValueRepo, idsSerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: StringFormat -) = configureStandardKeyValueRepoRoutes( +) = configureKeyValueRepoRoutes( originalRepo, { serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) @@ -41,12 +30,12 @@ inline fun Route.configureStandardKeyVa } ) -inline fun Route.configureStandardKeyValueRepoRoutes( - originalRepo: StandardKeyValueRepo, +inline fun Route.configureKeyValueRepoRoutes( + originalRepo: KeyValueRepo, idsSerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: BinaryFormat -) = configureStandardKeyValueRepoRoutes( +) = configureKeyValueRepoRoutes( originalRepo, { serialFormat.decodeHex(idsSerializer, it) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt index a0f55ecc488..67eec8bda59 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt @@ -4,7 +4,7 @@ import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination -import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.* import dev.inmo.micro_utils.repos.ktor.common.containsRoute import dev.inmo.micro_utils.repos.ktor.common.countRoute @@ -20,8 +20,8 @@ import io.ktor.util.reflect.typeInfo import kotlinx.serialization.* @OptIn(InternalAPI::class) -inline fun Route.configureReadStandardKeyValueRepoRoutes ( - originalRepo: ReadStandardKeyValueRepo, +inline fun Route.configureReadKeyValueRepoRoutes ( + originalRepo: ReadKeyValueRepo, noinline idDeserializer: suspend (String) -> Key, noinline valueDeserializer: suspend (String) -> Value ) { @@ -74,12 +74,12 @@ inline fun Route.configureReadStandardKeyValueRepoR } } -inline fun Route.configureReadStandardKeyValueRepoRoutes( - originalRepo: ReadStandardKeyValueRepo, +inline fun Route.configureReadKeyValueRepoRoutes( + originalRepo: ReadKeyValueRepo, idsSerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: StringFormat -) = configureReadStandardKeyValueRepoRoutes( +) = configureReadKeyValueRepoRoutes( originalRepo, { serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) @@ -89,12 +89,12 @@ inline fun Route.configureReadStandardKeyValueRepoR } ) -inline fun Route.configureReadStandardKeyValueRepoRoutes( - originalRepo: ReadStandardKeyValueRepo, +inline fun Route.configureReadKeyValueRepoRoutes( + originalRepo: ReadKeyValueRepo, idsSerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: BinaryFormat -) = configureReadStandardKeyValueRepoRoutes( +) = configureReadKeyValueRepoRoutes( originalRepo, { serialFormat.decodeHex(idsSerializer, it) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt index bac70871d05..8ad82d6695d 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt @@ -1,7 +1,7 @@ package dev.inmo.micro_utils.repos.ktor.server.key_value import dev.inmo.micro_utils.ktor.server.* -import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo +import dev.inmo.micro_utils.repos.WriteKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.http.HttpStatusCode import io.ktor.server.application.call @@ -11,8 +11,8 @@ import io.ktor.server.routing.Route import io.ktor.server.routing.post import io.ktor.util.reflect.typeInfo -inline fun Route.configureWriteStandardKeyValueRepoRoutes ( - originalRepo: WriteStandardKeyValueRepo +inline fun Route.configureWriteValueRepoRoutes ( + originalRepo: WriteKeyValueRepo ) { includeWebsocketHandling( onNewValueRoute, From 8d955c4b9d267377209c04600f023dffe4d14306 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Jun 2022 15:00:23 +0600 Subject: [PATCH 07/14] renames in kevalues repos --- .../repos/cache/KeyValuesCacheRepo.kt | 2 - ...ToManyKeyValueRepo.kt => KeyValuesRepo.kt} | 49 +++++++++-------- .../repos/mappers/OneToManyKeyValueMappers.kt | 52 +++++++++++-------- .../OneToManyPaginationExtensions.kt | 4 +- .../repos/onetomany/OneToManyAndroidRepo.kt | 3 +- ...eyValueRepo.kt => ExposedKeyValuesRepo.kt} | 8 +-- ...lueRepo.kt => ExposedReadKeyValuesRepo.kt} | 9 ++-- ...anyKeyValueRepo.kt => MapKeyValuesRepo.kt} | 27 ++++++---- ...poClient.kt => KtorKeyValuesRepoClient.kt} | 24 ++++----- .../one_to_many/KtorOneToManyKeyValueRepo.kt | 9 ++-- ...ient.kt => KtorReadKeyValuesRepoClient.kt} | 19 ++++--- .../KtorReadOneToManyKeyValueRepo.kt | 7 +-- ...ent.kt => KtorWriteKeyValuesRepoClient.kt} | 8 +-- .../KtorWriteOneToManyKeyValueRepo.kt | 5 +- .../common/src/jvmTest/kotlin/KVsTests.kt | 10 ++-- .../ConfigureOneToManyKeyValueRepoRoutes.kt | 6 +-- ...onfigureOneToManyReadKeyValueRepoRoutes.kt | 6 +-- ...nfigureOneToManyWriteKeyValueRepoRoutes.kt | 6 +-- ...luesRepo.kt => KtorKeyValuesRepoRoutes.kt} | 20 +++---- ...Repo.kt => KtorReadKeyValuesRepoRoutes.kt} | 18 +++---- ...epo.kt => KtorWriteKeyValuesRepoRoutes.kt} | 6 +-- 21 files changed, 155 insertions(+), 143 deletions(-) rename repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/{OneToManyKeyValueRepo.kt => KeyValuesRepo.kt} (62%) rename repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/{ExposedOneToManyKeyValueRepo.kt => ExposedKeyValuesRepo.kt} (89%) rename repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/{ExposedReadOneToManyKeyValueRepo.kt => ExposedReadKeyValuesRepo.kt} (85%) rename repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/{MapOneToManyKeyValueRepo.kt => MapKeyValuesRepo.kt} (74%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/{KtorStandardKeyValuesRepoClient.kt => KtorKeyValuesRepoClient.kt} (76%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/{KtorReadStandardKeyValuesRepoClient.kt => KtorReadKeyValuesRepoClient.kt} (87%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/{KtorWriteStandardKeyValuesRepoClient.kt => KtorWriteKeyValuesRepoClient.kt} (93%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/{NewKtorStandartKeyValuesRepo.kt => KtorKeyValuesRepoRoutes.kt} (70%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/{NewKtorStandartReadKeyValuesRepo.kt => KtorReadKeyValuesRepoRoutes.kt} (84%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/{NewKtorStandartWriteKeyValuesRepo.kt => KtorWriteKeyValuesRepoRoutes.kt} (90%) diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt index ff2b36fbf01..1948279461c 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValuesCacheRepo.kt @@ -8,8 +8,6 @@ import dev.inmo.micro_utils.repos.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.* -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.withLock open class ReadKeyValuesCacheRepo( protected val parentRepo: ReadKeyValuesRepo, 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/KeyValuesRepo.kt similarity index 62% rename from repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt rename to repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/KeyValuesRepo.kt index ce9836c59cd..1754abb70b8 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/KeyValuesRepo.kt @@ -1,11 +1,10 @@ package dev.inmo.micro_utils.repos import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.pagination.utils.doForAllWithCurrentPaging import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import kotlinx.coroutines.flow.Flow -interface ReadOneToManyKeyValueRepo : Repo { +interface ReadKeyValuesRepo : Repo { suspend fun get(k: Key, pagination: Pagination, reversed: Boolean = false): PaginationResult suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean = false): PaginationResult @@ -36,9 +35,9 @@ interface ReadOneToManyKeyValueRepo : Repo { } } } -typealias ReadKeyValuesRepo = ReadOneToManyKeyValueRepo +typealias ReadOneToManyKeyValueRepo = ReadKeyValuesRepo -interface WriteOneToManyKeyValueRepo : Repo { +interface WriteKeyValuesRepo : Repo { val onNewValue: Flow> val onValueRemoved: Flow> val onDataCleared: Flow @@ -55,41 +54,41 @@ interface WriteOneToManyKeyValueRepo : Repo { add(toSet) } } -typealias WriteKeyValuesRepo = WriteOneToManyKeyValueRepo +typealias WriteOneToManyKeyValueRepo = WriteKeyValuesRepo -suspend inline fun > REPO.add( +suspend inline fun > REPO.add( keysAndValues: List>> ) = add(keysAndValues.toMap()) -suspend inline fun > REPO.add( +suspend inline fun > REPO.add( vararg keysAndValues: Pair> ) = add(keysAndValues.toMap()) -suspend inline fun WriteOneToManyKeyValueRepo.add( +suspend inline fun WriteKeyValuesRepo.add( k: Key, v: List ) = add(mapOf(k to v)) -suspend inline fun WriteOneToManyKeyValueRepo.add( +suspend inline fun WriteKeyValuesRepo.add( k: Key, vararg v: Value ) = add(k, v.toList()) -suspend inline fun > REPO.set( +suspend inline fun > REPO.set( keysAndValues: List>> ) = set(keysAndValues.toMap()) -suspend inline fun > REPO.set( +suspend inline fun > REPO.set( vararg keysAndValues: Pair> ) = set(keysAndValues.toMap()) -suspend inline fun WriteOneToManyKeyValueRepo.set( +suspend inline fun WriteKeyValuesRepo.set( k: Key, v: List ) = set(mapOf(k to v)) -suspend inline fun WriteOneToManyKeyValueRepo.set( +suspend inline fun WriteKeyValuesRepo.set( k: Key, vararg v: Value ) = set(k, v.toList()) -interface OneToManyKeyValueRepo : ReadOneToManyKeyValueRepo, WriteOneToManyKeyValueRepo { +interface KeyValuesRepo : ReadKeyValuesRepo, WriteKeyValuesRepo { override suspend fun clearWithValue(v: Value) { doWithPagination { val keysResult = keys(v, it) @@ -102,29 +101,29 @@ interface OneToManyKeyValueRepo : ReadOneToManyKeyValueRepo = OneToManyKeyValueRepo +typealias OneToManyKeyValueRepo = KeyValuesRepo -class DelegateBasedOneToManyKeyValueRepo( - readDelegate: ReadOneToManyKeyValueRepo, - writeDelegate: WriteOneToManyKeyValueRepo -) : OneToManyKeyValueRepo, - ReadOneToManyKeyValueRepo by readDelegate, - WriteOneToManyKeyValueRepo by writeDelegate +class DelegateBasedKeyValuesRepo( + readDelegate: ReadKeyValuesRepo, + writeDelegate: WriteKeyValuesRepo +) : KeyValuesRepo, + ReadKeyValuesRepo by readDelegate, + WriteKeyValuesRepo by writeDelegate -suspend inline fun WriteOneToManyKeyValueRepo.remove( +suspend inline fun WriteKeyValuesRepo.remove( keysAndValues: List>> ) = remove(keysAndValues.toMap()) -suspend inline fun WriteOneToManyKeyValueRepo.remove( +suspend inline fun WriteKeyValuesRepo.remove( vararg keysAndValues: Pair> ) = remove(keysAndValues.toMap()) -suspend inline fun WriteOneToManyKeyValueRepo.remove( +suspend inline fun WriteKeyValuesRepo.remove( k: Key, v: List ) = remove(mapOf(k to v)) -suspend inline fun WriteOneToManyKeyValueRepo.remove( +suspend inline fun WriteKeyValuesRepo.remove( k: Key, vararg v: Value ) = remove(k, v.toList()) 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 74751f79518..5e5d5b44d5f 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 @@ -6,10 +6,12 @@ import dev.inmo.micro_utils.repos.* import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map -open class MapperReadOneToManyKeyValueRepo( - private val to: ReadOneToManyKeyValueRepo, +@Deprecated("Renamed", ReplaceWith("MapperReadKeyValuesRepo", "dev.inmo.micro_utils.repos.mappers.MapperReadKeyValuesRepo")) +typealias MapperReadOneToManyKeyValueRepo = MapperReadKeyValuesRepo +open class MapperReadKeyValuesRepo( + private val to: ReadKeyValuesRepo, mapper: MapperRepo -) : ReadOneToManyKeyValueRepo, MapperRepo by mapper { +) : ReadKeyValuesRepo, MapperRepo by mapper { override suspend fun get( k: FromKey, pagination: Pagination, @@ -67,24 +69,26 @@ open class MapperReadOneToManyKeyValueRepo( } @Suppress("NOTHING_TO_INLINE") -inline fun ReadOneToManyKeyValueRepo.withMapper( +inline fun ReadKeyValuesRepo.withMapper( mapper: MapperRepo -): ReadOneToManyKeyValueRepo = MapperReadOneToManyKeyValueRepo(this, mapper) +): ReadKeyValuesRepo = MapperReadKeyValuesRepo(this, mapper) @Suppress("NOTHING_TO_INLINE") -inline fun ReadOneToManyKeyValueRepo.withMapper( +inline fun ReadKeyValuesRepo.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 }, -): ReadOneToManyKeyValueRepo = withMapper( +): ReadKeyValuesRepo = withMapper( mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom) ) -open class MapperWriteOneToManyKeyValueRepo( - private val to: WriteOneToManyKeyValueRepo, +@Deprecated("Renamed", ReplaceWith("MapperWriteKeyValuesRepo", "dev.inmo.micro_utils.repos.mappers.MapperWriteKeyValuesRepo")) +typealias MapperWriteOneToManyKeyValueRepo = MapperWriteKeyValuesRepo +open class MapperWriteKeyValuesRepo( + private val to: WriteKeyValuesRepo, mapper: MapperRepo -) : WriteOneToManyKeyValueRepo, MapperRepo by mapper { +) : WriteKeyValuesRepo, MapperRepo by mapper { override val onNewValue: Flow> = to.onNewValue.map { (k, v) -> k.toInnerKey() to v.toInnerValue() } @@ -118,40 +122,42 @@ open class MapperWriteOneToManyKeyValueRepo( } @Suppress("NOTHING_TO_INLINE") -inline fun WriteOneToManyKeyValueRepo.withMapper( +inline fun WriteKeyValuesRepo.withMapper( mapper: MapperRepo -): WriteOneToManyKeyValueRepo = MapperWriteOneToManyKeyValueRepo(this, mapper) +): WriteKeyValuesRepo = MapperWriteKeyValuesRepo(this, mapper) @Suppress("NOTHING_TO_INLINE") -inline fun WriteOneToManyKeyValueRepo.withMapper( +inline fun WriteKeyValuesRepo.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 }, -): WriteOneToManyKeyValueRepo = withMapper( +): WriteKeyValuesRepo = withMapper( mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom) ) +@Deprecated("Renamed", ReplaceWith("MapperKeyValuesRepo", "dev.inmo.micro_utils.repos.mappers.MapperKeyValuesRepo")) +typealias MapperOneToManyKeyValueRepo = MapperKeyValuesRepo @Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE") -open class MapperOneToManyKeyValueRepo( - private val to: OneToManyKeyValueRepo, +open class MapperKeyValuesRepo( + private val to: KeyValuesRepo, mapper: MapperRepo -) : OneToManyKeyValueRepo, +) : KeyValuesRepo, MapperRepo by mapper, - ReadOneToManyKeyValueRepo by MapperReadOneToManyKeyValueRepo(to, mapper), - WriteOneToManyKeyValueRepo by MapperWriteOneToManyKeyValueRepo(to, mapper) + ReadKeyValuesRepo by MapperReadKeyValuesRepo(to, mapper), + WriteKeyValuesRepo by MapperWriteKeyValuesRepo(to, mapper) @Suppress("NOTHING_TO_INLINE") -inline fun OneToManyKeyValueRepo.withMapper( +inline fun KeyValuesRepo.withMapper( mapper: MapperRepo -): OneToManyKeyValueRepo = MapperOneToManyKeyValueRepo(this, mapper) +): KeyValuesRepo = MapperKeyValuesRepo(this, mapper) @Suppress("NOTHING_TO_INLINE") -inline fun OneToManyKeyValueRepo.withMapper( +inline fun KeyValuesRepo.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 }, -): OneToManyKeyValueRepo = withMapper( +): KeyValuesRepo = withMapper( mapper(keyFromToTo, valueFromToTo, keyToToFrom, valueToToFrom) ) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/OneToManyPaginationExtensions.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/OneToManyPaginationExtensions.kt index 5a2749a0f1e..5e825390297 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/OneToManyPaginationExtensions.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/OneToManyPaginationExtensions.kt @@ -2,9 +2,9 @@ package dev.inmo.micro_utils.repos.pagination import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging -import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValuesRepo -suspend inline fun > REPO.getAll( +suspend inline fun > REPO.getAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult ): List>> = getAllWithNextPaging { 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 31abcac4e61..11a29614c39 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 @@ -9,7 +9,6 @@ import dev.inmo.micro_utils.repos.* import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.asSharedFlow -import kotlinx.coroutines.runBlocking import kotlinx.serialization.KSerializer import kotlinx.serialization.json.Json @@ -26,7 +25,7 @@ class OneToManyAndroidRepo( private val keyFromString: String.() -> Key, private val valueFromString: String.() -> Value, private val helper: SQLiteOpenHelper -) : OneToManyKeyValueRepo { +) : KeyValuesRepo { private val _onNewValue: MutableSharedFlow> = MutableSharedFlow() override val onNewValue: Flow> = _onNewValue.asSharedFlow() private val _onValueRemoved: MutableSharedFlow> = MutableSharedFlow() diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedOneToManyKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedKeyValuesRepo.kt similarity index 89% rename from repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedOneToManyKeyValueRepo.kt rename to repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedKeyValuesRepo.kt index 0c839ea7834..6bb8bd67a63 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedOneToManyKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedKeyValuesRepo.kt @@ -1,18 +1,18 @@ package dev.inmo.micro_utils.repos.exposed.onetomany -import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.KeyValuesRepo import dev.inmo.micro_utils.repos.exposed.ColumnAllocator import kotlinx.coroutines.flow.* import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction -typealias ExposedKeyValuesRepo = ExposedOneToManyKeyValueRepo -open class ExposedOneToManyKeyValueRepo( +typealias ExposedOneToManyKeyValueRepo1 = ExposedKeyValuesRepo +open class ExposedKeyValuesRepo( database: Database, keyColumnAllocator: ColumnAllocator, valueColumnAllocator: ColumnAllocator, tableName: String? = null -) : OneToManyKeyValueRepo, ExposedReadOneToManyKeyValueRepo( +) : KeyValuesRepo, ExposedReadKeyValuesRepo( database, keyColumnAllocator, valueColumnAllocator, diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedReadOneToManyKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedReadKeyValuesRepo.kt similarity index 85% rename from repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedReadOneToManyKeyValueRepo.kt rename to repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedReadKeyValuesRepo.kt index 7c8fa8a2fd7..d6903b764b3 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedReadOneToManyKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedReadKeyValuesRepo.kt @@ -1,20 +1,19 @@ package dev.inmo.micro_utils.repos.exposed.onetomany import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValuesRepo import dev.inmo.micro_utils.repos.exposed.* -import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedReadKeyValueRepo import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction -typealias ExposedReadKeyValuesRepo = ExposedReadOneToManyKeyValueRepo +typealias ExposedReadOneToManyKeyValueRepo = ExposedReadKeyValuesRepo -open class ExposedReadOneToManyKeyValueRepo( +open class ExposedReadKeyValuesRepo( override val database: Database, keyColumnAllocator: ColumnAllocator, valueColumnAllocator: ColumnAllocator, tableName: String? = null -) : ReadOneToManyKeyValueRepo, ExposedRepo, Table(tableName ?: "") { +) : ReadKeyValuesRepo, ExposedRepo, Table(tableName ?: "") { val keyColumn: Column = keyColumnAllocator() val valueColumn: Column = valueColumnAllocator() diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValuesRepo.kt similarity index 74% rename from repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt rename to repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValuesRepo.kt index be4dbfd7ab0..4ab0d3ee2a4 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValuesRepo.kt @@ -5,9 +5,11 @@ import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.reverse import kotlinx.coroutines.flow.* -class MapReadOneToManyKeyValueRepo( +@Deprecated("Renamed", ReplaceWith("MapReadKeyValuesRepo", "dev.inmo.micro_utils.repos.MapReadKeyValuesRepo")) +typealias MapReadOneToManyKeyValueRepo = MapReadKeyValuesRepo +class MapReadKeyValuesRepo( private val map: Map> = emptyMap() -) : ReadOneToManyKeyValueRepo { +) : ReadKeyValuesRepo { override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult { val list = map[k] ?: return emptyPaginationResult() @@ -53,9 +55,11 @@ class MapReadOneToManyKeyValueRepo( override suspend fun count(): Long = map.size.toLong() } -class MapWriteOneToManyKeyValueRepo( +@Deprecated("Renamed", ReplaceWith("MapWriteKeyValuesRepo", "dev.inmo.micro_utils.repos.MapWriteKeyValuesRepo")) +typealias MapWriteOneToManyKeyValueRepo = MapWriteKeyValuesRepo +class MapWriteKeyValuesRepo( private val map: MutableMap> = mutableMapOf() -) : WriteOneToManyKeyValueRepo { +) : WriteKeyValuesRepo { private val _onNewValue: MutableSharedFlow> = MutableSharedFlow() override val onNewValue: Flow> = _onNewValue.asSharedFlow() private val _onValueRemoved: MutableSharedFlow> = MutableSharedFlow() @@ -98,12 +102,17 @@ class MapWriteOneToManyKeyValueRepo( } } -class MapOneToManyKeyValueRepo( +@Deprecated("Renamed", ReplaceWith("MapKeyValuesRepo", "dev.inmo.micro_utils.repos.MapKeyValuesRepo")) +typealias MapOneToManyKeyValueRepo1 = MapKeyValuesRepo +class MapKeyValuesRepo( private val map: MutableMap> = mutableMapOf() -) : OneToManyKeyValueRepo, - ReadOneToManyKeyValueRepo by MapReadOneToManyKeyValueRepo(map), - WriteOneToManyKeyValueRepo by MapWriteOneToManyKeyValueRepo(map) +) : KeyValuesRepo, + ReadKeyValuesRepo by MapReadKeyValuesRepo(map), + WriteKeyValuesRepo by MapWriteKeyValuesRepo(map) -fun MutableMap>.asOneToManyKeyValueRepo(): OneToManyKeyValueRepo = MapOneToManyKeyValueRepo( +fun MutableMap>.asKeyValuesRepo(): KeyValuesRepo = MapKeyValuesRepo( map { (k, v) -> k to v.toMutableList() }.toMap().toMutableMap() ) + +@Deprecated("Renamed", ReplaceWith("asKeyValuesRepo", "dev.inmo.micro_utils.repos.asKeyValuesRepo")) +fun MutableMap>.asOneToManyKeyValueRepo(): KeyValuesRepo = asKeyValuesRepo() diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorStandardKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorKeyValuesRepoClient.kt similarity index 76% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorStandardKeyValuesRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorKeyValuesRepoClient.kt index 6f1e2f43087..a7863a79560 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorStandardKeyValuesRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorKeyValuesRepoClient.kt @@ -7,10 +7,10 @@ import io.ktor.http.ContentType import io.ktor.http.encodeURLQueryComponent import kotlinx.serialization.* -class KtorStandardKeyValuesRepoClient ( - readDelegate: ReadOneToManyKeyValueRepo, - writeDelegate: WriteOneToManyKeyValueRepo -) : OneToManyKeyValueRepo by DelegateBasedOneToManyKeyValueRepo( +class KtorKeyValuesRepoClient ( + readDelegate: ReadKeyValuesRepo, + writeDelegate: WriteKeyValuesRepo +) : KeyValuesRepo by DelegateBasedKeyValuesRepo( readDelegate, writeDelegate ) { @@ -21,15 +21,15 @@ class KtorStandardKeyValuesRepoClient ( contentType: ContentType, noinline keySerializer: suspend (Key) -> String, noinline valueSerializer: suspend (Value) -> String - ) = KtorStandardKeyValuesRepoClient( - KtorReadStandardKeyValuesRepoClient( + ) = KtorKeyValuesRepoClient( + KtorReadKeyValuesRepoClient( baseUrl, httpClient, contentType, keySerializer, valueSerializer ), - KtorWriteStandardKeyValuesRepoClient( + KtorWriteKeyValuesRepoClient( baseUrl, httpClient, contentType @@ -42,7 +42,7 @@ class KtorStandardKeyValuesRepoClient ( contentType: ContentType, noinline keySerializer: suspend (Key) -> String, noinline valueSerializer: suspend (Value) -> String - ) = KtorStandardKeyValuesRepoClient( + ) = KtorKeyValuesRepoClient( buildStandardUrl(baseUrl, subpart), httpClient, contentType, @@ -52,14 +52,14 @@ class KtorStandardKeyValuesRepoClient ( } } -inline fun KtorStandardKeyValuesRepoClient( +inline fun KtorKeyValuesRepoClient( baseUrl: String, httpClient: HttpClient, contentType: ContentType, keySerializer: SerializationStrategy, valueSerializer: SerializationStrategy, serialFormat: StringFormat, -) = KtorStandardKeyValuesRepoClient( +) = KtorKeyValuesRepoClient( baseUrl, httpClient, contentType, @@ -70,14 +70,14 @@ inline fun KtorStandardKeyValuesRepoCli serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() } -inline fun KtorStandardKeyValuesRepoClient( +inline fun KtorKeyValuesRepoClient( baseUrl: String, httpClient: HttpClient, contentType: ContentType, keySerializer: SerializationStrategy, valueSerializer: SerializationStrategy, serialFormat: BinaryFormat, -) = KtorStandardKeyValuesRepoClient( +) = KtorKeyValuesRepoClient( baseUrl, httpClient, contentType, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt index d50c23bb7e8..5bf8155d36d 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt @@ -7,20 +7,21 @@ import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer +@Deprecated("Should be replaced with KtorKeyValuesRepoClient") class KtorOneToManyKeyValueRepo( baseUrl: String, baseSubpart: String, unifiedRequester: UnifiedRequester, keySerializer: KSerializer, valueSerializer: KSerializer, -) : OneToManyKeyValueRepo, - ReadOneToManyKeyValueRepo by KtorReadOneToManyKeyValueRepo ( +) : KeyValuesRepo, + ReadKeyValuesRepo by KtorReadOneToManyKeyValueRepo ( "$baseUrl/$baseSubpart", unifiedRequester, keySerializer, valueSerializer, ), - WriteOneToManyKeyValueRepo by KtorWriteOneToManyKeyValueRepo ( + WriteKeyValuesRepo by KtorWriteOneToManyKeyValueRepo ( "$baseUrl/$baseSubpart", unifiedRequester, keySerializer, @@ -34,4 +35,4 @@ class KtorOneToManyKeyValueRepo( valueSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat ) : this (baseUrl, baseSubpart, UnifiedRequester(client, serialFormat), keySerializer, valueSerializer) -} \ No newline at end of file +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadStandardKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadKeyValuesRepoClient.kt similarity index 87% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadStandardKeyValuesRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadKeyValuesRepoClient.kt index f05478c1ea7..3a32621a230 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadStandardKeyValuesRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadKeyValuesRepoClient.kt @@ -2,9 +2,8 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValuesRepo import dev.inmo.micro_utils.repos.ktor.common.* -import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import io.ktor.client.HttpClient @@ -15,7 +14,7 @@ import io.ktor.util.reflect.TypeInfo import io.ktor.util.reflect.typeInfo import kotlinx.serialization.* -class KtorReadStandardKeyValuesRepoClient( +class KtorReadKeyValuesRepoClient( private val baseUrl: String, private val httpClient: HttpClient, private val contentType: ContentType, @@ -23,7 +22,7 @@ class KtorReadStandardKeyValuesRepoClient( private val paginationResultKeysTypeInfo: TypeInfo, private val keySerializer: suspend (Key) -> String, private val valueSerializer: suspend (Value) -> String -) : ReadOneToManyKeyValueRepo { +) : ReadKeyValuesRepo { override suspend fun get( k: Key, pagination: Pagination, @@ -106,13 +105,13 @@ class KtorReadStandardKeyValuesRepoClient( }.body() } -inline fun KtorReadStandardKeyValuesRepoClient( +inline fun KtorReadKeyValuesRepoClient( baseUrl: String, httpClient: HttpClient, contentType: ContentType, noinline keySerializer: suspend (Key) -> String, noinline valueSerializer: suspend (Value) -> String -) = KtorReadStandardKeyValuesRepoClient( +) = KtorReadKeyValuesRepoClient( baseUrl, httpClient, contentType, @@ -122,14 +121,14 @@ inline fun KtorReadStandardKeyValuesRepoClient( valueSerializer ) -inline fun KtorReadStandardKeyValuesRepoClient( +inline fun KtorReadKeyValuesRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, valueSerializer: KSerializer, serialFormat: StringFormat, contentType: ContentType, -) = KtorReadStandardKeyValuesRepoClient( +) = KtorReadKeyValuesRepoClient( baseUrl, httpClient, contentType, @@ -140,14 +139,14 @@ inline fun KtorReadStandardKeyValuesRepoClient( serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() } -inline fun KtorReadStandardKeyValuesRepoClient( +inline fun KtorReadKeyValuesRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, valuesSerializer: KSerializer, serialFormat: BinaryFormat, contentType: ContentType, -) = KtorReadStandardKeyValuesRepoClient( +) = KtorReadKeyValuesRepoClient( baseUrl, httpClient, contentType, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt index 5eb413eda8c..6a91a21a208 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValuesRepo import dev.inmo.micro_utils.repos.ktor.common.keyParameterName import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName @@ -12,12 +12,13 @@ import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer +@Deprecated("Should be replaced with KtorReadKeyValuesRepoClient") class KtorReadOneToManyKeyValueRepo ( private val baseUrl: String, private val unifiedRequester: UnifiedRequester, private val keySerializer: KSerializer, private val valueSerializer: KSerializer -) : ReadOneToManyKeyValueRepo { +) : ReadKeyValuesRepo { private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer) private val paginationKeyResultSerializer = PaginationResult.serializer(keySerializer) @@ -104,4 +105,4 @@ class KtorReadOneToManyKeyValueRepo ( Long.serializer() ) -} \ No newline at end of file +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteStandardKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteKeyValuesRepoClient.kt similarity index 93% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteStandardKeyValuesRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteKeyValuesRepoClient.kt index 1f77a79f57f..8b087ae452f 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteStandardKeyValuesRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteKeyValuesRepoClient.kt @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow import dev.inmo.micro_utils.ktor.client.throwOnUnsuccess import dev.inmo.micro_utils.ktor.common.* -import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.WriteKeyValuesRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.client.HttpClient import io.ktor.client.request.post @@ -13,7 +13,7 @@ import io.ktor.util.reflect.TypeInfo import io.ktor.util.reflect.typeInfo import kotlinx.coroutines.flow.Flow -class KtorWriteStandardKeyValuesRepoClient( +class KtorWriteKeyValuesRepoClient( private val baseUrl: String, private val httpClient: HttpClient, private val contentType: ContentType, @@ -23,7 +23,7 @@ class KtorWriteStandardKeyValuesRepoClient( private val keyTypeInfo: TypeInfo, private val valueTypeInfo: TypeInfo, private val keyToValuesMapTypeInfo: TypeInfo -) : WriteOneToManyKeyValueRepo { +) : WriteKeyValuesRepo { @OptIn(InternalAPI::class) override suspend fun add(toAdd: Map>) { @@ -85,7 +85,7 @@ class KtorWriteStandardKeyValuesRepoClient( baseUrl: String, httpClient: HttpClient, contentType: ContentType - ) = KtorWriteStandardKeyValuesRepoClient( + ) = KtorWriteKeyValuesRepoClient( baseUrl, httpClient, contentType, 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 1a2122d25d7..6430b7252e2 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 @@ -2,19 +2,20 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.* -import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.WriteKeyValuesRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.client.HttpClient import kotlinx.coroutines.flow.Flow import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* +@Deprecated("Should be replaced with KtorWriteKeyValuesRepoClient") class KtorWriteOneToManyKeyValueRepo ( private val baseUrl: String, private val unifiedRequester: UnifiedRequester, private val keySerializer: KSerializer, private val valueSerializer: KSerializer -) : WriteOneToManyKeyValueRepo { +) : WriteKeyValuesRepo { private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer) private val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer)) diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt index 20a8427e3e2..1db65f3e605 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt @@ -1,8 +1,8 @@ import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.* -import dev.inmo.micro_utils.repos.ktor.client.one_to_many.KtorStandardKeyValuesRepoClient -import dev.inmo.micro_utils.repos.ktor.server.one_to_many.configureStandardKeyValuesRepoRoutes +import dev.inmo.micro_utils.repos.ktor.client.one_to_many.KtorKeyValuesRepoClient +import dev.inmo.micro_utils.repos.ktor.server.one_to_many.configureKeyValuesRepoRoutes import io.ktor.client.HttpClient import io.ktor.client.plugins.logging.Logging import io.ktor.http.ContentType @@ -25,7 +25,7 @@ class KVsTests { fun testKVsFunctions() { runTest { val map = mutableMapOf>() - val repo = MapOneToManyKeyValueRepo(map) + val repo = MapKeyValuesRepo(map) val server = io.ktor.server.engine.embeddedServer( CIO, 23456, @@ -38,7 +38,7 @@ class KVsTests { contentConverter = KotlinxWebsocketSerializationConverter(Json) } routing { - configureStandardKeyValuesRepoRoutes( + configureKeyValuesRepoRoutes( repo, Int.serializer(), ComplexData.serializer(), @@ -55,7 +55,7 @@ class KVsTests { contentConverter = KotlinxWebsocketSerializationConverter(Json) } } - val crudClient = KtorStandardKeyValuesRepoClient( + val crudClient = KtorKeyValuesRepoClient( "http://127.0.0.1:23456", client, ContentType.Application.Json, diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyKeyValueRepoRoutes.kt index 4f2c694c25d..9b68c0ac04f 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyKeyValueRepoRoutes.kt @@ -4,7 +4,7 @@ import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.UnifiedRouter import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType -import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.KeyValuesRepo import io.ktor.http.ContentType import io.ktor.server.routing.Route import io.ktor.server.routing.route @@ -12,7 +12,7 @@ import kotlinx.serialization.KSerializer fun Route.configureOneToManyKeyValueRepoRoutes( baseSubpart: String, - originalRepo: OneToManyKeyValueRepo, + originalRepo: KeyValuesRepo, keySerializer: KSerializer, valueSerializer: KSerializer, unifiedRouter: UnifiedRouter @@ -25,7 +25,7 @@ fun Route.configureOneToManyKeyValueRepoRoutes( fun Route.configureOneToManyKeyValueRepoRoutes( baseSubpart: String, - originalRepo: OneToManyKeyValueRepo, + originalRepo: KeyValuesRepo, keySerializer: KSerializer, valueSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt index c0795d2280b..41a035c4730 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt @@ -5,7 +5,7 @@ import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination -import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValuesRepo import dev.inmo.micro_utils.repos.ktor.common.keyParameterName import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import dev.inmo.micro_utils.repos.ktor.common.valueParameterName @@ -18,7 +18,7 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer fun Route.configureOneToManyReadKeyValueRepoRoutes( - originalRepo: ReadOneToManyKeyValueRepo, + originalRepo: ReadKeyValuesRepo, keySerializer: KSerializer, valueSerializer: KSerializer, unifiedRouter: UnifiedRouter @@ -121,7 +121,7 @@ fun Route.configureOneToManyReadKeyValueRepoRoutes( } inline fun Route.configureOneToManyReadKeyValueRepoRoutes( - originalRepo: ReadOneToManyKeyValueRepo, + originalRepo: ReadKeyValuesRepo, keySerializer: KSerializer, valueSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, 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 dbef2f3e416..301350b64c4 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 @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.server.one_to_many import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* -import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.WriteKeyValuesRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.http.ContentType import io.ktor.server.routing.Route @@ -12,7 +12,7 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* fun Route.configureOneToManyWriteKeyValueRepoRoutes( - originalRepo: WriteOneToManyKeyValueRepo, + originalRepo: WriteKeyValuesRepo, keySerializer: KSerializer, valueSerializer: KSerializer, unifiedRouter: UnifiedRouter @@ -95,7 +95,7 @@ fun Route.configureOneToManyWriteKeyValueRepoRoutes( } fun Route.configureOneToManyWriteKeyValueRepoRoutes( - originalRepo: WriteOneToManyKeyValueRepo, + originalRepo: WriteKeyValuesRepo, keySerializer: KSerializer, valueSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartKeyValuesRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorKeyValuesRepoRoutes.kt similarity index 70% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartKeyValuesRepo.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorKeyValuesRepoRoutes.kt index 625ddc25529..c0aaa389810 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartKeyValuesRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorKeyValuesRepoRoutes.kt @@ -6,21 +6,21 @@ import io.ktor.http.* import io.ktor.server.routing.Route import kotlinx.serialization.* -inline fun Route.configureStandardKeyValuesRepoRoutes ( - originalRepo: OneToManyKeyValueRepo, +inline fun Route.configureKeyValuesRepoRoutes ( + originalRepo: KeyValuesRepo, noinline idDeserializer: suspend (String) -> Key, noinline valueDeserializer: suspend (String) -> Value ) { - configureReadStandardKeyValuesRepoRoutes(originalRepo, idDeserializer, valueDeserializer) - configureWriteStandardKeyValuesRepoRoutes(originalRepo) + configureReadKeyValuesRepoRoutes(originalRepo, idDeserializer, valueDeserializer) + configureWriteKeyValuesRepoRoutes(originalRepo) } -inline fun Route.configureStandardKeyValuesRepoRoutes( - originalRepo: OneToManyKeyValueRepo, +inline fun Route.configureKeyValuesRepoRoutes( + originalRepo: KeyValuesRepo, idsSerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: StringFormat -) = configureStandardKeyValuesRepoRoutes( +) = configureKeyValuesRepoRoutes( originalRepo, { serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) @@ -30,12 +30,12 @@ inline fun Route.configureStandardKeyVa } ) -inline fun Route.configureStandardKeyValuesRepoRoutes( - originalRepo: OneToManyKeyValueRepo, +inline fun Route.configureKeyValuesRepoRoutes( + originalRepo: KeyValuesRepo, idsSerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: BinaryFormat -) = configureStandardKeyValuesRepoRoutes( +) = configureKeyValuesRepoRoutes( originalRepo, { serialFormat.decodeHex(idsSerializer, it) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartReadKeyValuesRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorReadKeyValuesRepoRoutes.kt similarity index 84% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartReadKeyValuesRepo.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorReadKeyValuesRepoRoutes.kt index c8f5c0b7b7c..80ac3e7dfd2 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartReadKeyValuesRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorReadKeyValuesRepoRoutes.kt @@ -4,7 +4,7 @@ import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination -import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValuesRepo import dev.inmo.micro_utils.repos.ktor.common.* import dev.inmo.micro_utils.repos.ktor.common.containsRoute import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* @@ -18,8 +18,8 @@ import io.ktor.util.reflect.typeInfo import kotlinx.serialization.* @OptIn(InternalAPI::class) -inline fun Route.configureReadStandardKeyValuesRepoRoutes ( - originalRepo: ReadOneToManyKeyValueRepo, +inline fun Route.configureReadKeyValuesRepoRoutes ( + originalRepo: ReadKeyValuesRepo, noinline idDeserializer: suspend (String) -> Key, noinline valueDeserializer: suspend (String) -> Value ) { @@ -75,12 +75,12 @@ inline fun Route.configureReadStandardKeyValuesRepo } } -inline fun Route.configureReadStandardKeyValuesRepoRoutes( - originalRepo: ReadOneToManyKeyValueRepo, +inline fun Route.configureReadKeyValuesRepoRoutes( + originalRepo: ReadKeyValuesRepo, idsSerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: StringFormat -) = configureReadStandardKeyValuesRepoRoutes( +) = configureReadKeyValuesRepoRoutes( originalRepo, { serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) @@ -90,12 +90,12 @@ inline fun Route.configureReadStandardKeyValuesRepo } ) -inline fun Route.configureReadStandardKeyValuesRepoRoutes( - originalRepo: ReadOneToManyKeyValueRepo, +inline fun Route.configureReadKeyValuesRepoRoutes( + originalRepo: ReadKeyValuesRepo, idsSerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: BinaryFormat -) = configureReadStandardKeyValuesRepoRoutes( +) = configureReadKeyValuesRepoRoutes( originalRepo, { serialFormat.decodeHex(idsSerializer, it) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartWriteKeyValuesRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorWriteKeyValuesRepoRoutes.kt similarity index 90% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartWriteKeyValuesRepo.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorWriteKeyValuesRepoRoutes.kt index a33e9062a72..440821ed17c 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/NewKtorStandartWriteKeyValuesRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorWriteKeyValuesRepoRoutes.kt @@ -1,7 +1,7 @@ package dev.inmo.micro_utils.repos.ktor.server.one_to_many import dev.inmo.micro_utils.ktor.server.* -import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.WriteKeyValuesRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.http.HttpStatusCode import io.ktor.server.application.call @@ -11,8 +11,8 @@ import io.ktor.server.routing.Route import io.ktor.server.routing.post import io.ktor.util.reflect.typeInfo -inline fun Route.configureWriteStandardKeyValuesRepoRoutes ( - originalRepo: WriteOneToManyKeyValueRepo +inline fun Route.configureWriteKeyValuesRepoRoutes ( + originalRepo: WriteKeyValuesRepo ) { includeWebsocketHandling( onNewValueRoute, From 3bbde61f39a505dadb07cb91c65756549f03dd7f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Jun 2022 15:15:17 +0600 Subject: [PATCH 08/14] renames in crud repos and some little refactoring --- .../micro_utils/repos/StandartCRUDRepo.kt | 32 +++++++-------- .../pagination/CRUDPaginationExtensions.kt | 5 +-- .../repos/crud/AbstractAndroidCRUDRepo.kt | 4 +- .../crud/AbstractMutableAndroidCRUDRepo.kt | 4 +- .../repos/exposed/AbstractExposedCRUDRepo.kt | 4 +- .../exposed/AbstractExposedReadCRUDRepo.kt | 4 +- .../exposed/AbstractExposedWriteCRUDRepo.kt | 4 +- .../dev/inmo/micro_utils/repos/MapCRUDRepo.kt | 8 ++-- ...rudRepoClient.kt => KtorCRUDRepoClient.kt} | 41 +++++++++---------- ...epoClient.kt => KtorReadCRUDRepoClient.kt} | 18 ++++---- .../client/crud/KtorReadStandardCrudRepo.kt | 6 +-- .../ktor/client/crud/KtorStandardCrudRepo.kt | 8 ++-- ...poClient.kt => KtorWriteCrudRepoClient.kt} | 8 ++-- .../client/crud/KtorWriteStandardCrudRepo.kt | 6 +-- .../common/src/jvmTest/kotlin/CRUDTests.kt | 8 ++-- .../ktor/server/crud/KtorCRUDRepoRoutes.kt | 30 ++++++++++++++ ...dCrudRepo.kt => KtorReadCRUDRepoRoutes.kt} | 19 ++++----- .../server/crud/KtorReadStandardCrudRepo.kt | 12 +++--- .../ktor/server/crud/KtorStandardCrudRepo.kt | 16 ++++---- ...CrudRepo.kt => KtorWriteCRUDRepoRoutes.kt} | 6 +-- .../server/crud/KtorWriteStandardCrudRepo.kt | 12 +++--- .../server/crud/NewKtorStandardCrudRepo.kt | 34 --------------- ...ValueRepo.kt => KtorKeyValueRepoRoutes.kt} | 2 +- ...eRepo.kt => KtorReadKeyValueRepoRoutes.kt} | 0 .../key_value/KtorStandartKeyValueRepo.kt | 2 +- .../KtorStandartWriteKeyValueRepo.kt | 4 +- ...Repo.kt => KtorWriteKeyValueRepoRoutes.kt} | 2 +- 27 files changed, 146 insertions(+), 153 deletions(-) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/{KtorStandardCrudRepoClient.kt => KtorCRUDRepoClient.kt} (68%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/{KtorReadStandardCrudRepoClient.kt => KtorReadCRUDRepoClient.kt} (80%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/{KtorWriteStandardCrudRepoClient.kt => KtorWriteCrudRepoClient.kt} (91%) create mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorCRUDRepoRoutes.kt rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/{NewKtorReadStandardCrudRepo.kt => KtorReadCRUDRepoRoutes.kt} (80%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/{NewKtorWriteStandardCrudRepo.kt => KtorWriteCRUDRepoRoutes.kt} (84%) delete mode 100644 repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorStandardCrudRepo.kt rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/{NewKtorStandartKeyValueRepo.kt => KtorKeyValueRepoRoutes.kt} (96%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/{NewKtorStandartReadKeyValueRepo.kt => KtorReadKeyValueRepoRoutes.kt} (100%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/{NewKtorStandartWriteKeyValueRepo.kt => KtorWriteKeyValueRepoRoutes.kt} (97%) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt index 33911b8d8ed..265de6dab16 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt @@ -4,13 +4,13 @@ import dev.inmo.micro_utils.pagination.Pagination import dev.inmo.micro_utils.pagination.PaginationResult import kotlinx.coroutines.flow.Flow -interface ReadStandardCRUDRepo : Repo { +interface ReadCRUDRepo : Repo { suspend fun getByPagination(pagination: Pagination): PaginationResult suspend fun getById(id: IdType): ObjectType? suspend fun contains(id: IdType): Boolean suspend fun count(): Long } -typealias ReadCRUDRepo = ReadStandardCRUDRepo +typealias ReadStandardCRUDRepo = ReadCRUDRepo typealias UpdatedValuePair = Pair val UpdatedValuePair.id @@ -18,7 +18,7 @@ val UpdatedValuePair.id val UpdatedValuePair<*, ValueType>.value get() = second -interface WriteStandardCRUDRepo : Repo { +interface WriteCRUDRepo : Repo { val newObjectsFlow: Flow val updatedObjectsFlow: Flow val deletedObjectsIdsFlow: Flow @@ -28,25 +28,25 @@ interface WriteStandardCRUDRepo : Repo { suspend fun update(values: List>): List suspend fun deleteById(ids: List) } -typealias WriteCRUDRepo = WriteStandardCRUDRepo +typealias WriteStandardCRUDRepo = WriteCRUDRepo -suspend fun WriteStandardCRUDRepo.create( +suspend fun WriteCRUDRepo.create( vararg values: InputValueType ): List = create(values.toList()) -suspend fun WriteStandardCRUDRepo.update( +suspend fun WriteCRUDRepo.update( vararg values: UpdatedValuePair ): List = update(values.toList()) -suspend fun WriteStandardCRUDRepo.deleteById( +suspend fun WriteCRUDRepo.deleteById( vararg ids: IdType ) = deleteById(ids.toList()) -interface StandardCRUDRepo : ReadStandardCRUDRepo, - WriteStandardCRUDRepo -typealias CRUDRepo = StandardCRUDRepo +interface CRUDRepo : ReadCRUDRepo, + WriteCRUDRepo +typealias StandardCRUDRepo = CRUDRepo -class DelegateBasedStandardCRUDRepo( - readDelegate: ReadStandardCRUDRepo, - writeDelegate: WriteStandardCRUDRepo -) : StandardCRUDRepo, - ReadStandardCRUDRepo by readDelegate, - WriteStandardCRUDRepo by writeDelegate +class DelegateBasedCRUDRepo( + readDelegate: ReadCRUDRepo, + writeDelegate: WriteCRUDRepo +) : CRUDRepo, + ReadCRUDRepo by readDelegate, + WriteCRUDRepo by writeDelegate diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt index eeda0b9a748..52bb8231a54 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt @@ -1,11 +1,10 @@ package dev.inmo.micro_utils.repos.pagination import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging -import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ReadCRUDRepo -suspend inline fun > REPO.getAll( +suspend inline fun > REPO.getAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult ): List = getAllWithNextPaging { diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractAndroidCRUDRepo.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractAndroidCRUDRepo.kt index 64440100acf..817e3aeb3fe 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractAndroidCRUDRepo.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractAndroidCRUDRepo.kt @@ -12,7 +12,7 @@ val T.asId: String abstract class AbstractAndroidCRUDRepo( protected val helper: StandardSQLHelper -) : ReadStandardCRUDRepo { +) : ReadCRUDRepo { protected abstract val tableName: String protected abstract val idColumnName: String protected abstract suspend fun Cursor.toObject(): ObjectType @@ -64,4 +64,4 @@ abstract class AbstractAndroidCRUDRepo( } } } -} \ No newline at end of file +} diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractMutableAndroidCRUDRepo.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractMutableAndroidCRUDRepo.kt index 29103d3f2ab..8facd8a6e12 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractMutableAndroidCRUDRepo.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractMutableAndroidCRUDRepo.kt @@ -9,9 +9,9 @@ abstract class AbstractMutableAndroidCRUDRepo, +) : WriteCRUDRepo, AbstractAndroidCRUDRepo(helper), - StandardCRUDRepo { + CRUDRepo { protected val newObjectsChannel = MutableSharedFlow(replyInFlows, extraBufferCapacityInFlows) protected val updateObjectsChannel = MutableSharedFlow(replyInFlows, extraBufferCapacityInFlows) protected val deleteObjectsIdsChannel = MutableSharedFlow(replyInFlows, extraBufferCapacityInFlows) diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedCRUDRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedCRUDRepo.kt index 636a645fd0c..11cb0154a2b 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedCRUDRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedCRUDRepo.kt @@ -1,6 +1,6 @@ package dev.inmo.micro_utils.repos.exposed -import dev.inmo.micro_utils.repos.StandardCRUDRepo +import dev.inmo.micro_utils.repos.CRUDRepo abstract class AbstractExposedCRUDRepo( flowsChannelsSize: Int = 0, @@ -11,4 +11,4 @@ abstract class AbstractExposedCRUDRepo( tableName ), ExposedCRUDRepo, - StandardCRUDRepo + CRUDRepo 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 916e0a80512..bcac643e6dc 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,14 +1,14 @@ package dev.inmo.micro_utils.repos.exposed import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ReadCRUDRepo import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction abstract class AbstractExposedReadCRUDRepo( tableName: String ) : - ReadStandardCRUDRepo, + ReadCRUDRepo, ExposedCRUDRepo, Table(tableName) { 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 c64d9c47f91..1e2816e4a02 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 dev.inmo.micro_utils.repos.UpdatedValuePair -import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo +import dev.inmo.micro_utils.repos.WriteCRUDRepo import kotlinx.coroutines.flow.* import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.statements.InsertStatement @@ -15,7 +15,7 @@ abstract class AbstractExposedWriteCRUDRepo( ) : AbstractExposedReadCRUDRepo(tableName), ExposedCRUDRepo, - WriteStandardCRUDRepo + WriteCRUDRepo { protected val _newObjectsFlow = MutableSharedFlow(replyCacheInFlows, flowsChannelsSize) protected val _updatedObjectsFlow = MutableSharedFlow(replyCacheInFlows, flowsChannelsSize) diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt index e040c478112..875510b5ee2 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt @@ -5,7 +5,7 @@ import kotlinx.coroutines.flow.* class ReadMapCRUDRepo( private val map: Map = emptyMap() -) : ReadStandardCRUDRepo { +) : ReadCRUDRepo { override suspend fun getByPagination(pagination: Pagination): PaginationResult { return map.keys.drop(pagination.firstIndex).take(pagination.size).mapNotNull { map[it] @@ -24,7 +24,7 @@ class ReadMapCRUDRepo( abstract class WriteMapCRUDRepo( protected val map: MutableMap = mutableMapOf() -) : WriteStandardCRUDRepo { +) : WriteCRUDRepo { protected val _newObjectsFlow: MutableSharedFlow = MutableSharedFlow() override val newObjectsFlow: Flow = _newObjectsFlow.asSharedFlow() protected val _updatedObjectsFlow: MutableSharedFlow = MutableSharedFlow() @@ -68,8 +68,8 @@ abstract class WriteMapCRUDRepo( abstract class MapCRUDRepo( map: MutableMap -) : StandardCRUDRepo, - ReadStandardCRUDRepo by ReadMapCRUDRepo(map), +) : CRUDRepo, + ReadCRUDRepo by ReadMapCRUDRepo(map), WriteMapCRUDRepo(map) fun MapCRUDRepo( diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorCRUDRepoClient.kt similarity index 68% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorCRUDRepoClient.kt index 87d79182c6d..7bb55318f6d 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorCRUDRepoClient.kt @@ -3,16 +3,15 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient -import io.ktor.client.utils.EmptyContent.contentType import io.ktor.http.ContentType import io.ktor.util.reflect.TypeInfo import io.ktor.util.reflect.typeInfo import kotlinx.serialization.* -class KtorStandardCrudRepoClient ( - readDelegate: ReadStandardCRUDRepo, - writeDelegate: WriteStandardCRUDRepo -) : StandardCRUDRepo by DelegateBasedStandardCRUDRepo( +class KtorCRUDRepoClient ( + readDelegate: ReadCRUDRepo, + writeDelegate: WriteCRUDRepo +) : CRUDRepo by DelegateBasedCRUDRepo( readDelegate, writeDelegate ) { @@ -23,15 +22,15 @@ class KtorStandardCrudRepoClient ( objectTypeInfo: TypeInfo, contentType: ContentType, noinline idSerializer: suspend (IdType) -> String - ) = KtorStandardCrudRepoClient( - KtorReadStandardCrudRepoClient( + ) = KtorCRUDRepoClient( + KtorReadCRUDRepoClient( baseUrl, httpClient, objectTypeInfo, contentType, idSerializer ), - KtorWriteStandardCrudRepoClient( + KtorWriteCrudRepoClient( baseUrl, httpClient, contentType @@ -45,7 +44,7 @@ class KtorStandardCrudRepoClient ( objectTypeInfo: TypeInfo, contentType: ContentType, noinline idSerializer: suspend (IdType) -> String - ) = KtorStandardCrudRepoClient( + ) = KtorCRUDRepoClient( buildStandardUrl(baseUrl, subpart), httpClient, objectTypeInfo, @@ -56,12 +55,12 @@ class KtorStandardCrudRepoClient ( } -inline fun KtorStandardCrudRepoClient( +inline fun KtorCRUDRepoClient( baseUrl: String, httpClient: HttpClient, contentType: ContentType, noinline idSerializer: suspend (IdType) -> String -) = KtorStandardCrudRepoClient( +) = KtorCRUDRepoClient( baseUrl, httpClient, typeInfo(), @@ -69,54 +68,54 @@ inline fun KtorStandard idSerializer ) -inline fun KtorStandardCrudRepoClient( +inline fun KtorCRUDRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, serialFormat: StringFormat, contentType: ContentType, -) = KtorStandardCrudRepoClient(baseUrl, httpClient, contentType) { +) = KtorCRUDRepoClient(baseUrl, httpClient, contentType) { serialFormat.encodeToString(idsSerializer, it) } -inline fun KtorStandardCrudRepoClient( +inline fun KtorCRUDRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, serialFormat: BinaryFormat, contentType: ContentType, -) = KtorStandardCrudRepoClient(baseUrl, httpClient, contentType) { +) = KtorCRUDRepoClient(baseUrl, httpClient, contentType) { serialFormat.encodeHex(idsSerializer, it) } -inline fun KtorStandardCrudRepoClient( +inline fun KtorCRUDRepoClient( baseUrl: String, subpart: String, httpClient: HttpClient, contentType: ContentType, noinline idSerializer: suspend (IdType) -> String -) = KtorStandardCrudRepoClient( +) = KtorCRUDRepoClient( buildStandardUrl(baseUrl, subpart), httpClient, contentType, idSerializer ) -inline fun KtorStandardCrudRepoClient( +inline fun KtorCRUDRepoClient( baseUrl: String, subpart: String, httpClient: HttpClient, idsSerializer: KSerializer, serialFormat: StringFormat, contentType: ContentType, -) = KtorStandardCrudRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat, contentType) +) = KtorCRUDRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat, contentType) -inline fun KtorStandardCrudRepoClient( +inline fun KtorCRUDRepoClient( baseUrl: String, subpart: String, httpClient: HttpClient, idsSerializer: KSerializer, serialFormat: BinaryFormat, contentType: ContentType, -) = KtorStandardCrudRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat, contentType) +) = KtorCRUDRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat, contentType) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt similarity index 80% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt index 088cea5e31c..5c49eafa7b0 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt @@ -2,7 +2,7 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ReadCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.countRouting import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.idParameterName @@ -14,13 +14,13 @@ import io.ktor.util.reflect.TypeInfo import io.ktor.util.reflect.typeInfo import kotlinx.serialization.* -class KtorReadStandardCrudRepoClient ( +class KtorReadCRUDRepoClient ( private val baseUrl: String, private val httpClient: HttpClient, private val objectType: TypeInfo, private val contentType: ContentType, private val idSerializer: suspend (IdType) -> String -) : ReadStandardCRUDRepo { +) : ReadCRUDRepo { override suspend fun getByPagination(pagination: Pagination): PaginationResult = httpClient.get( buildStandardUrl(baseUrl, getByPaginationRouting, pagination.asUrlQueryParts) ) { @@ -61,12 +61,12 @@ class KtorReadStandardCrudRepoClient ( }.body() } -inline fun KtorReadStandardCrudRepoClient( +inline fun KtorReadCRUDRepoClient( baseUrl: String, httpClient: HttpClient, contentType: ContentType, noinline idSerializer: suspend (IdType) -> String -) = KtorReadStandardCrudRepoClient( +) = KtorReadCRUDRepoClient( baseUrl, httpClient, typeInfo(), @@ -74,22 +74,22 @@ inline fun KtorReadStandardCrudRepoClient( idSerializer ) -inline fun KtorReadStandardCrudRepoClient( +inline fun KtorReadCRUDRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, serialFormat: StringFormat, contentType: ContentType, -) = KtorReadStandardCrudRepoClient(baseUrl, httpClient, contentType) { +) = KtorReadCRUDRepoClient(baseUrl, httpClient, contentType) { serialFormat.encodeToString(idsSerializer, it) } -inline fun KtorReadStandardCrudRepoClient( +inline fun KtorReadCRUDRepoClient( baseUrl: String, httpClient: HttpClient, idsSerializer: KSerializer, serialFormat: BinaryFormat, contentType: ContentType, -) = KtorReadStandardCrudRepoClient(baseUrl, httpClient, contentType) { +) = KtorReadCRUDRepoClient(baseUrl, httpClient, contentType) { serialFormat.encodeHex(idsSerializer, it) } diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt index dfb7ff57df9..877e16c1547 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ReadCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.countRouting import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.idParameterName @@ -11,14 +11,14 @@ import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer -@Deprecated("Use KtorReadStandardCrudRepoClient instead") +@Deprecated("Use KtorReadCRUDRepoClient instead") class KtorReadStandardCrudRepo ( private val baseUrl: String, private val unifiedRequester: UnifiedRequester, private val objectsSerializer: KSerializer, private val objectsSerializerNullable: KSerializer, private val idsSerializer: KSerializer -) : ReadStandardCRUDRepo { +) : ReadCRUDRepo { private val paginationResultSerializer = PaginationResult.serializer(objectsSerializer) constructor( diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt index 1f0b31fafd4..2b3f7528df9 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt @@ -7,7 +7,7 @@ import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer -@Deprecated("Use KtorStandardCrudRepoClient instead") +@Deprecated("Use KtorCRUDRepoClient instead") class KtorStandardCrudRepo ( baseUrl: String, baseSubpart: String, @@ -16,15 +16,15 @@ class KtorStandardCrudRepo ( objectsNullableSerializer: KSerializer, inputsSerializer: KSerializer, idsSerializer: KSerializer -) : StandardCRUDRepo, - ReadStandardCRUDRepo by KtorReadStandardCrudRepo( +) : CRUDRepo, + ReadCRUDRepo by KtorReadStandardCrudRepo( "$baseUrl/$baseSubpart", unifiedRequester, objectsSerializer, objectsNullableSerializer, idsSerializer ), - WriteStandardCRUDRepo by KtorWriteStandardCrudRepo( + WriteCRUDRepo by KtorWriteStandardCrudRepo( "$baseUrl/$baseSubpart", unifiedRequester, objectsSerializer, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteCrudRepoClient.kt similarity index 91% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteCrudRepoClient.kt index 6caf6969b83..c28e553f629 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteCrudRepoClient.kt @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.UpdatedValuePair -import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo +import dev.inmo.micro_utils.repos.WriteCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.client.HttpClient import io.ktor.client.call.body @@ -13,7 +13,7 @@ import io.ktor.http.ContentType import io.ktor.http.contentType import kotlinx.coroutines.flow.Flow -class KtorWriteStandardCrudRepoClient ( +class KtorWriteCrudRepoClient ( private val baseUrl: String, private val httpClient: HttpClient, override val newObjectsFlow: Flow, @@ -24,7 +24,7 @@ class KtorWriteStandardCrudRepoClient ( private val deleteByIdSetup: suspend HttpRequestBuilder.(List) -> Unit, private val createBodyGetter: suspend HttpResponse.() -> List, private val updateBodyGetter: suspend HttpResponse.() -> List -) : WriteStandardCRUDRepo { +) : WriteCRUDRepo { override suspend fun create(values: List): List = httpClient.post( buildStandardUrl(baseUrl, createRouting) ) { @@ -54,7 +54,7 @@ class KtorWriteStandardCrudRepoClient ( baseUrl: String, httpClient: HttpClient, contentType: ContentType - ) = KtorWriteStandardCrudRepoClient( + ) = KtorWriteCrudRepoClient( baseUrl, httpClient, httpClient.createStandardWebsocketFlow( diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt index 2013041d432..1b417090bb5 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteStandardCrudRepo.kt @@ -3,14 +3,14 @@ package dev.inmo.micro_utils.repos.ktor.client.crud import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.UpdatedValuePair -import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo +import dev.inmo.micro_utils.repos.WriteCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.client.HttpClient import kotlinx.coroutines.flow.Flow import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* -@Deprecated("Use KtorWriteStandardCrudRepoClient instead") +@Deprecated("Use KtorWriteCRUDRepoClient instead") class KtorWriteStandardCrudRepo ( private val baseUrl: String, private val unifiedRequester: UnifiedRequester, @@ -18,7 +18,7 @@ class KtorWriteStandardCrudRepo ( private val objectsNullableSerializer: KSerializer, private val inputsSerializer: KSerializer, private val idsSerializer: KSerializer -) : WriteStandardCRUDRepo { +) : WriteCRUDRepo { private val listObjectsSerializer = ListSerializer(objectsSerializer) private val listInputSerializer = ListSerializer(inputsSerializer) private val listIdsSerializer = ListSerializer(idsSerializer) diff --git a/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt b/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt index 4ac4993dffc..fdf2995b165 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt @@ -1,6 +1,6 @@ import dev.inmo.micro_utils.repos.* -import dev.inmo.micro_utils.repos.ktor.client.crud.KtorStandardCrudRepoClient -import dev.inmo.micro_utils.repos.ktor.server.crud.configureStandardCrudRepoRoutes +import dev.inmo.micro_utils.repos.ktor.client.crud.KtorCRUDRepoClient +import dev.inmo.micro_utils.repos.ktor.server.crud.configureCRUDRepoRoutes import io.ktor.client.HttpClient import io.ktor.client.plugins.logging.Logging import io.ktor.http.ContentType @@ -43,7 +43,7 @@ class CRUDTests { contentConverter = KotlinxWebsocketSerializationConverter(Json) } routing { - configureStandardCrudRepoRoutes( + configureCRUDRepoRoutes( repo ) { it.toInt() @@ -59,7 +59,7 @@ class CRUDTests { contentConverter = KotlinxWebsocketSerializationConverter(Json) } } - val crudClient = KtorStandardCrudRepoClient( + val crudClient = KtorCRUDRepoClient( "http://127.0.0.1:23456", client, ContentType.Application.Json diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorCRUDRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorCRUDRepoRoutes.kt new file mode 100644 index 00000000000..f7d950229bd --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorCRUDRepoRoutes.kt @@ -0,0 +1,30 @@ +package dev.inmo.micro_utils.repos.ktor.server.crud + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.repos.CRUDRepo +import io.ktor.server.routing.Route +import kotlinx.serialization.* + +inline fun Route.configureCRUDRepoRoutes( + originalRepo: CRUDRepo, + noinline idDeserializer: suspend (String) -> IdType +) { + configureReadCRUDRepoRoutes(originalRepo, idDeserializer) + configureWriteCRUDRepoRoutes(originalRepo) +} + +inline fun Route.configureCRUDRepoRoutes( + originalRepo: CRUDRepo, + idsSerializer: KSerializer, + serialFormat: StringFormat +) = configureCRUDRepoRoutes(originalRepo) { + serialFormat.decodeFromString(idsSerializer, it) +} + +inline fun Route.configureCRUDRepoRoutes( + originalRepo: CRUDRepo, + idsSerializer: KSerializer, + serialFormat: BinaryFormat +) = configureCRUDRepoRoutes(originalRepo) { + serialFormat.decodeHex(idsSerializer, it) +} diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadCRUDRepoRoutes.kt similarity index 80% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadCRUDRepoRoutes.kt index 53354208e16..fd5acf5cf4b 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorReadStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadCRUDRepoRoutes.kt @@ -3,11 +3,10 @@ package dev.inmo.micro_utils.repos.ktor.server.crud import dev.inmo.micro_utils.ktor.common.decodeHex import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.extractPagination -import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ReadCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.countRouting import dev.inmo.micro_utils.repos.ktor.common.crud.* import dev.inmo.micro_utils.repos.ktor.common.idParameterName -import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode import io.ktor.server.application.call import io.ktor.server.response.respond @@ -15,8 +14,8 @@ import io.ktor.server.routing.Route import io.ktor.server.routing.get import kotlinx.serialization.* -inline fun Route.configureReadStandardCrudRepoRoutes( - originalRepo: ReadStandardCRUDRepo, +inline fun Route.configureReadCRUDRepoRoutes( + originalRepo: ReadCRUDRepo, noinline idDeserializer: suspend (String) -> IdType ) { get(getByPaginationRouting) { @@ -56,18 +55,18 @@ inline fun Route.configureReadStandardCrudR } } -inline fun Route.configureReadStandardCrudRepoRoutes( - originalRepo: ReadStandardCRUDRepo, +inline fun Route.configureReadCRUDRepoRoutes( + originalRepo: ReadCRUDRepo, idsSerializer: KSerializer, serialFormat: StringFormat -) = configureReadStandardCrudRepoRoutes(originalRepo) { +) = configureReadCRUDRepoRoutes(originalRepo) { serialFormat.decodeFromString(idsSerializer, it) } -inline fun Route.configureReadStandardCrudRepoRoutes( - originalRepo: ReadStandardCRUDRepo, +inline fun Route.configureReadCRUDRepoRoutes( + originalRepo: ReadCRUDRepo, idsSerializer: KSerializer, serialFormat: BinaryFormat -) = configureReadStandardCrudRepoRoutes(originalRepo) { +) = configureReadCRUDRepoRoutes(originalRepo) { serialFormat.decodeHex(idsSerializer, it) } diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt index e5bdbaa5b84..cc0483a8ca1 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt @@ -5,7 +5,7 @@ import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination -import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ReadCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.countRouting import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.http.ContentType @@ -15,8 +15,8 @@ import io.ktor.server.routing.get import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer -fun Route.configureReadStandardCrudRepoRoutes( - originalRepo: ReadStandardCRUDRepo, +fun Route.configureReadCRUDRepoRoutes( + originalRepo: ReadCRUDRepo, objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, idsSerializer: KSerializer, @@ -73,11 +73,11 @@ fun Route.configureReadStandardCrudRepoRoutes( } } -inline fun Route.configureReadStandardCrudRepoRoutes( - originalRepo: ReadStandardCRUDRepo, +inline fun Route.configureReadCRUDRepoRoutes( + originalRepo: ReadCRUDRepo, objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, idsSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, serialFormatContentType: ContentType = standardKtorSerialFormatContentType -) = configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) +) = configureReadCRUDRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorStandardCrudRepo.kt index d3fd8832d05..112659afa68 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorStandardCrudRepo.kt @@ -4,15 +4,15 @@ import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.UnifiedRouter import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType -import dev.inmo.micro_utils.repos.StandardCRUDRepo +import dev.inmo.micro_utils.repos.CRUDRepo import io.ktor.http.ContentType import io.ktor.server.routing.Route import io.ktor.server.routing.route import kotlinx.serialization.KSerializer -fun Route.configureStandardCrudRepoRoutes( +fun Route.configureCRUDRepoRoutes( baseSubpart: String, - originalRepo: StandardCRUDRepo, + originalRepo: CRUDRepo, objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, inputsSerializer: KSerializer, @@ -20,20 +20,20 @@ fun Route.configureStandardCrudRepoRoutes( unifiedRouter: UnifiedRouter ) { route(baseSubpart) { - configureReadStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, unifiedRouter) - configureWriteStandardCrudRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, unifiedRouter) + configureReadCRUDRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, idsSerializer, unifiedRouter) + configureWriteCRUDRepoRoutes(originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, unifiedRouter) } } -fun Route.configureStandardCrudRepoRoutes( +fun Route.configureCRUDRepoRoutes( baseSubpart: String, - originalRepo: StandardCRUDRepo, + originalRepo: CRUDRepo, objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, inputsSerializer: KSerializer, idsSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, serialFormatContentType: ContentType = standardKtorSerialFormatContentType -) = configureStandardCrudRepoRoutes( +) = configureCRUDRepoRoutes( baseSubpart, originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType) ) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteCRUDRepoRoutes.kt similarity index 84% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteCRUDRepoRoutes.kt index 3dbe3c78188..7a05ea287ca 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorWriteStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteCRUDRepoRoutes.kt @@ -1,7 +1,7 @@ package dev.inmo.micro_utils.repos.ktor.server.crud import dev.inmo.micro_utils.ktor.server.* -import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo +import dev.inmo.micro_utils.repos.WriteCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.server.application.call import io.ktor.server.request.receive @@ -9,8 +9,8 @@ import io.ktor.server.response.respond import io.ktor.server.routing.Route import io.ktor.server.routing.post -inline fun Route.configureWriteStandardCrudRepoRoutes( - originalRepo: WriteStandardCRUDRepo +inline fun Route.configureWriteCRUDRepoRoutes( + originalRepo: WriteCRUDRepo ) { includeWebsocketHandling( newObjectsFlowRouting, diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteStandardCrudRepo.kt index f993ab22292..5fa586a6a4a 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteStandardCrudRepo.kt @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.server.crud import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat import dev.inmo.micro_utils.ktor.common.standardKtorSerialFormat import dev.inmo.micro_utils.ktor.server.* -import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo +import dev.inmo.micro_utils.repos.WriteCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.http.ContentType import io.ktor.server.routing.Route @@ -11,8 +11,8 @@ import io.ktor.server.routing.post import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* -fun Route.configureWriteStandardCrudRepoRoutes( - originalRepo: WriteStandardCRUDRepo, +fun Route.configureWriteCRUDRepoRoutes( + originalRepo: WriteCRUDRepo, objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, inputsSerializer: KSerializer, @@ -94,14 +94,14 @@ fun Route.configureWriteStandardCrudRepoRoutes( } } -fun Route.configureWriteStandardCrudRepoRoutes( - originalRepo: WriteStandardCRUDRepo, +fun Route.configureWriteCRUDRepoRoutes( + originalRepo: WriteCRUDRepo, objectsSerializer: KSerializer, objectsNullableSerializer: KSerializer, inputsSerializer: KSerializer, idsSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, serialFormatContentType: ContentType = standardKtorSerialFormatContentType -) = configureWriteStandardCrudRepoRoutes( +) = configureWriteCRUDRepoRoutes( originalRepo, objectsSerializer, objectsNullableSerializer, inputsSerializer, idsSerializer, UnifiedRouter(serialFormat, serialFormatContentType) ) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorStandardCrudRepo.kt deleted file mode 100644 index 30aa02b89da..00000000000 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/NewKtorStandardCrudRepo.kt +++ /dev/null @@ -1,34 +0,0 @@ -package dev.inmo.micro_utils.repos.ktor.server.crud - -import dev.inmo.micro_utils.ktor.common.* -import dev.inmo.micro_utils.ktor.server.UnifiedRouter -import dev.inmo.micro_utils.ktor.server.standardKtorSerialFormatContentType -import dev.inmo.micro_utils.repos.StandardCRUDRepo -import io.ktor.http.ContentType -import io.ktor.server.routing.Route -import io.ktor.server.routing.route -import kotlinx.serialization.* - -inline fun Route.configureStandardCrudRepoRoutes( - originalRepo: StandardCRUDRepo, - noinline idDeserializer: suspend (String) -> IdType -) { - configureReadStandardCrudRepoRoutes(originalRepo, idDeserializer) - configureWriteStandardCrudRepoRoutes(originalRepo) -} - -inline fun Route.configureStandardCrudRepoRoutes( - originalRepo: StandardCRUDRepo, - idsSerializer: KSerializer, - serialFormat: StringFormat -) = configureStandardCrudRepoRoutes(originalRepo) { - serialFormat.decodeFromString(idsSerializer, it) -} - -inline fun Route.configureStandardCrudRepoRoutes( - originalRepo: StandardCRUDRepo, - idsSerializer: KSerializer, - serialFormat: BinaryFormat -) = configureStandardCrudRepoRoutes(originalRepo) { - serialFormat.decodeHex(idsSerializer, it) -} diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorKeyValueRepoRoutes.kt similarity index 96% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartKeyValueRepo.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorKeyValueRepoRoutes.kt index b62db0700ff..099a4a9b963 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorKeyValueRepoRoutes.kt @@ -12,7 +12,7 @@ inline fun Route.configureKeyValueRepoR noinline valueDeserializer: suspend (String) -> Value ) { configureReadKeyValueRepoRoutes(originalRepo, idDeserializer, valueDeserializer) - configureWriteValueRepoRoutes(originalRepo) + configureWriteKeyValueRepoRoutes(originalRepo) } inline fun Route.configureKeyValueRepoRoutes( diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt similarity index 100% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartReadKeyValueRepo.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt index daec736b46c..bf332f72e29 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartKeyValueRepo.kt @@ -26,7 +26,7 @@ fun Route.configureKeyValueRepoRoutes( valueNullableSerializer, unifiedRouter ) - configureWriteValueRepoRoutes( + configureWriteKeyValueRepoRoutes( originalRepo, keySerializer, valueSerializer, diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt index accc61d9f6c..0313368b2e3 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt @@ -11,7 +11,7 @@ import io.ktor.server.routing.post import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.* -fun Route.configureWriteValueRepoRoutes ( +fun Route.configureWriteKeyValueRepoRoutes ( originalRepo: WriteKeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, @@ -67,4 +67,4 @@ fun Route.configureWriteStandartKeyValueRepoRoutes ( valueSerializer: KSerializer, serialFormat: StandardKtorSerialFormat = standardKtorSerialFormat, serialFormatContentType: ContentType = standardKtorSerialFormatContentType -) = configureWriteValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) +) = configureWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorWriteKeyValueRepoRoutes.kt similarity index 97% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorWriteKeyValueRepoRoutes.kt index 8ad82d6695d..2ed9a2dcc4f 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/NewKtorStandartWriteKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorWriteKeyValueRepoRoutes.kt @@ -11,7 +11,7 @@ import io.ktor.server.routing.Route import io.ktor.server.routing.post import io.ktor.util.reflect.typeInfo -inline fun Route.configureWriteValueRepoRoutes ( +inline fun Route.configureWriteKeyValueRepoRoutes ( originalRepo: WriteKeyValueRepo ) { includeWebsocketHandling( From 8fc1ff1d59e5623e4efcf0741056d6f1d6525a94 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Jun 2022 16:18:48 +0600 Subject: [PATCH 09/14] fixes and filling of cru.yml for swagger --- .../client/crud/KtorWriteCrudRepoClient.kt | 2 +- repos/ktor/server/src/crud.yml | 196 ++++++++++++++++++ .../server/crud/KtorWriteCRUDRepoRoutes.kt | 6 +- 3 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 repos/ktor/server/src/crud.yml diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteCrudRepoClient.kt index c28e553f629..76270d80271 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteCrudRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteCrudRepoClient.kt @@ -34,7 +34,7 @@ class KtorWriteCrudRepoClient ( override suspend fun update( values: List> ): List = httpClient.post( - buildStandardUrl(baseUrl, updateManyRouting) + buildStandardUrl(baseUrl, updateRouting) ) { updateSetup(values) }.updateBodyGetter() diff --git a/repos/ktor/server/src/crud.yml b/repos/ktor/server/src/crud.yml new file mode 100644 index 00000000000..e5544211a6a --- /dev/null +++ b/repos/ktor/server/src/crud.yml @@ -0,0 +1,196 @@ +swagger: "2.0" +info: + description: "This is a template for the CRUD repositories from [microutils](https://github.com/InsanusMokrassar/MicroUtils/tree/master/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud)" + version: "0.11.0" + title: "CRUD Repo" + contact: + email: "ovsyannikov.alexey95@gmail.com" +tags: + - name: "Read" + description: "Operations with `get` request in most cases" + - name: "Write" + description: "Operations with `post` request in most cases" + +parameters: + IdInQuery: + in: "query" + name: "id" + allOf: + - $ref: "#/definitions/Key" + IdsInBody: + in: "body" + name: "body" + type: array + items: + $ref: "#/definitions/Key" + NewValuesInBody: + in: "body" + name: "body" + type: array + allOf: + - $ref: "#/definitions/NewValues" + NewValuesWithIdsInBody: + in: "body" + name: "body" + type: array + items: + allOf: + - $ref: "#/definitions/Pair" + - properties: + first: + $ref: "#/definitions/Key" + second: + $ref: "#/definitions/NewValue" + PaginationInQueryPage: + in: "query" + type: integer + name: "ppage" + description: "Page of pagination" + required: false + PaginationInQuerySize: + in: "query" + type: integer + name: "psize" + description: "Size of each page in pagination" + required: false + + +definitions: + Key: + type: integer + description: "REWRITE THIS TYPE AS KEY IN SWAGGER FILE" + Value: + type: integer + description: "REWRITE THIS TYPE AS VALUE IN SWAGGER FILE" + Values: + type: array + items: + $ref: "#/definitions/Value" + NewValue: + type: integer + description: "REWRITE THIS TYPE AS NEW VALUE IN SWAGGER FILE" + Pair: + type: object + description: "Pair of objects" + properties: + first: + second: + NewValues: + type: array + items: + $ref: "#/definitions/NewValue" + Pagination: + type: object + properties: + page: + type: integer + description: "Page of pagination" + size: + type: integer + description: "Size of each page in pagination" + PaginationResult: + type: object + properties: + page: + type: integer + description: "Page of pagination" + pagesNumber: + type: integer + description: "Count of pages with the size from this pagination" + size: + type: integer + description: "Size of each page in pagination" + results: + type: array + description: "Array of all elements on that page. Size of pagination and size of array can be different and it can be interpreted like current page is the last one" + items: + type: object + +paths: + /getByPagination: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/PaginationInQueryPage" + - $ref: "#/parameters/PaginationInQuerySize" + responses: + "200": + description: "Pagination of elements" + schema: + allOf: + - $ref: "#/definitions/PaginationResult" + - properties: + results: + items: + $ref: "#/definitions/Value" + /getById: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/IdInQuery" + required: true + responses: + "200": + description: "Result object" + schema: + $ref: "#/definitions/Value" + "204": + description: "No value by id" + /contains: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/IdInQuery" + required: true + responses: + "200": + description: "Object with id availability in repo" + schema: + type: boolean + /count: + get: + tags: + - "Read" + responses: + "200": + description: "Amount of objects in repo" + schema: + type: integer + + + /create: + post: + tags: + - "Write" + parameters: + - $ref: "#/parameters/NewValuesInBody" + responses: + "200": + description: "Objects has been created and saved" + schema: + $ref: "#/definitions/Values" + /update: + post: + tags: + - "Write" + parameters: + - $ref: "#/parameters/NewValuesWithIdsInBody" + responses: + "200": + description: "Objects has been updated" + schema: + $ref: "#/definitions/Values" + /deleteById: + post: + tags: + - "Write" + parameters: + - $ref: "#/parameters/IdsInBody" + responses: + "200": + description: "Objects has been updated" + schema: + $ref: "#/definitions/Values" diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteCRUDRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteCRUDRepoRoutes.kt index 7a05ea287ca..368f928d141 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteCRUDRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteCRUDRepoRoutes.kt @@ -3,6 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.server.crud import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.repos.WriteCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.crud.* +import io.ktor.http.HttpStatusCode import io.ktor.server.application.call import io.ktor.server.request.receive import io.ktor.server.response.respond @@ -29,11 +30,12 @@ inline fun Date: Sat, 4 Jun 2022 16:47:22 +0600 Subject: [PATCH 10/14] add kv swagger template --- .../key_value/KtorReadKeyValueRepoClient.kt | 5 +- repos/ktor/{server/src => }/crud.yml | 11 +- repos/ktor/kv.yml | 196 ++++++++++++++++++ .../key_value/KtorReadKeyValueRepoRoutes.kt | 5 +- 4 files changed, 203 insertions(+), 14 deletions(-) rename repos/ktor/{server/src => }/crud.yml (94%) create mode 100644 repos/ktor/kv.yml diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt index ded3486007d..2171773504a 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt @@ -5,6 +5,7 @@ import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.* import dev.inmo.micro_utils.repos.ktor.common.containsRoute +import dev.inmo.micro_utils.repos.ktor.common.keyParameterName import dev.inmo.micro_utils.repos.ktor.common.key_value.* import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import io.ktor.client.HttpClient @@ -30,7 +31,7 @@ class KtorReadKeyValueRepoClient( baseUrl, getRoute, mapOf( - idParameterName to idSerializer(k) + keyParameterName to idSerializer(k) ) ) ) { @@ -41,7 +42,7 @@ class KtorReadKeyValueRepoClient( buildStandardUrl( baseUrl, containsRoute, - idParameterName to idSerializer(key) + keyParameterName to idSerializer(key) ) ) { contentType(contentType) diff --git a/repos/ktor/server/src/crud.yml b/repos/ktor/crud.yml similarity index 94% rename from repos/ktor/server/src/crud.yml rename to repos/ktor/crud.yml index e5544211a6a..d634dc87b9c 100644 --- a/repos/ktor/server/src/crud.yml +++ b/repos/ktor/crud.yml @@ -79,15 +79,6 @@ definitions: type: array items: $ref: "#/definitions/NewValue" - Pagination: - type: object - properties: - page: - type: integer - description: "Page of pagination" - size: - type: integer - description: "Size of each page in pagination" PaginationResult: type: object properties: @@ -116,7 +107,7 @@ paths: - $ref: "#/parameters/PaginationInQuerySize" responses: "200": - description: "Pagination of elements" + description: "Pagination with elements" schema: allOf: - $ref: "#/definitions/PaginationResult" diff --git a/repos/ktor/kv.yml b/repos/ktor/kv.yml new file mode 100644 index 00000000000..52e2c13650e --- /dev/null +++ b/repos/ktor/kv.yml @@ -0,0 +1,196 @@ +swagger: "2.0" +info: + description: "This is a template for the KeyValue repositories from [microutils](https://github.com/InsanusMokrassar/MicroUtils/tree/master/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value)" + version: "0.11.0" + title: "KeyValue Repo" + contact: + email: "ovsyannikov.alexey95@gmail.com" +tags: + - name: "Read" + description: "Operations with `get` request in most cases" + - name: "Write" + description: "Operations with `post` request in most cases" + +parameters: + KeyInQuery: + in: "query" + name: "key" + allOf: + - $ref: "#/definitions/Key" + KeysInBody: + in: "body" + name: "body" + type: array + items: + $ref: "#/definitions/Key" + ValuesInBody: + in: "body" + name: "body" + type: array + items: + $ref: "#/definitions/Value" + PaginationInQueryPage: + in: "query" + type: integer + name: "ppage" + description: "Page of pagination" + required: false + PaginationInQuerySize: + in: "query" + type: integer + name: "psize" + description: "Size of each page in pagination" + required: false + ReversedInQuery: + in: "query" + type: boolean + name: "reversed" + description: "If passed, will tell to reverse the result pages" + required: false + ValueInQuery: + in: "query" + name: "value" + allOf: + - $ref: "#/definitions/Value" + MapInBody: + in: "body" + name: "body" + allOf: + - $ref: "#/definitions/Map" + + +definitions: + Key: + type: integer + description: "REWRITE THIS TYPE AS KEY IN SWAGGER FILE" + Value: + type: integer + description: "REWRITE THIS TYPE AS VALUE IN SWAGGER FILE" + Map: + type: object + description: "Map of objects" + PaginationResult: + type: object + properties: + page: + type: integer + description: "Page of pagination" + pagesNumber: + type: integer + description: "Count of pages with the size from this pagination" + size: + type: integer + description: "Size of each page in pagination" + results: + type: array + description: "Array of all elements on that page. Size of pagination and size of array can be different and it can be interpreted like current page is the last one" + items: + type: object + +paths: + /get: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/KeyInQuery" + required: true + responses: + "200": + description: "Element by key" + schema: + $ref: "#/definitions/Value" + "204": + description: "No value by id" + /values: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/PaginationInQueryPage" + - $ref: "#/parameters/PaginationInQuerySize" + - $ref: "#/parameters/ReversedInQuery" + responses: + "200": + description: "Pagination with elements" + schema: + allOf: + - $ref: "#/definitions/PaginationResult" + - properties: + results: + items: + $ref: "#/definitions/Value" + /keys: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/PaginationInQueryPage" + - $ref: "#/parameters/PaginationInQuerySize" + - $ref: "#/parameters/ReversedInQuery" + - $ref: "#/parameters/ValueInQuery" + required: false + responses: + "200": + description: "Pagination with elements" + schema: + allOf: + - $ref: "#/definitions/PaginationResult" + - properties: + results: + items: + $ref: "#/definitions/Key" + /contains: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/KeyInQuery" + required: true + responses: + "200": + description: "Object with id availability in repo" + schema: + type: boolean + /count: + get: + tags: + - "Read" + responses: + "200": + description: "Amount of objects in repo" + schema: + type: integer + + + /set: + post: + tags: + - "Write" + parameters: + - allOf: + - $ref: "#/parameters/MapInBody" + - additionalProperties: + $ref: "#/definitions/Value" + description: "Map with new elements to set. Use keys as a keys of this map" + responses: + "200": + description: "Will return 200 if everything has been completed ok" + /unset: + post: + tags: + - "Write" + parameters: + - $ref: "#/parameters/KeysInBody" + responses: + "200": + description: "Objects with keys from body has been unset" + /unsetWithValues: + post: + tags: + - "Write" + parameters: + - $ref: "#/parameters/ValuesInBody" + responses: + "200": + description: "Objects with values from body has been unset" diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt index 67eec8bda59..9b2d2f98e6a 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt @@ -8,6 +8,7 @@ import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.* import dev.inmo.micro_utils.repos.ktor.common.containsRoute import dev.inmo.micro_utils.repos.ktor.common.countRoute +import dev.inmo.micro_utils.repos.ktor.common.keyParameterName import dev.inmo.micro_utils.repos.ktor.common.key_value.* import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName import io.ktor.http.* @@ -30,7 +31,7 @@ inline fun Route.configureReadKeyValueRepoRoutes ( get(getRoute) { val key = idDeserializer( - call.getQueryParameterOrSendError(idParameterName) ?: return@get + call.getQueryParameterOrSendError(keyParameterName) ?: return@get ) originalRepo.get(key) ?.let { @@ -63,7 +64,7 @@ inline fun Route.configureReadKeyValueRepoRoutes ( get(containsRoute) { val key = idDeserializer( - call.getQueryParameterOrSendError(idParameterName) ?: return@get + call.getQueryParameterOrSendError(keyParameterName) ?: return@get ) call.respond(originalRepo.contains(key)) From 78bd3b9853d61baa9207902ad1c7ec3685322647 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Jun 2022 16:52:01 +0600 Subject: [PATCH 11/14] replaces of new API of ktor --- .../client/{key_value => key/value}/KtorKeyValueRepoClient.kt | 2 +- .../{key_value => key/value}/KtorReadKeyValueRepoClient.kt | 2 +- .../{key_value => key/value}/KtorWriteKeyValueRepoClient.kt | 2 +- .../{one_to_many => key/values}/KtorKeyValuesRepoClient.kt | 2 +- .../{one_to_many => key/values}/KtorReadKeyValuesRepoClient.kt | 2 +- .../{one_to_many => key/values}/KtorWriteKeyValuesRepoClient.kt | 2 +- repos/ktor/common/src/jvmTest/kotlin/KVTests.kt | 2 +- repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt | 2 +- .../server/{key_value => key/value}/KtorKeyValueRepoRoutes.kt | 0 .../{key_value => key/value}/KtorReadKeyValueRepoRoutes.kt | 0 .../{key_value => key/value}/KtorWriteKeyValueRepoRoutes.kt | 0 .../{one_to_many => key/values}/KtorKeyValuesRepoRoutes.kt | 0 .../{one_to_many => key/values}/KtorReadKeyValuesRepoRoutes.kt | 0 .../{one_to_many => key/values}/KtorWriteKeyValuesRepoRoutes.kt | 0 14 files changed, 8 insertions(+), 8 deletions(-) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/{key_value => key/value}/KtorKeyValueRepoClient.kt (97%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/{key_value => key/value}/KtorReadKeyValueRepoClient.kt (98%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/{key_value => key/value}/KtorWriteKeyValueRepoClient.kt (97%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/{one_to_many => key/values}/KtorKeyValuesRepoClient.kt (97%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/{one_to_many => key/values}/KtorReadKeyValuesRepoClient.kt (98%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/{one_to_many => key/values}/KtorWriteKeyValuesRepoClient.kt (98%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/{key_value => key/value}/KtorKeyValueRepoRoutes.kt (100%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/{key_value => key/value}/KtorReadKeyValueRepoRoutes.kt (100%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/{key_value => key/value}/KtorWriteKeyValueRepoRoutes.kt (100%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/{one_to_many => key/values}/KtorKeyValuesRepoRoutes.kt (100%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/{one_to_many => key/values}/KtorReadKeyValuesRepoRoutes.kt (100%) rename repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/{one_to_many => key/values}/KtorWriteKeyValuesRepoRoutes.kt (100%) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorKeyValueRepoClient.kt similarity index 97% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorKeyValueRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorKeyValueRepoClient.kt index dc7d6f0b0b7..b0f6aca4ad3 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorKeyValueRepoClient.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.client.key_value +package dev.inmo.micro_utils.repos.ktor.client.key.value import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.* diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt similarity index 98% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt index 2171773504a..c8c40e7ce30 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.client.key_value +package dev.inmo.micro_utils.repos.ktor.client.key.value import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorWriteKeyValueRepoClient.kt similarity index 97% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteKeyValueRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorWriteKeyValueRepoClient.kt index 398e94de811..b8c2ca06f5e 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorWriteKeyValueRepoClient.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.client.key_value +package dev.inmo.micro_utils.repos.ktor.client.key.value import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow import dev.inmo.micro_utils.ktor.client.throwOnUnsuccess diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorKeyValuesRepoClient.kt similarity index 97% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorKeyValuesRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorKeyValuesRepoClient.kt index a7863a79560..3913fecd742 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorKeyValuesRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorKeyValuesRepoClient.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.client.one_to_many +package dev.inmo.micro_utils.repos.ktor.client.key.values import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.* diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorReadKeyValuesRepoClient.kt similarity index 98% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadKeyValuesRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorReadKeyValuesRepoClient.kt index 3a32621a230..f2be8e8759c 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadKeyValuesRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorReadKeyValuesRepoClient.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.client.one_to_many +package dev.inmo.micro_utils.repos.ktor.client.key.values import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.pagination.* diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorWriteKeyValuesRepoClient.kt similarity index 98% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteKeyValuesRepoClient.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorWriteKeyValuesRepoClient.kt index 8b087ae452f..900ae7612cc 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteKeyValuesRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorWriteKeyValuesRepoClient.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.client.one_to_many +package dev.inmo.micro_utils.repos.ktor.client.key.values import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow import dev.inmo.micro_utils.ktor.client.throwOnUnsuccess diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt index 8ffb2185f3e..e50ade397b5 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt @@ -1,7 +1,7 @@ import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.* -import dev.inmo.micro_utils.repos.ktor.client.key_value.KtorKeyValueRepoClient +import dev.inmo.micro_utils.repos.ktor.client.key.value.KtorKeyValueRepoClient import dev.inmo.micro_utils.repos.ktor.server.key_value.configureKeyValueRepoRoutes import io.ktor.client.HttpClient import io.ktor.client.plugins.logging.Logging diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt index 1db65f3e605..94d8daba786 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt @@ -1,7 +1,7 @@ import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.* -import dev.inmo.micro_utils.repos.ktor.client.one_to_many.KtorKeyValuesRepoClient +import dev.inmo.micro_utils.repos.ktor.client.key.values.KtorKeyValuesRepoClient import dev.inmo.micro_utils.repos.ktor.server.one_to_many.configureKeyValuesRepoRoutes import io.ktor.client.HttpClient import io.ktor.client.plugins.logging.Logging diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorKeyValueRepoRoutes.kt similarity index 100% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorKeyValueRepoRoutes.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorKeyValueRepoRoutes.kt diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt similarity index 100% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorReadKeyValueRepoRoutes.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorWriteKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorWriteKeyValueRepoRoutes.kt similarity index 100% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorWriteKeyValueRepoRoutes.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorWriteKeyValueRepoRoutes.kt diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorKeyValuesRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt similarity index 100% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorKeyValuesRepoRoutes.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorReadKeyValuesRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt similarity index 100% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorReadKeyValuesRepoRoutes.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorWriteKeyValuesRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorWriteKeyValuesRepoRoutes.kt similarity index 100% rename from repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/KtorWriteKeyValuesRepoRoutes.kt rename to repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorWriteKeyValuesRepoRoutes.kt From f705020aaa85027da2c4657b39b59e56be5af875 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Jun 2022 16:55:23 +0600 Subject: [PATCH 12/14] fixes in packages --- repos/ktor/common/src/jvmTest/kotlin/KVTests.kt | 2 +- repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt | 2 +- .../repos/ktor/server/key/value/KtorKeyValueRepoRoutes.kt | 2 +- .../repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt | 2 +- .../repos/ktor/server/key/value/KtorWriteKeyValueRepoRoutes.kt | 2 +- .../repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt | 2 +- .../repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt | 2 +- .../ktor/server/key/values/KtorWriteKeyValuesRepoRoutes.kt | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt index e50ade397b5..76702d189a2 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt @@ -2,7 +2,7 @@ import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.ktor.client.key.value.KtorKeyValueRepoClient -import dev.inmo.micro_utils.repos.ktor.server.key_value.configureKeyValueRepoRoutes +import dev.inmo.micro_utils.repos.ktor.server.key.value.configureKeyValueRepoRoutes import io.ktor.client.HttpClient import io.ktor.client.plugins.logging.Logging import io.ktor.http.ContentType diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt index 94d8daba786..de3ff95484a 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt @@ -2,7 +2,7 @@ import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.ktor.client.key.values.KtorKeyValuesRepoClient -import dev.inmo.micro_utils.repos.ktor.server.one_to_many.configureKeyValuesRepoRoutes +import dev.inmo.micro_utils.repos.ktor.server.key.values.configureKeyValuesRepoRoutes import io.ktor.client.HttpClient import io.ktor.client.plugins.logging.Logging import io.ktor.http.ContentType diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorKeyValueRepoRoutes.kt index 099a4a9b963..1ff64d93a00 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorKeyValueRepoRoutes.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.server.key_value +package dev.inmo.micro_utils.repos.ktor.server.key.value import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.KeyValueRepo diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt index 9b2d2f98e6a..b0b13738bbe 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.server.key_value +package dev.inmo.micro_utils.repos.ktor.server.key.value import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.ktor.server.* diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorWriteKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorWriteKeyValueRepoRoutes.kt index 2ed9a2dcc4f..78e39e8ff66 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorWriteKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorWriteKeyValueRepoRoutes.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.server.key_value +package dev.inmo.micro_utils.repos.ktor.server.key.value import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.repos.WriteKeyValueRepo diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt index c0aaa389810..6fd6eff3f01 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.server.one_to_many +package dev.inmo.micro_utils.repos.ktor.server.key.values import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.repos.* diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt index 80ac3e7dfd2..73e98c29d47 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.server.one_to_many +package dev.inmo.micro_utils.repos.ktor.server.key.values import dev.inmo.micro_utils.ktor.common.* import dev.inmo.micro_utils.ktor.server.* diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorWriteKeyValuesRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorWriteKeyValuesRepoRoutes.kt index 440821ed17c..0a37c28f67c 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorWriteKeyValuesRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorWriteKeyValuesRepoRoutes.kt @@ -1,4 +1,4 @@ -package dev.inmo.micro_utils.repos.ktor.server.one_to_many +package dev.inmo.micro_utils.repos.ktor.server.key.values import dev.inmo.micro_utils.ktor.server.* import dev.inmo.micro_utils.repos.WriteKeyValuesRepo From 0a8e0f617822fc3d96108842dc55e3bec9e04084 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 4 Jun 2022 17:12:05 +0600 Subject: [PATCH 13/14] complete swagger templates --- repos/ktor/kv.yml | 2 +- repos/ktor/kvs.yml | 222 ++++++++++++++++++ .../key/values/KtorKeyValuesRepoRoutes.kt | 12 +- .../key/values/KtorReadKeyValuesRepoRoutes.kt | 19 +- 4 files changed, 239 insertions(+), 16 deletions(-) create mode 100644 repos/ktor/kvs.yml diff --git a/repos/ktor/kv.yml b/repos/ktor/kv.yml index 52e2c13650e..176d40226e0 100644 --- a/repos/ktor/kv.yml +++ b/repos/ktor/kv.yml @@ -1,6 +1,6 @@ swagger: "2.0" info: - description: "This is a template for the KeyValue repositories from [microutils](https://github.com/InsanusMokrassar/MicroUtils/tree/master/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value)" + description: "This is a template for the KeyValue repositories from [microutils](https://github.com/InsanusMokrassar/MicroUtils/tree/master/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value)" version: "0.11.0" title: "KeyValue Repo" contact: diff --git a/repos/ktor/kvs.yml b/repos/ktor/kvs.yml new file mode 100644 index 00000000000..6f61a058526 --- /dev/null +++ b/repos/ktor/kvs.yml @@ -0,0 +1,222 @@ +swagger: "2.0" +info: + description: "This is a template for the KeyValues repositories from [microutils](https://github.com/InsanusMokrassar/MicroUtils/tree/master/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values)" + version: "0.11.0" + title: "KeyValues Repo" + contact: + email: "ovsyannikov.alexey95@gmail.com" +tags: + - name: "Read" + description: "Operations with `get` request in most cases" + - name: "Write" + description: "Operations with `post` request in most cases" + +parameters: + KeyInQuery: + in: "query" + name: "key" + allOf: + - $ref: "#/definitions/Key" + KeyInBody: + in: "body" + name: "body" + allOf: + - $ref: "#/definitions/Key" + KeysInBody: + in: "body" + name: "body" + type: array + items: + $ref: "#/definitions/Key" + ValuesInBody: + in: "body" + name: "body" + type: array + items: + $ref: "#/definitions/Value" + PaginationInQueryPage: + in: "query" + type: integer + name: "ppage" + description: "Page of pagination" + required: false + PaginationInQuerySize: + in: "query" + type: integer + name: "psize" + description: "Size of each page in pagination" + required: false + ReversedInQuery: + in: "query" + type: boolean + name: "reversed" + description: "If passed, will tell to reverse the result pages" + required: false + ValueInQuery: + in: "query" + name: "value" + allOf: + - $ref: "#/definitions/Value" + ValueInBody: + in: "body" + name: "body" + allOf: + - $ref: "#/definitions/Value" + MapInBody: + in: "body" + name: "body" + allOf: + - $ref: "#/definitions/Map" + + +definitions: + Key: + type: integer + description: "REWRITE THIS TYPE AS KEY IN SWAGGER FILE" + Value: + type: integer + description: "REWRITE THIS TYPE AS VALUE IN SWAGGER FILE" + Map: + type: object + description: "Map of objects" + PaginationResult: + type: object + properties: + page: + type: integer + description: "Page of pagination" + pagesNumber: + type: integer + description: "Count of pages with the size from this pagination" + size: + type: integer + description: "Size of each page in pagination" + results: + type: array + description: "Array of all elements on that page. Size of pagination and size of array can be different and it can be interpreted like current page is the last one" + items: + type: object + +paths: + /get: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/KeyInQuery" + required: true + - $ref: "#/parameters/PaginationInQueryPage" + - $ref: "#/parameters/PaginationInQuerySize" + - $ref: "#/parameters/ReversedInQuery" + responses: + "200": + description: "Elements by query" + schema: + allOf: + - $ref: "#/definitions/PaginationResult" + - properties: + results: + items: + $ref: "#/definitions/Value" + /keys: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/PaginationInQueryPage" + - $ref: "#/parameters/PaginationInQuerySize" + - $ref: "#/parameters/ReversedInQuery" + - $ref: "#/parameters/ValueInQuery" + required: false + responses: + "200": + description: "Pagination with elements" + schema: + allOf: + - $ref: "#/definitions/PaginationResult" + - properties: + results: + items: + $ref: "#/definitions/Key" + /contains: + get: + tags: + - "Read" + parameters: + - $ref: "#/parameters/KeyInQuery" + required: true + - $ref: "#/parameters/ValueInQuery" + required: false + responses: + "200": + description: "Object with key and optionally availability in repo" + schema: + type: boolean + /count: + get: + tags: + - "Read" + responses: + "200": + description: "Amount of objects in repo" + schema: + type: integer + + + /add: + post: + tags: + - "Write" + parameters: + - allOf: + - $ref: "#/parameters/MapInBody" + - additionalProperties: + $ref: "#/definitions/Value" + description: "Map with new elements to add. Use keys as a keys of this map. That values will be added to the end of current data by their keys" + responses: + "200": + description: "Will return 200 if everything has been completed ok" + /set: + post: + tags: + - "Write" + parameters: + - allOf: + - $ref: "#/parameters/MapInBody" + - additionalProperties: + $ref: "#/definitions/Value" + description: "Map with new elements to set. Use keys as a keys of this map. That values will overwrite all exists data by their keys" + responses: + "200": + description: "Will return 200 if everything has been completed ok" + /remove: + post: + tags: + - "Write" + parameters: + - allOf: + - $ref: "#/parameters/MapInBody" + - additionalProperties: + $ref: "#/definitions/Value" + description: "Map with data to remove. Removing will be processed by each value for its key" + responses: + "200": + description: "Objects with keys and values from body has been unset" + /clear: + post: + tags: + - "Write" + parameters: + - $ref: "#/parameters/KeyInBody" + responses: + "200": + description: "Data with corresponding key has been removed" + /clearWithValues: + post: + tags: + - "Write" + parameters: + - $ref: "#/parameters/ValueInBody" + responses: + "200": + description: "Will remove value from all keys data" diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt index 6fd6eff3f01..541ca5fd7ce 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt @@ -8,22 +8,22 @@ import kotlinx.serialization.* inline fun Route.configureKeyValuesRepoRoutes ( originalRepo: KeyValuesRepo, - noinline idDeserializer: suspend (String) -> Key, + noinline keyDeserializer: suspend (String) -> Key, noinline valueDeserializer: suspend (String) -> Value ) { - configureReadKeyValuesRepoRoutes(originalRepo, idDeserializer, valueDeserializer) + configureReadKeyValuesRepoRoutes(originalRepo, keyDeserializer, valueDeserializer) configureWriteKeyValuesRepoRoutes(originalRepo) } inline fun Route.configureKeyValuesRepoRoutes( originalRepo: KeyValuesRepo, - idsSerializer: DeserializationStrategy, + keySerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: StringFormat ) = configureKeyValuesRepoRoutes( originalRepo, { - serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) + serialFormat.decodeFromString(keySerializer, it.decodeURLQueryComponent()) }, { serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent()) @@ -32,13 +32,13 @@ inline fun Route.configureKeyValuesRepo inline fun Route.configureKeyValuesRepoRoutes( originalRepo: KeyValuesRepo, - idsSerializer: DeserializationStrategy, + keySerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: BinaryFormat ) = configureKeyValuesRepoRoutes( originalRepo, { - serialFormat.decodeHex(idsSerializer, it) + serialFormat.decodeHex(keySerializer, it) }, { serialFormat.decodeHex(valueSerializer, it) diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt index 73e98c29d47..555c9d5404a 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt @@ -7,6 +7,7 @@ import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.repos.ReadKeyValuesRepo import dev.inmo.micro_utils.repos.ktor.common.* import dev.inmo.micro_utils.repos.ktor.common.containsRoute +import dev.inmo.micro_utils.repos.ktor.common.countRoute import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.http.* import io.ktor.server.application.call @@ -20,14 +21,14 @@ import kotlinx.serialization.* @OptIn(InternalAPI::class) inline fun Route.configureReadKeyValuesRepoRoutes ( originalRepo: ReadKeyValuesRepo, - noinline idDeserializer: suspend (String) -> Key, + noinline keyDeserializer: suspend (String) -> Key, noinline valueDeserializer: suspend (String) -> Value ) { val paginationWithValuesTypeInfo = typeInfo>() val paginationWithKeysTypeInfo = typeInfo>() get(getRoute) { - val key = idDeserializer( + val key = keyDeserializer( call.getQueryParameterOrSendError(keyParameterName) ?: return@get ) val pagination = call.request.queryParameters.extractPagination @@ -53,7 +54,7 @@ inline fun Route.configureReadKeyValuesRepoRoutes ( } get(containsRoute) { - val key = idDeserializer( + val key = keyDeserializer( call.getQueryParameterOrSendError(keyParameterName) ?: return@get ) val value = call.getQueryParameter(valueParameterName) ?.let { @@ -65,9 +66,9 @@ inline fun Route.configureReadKeyValuesRepoRoutes ( ) } - get(dev.inmo.micro_utils.repos.ktor.common.countRoute) { + get(countRoute) { val id = call.getQueryParameter(keyParameterName) ?.let { - idDeserializer(it) + keyDeserializer(it) } call.respond( id ?.let { originalRepo.count(it) } ?: originalRepo.count() @@ -77,13 +78,13 @@ inline fun Route.configureReadKeyValuesRepoRoutes ( inline fun Route.configureReadKeyValuesRepoRoutes( originalRepo: ReadKeyValuesRepo, - idsSerializer: DeserializationStrategy, + keySerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: StringFormat ) = configureReadKeyValuesRepoRoutes( originalRepo, { - serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) + serialFormat.decodeFromString(keySerializer, it.decodeURLQueryComponent()) }, { serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent()) @@ -92,13 +93,13 @@ inline fun Route.configureReadKeyValuesRepoRoutes( inline fun Route.configureReadKeyValuesRepoRoutes( originalRepo: ReadKeyValuesRepo, - idsSerializer: DeserializationStrategy, + keySerializer: DeserializationStrategy, valueSerializer: DeserializationStrategy, serialFormat: BinaryFormat ) = configureReadKeyValuesRepoRoutes( originalRepo, { - serialFormat.decodeHex(idsSerializer, it) + serialFormat.decodeHex(keySerializer, it) }, { serialFormat.decodeHex(valueSerializer, it) From 62df81bb4ee8711b3afb6035a2f2840a1cefd697 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 5 Jun 2022 00:34:31 +0600 Subject: [PATCH 14/14] update uuid and partially fill changelog --- CHANGELOG.md | 9 ++++++++- gradle/libs.versions.toml | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33eb21d63d9..d47442e0c81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,18 @@ ## 0.11.0 +* `Versions` + * `UUID`: `0.4.0` -> `0.4.1` * `Ktor` - * + * `Client`: + * New extension fun `HttpResponse#throwOnUnsuccess` + * All old functions, classes and extensions has been rewritten with new ktor-way with types info and keeping `ContentNegotiation` in mind + * `Server`: + * All old functions, classes and extensions has been rewritten with new ktor-way with types info and keeping `ContentNegotiation` in mind * `Repos` * `Ktor`: * Fully rewritten work with all declared repositories + * All old functions, classes and extensions has been rewritten with new ktor-way with types info and keeping `ContentNegotiation` in mind ## 0.10.8 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 12aa141beb1..ed05a9933cd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,7 +9,7 @@ jb-exposed = "0.38.2" jb-dokka = "1.6.21" klock = "2.7.0" -uuid = "0.4.0" +uuid = "0.4.1" ktor = "2.0.2"