From d12cba2343d706ee79dd746a1f9386859c1912e6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 30 Jan 2021 20:59:26 +0600 Subject: [PATCH] replacing of withContent and requireWithContent and extending of extensions receivers types --- .../utils/CommonMessageConversations.kt | 11 ------ .../utils/ContentMessageConversations.kt | 10 ----- .../tgbotapi/extensions/utils/WithContent.kt | 39 +++++++++++++++++++ 3 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/CommonMessageConversations.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/CommonMessageConversations.kt index dfda0819a1..2909aa9637 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/CommonMessageConversations.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/CommonMessageConversations.kt @@ -5,17 +5,6 @@ import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent import dev.inmo.tgbotapi.types.message.content.abstracts.PossiblySentViaBotCommonMessage import kotlinx.coroutines.flow.* - -@Suppress("UNCHECKED_CAST") -inline fun CommonMessage<*>.withContent() = if (content is T) { - this as CommonMessage -} else { - null -} - -@Suppress("UNCHECKED_CAST") -inline fun CommonMessage<*>.requireWithContent() = this as CommonMessage - /** * Simple factory to convert [ContentMessage] to a [CommonMessage] */ diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentMessageConversations.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentMessageConversations.kt index 0bec218656..bcdff2ce59 100644 --- a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentMessageConversations.kt +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ContentMessageConversations.kt @@ -9,16 +9,6 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.mapNotNull import kotlin.reflect.KClass -@Suppress("UNCHECKED_CAST") -inline fun ContentMessage<*>.withContent() = if (content is T) { - this as ContentMessage -} else { - null -} - -@Suppress("UNCHECKED_CAST") -inline fun ContentMessage<*>.requireWithContent() = this as ContentMessage - private inline fun Flow>.withContentType() = mapNotNull { it.withContent() } diff --git a/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt new file mode 100644 index 0000000000..be45e0b6f1 --- /dev/null +++ b/tgbotapi.extensions.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/WithContent.kt @@ -0,0 +1,39 @@ +@file:Suppress("UNCHECKED_CAST") + +package dev.inmo.tgbotapi.extensions.utils + +import dev.inmo.tgbotapi.types.message.abstracts.* +import dev.inmo.tgbotapi.types.message.content.abstracts.* + +inline fun ContentMessage<*>.withContent() = if (content is T) { this as ContentMessage } else { null } +inline fun ContentMessage<*>.requireWithContent() = withContent()!! + +inline fun CommonMessage<*>.withContent() = if (content is T) { this as CommonMessage } else { null } +inline fun CommonMessage<*>.requireWithContent() = withContent()!! + +inline fun PossiblySentViaBotCommonMessage<*>.withContent() = if (content is T) { this as PossiblySentViaBotCommonMessage } else { null } +inline fun PossiblySentViaBotCommonMessage<*>.requireWithContent() = withContent()!! + +inline fun ChannelMessage<*>.withContent() = if (content is T) { this as ChannelMessage } else { null } +inline fun ChannelMessage<*>.requireWithContent() = withContent()!! + +inline fun PrivateMessage<*>.withContent() = if (content is T) { this as PrivateMessage } else { null } +inline fun PrivateMessage<*>.requireWithContent() = withContent()!! + +inline fun PublicMessage<*>.withContent() = if (content is T) { this as PublicMessage } else { null } +inline fun PublicMessage<*>.requireWithContent() = withContent()!! + +inline fun GroupMessage<*>.withContent() = if (content is T) { this as GroupMessage } else { null } +inline fun GroupMessage<*>.requireWithContent() = withContent()!! + +inline fun FromChannelGroupMessage<*>.withContent() = if (content is T) { this as FromChannelGroupMessage } else { null } +inline fun FromChannelGroupMessage<*>.requireWithContent() = withContent()!! + +inline fun AnonymousGroupMessage<*>.withContent() = if (content is T) { this as AnonymousGroupMessage } else { null } +inline fun AnonymousGroupMessage<*>.requireWithContent() = withContent()!! + +inline fun CommonGroupMessage<*>.withContent() = if (content is T) { this as CommonGroupMessage } else { null } +inline fun CommonGroupMessage<*>.requireWithContent() = withContent()!! + +inline fun MediaGroupMessage<*>.withContent() = if (content is T) { this as MediaGroupMessage } else { null } +inline fun MediaGroupMessage<*>.requireWithContent() = withContent()!!