Skip to content

Commit

Permalink
Add nullable type converter to Interaction service (#1996)
Browse files Browse the repository at this point in the history
* Add nullable type converter to Interaction service

* update NullableConverter CanConvertTo body
  • Loading branch information
quinchs authored Dec 24, 2021
1 parent cb1aad3 commit ccc365e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Discord.Net.Interactions/InteractionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ private InteractionService (Func<DiscordRestClient> getRestClient, InteractionSe
[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>
Expand Down Expand Up @@ -693,8 +694,8 @@ internal TypeConverter GetTypeConverter (Type type, IServiceProvider services =
{
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;

Expand Down Expand Up @@ -927,6 +928,9 @@ private Type GetMostSpecificTypeConverter (Type type)
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)));
Expand Down
29 changes: 29 additions & 0 deletions src/Discord.Net.Interactions/TypeConverters/NullableConverter.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit ccc365e

Please sign in to comment.