From ee3867e9465e88712ca97b6fe6d16afdce3b54af Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 26 Aug 2020 11:48:02 +0600 Subject: [PATCH] add fromHex extension --- .../insanusmokrassar/postssystem/ktor/HexConversations.kt | 4 ++-- .../postssystem/ktor/server/ServerRoutingShortcuts.kt | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/HexConversations.kt b/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/HexConversations.kt index 94e7603d..eb7cb1d3 100644 --- a/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/HexConversations.kt +++ b/ktor/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/ktor/HexConversations.kt @@ -1,6 +1,6 @@ package com.insanusmokrassar.postssystem.ktor -import kotlinx.serialization.SerializationStrategy -import kotlinx.serialization.encodeToHexString +import kotlinx.serialization.* fun T.toHex(with: SerializationStrategy) = standardKtorSerialFormat.encodeToHexString(with, this) +fun String.fromHex(with: DeserializationStrategy): T = standardKtorSerialFormat.decodeFromHexString(with, this) diff --git a/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/ktor/server/ServerRoutingShortcuts.kt b/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/ktor/server/ServerRoutingShortcuts.kt index 3ce44c91..5cd6db27 100644 --- a/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/ktor/server/ServerRoutingShortcuts.kt +++ b/ktor/server/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/ktor/server/ServerRoutingShortcuts.kt @@ -1,5 +1,6 @@ package com.insanusmokrassar.postssystem.ktor.server +import com.insanusmokrassar.postssystem.ktor.fromHex import com.insanusmokrassar.postssystem.ktor.standardKtorSerialFormat import io.ktor.application.ApplicationCall import io.ktor.http.HttpStatusCode @@ -28,12 +29,7 @@ suspend fun ApplicationCall.uniload( fun ApplicationCall.uniloadFromQuery( parameterName: String, deserializer: DeserializationStrategy -): T? { - return standardKtorSerialFormat.decodeFromHexString( - deserializer, - getQueryParameter(parameterName) ?: return null - ) -} +): T? = getQueryParameter(parameterName) ?.fromHex(deserializer) suspend fun ApplicationCall.uniloadFromQueryOrSendError( parameterName: String,