1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

replacing of withContent and requireWithContent and extending of extensions receivers types

This commit is contained in:
InsanusMokrassar 2021-01-30 20:59:26 +06:00
parent 450c7e6474
commit d12cba2343
3 changed files with 39 additions and 21 deletions

View File

@ -5,17 +5,6 @@ import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
import dev.inmo.tgbotapi.types.message.content.abstracts.PossiblySentViaBotCommonMessage import dev.inmo.tgbotapi.types.message.content.abstracts.PossiblySentViaBotCommonMessage
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
@Suppress("UNCHECKED_CAST")
inline fun <reified T : MessageContent> CommonMessage<*>.withContent() = if (content is T) {
this as CommonMessage<T>
} else {
null
}
@Suppress("UNCHECKED_CAST")
inline fun <reified T : MessageContent> CommonMessage<*>.requireWithContent() = this as CommonMessage<T>
/** /**
* Simple factory to convert [ContentMessage] to a [CommonMessage] * Simple factory to convert [ContentMessage] to a [CommonMessage]
*/ */

View File

@ -9,16 +9,6 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.mapNotNull
import kotlin.reflect.KClass import kotlin.reflect.KClass
@Suppress("UNCHECKED_CAST")
inline fun <reified T : MessageContent> ContentMessage<*>.withContent() = if (content is T) {
this as ContentMessage<T>
} else {
null
}
@Suppress("UNCHECKED_CAST")
inline fun <reified T : MessageContent> ContentMessage<*>.requireWithContent() = this as ContentMessage<T>
private inline fun <reified T : MessageContent> Flow<ContentMessage<*>>.withContentType() = mapNotNull { private inline fun <reified T : MessageContent> Flow<ContentMessage<*>>.withContentType() = mapNotNull {
it.withContent<T>() it.withContent<T>()
} }

View File

@ -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 <reified T : MessageContent> ContentMessage<*>.withContent() = if (content is T) { this as ContentMessage<T> } else { null }
inline fun <reified T : MessageContent> ContentMessage<*>.requireWithContent() = withContent<T>()!!
inline fun <reified T : MessageContent> CommonMessage<*>.withContent() = if (content is T) { this as CommonMessage<T> } else { null }
inline fun <reified T : MessageContent> CommonMessage<*>.requireWithContent() = withContent<T>()!!
inline fun <reified T : MessageContent> PossiblySentViaBotCommonMessage<*>.withContent() = if (content is T) { this as PossiblySentViaBotCommonMessage<T> } else { null }
inline fun <reified T : MessageContent> PossiblySentViaBotCommonMessage<*>.requireWithContent() = withContent<T>()!!
inline fun <reified T : MessageContent> ChannelMessage<*>.withContent() = if (content is T) { this as ChannelMessage<T> } else { null }
inline fun <reified T : MessageContent> ChannelMessage<*>.requireWithContent() = withContent<T>()!!
inline fun <reified T : MessageContent> PrivateMessage<*>.withContent() = if (content is T) { this as PrivateMessage<T> } else { null }
inline fun <reified T : MessageContent> PrivateMessage<*>.requireWithContent() = withContent<T>()!!
inline fun <reified T : MessageContent> PublicMessage<*>.withContent() = if (content is T) { this as PublicMessage<T> } else { null }
inline fun <reified T : MessageContent> PublicMessage<*>.requireWithContent() = withContent<T>()!!
inline fun <reified T : MessageContent> GroupMessage<*>.withContent() = if (content is T) { this as GroupMessage<T> } else { null }
inline fun <reified T : MessageContent> GroupMessage<*>.requireWithContent() = withContent<T>()!!
inline fun <reified T : MessageContent> FromChannelGroupMessage<*>.withContent() = if (content is T) { this as FromChannelGroupMessage<T> } else { null }
inline fun <reified T : MessageContent> FromChannelGroupMessage<*>.requireWithContent() = withContent<T>()!!
inline fun <reified T : MessageContent> AnonymousGroupMessage<*>.withContent() = if (content is T) { this as AnonymousGroupMessage<T> } else { null }
inline fun <reified T : MessageContent> AnonymousGroupMessage<*>.requireWithContent() = withContent<T>()!!
inline fun <reified T : MessageContent> CommonGroupMessage<*>.withContent() = if (content is T) { this as CommonGroupMessage<T> } else { null }
inline fun <reified T : MessageContent> CommonGroupMessage<*>.requireWithContent() = withContent<T>()!!
inline fun <reified T : MediaGroupContent> MediaGroupMessage<*>.withContent() = if (content is T) { this as MediaGroupMessage<T> } else { null }
inline fun <reified T : MediaGroupContent> MediaGroupMessage<*>.requireWithContent() = withContent<T>()!!