From a4c024aa13fd042d45520d1f54fd3563b77e04e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Hjorth=C3=B8j?= Date: Fri, 24 Sep 2021 14:51:33 +0200 Subject: [PATCH] Use 'switch' expression (#187) * Use 'switch' expression * Reverted it to the old switch case --- src/Discord.Net.Core/CDN.cs | 35 +++++++------------ .../Interactions/AutocompleteResult.cs | 20 ++++------- .../Permissions/ChannelPermissions.cs | 16 ++++----- .../API/Common/ActionRowComponent.cs | 13 +++---- src/Discord.Net.Rest/DiscordRestApiClient.cs | 35 +++++++------------ .../Entities/Channels/RestChannel.cs | 32 ++++++----------- .../Entities/Channels/RestGuildChannel.cs | 25 +++++-------- .../Interactions/InteractionHelper.cs | 2 +- .../Net/Converters/EmbedTypeConverter.cs | 33 +++++++---------- .../Net/Converters/UserStatusConverter.cs | 24 +++++-------- src/Discord.Net.Rest/Net/DefaultRestClient.cs | 16 ++++----- .../Entities/Channels/SocketChannel.cs | 13 +++---- .../Entities/Channels/SocketChannelHelper.cs | 12 +++---- .../Entities/Channels/SocketGuildChannel.cs | 25 +++++-------- .../Entities/Interaction/SocketInteraction.cs | 15 ++++---- .../Entities/Invites/SocketInvite.cs | 18 +++++----- 16 files changed, 128 insertions(+), 206 deletions(-) diff --git a/src/Discord.Net.Core/CDN.cs b/src/Discord.Net.Core/CDN.cs index b48bef379..78fba574f 100644 --- a/src/Discord.Net.Core/CDN.cs +++ b/src/Discord.Net.Core/CDN.cs @@ -190,37 +190,26 @@ namespace Discord private static string FormatToExtension(StickerFormatType format) { - switch (format) + return format switch { - case StickerFormatType.None: - case StickerFormatType.Png: - case StickerFormatType.Apng: // In the case of the Sticker endpoint, the sticker will be available as PNG if its format_type is PNG or APNG, and as Lottie if its format_type is LOTTIE. - return "png"; - case StickerFormatType.Lottie: - return "lottie"; - default: - throw new ArgumentException(nameof(format)); - - } + StickerFormatType.None or StickerFormatType.Png or StickerFormatType.Apng => "png", // In the case of the Sticker endpoint, the sticker will be available as PNG if its format_type is PNG or APNG, and as Lottie if its format_type is LOTTIE. + StickerFormatType.Lottie => "lottie", + _ => throw new ArgumentException(nameof(format)), + }; } private static string FormatToExtension(ImageFormat format, string imageId) { if (format == ImageFormat.Auto) format = imageId.StartsWith("a_") ? ImageFormat.Gif : ImageFormat.Png; - switch (format) + return format switch { - case ImageFormat.Gif: - return "gif"; - case ImageFormat.Jpeg: - return "jpeg"; - case ImageFormat.Png: - return "png"; - case ImageFormat.WebP: - return "webp"; - default: - throw new ArgumentException(nameof(format)); - } + ImageFormat.Gif => "gif", + ImageFormat.Jpeg => "jpeg", + ImageFormat.Png => "png", + ImageFormat.WebP => "webp", + _ => throw new ArgumentException(nameof(format)), + }; } } } diff --git a/src/Discord.Net.Core/Entities/Interactions/AutocompleteResult.cs b/src/Discord.Net.Core/Entities/Interactions/AutocompleteResult.cs index 2536c3c51..c152c1f27 100644 --- a/src/Discord.Net.Core/Entities/Interactions/AutocompleteResult.cs +++ b/src/Discord.Net.Core/Entities/Interactions/AutocompleteResult.cs @@ -53,21 +53,13 @@ namespace Discord if (value == null) throw new ArgumentNullException("Value cannot be null"); - switch (value) + _value = value switch { - case string str: - _value = str; - break; - case int integer: - _value = integer; - break; - case double number: - _value = number; - break; - - default: - throw new ArgumentException($"Type {value.GetType().Name} cannot be set as a value! Only string, int, and double allowed!"); - } + string str => str, + int integer => integer, + double number => number, + _ => throw new ArgumentException($"Type {value.GetType().Name} cannot be set as a value! Only string, int, and double allowed!"), + }; } } diff --git a/src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs b/src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs index d774cc51d..287e69783 100644 --- a/src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs +++ b/src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs @@ -24,15 +24,15 @@ namespace Discord /// Unknown channel type. public static ChannelPermissions All(IChannel channel) { - switch (channel) + return channel switch { - case ITextChannel _: return Text; - case IVoiceChannel _: return Voice; - case ICategoryChannel _: return Category; - case IDMChannel _: return DM; - case IGroupChannel _: return Group; - default: throw new ArgumentException(message: "Unknown channel type.", paramName: nameof(channel)); - } + ITextChannel _ => Text, + IVoiceChannel _ => Voice, + ICategoryChannel _ => Category, + IDMChannel _ => DM, + IGroupChannel _ => Group, + _ => throw new ArgumentException(message: "Unknown channel type.", paramName: nameof(channel)), + }; } /// Gets a packed value representing all the permissions in this . diff --git a/src/Discord.Net.Rest/API/Common/ActionRowComponent.cs b/src/Discord.Net.Rest/API/Common/ActionRowComponent.cs index 417fb1b98..92778849c 100644 --- a/src/Discord.Net.Rest/API/Common/ActionRowComponent.cs +++ b/src/Discord.Net.Rest/API/Common/ActionRowComponent.cs @@ -22,15 +22,12 @@ namespace Discord.API Type = c.Type; Components = c.Components?.Select(x => { - switch (x.Type) + return x.Type switch { - case ComponentType.Button: - return new ButtonComponent(x as Discord.ButtonComponent); - case ComponentType.SelectMenu: - return new SelectMenuComponent(x as Discord.SelectMenuComponent); - default: return null; - - } + ComponentType.Button => new ButtonComponent(x as Discord.ButtonComponent), + ComponentType.SelectMenu => new SelectMenuComponent(x as Discord.SelectMenuComponent), + _ => null, + }; }).ToArray(); } diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index dfe003f53..461981e28 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -715,22 +715,12 @@ namespace Discord.API int limit = args.Limit.GetValueOrDefault(DiscordConfig.MaxMessagesPerBatch); ulong? relativeId = args.RelativeMessageId.IsSpecified ? args.RelativeMessageId.Value : (ulong?)null; - string relativeDir; - - switch (args.RelativeDirection.GetValueOrDefault(Direction.Before)) + var relativeDir = args.RelativeDirection.GetValueOrDefault(Direction.Before) switch { - case Direction.Before: - default: - relativeDir = "before"; - break; - case Direction.After: - relativeDir = "after"; - break; - case Direction.Around: - relativeDir = "around"; - break; - } - + Direction.After => "after", + Direction.Around => "around", + _ => "before", + }; var ids = new BucketIds(channelId: channelId); Expression> endpoint; if (relativeId != null) @@ -2181,15 +2171,14 @@ namespace Discord.API internal static int? GetIndex(string name) { - switch (name) + return name switch { - case "httpMethod": return 0; - case "guildId": return 1; - case "channelId": return 2; - case "webhookId": return 3; - default: - return null; - } + "httpMethod" => 0, + "guildId" => 1, + "channelId" => 2, + "webhookId" => 3, + _ => null, + }; } } diff --git a/src/Discord.Net.Rest/Entities/Channels/RestChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestChannel.cs index b653a2db4..ea7988155 100644 --- a/src/Discord.Net.Rest/Entities/Channels/RestChannel.cs +++ b/src/Discord.Net.Rest/Entities/Channels/RestChannel.cs @@ -22,33 +22,23 @@ namespace Discord.Rest /// Unexpected channel type. internal static RestChannel Create(BaseDiscordClient discord, Model model) { - switch (model.Type) + return model.Type switch { - case ChannelType.News: - case ChannelType.Text: - case ChannelType.Voice: - return RestGuildChannel.Create(discord, new RestGuild(discord, model.GuildId.Value), model); - case ChannelType.DM: - case ChannelType.Group: - return CreatePrivate(discord, model) as RestChannel; - case ChannelType.Category: - return RestCategoryChannel.Create(discord, new RestGuild(discord, model.GuildId.Value), model); - default: - return new RestChannel(discord, model.Id); - } + ChannelType.News or ChannelType.Text or ChannelType.Voice => RestGuildChannel.Create(discord, new RestGuild(discord, model.GuildId.Value), model), + ChannelType.DM or ChannelType.Group => CreatePrivate(discord, model) as RestChannel, + ChannelType.Category => RestCategoryChannel.Create(discord, new RestGuild(discord, model.GuildId.Value), model), + _ => new RestChannel(discord, model.Id), + }; } /// Unexpected channel type. internal static IRestPrivateChannel CreatePrivate(BaseDiscordClient discord, Model model) { - switch (model.Type) + return model.Type switch { - case ChannelType.DM: - return RestDMChannel.Create(discord, model); - case ChannelType.Group: - return RestGroupChannel.Create(discord, model); - default: - throw new InvalidOperationException($"Unexpected channel type: {model.Type}"); - } + ChannelType.DM => RestDMChannel.Create(discord, model), + ChannelType.Group => RestGroupChannel.Create(discord, model), + _ => throw new InvalidOperationException($"Unexpected channel type: {model.Type}"), + }; } internal virtual void Update(Model model) { } diff --git a/src/Discord.Net.Rest/Entities/Channels/RestGuildChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestGuildChannel.cs index 549a4ba31..70267bf73 100644 --- a/src/Discord.Net.Rest/Entities/Channels/RestGuildChannel.cs +++ b/src/Discord.Net.Rest/Entities/Channels/RestGuildChannel.cs @@ -33,23 +33,16 @@ namespace Discord.Rest } internal static RestGuildChannel Create(BaseDiscordClient discord, IGuild guild, Model model) { - switch (model.Type) + return model.Type switch { - case ChannelType.News: - return RestNewsChannel.Create(discord, guild, model); - case ChannelType.Text: - return RestTextChannel.Create(discord, guild, model); - case ChannelType.Voice: - return RestVoiceChannel.Create(discord, guild, model); - case ChannelType.Stage: - return RestStageChannel.Create(discord, guild, model); - case ChannelType.Category: - return RestCategoryChannel.Create(discord, guild, model); - case ChannelType.PublicThread or ChannelType.PrivateThread or ChannelType.NewsThread: - return RestThreadChannel.Create(discord, guild, model); - default: - return new RestGuildChannel(discord, guild, model.Id); - } + ChannelType.News => RestNewsChannel.Create(discord, guild, model), + ChannelType.Text => RestTextChannel.Create(discord, guild, model), + ChannelType.Voice => RestVoiceChannel.Create(discord, guild, model), + ChannelType.Stage => RestStageChannel.Create(discord, guild, model), + ChannelType.Category => RestCategoryChannel.Create(discord, guild, model), + ChannelType.PublicThread or ChannelType.PrivateThread or ChannelType.NewsThread => RestThreadChannel.Create(discord, guild, model), + _ => new RestGuildChannel(discord, guild, model.Id), + }; } internal override void Update(Model model) { diff --git a/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs b/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs index 52449a121..8f9163692 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs @@ -43,7 +43,7 @@ namespace Discord.Rest RestFollowupMessage entity = RestFollowupMessage.Create(client, model, token, channel); return entity; } -#endregion + #endregion #region Global commands public static async Task GetGlobalCommandAsync(BaseDiscordClient client, ulong id, diff --git a/src/Discord.Net.Rest/Net/Converters/EmbedTypeConverter.cs b/src/Discord.Net.Rest/Net/Converters/EmbedTypeConverter.cs index 1e03fb698..cacd2e2e1 100644 --- a/src/Discord.Net.Rest/Net/Converters/EmbedTypeConverter.cs +++ b/src/Discord.Net.Rest/Net/Converters/EmbedTypeConverter.cs @@ -13,28 +13,19 @@ namespace Discord.Net.Converters public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - switch ((string)reader.Value) + return (string)reader.Value switch { - case "rich": - return EmbedType.Rich; - case "link": - return EmbedType.Link; - case "video": - return EmbedType.Video; - case "image": - return EmbedType.Image; - case "gifv": - return EmbedType.Gifv; - case "article": - return EmbedType.Article; - case "tweet": - return EmbedType.Tweet; - case "html": - return EmbedType.Html; - case "application_news": // TODO 2.2 EmbedType.News - default: - return EmbedType.Unknown; - } + "rich" => EmbedType.Rich, + "link" => EmbedType.Link, + "video" => EmbedType.Video, + "image" => EmbedType.Image, + "gifv" => EmbedType.Gifv, + "article" => EmbedType.Article, + "tweet" => EmbedType.Tweet, + "html" => EmbedType.Html, + // TODO 2.2 EmbedType.News + _ => EmbedType.Unknown, + }; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) diff --git a/src/Discord.Net.Rest/Net/Converters/UserStatusConverter.cs b/src/Discord.Net.Rest/Net/Converters/UserStatusConverter.cs index c0a287c16..8a13e79a5 100644 --- a/src/Discord.Net.Rest/Net/Converters/UserStatusConverter.cs +++ b/src/Discord.Net.Rest/Net/Converters/UserStatusConverter.cs @@ -1,4 +1,4 @@ -using Newtonsoft.Json; +using Newtonsoft.Json; using System; namespace Discord.Net.Converters @@ -13,21 +13,15 @@ namespace Discord.Net.Converters public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - switch ((string)reader.Value) + return (string)reader.Value switch { - case "online": - return UserStatus.Online; - case "idle": - return UserStatus.Idle; - case "dnd": - return UserStatus.DoNotDisturb; - case "invisible": - return UserStatus.Invisible; //Should never happen - case "offline": - return UserStatus.Offline; - default: - throw new JsonSerializationException("Unknown user status"); - } + "online" => UserStatus.Online, + "idle" => UserStatus.Idle, + "dnd" => UserStatus.DoNotDisturb, + "invisible" => UserStatus.Invisible,//Should never happen + "offline" => UserStatus.Offline, + _ => throw new JsonSerializationException("Unknown user status"), + }; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) diff --git a/src/Discord.Net.Rest/Net/DefaultRestClient.cs b/src/Discord.Net.Rest/Net/DefaultRestClient.cs index 62ebd6d78..1db743609 100644 --- a/src/Discord.Net.Rest/Net/DefaultRestClient.cs +++ b/src/Discord.Net.Rest/Net/DefaultRestClient.cs @@ -157,15 +157,15 @@ namespace Discord.Net.Rest private static readonly HttpMethod Patch = new HttpMethod("PATCH"); private HttpMethod GetMethod(string method) { - switch (method) + return method switch { - case "DELETE": return HttpMethod.Delete; - case "GET": return HttpMethod.Get; - case "PATCH": return Patch; - case "POST": return HttpMethod.Post; - case "PUT": return HttpMethod.Put; - default: throw new ArgumentOutOfRangeException(nameof(method), $"Unknown HttpMethod: {method}"); - } + "DELETE" => HttpMethod.Delete, + "GET" => HttpMethod.Get, + "PATCH" => Patch, + "POST" => HttpMethod.Post, + "PUT" => HttpMethod.Put, + _ => throw new ArgumentOutOfRangeException(nameof(method), $"Unknown HttpMethod: {method}"), + }; } } } diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs index 80c43a0c3..758ee9271 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs @@ -31,15 +31,12 @@ namespace Discord.WebSocket /// Unexpected channel type is created. internal static ISocketPrivateChannel CreatePrivate(DiscordSocketClient discord, ClientState state, Model model) { - switch (model.Type) + return model.Type switch { - case ChannelType.DM: - return SocketDMChannel.Create(discord, state, model); - case ChannelType.Group: - return SocketGroupChannel.Create(discord, state, model); - default: - throw new InvalidOperationException($"Unexpected channel type: {model.Type}"); - } + ChannelType.DM => SocketDMChannel.Create(discord, state, model), + ChannelType.Group => SocketGroupChannel.Create(discord, state, model), + _ => throw new InvalidOperationException($"Unexpected channel type: {model.Type}"), + }; } internal abstract void Update(ClientState state, Model model); #endregion diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs index 4a1dc45c7..ccbf9b2b6 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs @@ -79,13 +79,13 @@ namespace Discord.WebSocket public static SocketMessage RemoveMessage(ISocketMessageChannel channel, DiscordSocketClient discord, ulong id) { - switch (channel) + return channel switch { - case SocketDMChannel dmChannel: return dmChannel.RemoveMessage(id); - case SocketGroupChannel groupChannel: return groupChannel.RemoveMessage(id); - case SocketTextChannel textChannel: return textChannel.RemoveMessage(id); - default: throw new NotSupportedException($"Unexpected {nameof(ISocketMessageChannel)} type."); - } + SocketDMChannel dmChannel => dmChannel.RemoveMessage(id), + SocketGroupChannel groupChannel => groupChannel.RemoveMessage(id), + SocketTextChannel textChannel => textChannel.RemoveMessage(id), + _ => throw new NotSupportedException($"Unexpected {nameof(ISocketMessageChannel)} type."), + }; } } } diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketGuildChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketGuildChannel.cs index a504a2a94..d38a8975b 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketGuildChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketGuildChannel.cs @@ -47,23 +47,16 @@ namespace Discord.WebSocket } internal static SocketGuildChannel Create(SocketGuild guild, ClientState state, Model model) { - switch (model.Type) + return model.Type switch { - case ChannelType.News: - return SocketNewsChannel.Create(guild, state, model); - case ChannelType.Text: - return SocketTextChannel.Create(guild, state, model); - case ChannelType.Voice: - return SocketVoiceChannel.Create(guild, state, model); - case ChannelType.Category: - return SocketCategoryChannel.Create(guild, state, model); - case ChannelType.PrivateThread or ChannelType.PublicThread or ChannelType.NewsThread: - return SocketThreadChannel.Create(guild, state, model); - case ChannelType.Stage: - return SocketStageChannel.Create(guild, state, model); - default: - return new SocketGuildChannel(guild.Discord, model.Id, guild); - } + ChannelType.News => SocketNewsChannel.Create(guild, state, model), + ChannelType.Text => SocketTextChannel.Create(guild, state, model), + ChannelType.Voice => SocketVoiceChannel.Create(guild, state, model), + ChannelType.Category => SocketCategoryChannel.Create(guild, state, model), + ChannelType.PrivateThread or ChannelType.PublicThread or ChannelType.NewsThread => SocketThreadChannel.Create(guild, state, model), + ChannelType.Stage => SocketStageChannel.Create(guild, state, model), + _ => new SocketGuildChannel(guild.Discord, model.Id, guild), + }; } /// internal override void Update(ClientState state, Model model) diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs index 6a721907a..c8e07bf97 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs @@ -72,16 +72,13 @@ namespace Discord.WebSocket if (dataModel == null) return null; - switch (dataModel.Type) + return dataModel.Type switch { - case ApplicationCommandType.Slash: - return SocketSlashCommand.Create(client, model, channel); - case ApplicationCommandType.Message: - return SocketMessageCommand.Create(client, model, channel); - case ApplicationCommandType.User: - return SocketUserCommand.Create(client, model, channel); - default: return null; - } + ApplicationCommandType.Slash => SocketSlashCommand.Create(client, model, channel), + ApplicationCommandType.Message => SocketMessageCommand.Create(client, model, channel), + ApplicationCommandType.User => SocketUserCommand.Create(client, model, channel), + _ => null, + }; } else if (model.Type == InteractionType.MessageComponent) return SocketMessageComponent.Create(client, model, channel); diff --git a/src/Discord.Net.WebSocket/Entities/Invites/SocketInvite.cs b/src/Discord.Net.WebSocket/Entities/Invites/SocketInvite.cs index acc9c4978..abc418d86 100644 --- a/src/Discord.Net.WebSocket/Entities/Invites/SocketInvite.cs +++ b/src/Discord.Net.WebSocket/Entities/Invites/SocketInvite.cs @@ -31,16 +31,16 @@ namespace Discord.WebSocket { get { - switch (Channel) + return Channel switch { - case IVoiceChannel voiceChannel: return ChannelType.Voice; - case ICategoryChannel categoryChannel: return ChannelType.Category; - case IDMChannel dmChannel: return ChannelType.DM; - case IGroupChannel groupChannel: return ChannelType.Group; - case INewsChannel newsChannel: return ChannelType.News; - case ITextChannel textChannel: return ChannelType.Text; - default: throw new InvalidOperationException("Invalid channel type."); - } + IVoiceChannel voiceChannel => ChannelType.Voice, + ICategoryChannel categoryChannel => ChannelType.Category, + IDMChannel dmChannel => ChannelType.DM, + IGroupChannel groupChannel => ChannelType.Group, + INewsChannel newsChannel => ChannelType.News, + ITextChannel textChannel => ChannelType.Text, + _ => throw new InvalidOperationException("Invalid channel type."), + }; } } ///