diff --git a/CHANGELOG.md b/CHANGELOG.md index 1716489a86a..d47442e0c81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,20 @@ # Changelog +## 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 * `Common` 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 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 145cf936447..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" @@ -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/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/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..4ee959eceb3 --- /dev/null +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/NewFlowsWebsockets.kt @@ -0,0 +1,55 @@ +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 io.ktor.websocket.serialization.sendSerializedBase +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, + noinline 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/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/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/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/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 65% 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 df5bafa86d5..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,22 +101,29 @@ interface OneToManyKeyValueRepo : ReadOneToManyKeyValueRepo = OneToManyKeyValueRepo +typealias OneToManyKeyValueRepo = KeyValuesRepo -suspend inline fun WriteOneToManyKeyValueRepo.remove( +class DelegateBasedKeyValuesRepo( + readDelegate: ReadKeyValuesRepo, + writeDelegate: WriteKeyValuesRepo +) : KeyValuesRepo, + ReadKeyValuesRepo by readDelegate, + WriteKeyValuesRepo by writeDelegate + +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/StandartCRUDRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt index df64fc47de7..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,18 +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 \ No newline at end of file +interface CRUDRepo : ReadCRUDRepo, + WriteCRUDRepo +typealias StandardCRUDRepo = CRUDRepo + +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/StandartKeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt index 90ad3a0b92f..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,4 +49,11 @@ interface StandardKeyValueRepo : ReadStandardKeyValueRepo = StandardKeyValueRepo +typealias StandardKeyValueRepo = KeyValueRepo + +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/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/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/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/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/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/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/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/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/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/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/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/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/MapCRUDRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt index 1d605a4c77b..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,25 +68,30 @@ abstract class WriteMapCRUDRepo( abstract class MapCRUDRepo( map: MutableMap -) : StandardCRUDRepo, - ReadStandardCRUDRepo by ReadMapCRUDRepo(map), +) : CRUDRepo, + ReadCRUDRepo by ReadMapCRUDRepo(map), WriteMapCRUDRepo(map) 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/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..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 @@ -78,19 +78,19 @@ 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) } } } 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/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 71% 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 08ebbb45f90..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() @@ -80,6 +84,10 @@ class MapWriteOneToManyKeyValueRepo( _onValueRemoved.emit(k to v) } } + if (map[k] ?.isEmpty() == true) { + map.remove(k) + _onDataCleared.emit(k) + } } } @@ -94,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/crud/KtorCRUDRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorCRUDRepoClient.kt new file mode 100644 index 00000000000..7bb55318f6d --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorCRUDRepoClient.kt @@ -0,0 +1,121 @@ +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.http.ContentType +import io.ktor.util.reflect.TypeInfo +import io.ktor.util.reflect.typeInfo +import kotlinx.serialization.* + +class KtorCRUDRepoClient ( + readDelegate: ReadCRUDRepo, + writeDelegate: WriteCRUDRepo +) : CRUDRepo by DelegateBasedCRUDRepo( + readDelegate, + writeDelegate +) { + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + objectTypeInfo: TypeInfo, + contentType: ContentType, + noinline idSerializer: suspend (IdType) -> String + ) = KtorCRUDRepoClient( + KtorReadCRUDRepoClient( + baseUrl, + httpClient, + objectTypeInfo, + contentType, + idSerializer + ), + KtorWriteCrudRepoClient( + baseUrl, + httpClient, + contentType + ) + ) + + inline operator fun invoke( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + objectTypeInfo: TypeInfo, + contentType: ContentType, + noinline idSerializer: suspend (IdType) -> String + ) = KtorCRUDRepoClient( + buildStandardUrl(baseUrl, subpart), + httpClient, + objectTypeInfo, + contentType, + idSerializer + ) + } +} + + +inline fun KtorCRUDRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + noinline idSerializer: suspend (IdType) -> String +) = KtorCRUDRepoClient( + baseUrl, + httpClient, + typeInfo(), + contentType, + idSerializer +) + +inline fun KtorCRUDRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: StringFormat, + contentType: ContentType, +) = KtorCRUDRepoClient(baseUrl, httpClient, contentType) { + serialFormat.encodeToString(idsSerializer, it) +} + +inline fun KtorCRUDRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: BinaryFormat, + contentType: ContentType, +) = KtorCRUDRepoClient(baseUrl, httpClient, contentType) { + serialFormat.encodeHex(idsSerializer, it) +} + + +inline fun KtorCRUDRepoClient( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + contentType: ContentType, + noinline idSerializer: suspend (IdType) -> String +) = KtorCRUDRepoClient( + buildStandardUrl(baseUrl, subpart), + httpClient, + contentType, + idSerializer +) + +inline fun KtorCRUDRepoClient( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: StringFormat, + contentType: ContentType, +) = KtorCRUDRepoClient(buildStandardUrl(baseUrl, subpart), httpClient, idsSerializer, serialFormat, contentType) + +inline fun KtorCRUDRepoClient( + baseUrl: String, + subpart: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: BinaryFormat, + contentType: 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/KtorReadCRUDRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt new file mode 100644 index 00000000000..5c49eafa7b0 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt @@ -0,0 +1,95 @@ +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.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.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 KtorReadCRUDRepoClient ( + private val baseUrl: String, + private val httpClient: HttpClient, + private val objectType: TypeInfo, + private val contentType: ContentType, + private val idSerializer: suspend (IdType) -> String +) : ReadCRUDRepo { + override suspend fun getByPagination(pagination: Pagination): PaginationResult = httpClient.get( + buildStandardUrl(baseUrl, getByPaginationRouting, pagination.asUrlQueryParts) + ) { + contentType(contentType) + }.body() + + override suspend fun getById(id: IdType): ObjectType? = httpClient.get( + buildStandardUrl( + baseUrl, + getByIdRouting, + mapOf( + idParameterName to idSerializer(id) + ) + ) + ) { + contentType(contentType) + }.takeIf { it.status != HttpStatusCode.NoContent } ?.body(objectType) + + override suspend fun contains(id: IdType): Boolean = httpClient.get( + buildStandardUrl( + baseUrl, + containsRouting, + mapOf( + idParameterName to idSerializer(id) + ) + ) + ) { + contentType(contentType) + }.body() + + override suspend fun count(): Long = httpClient.get( + buildStandardUrl( + baseUrl, + countRouting + ) + ) { + contentType(contentType) + }.body() +} + +inline fun KtorReadCRUDRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + noinline idSerializer: suspend (IdType) -> String +) = KtorReadCRUDRepoClient( + baseUrl, + httpClient, + typeInfo(), + contentType, + idSerializer +) + +inline fun KtorReadCRUDRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: StringFormat, + contentType: ContentType, +) = KtorReadCRUDRepoClient(baseUrl, httpClient, contentType) { + serialFormat.encodeToString(idsSerializer, it) +} + +inline fun KtorReadCRUDRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + serialFormat: BinaryFormat, + contentType: 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 470249b0039..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,19 +3,22 @@ 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 import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer +@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( @@ -38,9 +41,7 @@ class KtorReadStandardCrudRepo ( buildStandardUrl( baseUrl, getByIdRouting, - mapOf( - "id" to unifiedRequester.encodeUrlQueryValue(idsSerializer, id) - ) + idParameterName to unifiedRequester.encodeUrlQueryValue(idsSerializer, id) ), objectsSerializerNullable ) @@ -49,9 +50,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/KtorStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorStandardCrudRepo.kt index 19561cd92eb..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,6 +7,7 @@ import dev.inmo.micro_utils.repos.* import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer +@Deprecated("Use KtorCRUDRepoClient instead") class KtorStandardCrudRepo ( baseUrl: String, baseSubpart: String, @@ -15,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/KtorWriteCrudRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteCrudRepoClient.kt new file mode 100644 index 00000000000..76270d80271 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorWriteCrudRepoClient.kt @@ -0,0 +1,85 @@ +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.WriteCRUDRepo +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 kotlinx.coroutines.flow.Flow + +class KtorWriteCrudRepoClient ( + private val baseUrl: String, + 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 +) : WriteCRUDRepo { + override suspend fun create(values: List): List = httpClient.post( + buildStandardUrl(baseUrl, createRouting) + ) { + createSetup(values) + }.createBodyGetter() + + override suspend fun update( + values: List> + ): List = httpClient.post( + buildStandardUrl(baseUrl, updateRouting) + ) { + 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( + buildStandardUrl(baseUrl, deleteByIdRouting) + ) { + deleteByIdSetup(ids) + }.throwOnUnsuccess { "Unable to delete $ids" } + } + + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType + ) = KtorWriteCrudRepoClient( + 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() } + ) + } +} 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..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,13 +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 KtorWriteCRUDRepoClient instead") class KtorWriteStandardCrudRepo ( private val baseUrl: String, private val unifiedRequester: UnifiedRequester, @@ -17,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/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 new file mode 100644 index 00000000000..b0f6aca4ad3 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorKeyValueRepoClient.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 KtorKeyValueRepoClient ( + readDelegate: ReadKeyValueRepo, + writeDelegate: WriteKeyValueRepo +) : KeyValueRepo by DelegateBasedKeyValueRepo( + readDelegate, + writeDelegate +) { + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + noinline idSerializer: suspend (Key) -> String, + noinline valueSerializer: suspend (Value) -> String + ) = KtorKeyValueRepoClient( + KtorReadKeyValueRepoClient( + baseUrl, httpClient, contentType, idSerializer, valueSerializer + ), + KtorWriteKeyValueRepoClient( + 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 + ) = KtorKeyValueRepoClient( + buildStandardUrl(baseUrl, subpart), + httpClient, + contentType, + idSerializer, + valueSerializer + ) + } +} + +inline fun KtorKeyValueRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + idSerializer: SerializationStrategy, + valueSerializer: SerializationStrategy, + serialFormat: StringFormat, +) = KtorKeyValueRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeToString(idSerializer, it).encodeURLQueryComponent() + } +) { + serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() +} + +inline fun KtorKeyValueRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + idSerializer: SerializationStrategy, + valueSerializer: SerializationStrategy, + serialFormat: BinaryFormat, +) = KtorKeyValueRepoClient( + 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/KtorReadKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt new file mode 100644 index 00000000000..c8c40e7ce30 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.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.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 +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 KtorReadKeyValueRepoClient( + 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 +) : ReadKeyValueRepo { + override suspend fun get(k: Key): Value? = httpClient.get( + buildStandardUrl( + baseUrl, + getRoute, + mapOf( + keyParameterName to idSerializer(k) + ) + ) + ) { + contentType(contentType) + }.takeIf { it.status != HttpStatusCode.NoContent } ?.body(objectType) + + override suspend fun contains(key: Key): Boolean = httpClient.get( + buildStandardUrl( + baseUrl, + containsRoute, + keyParameterName 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, + dev.inmo.micro_utils.repos.ktor.common.countRoute + ) + ) { + contentType(contentType) + }.body() +} + +inline fun KtorReadKeyValueRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + noinline idSerializer: suspend (Key) -> String, + noinline valueSerializer: suspend (Value) -> String +) = KtorReadKeyValueRepoClient( + baseUrl, + httpClient, + contentType, + typeInfo(), + typeInfo>(), + typeInfo>(), + idSerializer, + valueSerializer +) + +inline fun KtorReadKeyValueRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: StringFormat, + contentType: ContentType, +) = KtorReadKeyValueRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeToString(idsSerializer, it).encodeURLQueryComponent() + } +) { + serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() +} + +inline fun KtorReadKeyValueRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + valuesSerializer: KSerializer, + serialFormat: BinaryFormat, + contentType: ContentType, +) = KtorReadKeyValueRepoClient( + 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/KtorWriteKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorWriteKeyValueRepoClient.kt new file mode 100644 index 00000000000..b8c2ca06f5e --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorWriteKeyValueRepoClient.kt @@ -0,0 +1,79 @@ +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.WriteKeyValueRepo +import dev.inmo.micro_utils.repos.ktor.common.key_value.* +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 KtorWriteKeyValueRepoClient( + 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 +) : WriteKeyValueRepo { + @OptIn(InternalAPI::class) + override suspend fun unsetWithValues(toUnset: List) { + httpClient.post( + buildStandardUrl(baseUrl, unsetWithValuesRoute) + ) { + body = toUnset + bodyType = objectsListTypeInfo + contentType(contentType) + }.throwOnUnsuccess { "Unable to unset data with values $toUnset" } + } + + @OptIn(InternalAPI::class) + override suspend fun unset(toUnset: List) { + httpClient.post( + buildStandardUrl(baseUrl, unsetRoute) + ) { + body = toUnset + bodyType = idsListTypeInfo + contentType(contentType) + }.throwOnUnsuccess { "Unable to unset $toUnset" } + } + + @OptIn(InternalAPI::class) + override suspend fun set(toSet: Map) { + httpClient.post( + buildStandardUrl(baseUrl, setRoute) + ) { + body = toSet + bodyType = idsToObjectsMapTypeInfo + contentType(contentType) + }.throwOnUnsuccess { "Unable to set $toSet" } + } + + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType + ) = KtorWriteKeyValueRepoClient( + baseUrl, + httpClient, + contentType, + httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, onNewValueRoute), + ), + httpClient.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, onValueRemovedRoute), + ), + typeInfo>(), + typeInfo>(), + typeInfo>() + ) + } +} diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorKeyValuesRepoClient.kt new file mode 100644 index 00000000000..3913fecd742 --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorKeyValuesRepoClient.kt @@ -0,0 +1,89 @@ +package dev.inmo.micro_utils.repos.ktor.client.key.values + +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 KtorKeyValuesRepoClient ( + readDelegate: ReadKeyValuesRepo, + writeDelegate: WriteKeyValuesRepo +) : KeyValuesRepo by DelegateBasedKeyValuesRepo( + readDelegate, + writeDelegate +) { + companion object { + inline operator fun invoke( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + noinline keySerializer: suspend (Key) -> String, + noinline valueSerializer: suspend (Value) -> String + ) = KtorKeyValuesRepoClient( + KtorReadKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + keySerializer, + valueSerializer + ), + KtorWriteKeyValuesRepoClient( + 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 + ) = KtorKeyValuesRepoClient( + buildStandardUrl(baseUrl, subpart), + httpClient, + contentType, + keySerializer, + valueSerializer + ) + } +} + +inline fun KtorKeyValuesRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + keySerializer: SerializationStrategy, + valueSerializer: SerializationStrategy, + serialFormat: StringFormat, +) = KtorKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeToString(keySerializer, it).encodeURLQueryComponent() + } +) { + serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() +} + +inline fun KtorKeyValuesRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + keySerializer: SerializationStrategy, + valueSerializer: SerializationStrategy, + serialFormat: BinaryFormat, +) = KtorKeyValuesRepoClient( + 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/key/values/KtorReadKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorReadKeyValuesRepoClient.kt new file mode 100644 index 00000000000..f2be8e8759c --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorReadKeyValuesRepoClient.kt @@ -0,0 +1,158 @@ +package dev.inmo.micro_utils.repos.ktor.client.key.values + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.pagination.* +import dev.inmo.micro_utils.repos.ReadKeyValuesRepo +import dev.inmo.micro_utils.repos.ktor.common.* +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 KtorReadKeyValuesRepoClient( + 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 +) : ReadKeyValuesRepo { + 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 KtorReadKeyValuesRepoClient( + baseUrl: String, + httpClient: HttpClient, + contentType: ContentType, + noinline keySerializer: suspend (Key) -> String, + noinline valueSerializer: suspend (Value) -> String +) = KtorReadKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + typeInfo>(), + typeInfo>(), + keySerializer, + valueSerializer +) + +inline fun KtorReadKeyValuesRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + valueSerializer: KSerializer, + serialFormat: StringFormat, + contentType: ContentType, +) = KtorReadKeyValuesRepoClient( + baseUrl, + httpClient, + contentType, + { + serialFormat.encodeToString(idsSerializer, it).encodeURLQueryComponent() + } +) { + serialFormat.encodeToString(valueSerializer, it).encodeURLQueryComponent() +} + +inline fun KtorReadKeyValuesRepoClient( + baseUrl: String, + httpClient: HttpClient, + idsSerializer: KSerializer, + valuesSerializer: KSerializer, + serialFormat: BinaryFormat, + contentType: ContentType, +) = KtorReadKeyValuesRepoClient( + 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/values/KtorWriteKeyValuesRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorWriteKeyValuesRepoClient.kt new file mode 100644 index 00000000000..900ae7612cc --- /dev/null +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/values/KtorWriteKeyValuesRepoClient.kt @@ -0,0 +1,106 @@ +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 +import dev.inmo.micro_utils.ktor.common.* +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 +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 KtorWriteKeyValuesRepoClient( + 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 +) : WriteKeyValuesRepo { + + @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 + ) = KtorWriteKeyValuesRepoClient( + 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/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..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,20 +3,25 @@ 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 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 +@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/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/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/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/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/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/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/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/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 new file mode 100644 index 00000000000..fdf2995b165 --- /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.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 +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 { + configureCRUDRepoRoutes( + 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 = KtorCRUDRepoClient( + "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..76702d189a2 --- /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.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 +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 testKVFunctions() { + 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 { + configureKeyValueRepoRoutes( + 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 = KtorKeyValueRepoClient( + "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/KVsTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt new file mode 100644 index 00000000000..de3ff95484a --- /dev/null +++ b/repos/ktor/common/src/jvmTest/kotlin/KVsTests.kt @@ -0,0 +1,140 @@ +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.key.values.configureKeyValuesRepoRoutes +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 = MapKeyValuesRepo(map) + val server = io.ktor.server.engine.embeddedServer( + CIO, + 23456, + "127.0.0.1" + ) { + install(ContentNegotiation) { + json() + } + install(WebSockets) { + contentConverter = KotlinxWebsocketSerializationConverter(Json) + } + routing { + configureKeyValuesRepoRoutes( + 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 = KtorKeyValuesRepoClient( + "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/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/crud.yml b/repos/ktor/crud.yml new file mode 100644 index 00000000000..d634dc87b9c --- /dev/null +++ b/repos/ktor/crud.yml @@ -0,0 +1,187 @@ +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" + 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 with 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/kv.yml b/repos/ktor/kv.yml new file mode 100644 index 00000000000..176d40226e0 --- /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/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/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/KtorReadCRUDRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadCRUDRepoRoutes.kt new file mode 100644 index 00000000000..fd5acf5cf4b --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadCRUDRepoRoutes.kt @@ -0,0 +1,72 @@ +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.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.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.configureReadCRUDRepoRoutes( + originalRepo: ReadCRUDRepo, + 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(idParameterName) ?: 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(idParameterName) ?: return@get + ) + + call.respond( + originalRepo.contains(id) + ) + } + + get(countRouting) { + call.respond( + originalRepo.count() + ) + } +} + +inline fun Route.configureReadCRUDRepoRoutes( + originalRepo: ReadCRUDRepo, + idsSerializer: KSerializer, + serialFormat: StringFormat +) = configureReadCRUDRepoRoutes(originalRepo) { + serialFormat.decodeFromString(idsSerializer, it) +} + +inline fun Route.configureReadCRUDRepoRoutes( + originalRepo: ReadCRUDRepo, + idsSerializer: KSerializer, + serialFormat: BinaryFormat +) = 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 75053535d03..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,8 @@ 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 import io.ktor.server.application.call @@ -14,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, @@ -72,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/KtorWriteCRUDRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteCRUDRepoRoutes.kt new file mode 100644 index 00000000000..368f928d141 --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorWriteCRUDRepoRoutes.kt @@ -0,0 +1,41 @@ +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 +import io.ktor.server.routing.Route +import io.ktor.server.routing.post + +inline fun Route.configureWriteCRUDRepoRoutes( + originalRepo: WriteCRUDRepo +) { + includeWebsocketHandling( + newObjectsFlowRouting, + originalRepo.newObjectsFlow, + ) + includeWebsocketHandling( + updatedObjectsFlowRouting, + originalRepo.updatedObjectsFlow + ) + includeWebsocketHandling( + deletedObjectsIdsFlowRouting, + originalRepo.deletedObjectsIdsFlow + ) + + post(createRouting) { + call.respond(originalRepo.create(call.receive())) + } + + post(updateRouting) { + call.respond(originalRepo.update(call.receive())) + } + + post(deleteByIdRouting) { + originalRepo.deleteById(call.receive()) + call.respond(HttpStatusCode.OK) + } +} 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/key/value/KtorKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorKeyValueRepoRoutes.kt new file mode 100644 index 00000000000..1ff64d93a00 --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorKeyValueRepoRoutes.kt @@ -0,0 +1,47 @@ +package dev.inmo.micro_utils.repos.ktor.server.key.value + +import dev.inmo.micro_utils.ktor.common.* +import dev.inmo.micro_utils.repos.KeyValueRepo +import io.ktor.http.* +import io.ktor.server.routing.Route +import kotlinx.serialization.* + +inline fun Route.configureKeyValueRepoRoutes ( + originalRepo: KeyValueRepo, + noinline idDeserializer: suspend (String) -> Key, + noinline valueDeserializer: suspend (String) -> Value +) { + configureReadKeyValueRepoRoutes(originalRepo, idDeserializer, valueDeserializer) + configureWriteKeyValueRepoRoutes(originalRepo) +} + +inline fun Route.configureKeyValueRepoRoutes( + originalRepo: KeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: StringFormat +) = configureKeyValueRepoRoutes( + originalRepo, + { + serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) + }, + { + serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent()) + } +) + +inline fun Route.configureKeyValueRepoRoutes( + originalRepo: KeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: BinaryFormat +) = configureKeyValueRepoRoutes( + 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/KtorReadKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt new file mode 100644 index 00000000000..b0b13738bbe --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt @@ -0,0 +1,107 @@ +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.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.* +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.configureReadKeyValueRepoRoutes ( + originalRepo: ReadKeyValueRepo, + 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 + ) + + originalRepo.get(key) ?.let { + call.respond(it) + } ?: call.respond(HttpStatusCode.NoContent) + } + + get(valuesRoute) { + val pagination = call.request.queryParameters.extractPagination + val reversed = call.getQueryParameter(reversedParameterName) ?.toBoolean() ?: false + + call.respond( + originalRepo.values(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 + ) + + call.respond(originalRepo.contains(key)) + } + + get(countRoute) { + call.respond(originalRepo.count()) + } +} + +inline fun Route.configureReadKeyValueRepoRoutes( + originalRepo: ReadKeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: StringFormat +) = configureReadKeyValueRepoRoutes( + originalRepo, + { + serialFormat.decodeFromString(idsSerializer, it.decodeURLQueryComponent()) + }, + { + serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent()) + } +) + +inline fun Route.configureReadKeyValueRepoRoutes( + originalRepo: ReadKeyValueRepo, + idsSerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: BinaryFormat +) = configureReadKeyValueRepoRoutes( + 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/KtorWriteKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorWriteKeyValueRepoRoutes.kt new file mode 100644 index 00000000000..78e39e8ff66 --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorWriteKeyValueRepoRoutes.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.WriteKeyValueRepo +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.configureWriteKeyValueRepoRoutes ( + originalRepo: WriteKeyValueRepo +) { + 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) + } +} 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 new file mode 100644 index 00000000000..541ca5fd7ce --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorKeyValuesRepoRoutes.kt @@ -0,0 +1,47 @@ +package dev.inmo.micro_utils.repos.ktor.server.key.values + +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.configureKeyValuesRepoRoutes ( + originalRepo: KeyValuesRepo, + noinline keyDeserializer: suspend (String) -> Key, + noinline valueDeserializer: suspend (String) -> Value +) { + configureReadKeyValuesRepoRoutes(originalRepo, keyDeserializer, valueDeserializer) + configureWriteKeyValuesRepoRoutes(originalRepo) +} + +inline fun Route.configureKeyValuesRepoRoutes( + originalRepo: KeyValuesRepo, + keySerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: StringFormat +) = configureKeyValuesRepoRoutes( + originalRepo, + { + serialFormat.decodeFromString(keySerializer, it.decodeURLQueryComponent()) + }, + { + serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent()) + } +) + +inline fun Route.configureKeyValuesRepoRoutes( + originalRepo: KeyValuesRepo, + keySerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: BinaryFormat +) = configureKeyValuesRepoRoutes( + originalRepo, + { + 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 new file mode 100644 index 00000000000..555c9d5404a --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorReadKeyValuesRepoRoutes.kt @@ -0,0 +1,108 @@ +package dev.inmo.micro_utils.repos.ktor.server.key.values + +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.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 +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.configureReadKeyValuesRepoRoutes ( + originalRepo: ReadKeyValuesRepo, + noinline keyDeserializer: suspend (String) -> Key, + noinline valueDeserializer: suspend (String) -> Value +) { + val paginationWithValuesTypeInfo = typeInfo>() + val paginationWithKeysTypeInfo = typeInfo>() + + get(getRoute) { + val key = keyDeserializer( + 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 = keyDeserializer( + 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(countRoute) { + val id = call.getQueryParameter(keyParameterName) ?.let { + keyDeserializer(it) + } + call.respond( + id ?.let { originalRepo.count(it) } ?: originalRepo.count() + ) + } +} + +inline fun Route.configureReadKeyValuesRepoRoutes( + originalRepo: ReadKeyValuesRepo, + keySerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: StringFormat +) = configureReadKeyValuesRepoRoutes( + originalRepo, + { + serialFormat.decodeFromString(keySerializer, it.decodeURLQueryComponent()) + }, + { + serialFormat.decodeFromString(valueSerializer, it.decodeURLQueryComponent()) + } +) + +inline fun Route.configureReadKeyValuesRepoRoutes( + originalRepo: ReadKeyValuesRepo, + keySerializer: DeserializationStrategy, + valueSerializer: DeserializationStrategy, + serialFormat: BinaryFormat +) = configureReadKeyValuesRepoRoutes( + originalRepo, + { + 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/KtorWriteKeyValuesRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorWriteKeyValuesRepoRoutes.kt new file mode 100644 index 00000000000..0a37c28f67c --- /dev/null +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/values/KtorWriteKeyValuesRepoRoutes.kt @@ -0,0 +1,58 @@ +package dev.inmo.micro_utils.repos.ktor.server.key.values + +import dev.inmo.micro_utils.ktor.server.* +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 +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.configureWriteKeyValuesRepoRoutes ( + originalRepo: WriteKeyValuesRepo +) { + 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) + } +} 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..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 @@ -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( + configureWriteKeyValueRepoRoutes( 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 843b935fe18..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,9 +5,13 @@ 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 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 @@ -16,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, @@ -92,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..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 @@ -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.configureWriteKeyValueRepoRoutes ( + 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)) +) = configureWriteKeyValueRepoRoutes(originalRepo, keySerializer, valueSerializer, UnifiedRouter(serialFormat, serialFormatContentType)) 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,