From e5f1180d32954d5d85785b200c315255e1569a13 Mon Sep 17 00:00:00 2001 From: "LAPTOP-3CFGGVOS\\rabbit" Date: Sun, 4 Aug 2024 23:05:42 +0800 Subject: [PATCH] Revert "optimize BotSharpMessageParser" This reverts commit 237645033ccb0f47e914589b2a7e95365c12cdbc. --- .../BotSharp.Abstraction.csproj | 1 - .../Messaging/BotSharpMessageParser.cs | 99 +++++++------------ .../Messaging/Enums/RichTypeEnum.cs | 1 - .../Template/ProductTemplateMessage.cs | 2 +- .../Models/RichContent/TextMessage.cs | 2 - 5 files changed, 36 insertions(+), 69 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj index 94ec0f12..570f5c92 100644 --- a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj +++ b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj @@ -25,7 +25,6 @@ - diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/BotSharpMessageParser.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/BotSharpMessageParser.cs index 483900a4..1f495a44 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/BotSharpMessageParser.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/BotSharpMessageParser.cs @@ -2,52 +2,56 @@ using BotSharp.Abstraction.Messaging.Models.RichContent.Template; using BotSharp.Abstraction.Messaging.Models.RichContent; using System.Text.Json; using System.Reflection; -using System.Linq; namespace BotSharp.Abstraction.Messaging; public static class BotSharpMessageParser { - private static Dictionary GenericTemplateTypeMap = new(); - private static Dictionary ElementTypeMap = new(); - private static Dictionary NonGenericTemplateTypeMap = new(); - - static BotSharpMessageParser() - { - var types = AppDomain.CurrentDomain.GetAssemblies() - .SelectMany(assembly => assembly.GetTypes()) - .ToList(); - ElementTypeMap = types - .Where(type => type.Name.EndsWith("Element")) - .ToDictionary(k => k.Name, v => v); - - var richMessageTypes = types - .Where(type => typeof(IRichMessage).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract) - .ToDictionary(k => GetRichTypeValue(k), v => v); - GenericTemplateTypeMap = richMessageTypes.Where(p => p.Value.IsGenericType).ToDictionary(k => k.Key, v => v.Value); - NonGenericTemplateTypeMap = richMessageTypes.Where(p => !p.Value.IsGenericType).ToDictionary(k => k.Key, v => v.Value); - } public static IRichMessage? ParseRichMessage(JsonElement root, JsonSerializerOptions options) { IRichMessage? res = null; Type? targetType = null; + JsonElement element; var jsonText = root.GetRawText(); - if (!root.TryGetProperty("rich_type", out var richTypeElement)) return res; - - string? richType = richTypeElement.GetString(); - if (GenericTemplateTypeMap.TryGetValue(richType, out var wrapperType)) + if (root.TryGetProperty("rich_type", out element)) { - if (root.TryGetProperty("element_type", out var elementTypeElement)) + var richType = element.GetString(); + if (richType == RichTypeEnum.ButtonTemplate) { - string? elementType = elementTypeElement.GetString(); - targetType = CreateGenericElementType(wrapperType, elementType); + targetType = typeof(ButtonTemplateMessage); + } + else if (richType == RichTypeEnum.MultiSelectTemplate) + { + targetType = typeof(MultiSelectTemplateMessage); + } + else if (richType == RichTypeEnum.QuickReply) + { + targetType = typeof(QuickReplyMessage); + } + else if (richType == RichTypeEnum.CouponTemplate) + { + targetType = typeof(CouponTemplateMessage); + } + else if (richType == RichTypeEnum.Text) + { + targetType = typeof(TextMessage); + } + else if (richType == RichTypeEnum.GenericTemplate) + { + if (root.TryGetProperty("element_type", out element)) + { + var elementType = element.GetString(); + var wrapperType = typeof(GenericTemplateMessage<>); + var genericType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x => x.Name == elementType); + + if (wrapperType != null && genericType != null) + { + targetType = wrapperType.MakeGenericType(genericType); + } + } } - } - else if (NonGenericTemplateTypeMap.TryGetValue(richType, out targetType)) - { - // targetType is already set by the dictionary lookup } if (targetType != null) @@ -58,39 +62,6 @@ public static class BotSharpMessageParser return res; } - private static Type? CreateGenericElementType(Type wrapperType, string elementTypeName) - { - if (wrapperType != null && ElementTypeMap.TryGetValue(elementTypeName, out var elementType)) - { - return wrapperType.MakeGenericType(elementType); - } - - return null; - } - - private static string GetRichTypeValue(Type type) - { - var richTypeProperty = type.GetProperty("RichType", BindingFlags.Public | BindingFlags.Instance); - if (richTypeProperty != null && richTypeProperty.PropertyType == typeof(string)) - { - return CreateRichMessage(type)?.RichType; - } - return null; - } - - private static dynamic CreateRichMessage(Type type) - { - if (!type.IsGenericType) - { - return Activator.CreateInstance(type); - } - else - { - var genericType = type.MakeGenericType(typeof(object)); - return Activator.CreateInstance(genericType); - } - } - public static ITemplateMessage? ParseTemplateMessage(JsonElement root, JsonSerializerOptions options) { ITemplateMessage? res = null; diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/RichTypeEnum.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/RichTypeEnum.cs index 968aa30c..7603f790 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/RichTypeEnum.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Enums/RichTypeEnum.cs @@ -9,5 +9,4 @@ public static class RichTypeEnum public const string QuickReply = "quick_reply"; public const string Text = "text"; public const string Attachment = "attachment"; - public const string ProductTemplate = "product_template"; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ProductTemplateMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ProductTemplateMessage.cs index 201131be..05af097a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ProductTemplateMessage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ProductTemplateMessage.cs @@ -3,7 +3,7 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template; public class ProductTemplateMessage : IRichMessage, ITemplateMessage { [JsonPropertyName("rich_type")] - public string RichType => RichTypeEnum.ProductTemplate; + public string RichType => RichTypeEnum.GenericTemplate; [JsonPropertyName("text")] [Translate] diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs index 966701bd..3a37ad54 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/TextMessage.cs @@ -8,8 +8,6 @@ public class TextMessage : IRichMessage [Translate] public string Text { get; set; } = string.Empty; - public TextMessage() { } - public TextMessage(string text) { Text = text;