C#7 TODOs

This commit is contained in:
Christopher F
2017-04-23 15:13:31 -04:00
parent 3dfa54e56e
commit 6000b15c4d
5 changed files with 59 additions and 62 deletions

View File

@@ -81,23 +81,28 @@ namespace Discord.Commands
foreach (var attribute in attributes) foreach (var attribute in attributes)
{ {
// TODO: C#7 type switch switch (attribute)
if (attribute is NameAttribute)
builder.Name = (attribute as NameAttribute).Text;
else if (attribute is SummaryAttribute)
builder.Summary = (attribute as SummaryAttribute).Text;
else if (attribute is RemarksAttribute)
builder.Remarks = (attribute as RemarksAttribute).Text;
else if (attribute is AliasAttribute)
builder.AddAliases((attribute as AliasAttribute).Aliases);
else if (attribute is GroupAttribute)
{ {
var groupAttr = attribute as GroupAttribute; case NameAttribute name:
builder.Name = builder.Name ?? groupAttr.Prefix; builder.Name = name.Text;
builder.AddAliases(groupAttr.Prefix); break;
case SummaryAttribute summary:
builder.Summary = summary.Text;
break;
case RemarksAttribute remarks:
builder.Remarks = remarks.Text;
break;
case AliasAttribute alias:
builder.AddAliases(alias.Aliases);
break;
case GroupAttribute group:
builder.Name = builder.Name ?? group.Prefix;
builder.AddAliases(group.Prefix);
break;
case PreconditionAttribute precondition:
builder.AddPrecondition(precondition);
break;
} }
else if (attribute is PreconditionAttribute)
builder.AddPrecondition(attribute as PreconditionAttribute);
} }
//Check for unspecified info //Check for unspecified info

View File

@@ -20,13 +20,14 @@ namespace Discord
/// <summary> Gets a ChannelPermissions that grants all permissions for a given channelType. </summary> /// <summary> Gets a ChannelPermissions that grants all permissions for a given channelType. </summary>
public static ChannelPermissions All(IChannel channel) public static ChannelPermissions All(IChannel channel)
{ {
//TODO: C#7 Candidate for typeswitch switch (channel)
if (channel is ITextChannel) return Text; {
if (channel is IVoiceChannel) return Voice; case ITextChannel _: return Text;
if (channel is IDMChannel) return DM; case IVoiceChannel _: return Voice;
if (channel is IGroupChannel) return Group; case IDMChannel _: return DM;
case IGroupChannel _: return Group;
throw new ArgumentException("Unknown channel type", nameof(channel)); default: throw new ArgumentException("Unknown channel type", nameof(channel));
}
} }
/// <summary> Gets a packed value representing all the permissions in this ChannelPermissions. </summary> /// <summary> Gets a packed value representing all the permissions in this ChannelPermissions. </summary>

View File

@@ -150,9 +150,7 @@ namespace Discord
if (perms != null) if (perms != null)
resolvedPermissions = (resolvedPermissions & ~perms.Value.DenyValue) | perms.Value.AllowValue; resolvedPermissions = (resolvedPermissions & ~perms.Value.DenyValue) | perms.Value.AllowValue;
//TODO: C#7 Typeswitch candidate if (channel is ITextChannel textChannel)
var textChannel = channel as ITextChannel;
if (textChannel != null)
{ {
if (!GetValue(resolvedPermissions, ChannelPermission.ReadMessages)) if (!GetValue(resolvedPermissions, ChannelPermission.ReadMessages))
{ {

View File

@@ -87,29 +87,26 @@ namespace Discord.Net.Rest
{ {
foreach (var p in multipartParams) foreach (var p in multipartParams)
{ {
//TODO: C#7 Typeswitch candidate switch (p.Value)
var stringValue = p.Value as string;
if (stringValue != null) { content.Add(new StringContent(stringValue), p.Key); continue; }
var byteArrayValue = p.Value as byte[];
if (byteArrayValue != null) { content.Add(new ByteArrayContent(byteArrayValue), p.Key); continue; }
var streamValue = p.Value as Stream;
if (streamValue != null) { content.Add(new StreamContent(streamValue), p.Key); continue; }
if (p.Value is MultipartFile)
{ {
var fileValue = (MultipartFile)p.Value; case string stringValue: { content.Add(new StringContent(stringValue), p.Key); continue; }
var stream = fileValue.Stream; case byte[] byteArrayValue: { content.Add(new ByteArrayContent(byteArrayValue), p.Key); continue; }
if (!stream.CanSeek) case Stream streamValue: { content.Add(new StreamContent(streamValue), p.Key); continue; }
case MultipartFile fileValue:
{ {
var memoryStream = new MemoryStream(); var stream = fileValue.Stream;
await stream.CopyToAsync(memoryStream).ConfigureAwait(false); if (!stream.CanSeek)
memoryStream.Position = 0; {
stream = memoryStream; var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0;
stream = memoryStream;
}
content.Add(new StreamContent(stream), p.Key, fileValue.Filename);
continue;
} }
content.Add(new StreamContent(stream), p.Key, fileValue.Filename); default: throw new InvalidOperationException($"Unsupported param type \"{p.Value.GetType().Name}\"");
continue;
} }
throw new InvalidOperationException($"Unsupported param type \"{p.Value.GetType().Name}\"");
} }
} }
restRequest.Content = content; restRequest.Content = content;

View File

@@ -61,28 +61,24 @@ namespace Discord.WebSocket
public static void AddMessage(ISocketMessageChannel channel, DiscordSocketClient discord, public static void AddMessage(ISocketMessageChannel channel, DiscordSocketClient discord,
SocketMessage msg) SocketMessage msg)
{ {
//TODO: C#7 Candidate for pattern matching switch (channel)
if (channel is SocketDMChannel) {
(channel as SocketDMChannel).AddMessage(msg); case SocketDMChannel dmChannel: dmChannel.AddMessage(msg); break;
else if (channel is SocketGroupChannel) case SocketGroupChannel groupChannel: groupChannel.AddMessage(msg); break;
(channel as SocketGroupChannel).AddMessage(msg); case SocketTextChannel textChannel: textChannel.AddMessage(msg); break;
else if (channel is SocketTextChannel) default: throw new NotSupportedException("Unexpected ISocketMessageChannel type");
(channel as SocketTextChannel).AddMessage(msg); }
else
throw new NotSupportedException("Unexpected ISocketMessageChannel type");
} }
public static SocketMessage RemoveMessage(ISocketMessageChannel channel, DiscordSocketClient discord, public static SocketMessage RemoveMessage(ISocketMessageChannel channel, DiscordSocketClient discord,
ulong id) ulong id)
{ {
//TODO: C#7 Candidate for pattern matching switch (channel)
if (channel is SocketDMChannel) {
return (channel as SocketDMChannel).RemoveMessage(id); case SocketDMChannel dmChannel: return dmChannel.RemoveMessage(id);
else if (channel is SocketGroupChannel) case SocketGroupChannel groupChannel: return groupChannel.RemoveMessage(id);
return (channel as SocketGroupChannel).RemoveMessage(id); case SocketTextChannel textChannel: return textChannel.RemoveMessage(id);
else if (channel is SocketTextChannel) default: throw new NotSupportedException("Unexpected ISocketMessageChannel type");
return (channel as SocketTextChannel).RemoveMessage(id); }
else
throw new NotSupportedException("Unexpected ISocketMessageChannel type");
} }
} }
} }