Cleaned up primitive type readers. Fixed TimeSpan reader.
This commit is contained in:
@@ -37,24 +37,7 @@ namespace Discord.Commands
|
||||
_typeReaders = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, TypeReader>>();
|
||||
|
||||
_defaultTypeReaders = new ConcurrentDictionary<Type, TypeReader>
|
||||
{
|
||||
[typeof(bool)] = new SimpleTypeReader<bool>(),
|
||||
[typeof(char)] = new SimpleTypeReader<char>(),
|
||||
[typeof(string)] = new SimpleTypeReader<string>(),
|
||||
[typeof(byte)] = new SimpleTypeReader<byte>(),
|
||||
[typeof(sbyte)] = new SimpleTypeReader<sbyte>(),
|
||||
[typeof(ushort)] = new SimpleTypeReader<ushort>(),
|
||||
[typeof(short)] = new SimpleTypeReader<short>(),
|
||||
[typeof(uint)] = new SimpleTypeReader<uint>(),
|
||||
[typeof(int)] = new SimpleTypeReader<int>(),
|
||||
[typeof(ulong)] = new SimpleTypeReader<ulong>(),
|
||||
[typeof(long)] = new SimpleTypeReader<long>(),
|
||||
[typeof(float)] = new SimpleTypeReader<float>(),
|
||||
[typeof(double)] = new SimpleTypeReader<double>(),
|
||||
[typeof(decimal)] = new SimpleTypeReader<decimal>(),
|
||||
[typeof(DateTime)] = new SimpleTypeReader<DateTime>(),
|
||||
[typeof(DateTimeOffset)] = new SimpleTypeReader<DateTimeOffset>(),
|
||||
|
||||
{
|
||||
[typeof(IMessage)] = new MessageTypeReader<IMessage>(),
|
||||
[typeof(IUserMessage)] = new MessageTypeReader<IUserMessage>(),
|
||||
[typeof(IChannel)] = new ChannelTypeReader<IChannel>(),
|
||||
@@ -72,6 +55,9 @@ namespace Discord.Commands
|
||||
[typeof(IGroupUser)] = new UserTypeReader<IGroupUser>(),
|
||||
[typeof(IGuildUser)] = new UserTypeReader<IGuildUser>(),
|
||||
};
|
||||
foreach (var type in PrimitiveParsers.SupportedTypes)
|
||||
_defaultTypeReaders[type] = PrimitiveTypeReader.Create(type);
|
||||
|
||||
_caseSensitive = config.CaseSensitiveCommands;
|
||||
_defaultRunMode = config.DefaultRunMode;
|
||||
}
|
||||
|
||||
@@ -8,9 +8,11 @@ namespace Discord.Commands
|
||||
|
||||
internal static class PrimitiveParsers
|
||||
{
|
||||
private static readonly IReadOnlyDictionary<Type, Delegate> _parsers;
|
||||
private static readonly Lazy<IReadOnlyDictionary<Type, Delegate>> _parsers = new Lazy<IReadOnlyDictionary<Type, Delegate>>(CreateParsers);
|
||||
|
||||
static PrimitiveParsers()
|
||||
public static IEnumerable<Type> SupportedTypes = _parsers.Value.Keys;
|
||||
|
||||
static IReadOnlyDictionary<Type, Delegate> CreateParsers()
|
||||
{
|
||||
var parserBuilder = ImmutableDictionary.CreateBuilder<Type, Delegate>();
|
||||
parserBuilder[typeof(bool)] = (TryParseDelegate<bool>)bool.TryParse;
|
||||
@@ -34,10 +36,10 @@ namespace Discord.Commands
|
||||
value = str;
|
||||
return true;
|
||||
};
|
||||
_parsers = parserBuilder.ToImmutable();
|
||||
return parserBuilder.ToImmutable();
|
||||
}
|
||||
|
||||
public static TryParseDelegate<T> Get<T>() => (TryParseDelegate<T>)_parsers[typeof(T)];
|
||||
public static Delegate Get(Type type) => _parsers[type];
|
||||
public static TryParseDelegate<T> Get<T>() => (TryParseDelegate<T>)_parsers.Value[typeof(T)];
|
||||
public static Delegate Get(Type type) => _parsers.Value[type];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,22 @@
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
internal class SimpleTypeReader<T> : TypeReader
|
||||
internal static class PrimitiveTypeReader
|
||||
{
|
||||
public static TypeReader Create(Type type)
|
||||
{
|
||||
type = typeof(PrimitiveTypeReader<>).MakeGenericType(type);
|
||||
return Activator.CreateInstance(type) as TypeReader;
|
||||
}
|
||||
}
|
||||
|
||||
internal class PrimitiveTypeReader<T> : TypeReader
|
||||
{
|
||||
private readonly TryParseDelegate<T> _tryParse;
|
||||
|
||||
public SimpleTypeReader()
|
||||
public PrimitiveTypeReader()
|
||||
{
|
||||
_tryParse = PrimitiveParsers.Get<T>();
|
||||
}
|
||||
Reference in New Issue
Block a user