Expose the internal entity type readers (#986)

* Expose the internal entity type readers

* Add BestMatch property to TypeReaderResult for easily accessing the parsed object
This commit is contained in:
Joe4evr
2018-04-29 17:24:24 +02:00
committed by Christopher F
parent 6a7810b3a4
commit 660fec0cbc
5 changed files with 13 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Discord.Commands namespace Discord.Commands
{ {
internal class ChannelTypeReader<T> : TypeReader public class ChannelTypeReader<T> : TypeReader
where T : class, IChannel where T : class, IChannel
{ {
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)

View File

@@ -1,10 +1,10 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Discord.Commands namespace Discord.Commands
{ {
internal class MessageTypeReader<T> : TypeReader public class MessageTypeReader<T> : TypeReader
where T : class, IMessage where T : class, IMessage
{ {
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Discord.Commands namespace Discord.Commands
{ {
internal class RoleTypeReader<T> : TypeReader public class RoleTypeReader<T> : TypeReader
where T : class, IRole where T : class, IRole
{ {
public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Globalization; using System.Globalization;
@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Discord.Commands namespace Discord.Commands
{ {
internal class UserTypeReader<T> : TypeReader public class UserTypeReader<T> : TypeReader
where T : class, IUser where T : class, IUser
{ {
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services) public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)

View File

@@ -1,7 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
namespace Discord.Commands namespace Discord.Commands
{ {
@@ -30,6 +31,9 @@ namespace Discord.Commands
public string ErrorReason { get; } public string ErrorReason { get; }
public bool IsSuccess => !Error.HasValue; public bool IsSuccess => !Error.HasValue;
public object BestMatch => IsSuccess
? (Values.Count == 1 ? Values.Single().Value : Values.OrderByDescending(v => v.Score).First().Value)
: throw new InvalidOperationException("TypeReaderResult was not successful.");
private TypeReaderResult(IReadOnlyCollection<TypeReaderValue> values, CommandError? error, string errorReason) private TypeReaderResult(IReadOnlyCollection<TypeReaderValue> values, CommandError? error, string errorReason)
{ {