* 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<T> delegate * Added extension method for HandlersCollection for adding methods as handlers
This commit is contained in:
+1
-1
@@ -2,7 +2,7 @@
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
namespace Telegrator.Annotations.Targetted
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute for filtering message with command "start" in bot's private chats.
|
||||
@@ -1,5 +1,4 @@
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Providers;
|
||||
using Telegrator.Handlers.Building.Components;
|
||||
using Telegrator.MadiatorCore;
|
||||
|
||||
|
||||
@@ -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<TUpdate> : HandlerDescriptor where TUpdate : class
|
||||
{
|
||||
public MethodHandlerDescriptor() : base(DescriptorType.General, )
|
||||
}
|
||||
|
||||
public class MethodHandler<TUpdate> : AbstractUpdateHandler<TUpdate>
|
||||
{
|
||||
public override Task Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation)
|
||||
public MethodHandlerDescriptor(AbstractHandlerAction<TUpdate> action) : base(DescriptorType.General)
|
||||
{
|
||||
UpdateHandlerAttributeBase handlerAttribute = HandlerInspector.GetHandlerAttribute(action.Method);
|
||||
StateKeeperAttributeBase? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(action.Method);
|
||||
IFilter<Update>[] 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<TUpdate>(updateType)
|
||||
{
|
||||
private readonly MethodInfo Method = method;
|
||||
|
||||
public override async Task Execute(IAbstractHandlerContainer<TUpdate> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
||||
public UpdateType UpdateType
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
protected set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -79,7 +79,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
||||
public DescriptorFiltersSet Filters
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
protected set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -88,7 +88,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
||||
public object? ServiceKey
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
protected set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -97,7 +97,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
||||
public Func<UpdateHandlerBase>? InstanceFactory
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
protected set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -106,7 +106,7 @@ namespace Telegrator.MadiatorCore.Descriptors
|
||||
public UpdateHandlerBase? SingletonInstance
|
||||
{
|
||||
get;
|
||||
set;
|
||||
protected set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -127,6 +127,13 @@ namespace Telegrator.MadiatorCore.Descriptors
|
||||
set;
|
||||
}
|
||||
|
||||
internal HandlerDescriptor(DescriptorType descriptorType)
|
||||
{
|
||||
Type = descriptorType;
|
||||
HandlerType = null!;
|
||||
Filters = new DescriptorFiltersSet(null, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HandlerDescriptor"/> 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;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string ToString()
|
||||
=> DisplayString ?? HandlerType.Name;
|
||||
|
||||
@@ -356,12 +356,19 @@ namespace Telegrator
|
||||
public static HandlerBuilder<CallbackQuery> CreateCallbackQuery(this IHandlersCollection handlers)
|
||||
=> handlers.CreateHandler<CallbackQuery>(UpdateType.CallbackQuery);
|
||||
|
||||
/*
|
||||
public static IHandlersCollection AddMethod<TUpdate>(this IHandlersCollection handlers, Func<IAbstractHandlerContainer<TUpdate>, CancellationToken, Task> method)
|
||||
/// <summary>
|
||||
/// Creates implicit handler from method
|
||||
/// </summary>
|
||||
/// <typeparam name="TUpdate"></typeparam>
|
||||
/// <param name="handlers"></param>
|
||||
/// <param name="method"></param>
|
||||
/// <returns></returns>
|
||||
public static IHandlersCollection AddMethod<TUpdate>(this IHandlersCollection handlers, AbstractHandlerAction<TUpdate> method) where TUpdate : class
|
||||
{
|
||||
|
||||
MethodHandlerDescriptor<TUpdate> descriptor = new MethodHandlerDescriptor<TUpdate>(method);
|
||||
handlers.AddDescriptor(descriptor);
|
||||
return handlers;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user