1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 19:48:43 +00:00

add excluding of impls

This commit is contained in:
InsanusMokrassar 2023-06-12 12:25:43 +06:00
parent 6b94215a7c
commit ad5cc6ade6
6 changed files with 888 additions and 3639 deletions

View File

@ -49,7 +49,7 @@ sealed interface AbleToAddInAttachmentMenuChat : Chat {
}
@Serializable(PreviewChatSerializer::class)
@ClassCastsIncluded
@ClassCastsIncluded(excludeRegex = ".*Impl.kt")
sealed interface Chat {
val id: IdChatIdentifier
}

View File

@ -3,11 +3,13 @@ package dev.inmo.tgbotapi.types.chat
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.types.message.abstracts.TelegramBotAPIMessageDeserializeOnlySerializer
import dev.inmo.tgbotapi.utils.RiskFeature
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject
@Serializable
@RiskFeature("This class is a subject of changes. It is better to use ExtendedChannelChat due")
data class ExtendedChannelChatImpl(
@SerialName(idField)
override val id: ChatId,
@ -33,6 +35,7 @@ data class ExtendedChannelChatImpl(
) : ExtendedChannelChat
@Serializable
@RiskFeature("This class is a subject of changes. It is better to use ExtendedGroupChat due")
data class ExtendedGroupChatImpl(
@SerialName(idField)
override val id: ChatId,
@ -54,6 +57,7 @@ data class ExtendedGroupChatImpl(
) : ExtendedGroupChat
@Serializable
@RiskFeature("This class is a subject of changes. It is better to use ExtendedPrivateChat due")
data class ExtendedPrivateChatImpl(
@SerialName(idField)
override val id: UserId,
@ -80,6 +84,7 @@ data class ExtendedPrivateChatImpl(
typealias ExtendedUser = ExtendedPrivateChatImpl
@Serializable
@RiskFeature("This class is a subject of changes. It is better to use ExtendedSupergroupChat due")
data class ExtendedSupergroupChatImpl(
@SerialName(idField)
override val id: ChatId,
@ -121,6 +126,7 @@ data class ExtendedSupergroupChatImpl(
) : ExtendedSupergroupChat
@Serializable
@RiskFeature("This class is a subject of changes. It is better to use ExtendedForumChat due")
data class ExtendedForumChatImpl(
@SerialName(idField)
override val id: IdChatIdentifier,

View File

@ -4,10 +4,12 @@ import dev.inmo.micro_utils.language_codes.IetfLanguageCode
import dev.inmo.micro_utils.language_codes.IetfLanguageCodeSerializer
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.abstracts.WithOptionalLanguageCode
import dev.inmo.tgbotapi.utils.RiskFeature
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
@RiskFeature("This class is a subject of changes. It is better to use GroupChat due")
data class GroupChatImpl(
@SerialName(idField)
override val id: ChatId,
@ -16,6 +18,7 @@ data class GroupChatImpl(
) : GroupChat
@Serializable
@RiskFeature("This class is a subject of changes. It is better to use PrivateChat due")
data class PrivateChatImpl(
@SerialName(idField)
override val id: UserId,
@ -28,6 +31,7 @@ data class PrivateChatImpl(
) : PrivateChat
@Serializable
@RiskFeature("This class is a subject of changes. It is better to use SupergroupChat due")
data class SupergroupChatImpl(
@SerialName(idField)
override val id: ChatId,
@ -38,6 +42,7 @@ data class SupergroupChatImpl(
) : SupergroupChat
@Serializable
@RiskFeature("This class is a subject of changes. It is better to use ForumChat due")
data class ForumChatImpl(
@SerialName(idField)
override val id: IdChatIdentifier,
@ -48,6 +53,7 @@ data class ForumChatImpl(
) : ForumChat
@Serializable
@RiskFeature("This class is a subject of changes. It is better to use ChannelChat due")
data class ChannelChatImpl(
@SerialName(idField)
override val id: ChatId,

View File

@ -2,4 +2,4 @@ package dev.inmo.tgbotapi.utils.internal
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.SOURCE)
internal annotation class ClassCastsIncluded
internal annotation class ClassCastsIncluded(val typesRegex: String = ".*", val excludeRegex: String = "")

View File

@ -1,11 +1,14 @@
package dev.inmo.tgbotapi.ksp.processor
import com.google.devtools.ksp.getAllSuperTypes
import com.google.devtools.ksp.getAnnotationsByType
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.writeTo
import java.io.File
@ -15,17 +18,37 @@ class TelegramBotAPISymbolProcessor(
private val outputFile: String = "Output",
private val outputFolder: String? = null
) : SymbolProcessor {
private val classCastsIncludedClassName = ClassName("dev.inmo.tgbotapi.utils.internal", "ClassCastsIncluded")
override fun process(resolver: Resolver): List<KSAnnotated> {
val classes = resolver.getSymbolsWithAnnotation("dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded").filterIsInstance<KSClassDeclaration>()
val classes = resolver.getSymbolsWithAnnotation(classCastsIncludedClassName.canonicalName).filterIsInstance<KSClassDeclaration>()
val classesRegexes: Map<KSClassDeclaration, Pair<Regex, Regex?>> = classes.mapNotNull {
it to (it.annotations.firstNotNullOfOrNull {
runCatching {
if (it.annotationType.resolve().toClassName() == classCastsIncludedClassName) {
val regex = it.arguments.first().value as? String ?: return@runCatching null
val negativeRegex = (it.arguments.first().value as? String) ?.takeIf { it.isNotEmpty() }
Regex(regex) to (negativeRegex ?.let(::Regex))
} else {
null
}
}.getOrNull()
} ?: return@mapNotNull null)
}.toMap()
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)
for (currentClass in classes) {
val regexes = classesRegexes[currentClass]
when {
currentClass in allSupertypes
&& regexes ?.first ?.matches(potentialSubtype.simpleName.toString()) != false
&& regexes ?.second ?.matches(potentialSubtype.simpleName.toString()) != true-> {
classesSubtypes.getOrPut(currentClass) { mutableSetOf() }.add(potentialSubtype)
}
}
}
}