From d53dc5f07cbfa333e8224244d97e50f398370026 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Sun, 9 Apr 2023 14:14:56 +0200 Subject: [PATCH] Support IDictionary in CollectUsersChats (#137) --- src/Compat.cs | 2 +- src/TL.Extensions.cs | 6 +++--- src/TL.Helpers.cs | 8 ++++---- src/TL.cs | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Compat.cs b/src/Compat.cs index c4db45b..9364fde 100644 --- a/src/Compat.cs +++ b/src/Compat.cs @@ -47,7 +47,7 @@ namespace WTelegram return length; } - public static V GetValueOrDefault(this Dictionary dictionary, K key, V defaultValue = default) + public static V GetValueOrDefault(this IReadOnlyDictionary dictionary, K key, V defaultValue = default) => dictionary.TryGetValue(key, out V value) ? value : defaultValue; public static void Deconstruct(this KeyValuePair kvp, out K key, out V value) { key = kvp.Key; value = kvp.Value; } diff --git a/src/TL.Extensions.cs b/src/TL.Extensions.cs index 3038d0e..0e2a7fe 100644 --- a/src/TL.Extensions.cs +++ b/src/TL.Extensions.cs @@ -15,7 +15,7 @@ namespace TL public override long ID => 0; internal IDictionary _users; internal IDictionary _chats; - protected internal override IPeerInfo UserOrChat(IDictionary users, IDictionary chats) + protected internal override IPeerInfo UserOrChat(Dictionary users, Dictionary chats) { lock (_users) foreach (var user in users.Values) @@ -51,7 +51,7 @@ namespace TL /// Generate premium entities if any /// Dictionary used for tg://user?id= notation /// The array of formatting entities that you can pass (along with the plain text) to SendMessageAsync or SendMediaAsync - public static MessageEntity[] MarkdownToEntities(this Client _, ref string text, bool premium = false, Dictionary users = null) + public static MessageEntity[] MarkdownToEntities(this Client _, ref string text, bool premium = false, IReadOnlyDictionary users = null) { var entities = new List(); var sb = new StringBuilder(text); @@ -251,7 +251,7 @@ namespace TL /// Generate premium entities if any /// Dictionary used for tg://user?id= notation /// The array of formatting entities that you can pass (along with the plain text) to SendMessageAsync or SendMediaAsync - public static MessageEntity[] HtmlToEntities(this Client _, ref string text, bool premium = false, Dictionary users = null) + public static MessageEntity[] HtmlToEntities(this Client _, ref string text, bool premium = false, IReadOnlyDictionary users = null) { var entities = new List(); var sb = new StringBuilder(text); diff --git a/src/TL.Helpers.cs b/src/TL.Helpers.cs index a4bb8a3..463d3bc 100644 --- a/src/TL.Helpers.cs +++ b/src/TL.Helpers.cs @@ -118,25 +118,25 @@ namespace TL partial class Peer { public abstract long ID { get; } - protected internal abstract IPeerInfo UserOrChat(IDictionary users, IDictionary chats); + protected internal abstract IPeerInfo UserOrChat(Dictionary users, Dictionary chats); } partial class PeerUser { public override string ToString() => "user " + user_id; public override long ID => user_id; - protected internal override IPeerInfo UserOrChat(IDictionary users, IDictionary chats) => users.TryGetValue(user_id, out var user) ? user : null; + protected internal override IPeerInfo UserOrChat(Dictionary users, Dictionary chats) => users.TryGetValue(user_id, out var user) ? user : null; } partial class PeerChat { public override string ToString() => "chat " + chat_id; public override long ID => chat_id; - protected internal override IPeerInfo UserOrChat(IDictionary users, IDictionary chats) => chats.TryGetValue(chat_id, out var chat) ? chat : null; + protected internal override IPeerInfo UserOrChat(Dictionary users, Dictionary chats) => chats.TryGetValue(chat_id, out var chat) ? chat : null; } partial class PeerChannel { public override string ToString() => "channel " + channel_id; public override long ID => channel_id; - protected internal override IPeerInfo UserOrChat(IDictionary users, IDictionary chats) => chats.TryGetValue(channel_id, out var chat) ? chat : null; + protected internal override IPeerInfo UserOrChat(Dictionary users, Dictionary chats) => chats.TryGetValue(channel_id, out var chat) ? chat : null; } partial class UserBase : IPeerInfo diff --git a/src/TL.cs b/src/TL.cs index 97e88aa..20659c9 100644 --- a/src/TL.cs +++ b/src/TL.cs @@ -168,9 +168,9 @@ namespace TL else if (type == typeof(Int256)) return new Int256(reader); else if (type == typeof(Dictionary)) - return reader.ReadTLDictionary(u => u.ID); + return reader.ReadTLDictionary(); else if (type == typeof(Dictionary)) - return reader.ReadTLDictionary(c => c.ID); + return reader.ReadTLDictionary(); else return reader.ReadTLObject(); default: @@ -238,7 +238,7 @@ namespace TL throw new WTelegram.WTException($"Cannot deserialize {type.Name} with ctor #{ctorNb:x}"); } - internal static Dictionary ReadTLDictionary(this BinaryReader reader, Func getID) where T : class + internal static Dictionary ReadTLDictionary(this BinaryReader reader) where T : class, IPeerInfo { uint ctorNb = reader.ReadUInt32(); var elementType = typeof(T); @@ -249,7 +249,7 @@ namespace TL for (int i = 0; i < count; i++) { var value = (T)reader.ReadTLValue(elementType); - dict[getID(value)] = value is UserEmpty ? null : value; + dict[value.ID] = value is UserEmpty ? null : value; } return dict; }