From 92add58180d31e4464268021d5fd4d378ce4c788 Mon Sep 17 00:00:00 2001 From: Rikitav Date: Fri, 1 Aug 2025 15:03:19 +0400 Subject: [PATCH] * moved WelcomeAttribute to a separate namespace for special case-targetted attribute * Added "MethodHandlerDescriptor" a class that provides ability to implicitly create handlers from methods that matches the AbstractHandlerAction delegate * Added extension method for HandlersCollection for adding methods as handlers --- .../{ => Targetted}/WelcomeAttribute.cs | 2 +- .../Handlers/Building/HandlerBuilder.cs | 1 - .../Descriptors/DefaultCustomDescriptors.cs | 55 ++++++++++++++----- .../Descriptors/HandlerDescriptor.cs | 25 +++++++-- Telegrator/TypesExtensions.cs | 15 +++-- 5 files changed, 74 insertions(+), 24 deletions(-) rename Telegrator/Annotations/{ => Targetted}/WelcomeAttribute.cs (94%) diff --git a/Telegrator/Annotations/WelcomeAttribute.cs b/Telegrator/Annotations/Targetted/WelcomeAttribute.cs similarity index 94% rename from Telegrator/Annotations/WelcomeAttribute.cs rename to Telegrator/Annotations/Targetted/WelcomeAttribute.cs index 555b87f..590f36c 100644 --- a/Telegrator/Annotations/WelcomeAttribute.cs +++ b/Telegrator/Annotations/Targetted/WelcomeAttribute.cs @@ -2,7 +2,7 @@ using Telegram.Bot.Types.Enums; using Telegrator.Filters; -namespace Telegrator.Annotations +namespace Telegrator.Annotations.Targetted { /// /// Attribute for filtering message with command "start" in bot's private chats. diff --git a/Telegrator/Handlers/Building/HandlerBuilder.cs b/Telegrator/Handlers/Building/HandlerBuilder.cs index c7cd88b..a9dda1c 100644 --- a/Telegrator/Handlers/Building/HandlerBuilder.cs +++ b/Telegrator/Handlers/Building/HandlerBuilder.cs @@ -1,5 +1,4 @@ using Telegram.Bot.Types.Enums; -using Telegrator.Providers; using Telegrator.Handlers.Building.Components; using Telegrator.MadiatorCore; diff --git a/Telegrator/MadiatorCore/Descriptors/DefaultCustomDescriptors.cs b/Telegrator/MadiatorCore/Descriptors/DefaultCustomDescriptors.cs index 00c6c0a..d4728e8 100644 --- a/Telegrator/MadiatorCore/Descriptors/DefaultCustomDescriptors.cs +++ b/Telegrator/MadiatorCore/Descriptors/DefaultCustomDescriptors.cs @@ -1,24 +1,53 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Text; +using System.Reflection; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; +using Telegrator.Attributes.Components; +using Telegrator.Filters.Components; using Telegrator.Handlers; +using Telegrator.Handlers.Building; using Telegrator.Handlers.Components; namespace Telegrator.MadiatorCore.Descriptors { - /* - public class MethodHandlerDescriptor : HandlerDescriptor + public class MethodHandlerDescriptor : HandlerDescriptor where TUpdate : class { - public MethodHandlerDescriptor() : base(DescriptorType.General, ) - } - - public class MethodHandler : AbstractUpdateHandler - { - public override Task Execute(IAbstractHandlerContainer container, CancellationToken cancellation) + public MethodHandlerDescriptor(AbstractHandlerAction action) : base(DescriptorType.General) { + UpdateHandlerAttributeBase handlerAttribute = HandlerInspector.GetHandlerAttribute(action.Method); + StateKeeperAttributeBase? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(action.Method); + IFilter[] filters = HandlerInspector.GetFilterAttributes(action.Method, handlerAttribute.Type).ToArray(); + UpdateType = handlerAttribute.Type; + Indexer = handlerAttribute.GetIndexer(); + Filters = new DescriptorFiltersSet(handlerAttribute, stateKeeperAttribute, filters); + DisplayString = HandlerInspector.GetDisplayName(action.Method); + InstanceFactory = () => new MethodHandler(action.Method, UpdateType); + } + + private class MethodHandler(MethodInfo method, UpdateType updateType) : AbstractUpdateHandler(updateType) + { + private readonly MethodInfo Method = method; + + public override async Task Execute(IAbstractHandlerContainer container, CancellationToken cancellation) + { + if (Method is null) + throw new Exception(); + + if (Method.ReturnType == typeof(void)) + { + Method.Invoke(this, [container, cancellation]); + return; + } + else + { + object branchReturn = Method.Invoke(this, [container, cancellation]); + if (branchReturn == null) + return; + + if (branchReturn is Task branchTask) + await branchTask; + } + } } } - */ } diff --git a/Telegrator/MadiatorCore/Descriptors/HandlerDescriptor.cs b/Telegrator/MadiatorCore/Descriptors/HandlerDescriptor.cs index 6ceb42a..e582cea 100644 --- a/Telegrator/MadiatorCore/Descriptors/HandlerDescriptor.cs +++ b/Telegrator/MadiatorCore/Descriptors/HandlerDescriptor.cs @@ -61,7 +61,7 @@ namespace Telegrator.MadiatorCore.Descriptors public UpdateType UpdateType { get; - private set; + protected set; } /// @@ -79,7 +79,7 @@ namespace Telegrator.MadiatorCore.Descriptors public DescriptorFiltersSet Filters { get; - private set; + protected set; } /// @@ -88,7 +88,7 @@ namespace Telegrator.MadiatorCore.Descriptors public object? ServiceKey { get; - private set; + protected set; } /// @@ -97,7 +97,7 @@ namespace Telegrator.MadiatorCore.Descriptors public Func? InstanceFactory { get; - private set; + protected set; } /// @@ -106,7 +106,7 @@ namespace Telegrator.MadiatorCore.Descriptors public UpdateHandlerBase? SingletonInstance { get; - set; + protected set; } /// @@ -127,6 +127,13 @@ namespace Telegrator.MadiatorCore.Descriptors set; } + internal HandlerDescriptor(DescriptorType descriptorType) + { + Type = descriptorType; + HandlerType = null!; + Filters = new DescriptorFiltersSet(null, null, null); + } + /// /// Initializes a new instance of the class with the specified descriptor type and handler type. /// Automatically inspects the handler type to extract attributes, filters, and configuration. @@ -413,6 +420,14 @@ namespace Telegrator.MadiatorCore.Descriptors InstanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory)); } + public void SetInstance(UpdateHandlerBase instance) + { + if (SingletonInstance != null) + throw new Exception(); + + SingletonInstance = instance; + } + /// public override string ToString() => DisplayString ?? HandlerType.Name; diff --git a/Telegrator/TypesExtensions.cs b/Telegrator/TypesExtensions.cs index 55b76be..a65ce46 100644 --- a/Telegrator/TypesExtensions.cs +++ b/Telegrator/TypesExtensions.cs @@ -356,12 +356,19 @@ namespace Telegrator public static HandlerBuilder CreateCallbackQuery(this IHandlersCollection handlers) => handlers.CreateHandler(UpdateType.CallbackQuery); - /* - public static IHandlersCollection AddMethod(this IHandlersCollection handlers, Func, CancellationToken, Task> method) + /// + /// Creates implicit handler from method + /// + /// + /// + /// + /// + public static IHandlersCollection AddMethod(this IHandlersCollection handlers, AbstractHandlerAction method) where TUpdate : class { - + MethodHandlerDescriptor descriptor = new MethodHandlerDescriptor(method); + handlers.AddDescriptor(descriptor); + return handlers; } - */ } ///