Browse Source

Add nullable type converter to Interaction service (#1996)

* Add nullable type converter to Interaction service

* update NullableConverter CanConvertTo body
tags/3.1.0
Quin Lynch GitHub 3 years ago
parent
commit
ccc365edfd
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions
  1. +7
    -3
      src/Discord.Net.Interactions/InteractionService.cs
  2. +29
    -0
      src/Discord.Net.Interactions/TypeConverters/NullableConverter.cs

+ 7
- 3
src/Discord.Net.Interactions/InteractionService.cs View File

@@ -166,7 +166,8 @@ namespace Discord.Interactions
[typeof(IUser)] = typeof(DefaultUserConverter<>),
[typeof(IMentionable)] = typeof(DefaultMentionableConverter<>),
[typeof(IConvertible)] = typeof(DefaultValueConverter<>),
[typeof(Enum)] = typeof(EnumConverter<>)
[typeof(Enum)] = typeof(EnumConverter<>),
[typeof(Nullable<>)] = typeof(NullableConverter<>),
};

_typeConverters = new ConcurrentDictionary<Type, TypeConverter>
@@ -693,8 +694,8 @@ namespace Discord.Interactions
{
if (_typeConverters.TryGetValue(type, out var specific))
return specific;
else if (_genericTypeConverters.Any(x => x.Key.IsAssignableFrom(type)))
else if (_genericTypeConverters.Any(x => x.Key.IsAssignableFrom(type)
|| (x.Key.IsGenericTypeDefinition && type.IsGenericType && x.Key.GetGenericTypeDefinition() == type.GetGenericTypeDefinition())))
{
services ??= EmptyServiceProvider.Instance;

@@ -927,6 +928,9 @@ namespace Discord.Interactions
if (_genericTypeConverters.TryGetValue(type, out var matching))
return matching;

if (type.IsGenericType && _genericTypeConverters.TryGetValue(type.GetGenericTypeDefinition(), out var genericDefinition))
return genericDefinition;

var typeInterfaces = type.GetInterfaces();
var candidates = _genericTypeConverters.Where(x => x.Key.IsAssignableFrom(type))
.OrderByDescending(x => typeInterfaces.Count(y => y.IsAssignableFrom(x.Key)));


+ 29
- 0
src/Discord.Net.Interactions/TypeConverters/NullableConverter.cs View File

@@ -0,0 +1,29 @@
using System;
using System.Threading.Tasks;

namespace Discord.Interactions
{
internal class NullableConverter<T> : TypeConverter<T>
{
private readonly TypeConverter _typeConverter;

public NullableConverter(InteractionService interactionService, IServiceProvider services)
{
var type = Nullable.GetUnderlyingType(typeof(T));

if (type is null)
throw new ArgumentException($"No type {nameof(TypeConverter)} is defined for this {type.FullName}", "type");

_typeConverter = interactionService.GetTypeConverter(type, services);
}

public override ApplicationCommandOptionType GetDiscordType()
=> _typeConverter.GetDiscordType();

public override Task<TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services)
=> _typeConverter.ReadAsync(context, option, services);

public override void Write(ApplicationCommandOptionProperties properties, IParameterInfo parameter)
=> _typeConverter.Write(properties, parameter);
}
}

Loading…
Cancel
Save