1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

add generation of class casts

This commit is contained in:
InsanusMokrassar 2022-08-05 16:31:39 +06:00
parent d8f6429385
commit a18eda3db6
66 changed files with 4798 additions and 3732 deletions

View File

@ -7,6 +7,7 @@ buildscript {
dependencies { dependencies {
classpath libs.kotlin.gradle.plugin classpath libs.kotlin.gradle.plugin
classpath libs.kotlin.ksp.plugin
classpath libs.kotlin.serialization.plugin classpath libs.kotlin.serialization.plugin
classpath libs.kotlin.dokka.plugin classpath libs.kotlin.dokka.plugin
classpath libs.github.release.plugin classpath libs.github.release.plugin

View File

@ -10,6 +10,9 @@ korlibs = "3.0.0"
uuid = "0.5.0" uuid = "0.5.0"
ktor = "2.0.3" ktor = "2.0.3"
ksp = "1.7.10-1.0.6"
kotlin-poet = "1.12.0"
microutils = "0.12.0" microutils = "0.12.0"
github-release-plugin = "2.4.1" github-release-plugin = "2.4.1"
@ -45,14 +48,21 @@ microutils-languageCodes = { module = "dev.inmo:micro_utils.language_codes", ver
microutils-ktor-common = { module = "dev.inmo:micro_utils.ktor.common", version.ref = "microutils" } microutils-ktor-common = { module = "dev.inmo:micro_utils.ktor.common", version.ref = "microutils" }
microutils-fsm-common = { module = "dev.inmo:micro_utils.fsm.common", version.ref = "microutils" } microutils-fsm-common = { module = "dev.inmo:micro_utils.fsm.common", version.ref = "microutils" }
# ksp dependencies
kotlin-poet = { module = "com.squareup:kotlinpoet-ksp", version.ref = "kotlin-poet" }
ksp = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
# buildscript classpaths # buildscript classpaths
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlin-ksp-plugin = { module = "com.google.devtools.ksp:symbol-processing-gradle-plugin", version.ref = "ksp" }
kotlin-serialization-plugin = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" } kotlin-serialization-plugin = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" }
kotlin-dokka-plugin = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "kotlin" } kotlin-dokka-plugin = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "kotlin" }
github-release-plugin = { module = "com.github.breadmoirai:github-release", version.ref = "github-release-plugin" } github-release-plugin = { module = "com.github.breadmoirai:github-release", version.ref = "github-release-plugin" }
[plugins] [plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }

View File

@ -12,6 +12,8 @@ pluginManagement {
} }
include ":tgbotapi.core" include ":tgbotapi.core"
include ":tgbotapi.ksp:processor"
include ":tgbotapi.ksp:lib"
include ":tgbotapi.api" include ":tgbotapi.api"
include ":tgbotapi.utils" include ":tgbotapi.utils"
include ":tgbotapi.behaviour_builder" include ":tgbotapi.behaviour_builder"

View File

@ -4,7 +4,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asCallbackQueryUpdate import dev.inmo.tgbotapi.extensions.utils.callbackQueryUpdateOrNull
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.queries.callback.* import dev.inmo.tgbotapi.types.queries.callback.*
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
@ -22,7 +22,7 @@ suspend inline fun <reified O> BehaviourContext.waitCallbackQueries(
initRequest, initRequest,
errorFactory errorFactory
) { ) {
(it.asCallbackQueryUpdate() ?.data as O).let(::listOfNotNull) (it.callbackQueryUpdateOrNull() ?.data as O).let(::listOfNotNull)
} }

View File

@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asChatJoinRequestUpdate import dev.inmo.tgbotapi.extensions.utils.chatJoinRequestUpdateOrNull
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.chat.ChatJoinRequest import dev.inmo.tgbotapi.types.chat.ChatJoinRequest
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
@ -20,7 +20,7 @@ suspend inline fun <reified O> BehaviourContext.internalWaitChatJoinRequests(
initRequest, initRequest,
errorFactory errorFactory
) { ) {
(it.asChatJoinRequestUpdate() ?.data as? O).let(::listOfNotNull) (it.chatJoinRequestUpdateOrNull() ?.data as? O).let(::listOfNotNull)
} }

View File

@ -1,14 +1,12 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.utils.chosenInlineResultUpdateOrNull
import dev.inmo.tgbotapi.extensions.utils.asChosenInlineResultUpdate
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.InlineQueries.ChosenInlineResult.* import dev.inmo.tgbotapi.types.InlineQueries.ChosenInlineResult.*
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.toList
typealias ChosenInlineResultMapper<T> = suspend T.() -> T? typealias ChosenInlineResultMapper<T> = suspend T.() -> T?
@ -20,7 +18,7 @@ suspend inline fun <reified O> BehaviourContext.waitChosenInlineResults(
initRequest, initRequest,
errorFactory errorFactory
) { ) {
(it.asChosenInlineResultUpdate() ?.data as? O).let(::listOfNotNull) (it.chosenInlineResultUpdateOrNull() ?.data as? O).let(::listOfNotNull)
} }
suspend fun BehaviourContext.waitChosenInlineResult( suspend fun BehaviourContext.waitChosenInlineResult(

View File

@ -4,7 +4,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asCommonMessage import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull
import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.extensions.utils.withContent
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage

View File

@ -4,7 +4,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asCommonMessage import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull
import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.extensions.utils.withContent
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
@ -32,7 +32,7 @@ suspend inline fun <reified O : MessageContent> BehaviourContext.waitEditedConte
) { ) {
val messages = when (it) { val messages = when (it) {
is BaseEditMessageUpdate -> { is BaseEditMessageUpdate -> {
val commonMessage = it.data.asCommonMessage() ?: return@expectFlow emptyList() val commonMessage = it.data.commonMessageOrNull() ?: return@expectFlow emptyList()
if (commonMessage !is MediaGroupMessage<*> || includeMediaGroups) { if (commonMessage !is MediaGroupMessage<*> || includeMediaGroups) {
listOf(commonMessage) listOf(commonMessage)
} else { } else {

View File

@ -27,7 +27,7 @@ suspend inline fun <reified O : ChatEvent> BehaviourContext.waitEvents(
initRequest, initRequest,
errorFactory errorFactory
) { ) {
it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.withEvent<O>() ?.chatEvent.let(::listOfNotNull) it.baseSentMessageUpdateOrNull() ?.data ?.chatEventMessageOrNull() ?.withEvent<O>() ?.chatEvent.let(::listOfNotNull)
} }
suspend fun BehaviourContext.waitChannelEvents( suspend fun BehaviourContext.waitChannelEvents(

View File

@ -25,7 +25,7 @@ suspend inline fun <reified O : ChatEvent> BehaviourContext.waitEventsMessages(
initRequest, initRequest,
errorFactory errorFactory
) { ) {
it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.withEvent<O>().let(::listOfNotNull) it.baseSentMessageUpdateOrNull() ?.data ?.chatEventMessageOrNull() ?.withEvent<O>().let(::listOfNotNull)
} }
suspend fun BehaviourContext.waitChannelEventsMessages( suspend fun BehaviourContext.waitChannelEventsMessages(

View File

@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate import dev.inmo.tgbotapi.extensions.utils.inlineQueryUpdateOrNull
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.InlineQueries.query.* import dev.inmo.tgbotapi.types.InlineQueries.query.*
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
@ -20,7 +20,7 @@ suspend inline fun <reified O : InlineQuery> BehaviourContext.waitInlineQueries(
initRequest, initRequest,
errorFactory errorFactory
) { ) {
(it.asInlineQueryUpdate() ?.data as? O).let(::listOfNotNull) (it.inlineQueryUpdateOrNull() ?.data as? O).let(::listOfNotNull)
} }
suspend fun BehaviourContext.waitAnyInlineQuery( suspend fun BehaviourContext.waitAnyInlineQuery(

View File

@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asSentMediaGroupUpdate import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull
import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.extensions.utils.withContent
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage

View File

@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asSentMediaGroupUpdate import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull
import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.extensions.utils.withContent
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
@ -20,7 +20,7 @@ suspend inline fun <reified T : MediaGroupContent> BehaviourContext.buildMediaGr
initRequest: Request<*>? = null, initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null } noinline errorFactory: NullableRequestBuilder<*> = { null }
): Flow<List<MediaGroupMessage<T>>> = flowsUpdatesFilter.expectFlow(bot, initRequest, errorFactory) { update -> ): Flow<List<MediaGroupMessage<T>>> = flowsUpdatesFilter.expectFlow(bot, initRequest, errorFactory) { update ->
update.asSentMediaGroupUpdate() ?.data ?.let { mediaGroup -> update.sentMediaGroupUpdateOrNull() ?.data ?.let { mediaGroup ->
val mapped = mediaGroup.mapNotNull { it.withContent<T>() } val mapped = mediaGroup.mapNotNull { it.withContent<T>() }
listOf( listOf(
mapped mapped

View File

@ -2,8 +2,8 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate import dev.inmo.tgbotapi.extensions.utils.messageUpdateOrNull
import dev.inmo.tgbotapi.extensions.utils.asPassportMessage import dev.inmo.tgbotapi.extensions.utils.passportMessageOrNull
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.message.PassportMessage import dev.inmo.tgbotapi.types.message.PassportMessage
import dev.inmo.tgbotapi.types.passport.PassportData import dev.inmo.tgbotapi.types.passport.PassportData
@ -23,7 +23,7 @@ suspend inline fun <reified O : EncryptedPassportElement> BehaviourContext.waitP
initRequest, initRequest,
errorFactory errorFactory
) { ) {
it.asMessageUpdate() ?.data ?.asPassportMessage() ?.passportData ?.data ?.filterIsInstance<O>() ?: emptyList() it.messageUpdateOrNull() ?.data ?.passportMessageOrNull() ?.passportData ?.data ?.filterIsInstance<O>() ?: emptyList()
} }
suspend fun BehaviourContext.waitAnyPassportMessages( suspend fun BehaviourContext.waitAnyPassportMessages(

View File

@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asPollAnswerUpdate import dev.inmo.tgbotapi.extensions.utils.pollAnswerUpdateOrNull
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.polls.PollAnswer import dev.inmo.tgbotapi.types.polls.PollAnswer
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
@ -19,5 +19,5 @@ suspend fun BehaviourContext.waitPollAnswers(
initRequest, initRequest,
errorFactory errorFactory
) { ) {
it.asPollAnswerUpdate() ?.data.let(::listOfNotNull) it.pollAnswerUpdateOrNull() ?.data.let(::listOfNotNull)
} }

View File

@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asPollUpdate import dev.inmo.tgbotapi.extensions.utils.pollUpdateOrNull
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.polls.* import dev.inmo.tgbotapi.types.polls.*
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
@ -20,7 +20,7 @@ suspend inline fun <reified O : Poll> BehaviourContext.waitPolls(
initRequest, initRequest,
errorFactory errorFactory
) { ) {
(it.asPollUpdate() ?.data as? O).let(::listOfNotNull) (it.pollUpdateOrNull() ?.data as? O).let(::listOfNotNull)
} }
/** /**

View File

@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.asPreCheckoutQueryUpdate import dev.inmo.tgbotapi.extensions.utils.preCheckoutQueryUpdateOrNull
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.payments.PreCheckoutQuery import dev.inmo.tgbotapi.types.payments.PreCheckoutQuery
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
@ -19,5 +19,5 @@ suspend fun BehaviourContext.waitPreCheckoutQueries(
initRequest, initRequest,
errorFactory errorFactory
) { ) {
it.asPreCheckoutQueryUpdate() ?.data.let(::listOfNotNull) it.preCheckoutQueryUpdateOrNull() ?.data.let(::listOfNotNull)
} }

View File

@ -1,12 +1,10 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.utils.shippingQueryUpdateOrNull
import dev.inmo.tgbotapi.extensions.utils.asShippingQueryUpdate
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.payments.ShippingQuery import dev.inmo.tgbotapi.types.payments.ShippingQuery
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.toList
typealias ShippingQueryMapper = suspend ShippingQuery.() -> ShippingQuery? typealias ShippingQueryMapper = suspend ShippingQuery.() -> ShippingQuery?
@ -17,5 +15,5 @@ suspend fun BehaviourContext.waitShippingQueries(
initRequest, initRequest,
errorFactory errorFactory
) { ) {
(it.asShippingQueryUpdate() ?.data).let(::listOfNotNull) (it.shippingQueryUpdateOrNull() ?.data).let(::listOfNotNull)
} }

View File

@ -9,7 +9,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CallbackQueryFilte
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.* import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserCallbackQueryMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserCallbackQueryMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asCallbackQueryUpdate import dev.inmo.tgbotapi.extensions.utils.callbackQueryUpdateOrNull
import dev.inmo.tgbotapi.types.queries.callback.* import dev.inmo.tgbotapi.types.queries.callback.*
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@ -20,7 +20,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T : CallbackQuery> B
markerFactory: MarkerFactory<in T, Any> = ByUserCallbackQueryMarkerFactory, markerFactory: MarkerFactory<in T, Any> = ByUserCallbackQueryMarkerFactory,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T> noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
(it.asCallbackQueryUpdate() ?.data as? T) ?.let(::listOfNotNull) (it.callbackQueryUpdateOrNull() ?.data as? T) ?.let(::listOfNotNull)
} }
/** /**

View File

@ -5,7 +5,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.ChatJoinRequestFil
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatChatJoinRequestMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatChatJoinRequestMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asChatJoinRequestUpdate import dev.inmo.tgbotapi.extensions.utils.chatJoinRequestUpdateOrNull
import dev.inmo.tgbotapi.types.chat.ChatJoinRequest import dev.inmo.tgbotapi.types.chat.ChatJoinRequest
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
@ -29,5 +29,5 @@ suspend fun <BC : BehaviourContext> BC.onChatJoinRequest(
markerFactory: MarkerFactory<in ChatJoinRequest, Any> = ByChatChatJoinRequestMarkerFactory, markerFactory: MarkerFactory<in ChatJoinRequest, Any> = ByChatChatJoinRequestMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatJoinRequest> scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatJoinRequest>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
(it.asChatJoinRequestUpdate() ?.data) ?.let(::listOfNotNull) (it.chatJoinRequestUpdateOrNull() ?.data) ?.let(::listOfNotNull)
} }

View File

@ -6,7 +6,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserIdChosenInlineResultMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserIdChosenInlineResultMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asChosenInlineResultUpdate import dev.inmo.tgbotapi.extensions.utils.chosenInlineResultUpdateOrNull
import dev.inmo.tgbotapi.types.InlineQueries.ChosenInlineResult.* import dev.inmo.tgbotapi.types.InlineQueries.ChosenInlineResult.*
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
@ -16,7 +16,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T : ChosenInlineResu
markerFactory: MarkerFactory<in T, Any> = ByUserIdChosenInlineResultMarkerFactory, markerFactory: MarkerFactory<in T, Any> = ByUserIdChosenInlineResultMarkerFactory,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T> noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
(it.asChosenInlineResultUpdate() ?.data as? T) ?.let(::listOfNotNull) (it.chosenInlineResultUpdateOrNull() ?.data as? T) ?.let(::listOfNotNull)
} }
/** /**

View File

@ -10,7 +10,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByCha
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times
import dev.inmo.tgbotapi.extensions.utils.asBotCommandTextSource import dev.inmo.tgbotapi.extensions.utils.botCommandTextSourceOrNull
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.content.TextContent import dev.inmo.tgbotapi.types.message.content.TextContent
@ -35,7 +35,7 @@ internal suspend fun <BC : BehaviourContext> BC.commandUncounted(
true true
} }
sizeRequirement && textSources.any { sizeRequirement && textSources.any {
commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false) commandRegex.matches(it.botCommandTextSourceOrNull() ?.command ?: return@any false)
} }
}.let { }.let {
initialFilter ?.times(it) ?: it initialFilter ?.times(it) ?: it

View File

@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByCha
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times
import dev.inmo.tgbotapi.extensions.utils.asBotCommandTextSource import dev.inmo.tgbotapi.extensions.utils.botCommandTextSourceOrNull
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
import dev.inmo.tgbotapi.types.message.content.TextContent import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.types.message.content.TextMessage import dev.inmo.tgbotapi.types.message.content.TextMessage
@ -33,7 +33,7 @@ suspend fun <BC : BehaviourContext> BC.unhandledCommand(
true true
} }
sizeRequirement && textSources.any { sizeRequirement && textSources.any {
val command = it.asBotCommandTextSource() ?.command ?: return@any false val command = it.botCommandTextSourceOrNull() ?.command ?: return@any false
!triggersHolder.handleableCommandsHolder.isHandled(command) !triggersHolder.handleableCommandsHolder.isHandled(command)
} }
}.let { }.let {

View File

@ -20,7 +20,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilte
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asEditMessageUpdate import dev.inmo.tgbotapi.extensions.utils.editMessageUpdateOrNull
import dev.inmo.tgbotapi.extensions.utils.withContent import dev.inmo.tgbotapi.extensions.utils.withContent
import dev.inmo.tgbotapi.types.files.TelegramMediaFile import dev.inmo.tgbotapi.types.files.TelegramMediaFile
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
@ -41,7 +41,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T : MessageContent>
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<T>> noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<T>>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
when (it) { when (it) {
is BaseEditMessageUpdate -> (it.asEditMessageUpdate() ?.data ?.withContent<T>()) is BaseEditMessageUpdate -> (it.editMessageUpdateOrNull() ?.data ?.withContent<T>())
else -> null else -> null
} ?.let(::listOfNotNull) } ?.let(::listOfNotNull)
} }

View File

@ -7,8 +7,8 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByCha
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asBaseSentMessageUpdate import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull
import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage import dev.inmo.tgbotapi.extensions.utils.chatEventMessageOrNull
import dev.inmo.tgbotapi.types.message.ChatEvents.* import dev.inmo.tgbotapi.types.message.ChatEvents.*
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.* import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.*
import dev.inmo.tgbotapi.types.message.ChatEvents.voice.* import dev.inmo.tgbotapi.types.message.ChatEvents.voice.*
@ -24,7 +24,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T : ChatEvent> BC.on
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<T>> noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<T>>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
(it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.takeIf { it.chatEvent is T } as? ChatEventMessage<T>) ?.let(::listOfNotNull) (it.baseSentMessageUpdateOrNull() ?.data ?.chatEventMessageOrNull() ?.takeIf { it.chatEvent is T } as? ChatEventMessage<T>) ?.let(::listOfNotNull)
} }
/** /**

View File

@ -5,7 +5,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.InlineQueryFilterB
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserInlineQueryMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserInlineQueryMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate import dev.inmo.tgbotapi.extensions.utils.inlineQueryUpdateOrNull
import dev.inmo.tgbotapi.types.InlineQueries.query.* import dev.inmo.tgbotapi.types.InlineQueries.query.*
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
@ -15,7 +15,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T : InlineQuery> BC.
markerFactory: MarkerFactory<in T, Any> = ByUserInlineQueryMarkerFactory, markerFactory: MarkerFactory<in T, Any> = ByUserInlineQueryMarkerFactory,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T> noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
(it.asInlineQueryUpdate() ?.data as? T) ?.let(::listOfNotNull) (it.inlineQueryUpdateOrNull() ?.data as? T) ?.let(::listOfNotNull)
} }
/** /**

View File

@ -7,7 +7,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessagesFilterByCh
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMediaGroupMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMediaGroupMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asSentMediaGroupUpdate import dev.inmo.tgbotapi.extensions.utils.sentMediaGroupUpdateOrNull
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import dev.inmo.tgbotapi.types.message.content.* import dev.inmo.tgbotapi.types.message.content.*
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
@ -20,7 +20,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T : MediaGroupConten
markerFactory: MarkerFactory<in List<MediaGroupMessage<T>>, Any> = ByChatMediaGroupMarkerFactory, markerFactory: MarkerFactory<in List<MediaGroupMessage<T>>, Any> = ByChatMediaGroupMarkerFactory,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, List<MediaGroupMessage<T>>> noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, List<MediaGroupMessage<T>>>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
(it.asSentMediaGroupUpdate() ?.data ?.takeIf { messages -> (it.sentMediaGroupUpdateOrNull() ?.data ?.takeIf { messages ->
messages.all { message -> messages.all { message ->
message.content is T message.content is T
} }

View File

@ -5,8 +5,8 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByCha
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate import dev.inmo.tgbotapi.extensions.utils.messageUpdateOrNull
import dev.inmo.tgbotapi.extensions.utils.asPassportMessage import dev.inmo.tgbotapi.extensions.utils.passportMessageOrNull
import dev.inmo.tgbotapi.types.message.PassportMessage import dev.inmo.tgbotapi.types.message.PassportMessage
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElement import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.EncryptedPassportElement
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
@ -17,7 +17,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T : EncryptedPasspor
markerFactory: MarkerFactory<in PassportMessage, Any> = ByChatMessageMarkerFactory, markerFactory: MarkerFactory<in PassportMessage, Any> = ByChatMessageMarkerFactory,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, PassportMessage> noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, PassportMessage>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
(it.asMessageUpdate() ?.data ?.asPassportMessage() ?.takeIf { it.passportData.data.any { it is T } }) ?.let(::listOfNotNull) (it.messageUpdateOrNull() ?.data ?.passportMessageOrNull() ?.takeIf { it.passportData.data.any { it is T } }) ?.let(::listOfNotNull)
} }

View File

@ -6,7 +6,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByIdPollAnswerMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByIdPollAnswerMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asPollAnswerUpdate import dev.inmo.tgbotapi.extensions.utils.pollAnswerUpdateOrNull
import dev.inmo.tgbotapi.types.polls.PollAnswer import dev.inmo.tgbotapi.types.polls.PollAnswer
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
@ -16,7 +16,7 @@ internal suspend inline fun <BC : BehaviourContext> BC.onPollAnswered(
markerFactory: MarkerFactory<in PollAnswer, Any> = ByIdPollAnswerMarkerFactory, markerFactory: MarkerFactory<in PollAnswer, Any> = ByIdPollAnswerMarkerFactory,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, PollAnswer> noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, PollAnswer>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
(it.asPollAnswerUpdate() ?.data) ?.let(::listOfNotNull) (it.pollAnswerUpdateOrNull() ?.data) ?.let(::listOfNotNull)
} }
/** /**

View File

@ -6,7 +6,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByIdPollMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByIdPollMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asPollUpdate import dev.inmo.tgbotapi.extensions.utils.pollUpdateOrNull
import dev.inmo.tgbotapi.types.polls.* import dev.inmo.tgbotapi.types.polls.*
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
@ -16,7 +16,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T : Poll> BC.onPollU
markerFactory: MarkerFactory<in T, Any> = ByIdPollMarkerFactory, markerFactory: MarkerFactory<in T, Any> = ByIdPollMarkerFactory,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T> noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
(it.asPollUpdate() ?.data as? T) ?.let(::listOfNotNull) (it.pollUpdateOrNull() ?.data as? T) ?.let(::listOfNotNull)
} }
/** /**

View File

@ -5,7 +5,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.PreCheckoutQueryFi
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserPreCheckoutQueryMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserPreCheckoutQueryMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asPreCheckoutQueryUpdate import dev.inmo.tgbotapi.extensions.utils.preCheckoutQueryUpdateOrNull
import dev.inmo.tgbotapi.types.payments.PreCheckoutQuery import dev.inmo.tgbotapi.types.payments.PreCheckoutQuery
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
@ -31,5 +31,5 @@ suspend fun <BC : BehaviourContext> BC.onPreCheckoutQuery(
markerFactory: MarkerFactory<in PreCheckoutQuery, Any> = ByUserPreCheckoutQueryMarkerFactory, markerFactory: MarkerFactory<in PreCheckoutQuery, Any> = ByUserPreCheckoutQueryMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, PreCheckoutQuery> scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, PreCheckoutQuery>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
(it.asPreCheckoutQueryUpdate() ?.data) ?.let(::listOfNotNull) (it.preCheckoutQueryUpdateOrNull() ?.data) ?.let(::listOfNotNull)
} }

View File

@ -5,7 +5,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.ShippingQueryFilte
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserShippingQueryMarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserShippingQueryMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.asShippingQueryUpdate import dev.inmo.tgbotapi.extensions.utils.shippingQueryUpdateOrNull
import dev.inmo.tgbotapi.types.payments.ShippingQuery import dev.inmo.tgbotapi.types.payments.ShippingQuery
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
@ -31,5 +31,5 @@ suspend fun <BC : BehaviourContext> BC.onShippingQuery(
markerFactory: MarkerFactory<in ShippingQuery, Any> = ByUserShippingQueryMarkerFactory, markerFactory: MarkerFactory<in ShippingQuery, Any> = ByUserShippingQueryMarkerFactory,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ShippingQuery> scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ShippingQuery>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) { ) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
(it.asShippingQueryUpdate() ?.data) ?.let(::listOfNotNull) (it.shippingQueryUpdateOrNull() ?.data) ?.let(::listOfNotNull)
} }

View File

@ -1,6 +1,7 @@
plugins { plugins {
id "org.jetbrains.kotlin.multiplatform" id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization" id "org.jetbrains.kotlin.plugin.serialization"
id "com.google.devtools.ksp"
} }
project.description = "Core part of tgbotapi with all (and only) required functionality for working with Telegram Bot API" project.description = "Core part of tgbotapi with all (and only) required functionality for working with Telegram Bot API"
@ -28,6 +29,8 @@ kotlin {
api libs.microutils.languageCodes api libs.microutils.languageCodes
api libs.ktor.client.core api libs.ktor.client.core
api project(":tgbotapi.ksp:lib")
} }
} }
commonTest { commonTest {
@ -46,12 +49,15 @@ kotlin {
} }
} }
} }
}
// targets.all {
// compilations.all { dependencies {
// kotlinOptions { add("kspCommonMainMetadata", project(":tgbotapi.ksp:processor"))
// freeCompilerArgs += ["-Xuse-experimental=kotlinx.coroutines.ExperimentalCoroutinesApi", "-Xopt-in=kotlin.RequiresOptIn"] add("kspJvm", project(":tgbotapi.ksp:processor"))
// } }
// }
// } ksp {
arg("cctargetPackage", "dev.inmo.tgbotapi.extensions.utils")
arg("ccoutputFileName", "NewClassCasts")
arg("ccoutputFolder", project(":tgbotapi.utils").file("src/commonMain/kotlin").absolutePath)
} }

View File

@ -1,8 +1,10 @@
package dev.inmo.tgbotapi.abstracts package dev.inmo.tgbotapi.abstracts
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.payments.abstracts.Currencied import dev.inmo.tgbotapi.types.payments.abstracts.Currencied
import dev.inmo.tgbotapi.types.payments.abstracts.Priced import dev.inmo.tgbotapi.types.payments.abstracts.Priced
@ClassCastsIncluded
interface CommonSendInvoiceData : Titled, Currencied, Priced { interface CommonSendInvoiceData : Titled, Currencied, Priced {
val description: String val description: String
val payload: String val payload: String

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.abstracts package dev.inmo.tgbotapi.abstracts
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.chat.User
/** /**
@ -7,6 +8,7 @@ import dev.inmo.tgbotapi.types.chat.User
* *
* @see FromUser * @see FromUser
*/ */
@ClassCastsIncluded
interface WithUser { interface WithUser {
val user: User val user: User
} }

View File

@ -1,13 +1,15 @@
package dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.abstracts package dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.abstracts
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.serializers.InlineQueryResultSerializer import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.serializers.InlineQueryResultSerializer
import dev.inmo.tgbotapi.types.InlineQueryIdentifier import dev.inmo.tgbotapi.types.InlineQueryIdentifier
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable(InlineQueryResultSerializer::class) @Serializable(InlineQueryResultSerializer::class)
@ClassCastsIncluded
interface InlineQueryResult { interface InlineQueryResult {
val type: String val type: String
val id: InlineQueryIdentifier val id: InlineQueryIdentifier
val replyMarkup: InlineKeyboardMarkup? val replyMarkup: InlineKeyboardMarkup?
} }

View File

@ -1,7 +1,9 @@
package dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent package dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContentSerializer import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContentSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable(InputMessageContentSerializer::class) @Serializable(InputMessageContentSerializer::class)
@ClassCastsIncluded
sealed interface InputMessageContent sealed interface InputMessageContent

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.types.actions package dev.inmo.tgbotapi.types.actions
import dev.inmo.micro_utils.common.Warning import dev.inmo.micro_utils.common.Warning
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@ -13,6 +14,7 @@ import kotlinx.serialization.encoding.Encoder
* Use BotAction objects realisations to notify user about bot actions * Use BotAction objects realisations to notify user about bot actions
*/ */
@Serializable(BotActionSerializer::class) @Serializable(BotActionSerializer::class)
@ClassCastsIncluded
sealed interface BotAction { sealed interface BotAction {
val actionName: String val actionName: String
} }

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons package dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.games.CallbackGame import dev.inmo.tgbotapi.types.games.CallbackGame
import dev.inmo.tgbotapi.types.webapps.WebAppInfo import dev.inmo.tgbotapi.types.webapps.WebAppInfo
@ -11,6 +12,7 @@ import kotlinx.serialization.json.*
* https://core.telegram.org/bots/api#inlinekeyboardbutton for more info * https://core.telegram.org/bots/api#inlinekeyboardbutton for more info
*/ */
@Serializable(InlineKeyboardButtonSerializer::class) @Serializable(InlineKeyboardButtonSerializer::class)
@ClassCastsIncluded
sealed interface InlineKeyboardButton { sealed interface InlineKeyboardButton {
val text: String val text: String
} }

View File

@ -1,6 +1,8 @@
package dev.inmo.tgbotapi.types.buttons package dev.inmo.tgbotapi.types.buttons
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable(KeyboardMarkupSerializer::class) @Serializable(KeyboardMarkupSerializer::class)
@ClassCastsIncluded
sealed interface KeyboardMarkup sealed interface KeyboardMarkup

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.chat package dev.inmo.tgbotapi.types.chat
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@ -43,6 +44,7 @@ sealed interface AbleToAddInAttachmentMenuChat : Chat {
} }
@Serializable(PreviewChatSerializer::class) @Serializable(PreviewChatSerializer::class)
@ClassCastsIncluded
sealed interface Chat { sealed interface Chat {
val id: ChatId val id: ChatId
} }

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.dice package dev.inmo.tgbotapi.types.dice
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
@ -9,6 +10,7 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
@Serializable(DiceAnimationTypeSerializer::class) @Serializable(DiceAnimationTypeSerializer::class)
@ClassCastsIncluded
sealed interface DiceAnimationType { sealed interface DiceAnimationType {
val emoji: String val emoji: String
val valueLimits: IntRange val valueLimits: IntRange

View File

@ -1,11 +1,13 @@
package dev.inmo.tgbotapi.types.files package dev.inmo.tgbotapi.types.files
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.requests.abstracts.FileId import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.types.FileUniqueId import dev.inmo.tgbotapi.types.FileUniqueId
/** /**
* Declare common part of media files in Telegram. Note: it is not representation of JVM `File` type * Declare common part of media files in Telegram. Note: it is not representation of JVM `File` type
*/ */
@ClassCastsIncluded
sealed interface TelegramMediaFile { sealed interface TelegramMediaFile {
val fileId: FileId val fileId: FileId
val fileUniqueId: FileUniqueId val fileUniqueId: FileUniqueId

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.types.location package dev.inmo.tgbotapi.types.location
import dev.inmo.tgbotapi.abstracts.* import dev.inmo.tgbotapi.abstracts.*
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.* import kotlinx.serialization.*
@ -18,6 +19,7 @@ import kotlinx.serialization.json.JsonObject
* @see dev.inmo.tgbotapi.extensions.utils.asLiveLocation * @see dev.inmo.tgbotapi.extensions.utils.asLiveLocation
*/ */
@Serializable(LocationSerializer::class) @Serializable(LocationSerializer::class)
@ClassCastsIncluded
sealed interface Location : Locationed, HorizontallyAccured sealed interface Location : Locationed, HorizontallyAccured
@Serializable @Serializable

View File

@ -1,9 +1,11 @@
package dev.inmo.tgbotapi.types.media package dev.inmo.tgbotapi.types.media
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.requests.abstracts.InputFile import dev.inmo.tgbotapi.requests.abstracts.InputFile
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable(TelegramMediaSerializer::class) @Serializable(TelegramMediaSerializer::class)
@ClassCastsIncluded
sealed interface TelegramMedia { sealed interface TelegramMedia {
val type: String val type: String
val file: InputFile val file: InputFile

View File

@ -4,6 +4,9 @@ import dev.inmo.tgbotapi.abstracts.WithUser
import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.chat.User
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.PublicChatEvent import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.PublicChatEvent
data class LeftChatMember( data class LeftChatMemberEvent(
override val user: User override val user: User
) : PublicChatEvent, WithUser ) : PublicChatEvent, WithUser
@Deprecated("Renamed", ReplaceWith("dev.inmo.tgbotapi.types.message.ChatEvents", "LeftChatMemberEvent"))
typealias LeftChatMember = LeftChatMemberEvent

View File

@ -1,3 +1,6 @@
package dev.inmo.tgbotapi.types.message.ChatEvents.abstracts package dev.inmo.tgbotapi.types.message.ChatEvents.abstracts
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
@ClassCastsIncluded
interface ChatEvent interface ChatEvent

View File

@ -1,10 +1,12 @@
package dev.inmo.tgbotapi.types.message package dev.inmo.tgbotapi.types.message
import dev.inmo.tgbotapi.abstracts.FromUser import dev.inmo.tgbotapi.abstracts.FromUser
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.* import dev.inmo.tgbotapi.types.chat.*
import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.chat.User
@ClassCastsIncluded
sealed interface ForwardInfo { sealed interface ForwardInfo {
abstract val dateOfOriginal: TelegramDate abstract val dateOfOriginal: TelegramDate

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.types.message.abstracts package dev.inmo.tgbotapi.types.message.abstracts
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.MessageIdentifier import dev.inmo.tgbotapi.types.MessageIdentifier
import dev.inmo.tgbotapi.types.chat.Chat import dev.inmo.tgbotapi.types.chat.Chat
import dev.inmo.tgbotapi.types.message.RawMessage import dev.inmo.tgbotapi.types.message.RawMessage
@ -9,6 +10,7 @@ import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
@ClassCastsIncluded
interface Message { interface Message {
val messageId: MessageIdentifier val messageId: MessageIdentifier
val chat: Chat val chat: Chat

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.message.content package dev.inmo.tgbotapi.types.message.content
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.ChatIdentifier import dev.inmo.tgbotapi.types.ChatIdentifier
import dev.inmo.tgbotapi.types.MessageIdentifier import dev.inmo.tgbotapi.types.MessageIdentifier
@ -108,6 +109,7 @@ sealed interface MediaContent: MessageContent {
fun asTelegramMedia(): TelegramMedia fun asTelegramMedia(): TelegramMedia
} }
@ClassCastsIncluded
sealed interface ResendableContent { sealed interface ResendableContent {
fun createResend( fun createResend(
chatId: ChatIdentifier, chatId: ChatIdentifier,

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.message.textsources package dev.inmo.tgbotapi.types.message.textsources
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.captionLength import dev.inmo.tgbotapi.types.captionLength
import dev.inmo.tgbotapi.types.textLength import dev.inmo.tgbotapi.types.textLength
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@ -10,6 +11,7 @@ typealias TextSourcesList = List<TextSource>
typealias MutableTextSourcesList = MutableList<TextSource> typealias MutableTextSourcesList = MutableList<TextSource>
@Serializable(TextSourceSerializer::class) @Serializable(TextSourceSerializer::class)
@ClassCastsIncluded
sealed interface TextSource { sealed interface TextSource {
val markdown: String val markdown: String
val markdownV2: String val markdownV2: String

View File

@ -5,6 +5,7 @@ package dev.inmo.tgbotapi.types.passport
import dev.inmo.micro_utils.crypto.MD5 import dev.inmo.micro_utils.crypto.MD5
import dev.inmo.micro_utils.crypto.md5 import dev.inmo.micro_utils.crypto.md5
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.* import dev.inmo.tgbotapi.types.passport.encrypted.abstracts.*
import dev.inmo.tgbotapi.types.passport.encrypted.type import dev.inmo.tgbotapi.types.passport.encrypted.type
@ -18,6 +19,7 @@ import kotlinx.serialization.json.*
val ByteArray.passportFileHash: MD5 val ByteArray.passportFileHash: MD5
get() = md5() get() = md5()
@ClassCastsIncluded
@Serializable(PassportElementErrorSerializer::class) @Serializable(PassportElementErrorSerializer::class)
sealed class PassportElementError { sealed class PassportElementError {
abstract val source: String abstract val source: String

View File

@ -1,7 +1,9 @@
package dev.inmo.tgbotapi.types.passport.decrypted.abstracts package dev.inmo.tgbotapi.types.passport.decrypted.abstracts
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.passport.credentials.EndDataCredentials import dev.inmo.tgbotapi.types.passport.credentials.EndDataCredentials
@ClassCastsIncluded
interface SecureValue { interface SecureValue {
val credentials: List<EndDataCredentials> val credentials: List<EndDataCredentials>
} }

View File

@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.types.passport.encrypted.abstracts
import dev.inmo.micro_utils.crypto.SourceBytes import dev.inmo.micro_utils.crypto.SourceBytes
import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer import dev.inmo.micro_utils.serialization.base64.Base64BytesToFromStringSerializer
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer import dev.inmo.tgbotapi.types.passport.encrypted.EncryptedElementSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
@ -9,6 +10,7 @@ import kotlinx.serialization.json.JsonObject
typealias PassportElementHash = SourceBytes typealias PassportElementHash = SourceBytes
@Serializable(EncryptedElementSerializer::class) @Serializable(EncryptedElementSerializer::class)
@ClassCastsIncluded
interface EncryptedPassportElement { interface EncryptedPassportElement {
val hash: PassportElementHash val hash: PassportElementHash
} }

View File

@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.types.polls
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import com.soywiz.klock.TimeSpan import com.soywiz.klock.TimeSpan
import dev.inmo.tgbotapi.abstracts.TextedInput import dev.inmo.tgbotapi.abstracts.TextedInput
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.message.* import dev.inmo.tgbotapi.types.message.*
import dev.inmo.tgbotapi.types.message.RawMessageEntity import dev.inmo.tgbotapi.types.message.RawMessageEntity
@ -16,6 +17,7 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.* import kotlinx.serialization.json.*
@ClassCastsIncluded
sealed interface ScheduledCloseInfo { sealed interface ScheduledCloseInfo {
val closeDateTime: DateTime val closeDateTime: DateTime
} }
@ -45,6 +47,7 @@ val LongSeconds.asExactScheduledCloseInfo
) )
@Serializable(PollSerializer::class) @Serializable(PollSerializer::class)
@ClassCastsIncluded
sealed interface Poll { sealed interface Poll {
val id: PollIdentifier val id: PollIdentifier
val question: String val question: String

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.types.update.abstracts package dev.inmo.tgbotapi.types.update.abstracts
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import dev.inmo.tgbotapi.types.UpdateIdentifier import dev.inmo.tgbotapi.types.UpdateIdentifier
import dev.inmo.tgbotapi.types.update.RawUpdate import dev.inmo.tgbotapi.types.update.RawUpdate
import dev.inmo.tgbotapi.utils.RiskFeature import dev.inmo.tgbotapi.utils.RiskFeature
@ -10,6 +11,7 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonElement
@ClassCastsIncluded
interface Update { interface Update {
val updateId: UpdateIdentifier val updateId: UpdateIdentifier
val data: Any val data: Any

View File

@ -0,0 +1,3 @@
package dev.inmo.tgbotapi.utils.internal
import dev.inmo.tgbotapi.utils.RiskFeature

View File

@ -0,0 +1,8 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
}
project.description = "Class Casts generator KSP library to include into your library"
apply from: "$mppProjectWithSerializationPresetPath"
apply from: "$publishGradlePath"

View File

@ -0,0 +1,5 @@
package dev.inmo.tgbotapi.ksp.lib
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
annotation class ClassCastsIncluded

View File

@ -0,0 +1,13 @@
plugins {
id "org.jetbrains.kotlin.jvm"
}
repositories {
mavenCentral()
}
dependencies {
implementation libs.kotlin.poet
implementation libs.ksp
implementation project(":tgbotapi.ksp:lib")
}

View File

@ -0,0 +1,103 @@
package dev.inmo.tgbotapi.ksp.processor
import com.google.devtools.ksp.symbol.*
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.ksp.*
private fun FileSpec.Builder.addTopLevelImport(className: ClassName) {
className.topLevelClassName().let {
addImport(it.packageName, it.simpleNames)
}
}
private fun FileSpec.Builder.createTypeDefinition(ksClassDeclaration: KSClassDeclaration): TypeName {
val className = ksClassDeclaration.toClassName()
return if (ksClassDeclaration.typeParameters.isNotEmpty()) {
className.parameterizedBy(
ksClassDeclaration.typeParameters.map {
it.bounds.first().resolve().also {
val typeClassName = it.toClassName()
addTopLevelImport(typeClassName)
}.toTypeName()
}
)
} else {
className
}
}
fun FileSpec.Builder.fill(
sourceKSClassDeclaration: KSClassDeclaration,
subtypesMap: Map<KSClassDeclaration, Set<KSClassDeclaration>>,
targetClassDeclaration: KSClassDeclaration = sourceKSClassDeclaration
) {
if (sourceKSClassDeclaration == targetClassDeclaration) {
subtypesMap[sourceKSClassDeclaration] ?.forEach {
fill(sourceKSClassDeclaration, subtypesMap, it)
}
} else {
val sourceClassName = sourceKSClassDeclaration.toClassName()
val targetClassClassName = targetClassDeclaration.toClassName()
val targetClassTypeDefinition = createTypeDefinition(targetClassDeclaration)
val simpleName = targetClassDeclaration.simpleName.asString()
val withFirstLowerCase = simpleName.replaceFirstChar { it.lowercase() }
val castedOrNullName = "${withFirstLowerCase}OrNull"
addTopLevelImport(targetClassClassName)
addFunction(
FunSpec.builder(castedOrNullName).apply {
receiver(sourceClassName)
addCode(
"return this as? %L",
targetClassTypeDefinition
)
returns(targetClassTypeDefinition.copy(nullable = true))
addModifiers(KModifier.INLINE)
}.build()
)
addFunction(
FunSpec.builder("${withFirstLowerCase}OrThrow").apply {
receiver(sourceClassName)
addCode(
"return this as %L",
targetClassTypeDefinition
)
returns(targetClassTypeDefinition)
addModifiers(KModifier.INLINE)
}.build()
)
addFunction(
FunSpec.builder("if$simpleName").apply {
val genericType = TypeVariableName("T", null)
addTypeVariable(genericType)
receiver(sourceClassName)
addParameter(
"block",
LambdaTypeName.get(
null,
targetClassTypeDefinition,
returnType = genericType
)
)
addCode(
"return ${castedOrNullName}() ?.let(block)",
targetClassTypeDefinition
)
returns(genericType.copy(nullable = true))
addModifiers(KModifier.INLINE)
}.build()
)
subtypesMap[targetClassDeclaration] ?.let {
if (it.count { it.classKind == ClassKind.CLASS } > 1) {
it
} else {
it.filter { it.classKind != ClassKind.CLASS }
}
} ?.forEach {
fill(sourceKSClassDeclaration, subtypesMap, it)
fill(targetClassDeclaration, subtypesMap, it)
}
}
}

View File

@ -0,0 +1,65 @@
package dev.inmo.tgbotapi.ksp.processor
import com.google.devtools.ksp.getAllSuperTypes
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.ksp.writeTo
import dev.inmo.tgbotapi.ksp.lib.ClassCastsIncluded
import java.io.File
class TelegramBotAPISymbolProcessor(
private val codeGenerator: CodeGenerator,
private val targetPackage: String = "",
private val outputFile: String = "Output",
private val outputFolder: String? = null
) : SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
val classes = resolver.getSymbolsWithAnnotation(ClassCastsIncluded::class.qualifiedName!!).filterIsInstance<KSClassDeclaration>()
val classesSubtypes = mutableMapOf<KSClassDeclaration, MutableSet<KSClassDeclaration>>()
resolver.getAllFiles().forEach {
it.declarations.forEach { potentialSubtype ->
if (potentialSubtype is KSClassDeclaration) {
val allSupertypes = potentialSubtype.getAllSuperTypes().map { it.declaration }
classes.forEach {
if (it in allSupertypes) {
classesSubtypes.getOrPut(it) { mutableSetOf() }.add(potentialSubtype)
}
}
}
}
}
fun fillWithSealeds(source: KSClassDeclaration, current: KSClassDeclaration = source) {
current.getSealedSubclasses().forEach {
classesSubtypes.getOrPut(source) { mutableSetOf() }.add(it)
fillWithSealeds(source, it)
}
}
classes.forEach { fillWithSealeds(it) }
val fileSpec = FileSpec.builder(
targetPackage,
outputFile
).apply {
classes.forEach {
fill(
it,
classesSubtypes.toMap()
)
}
}.build()
runCatching {
outputFolder ?.also {
File(it).apply {
delete()
runCatching { mkdirs() }
fileSpec.writeTo(this)
}
} ?: fileSpec.writeTo(codeGenerator, false)
}
return emptyList()
}
}

View File

@ -0,0 +1,14 @@
package dev.inmo.tgbotapi.ksp.processor
import com.google.devtools.ksp.processing.*
class TelegramBotAPISymbolProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
return TelegramBotAPISymbolProcessor(
environment.codeGenerator,
environment.options["cctargetPackage"] ?: "",
environment.options["ccoutputFileName"] ?: "Output",
environment.options["ccoutputFolder"],
)
}
}

View File

@ -0,0 +1 @@
dev.inmo.tgbotapi.ksp.processor.TelegramBotAPISymbolProcessorProvider