Browse Source

Add remove methods

pull/2210/head
Quin Lynch 3 years ago
parent
commit
a556791fac
3 changed files with 79 additions and 0 deletions
  1. +31
    -0
      src/Discord.Net.Commands/CommandService.cs
  2. +36
    -0
      src/Discord.Net.Interactions/InteractionService.cs
  3. +12
    -0
      src/Discord.Net.Interactions/Map/TypeMap.cs

+ 31
- 0
src/Discord.Net.Commands/CommandService.cs View File

@@ -403,6 +403,37 @@ namespace Discord.Commands
AddNullableTypeReader(type, reader);
}
}

/// <summary>
/// Removes a type reader from the list of type readers.
/// </summary>
/// <param name="type">The type to remove the readers from.</param>
/// <param name="isDefaultTypeReader"><see langword="true"/> if the default readers for <paramref name="type"/> should be removed; otherwise <see langword="false"/>.</param>
/// <param name="readers">The removed collection of type readers.</param>
/// <returns><see langword="true"/> if the remove operation was successful; otherwise <see langword="false"/>.</returns>
public bool TryRemoveTypeReader(Type type, bool isDefaultTypeReader, out IDictionary<Type, TypeReader> readers)
{
readers = new Dictionary<Type, TypeReader>();

if (isDefaultTypeReader)
{
var isSuccess = _defaultTypeReaders.TryRemove(type, out var result);
if (isSuccess)
readers.Add(result?.GetType(), result);

return isSuccess;
}
else
{
var isSuccess = _typeReaders.TryRemove(type, out var result);

if (isSuccess)
readers = result;

return isSuccess;
}
}

internal bool HasDefaultTypeReader(Type type)
{
if (_defaultTypeReaders.ContainsKey(type))


+ 36
- 0
src/Discord.Net.Interactions/InteractionService.cs View File

@@ -905,6 +905,42 @@ namespace Discord.Interactions
public void AddGenericTypeReader(Type targetType, Type readerType) =>
_typeReaderMap.AddGeneric(targetType, readerType);

/// <summary>
/// Removes a type reader for the type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type to remove the readers from.</typeparam>
/// <param name="reader">The reader if the resulting remove operation was successful.</param>
/// <returns><see langword="true"/> if the remove operation was successful; otherwise <see langword="false"/>.</returns>
public bool TryRemoveTypeReader<T>(out TypeReader reader)
=> TryRemoveTypeReader(typeof(T), out reader);

/// <summary>
/// Removes a type reader for the given type.
/// </summary>
/// <param name="type">The type to remove the reader from.</param>
/// <param name="reader">The reader if the resulting remove operation was successful.</param>
/// <returns><see langword="true"/> if the remove operation was successful; otherwise <see langword="false"/>.</returns>
public bool TryRemoveTypeReader(Type type, out TypeReader reader)
=> _typeReaderMap.TryRemoveConcrete(type, out reader);

/// <summary>
/// Removes a generic type reader from the type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type to remove the readers from.</typeparam>
/// <param name="readerType">The removed readers type.</param>
/// <returns><see langword="true"/> if the remove operation was successful; otherwise <see langword="false"/>.</returns>
public bool TryRemoveGenericTypeReader<T>(out Type readerType)
=> TryRemoveGenericTypeReader(typeof(T), out readerType);

/// <summary>
/// Removes a generic type reader from the given type.
/// </summary>
/// <param name="type">The type to remove the reader from.</param>
/// <param name="readerType">The readers type if the remove operation was successful.</param>
/// <returns><see langword="true"/> if the remove operation was successful; otherwise <see langword="false"/>.</returns>
public bool TryRemoveGenericTypeReader(Type type, out Type readerType)
=> _typeReaderMap.TryRemoveGeneric(type, out readerType);

/// <summary>
/// Serialize an object using a <see cref="TypeReader"/> into a <see cref="string"/> to be placed in a Component CustomId.
/// </summary>


+ 12
- 0
src/Discord.Net.Interactions/Map/TypeMap.cs View File

@@ -74,6 +74,18 @@ namespace Discord.Interactions
_generics[targetType] = converterType;
}

public bool TryRemoveConcrete<TTarget>(out TConverter converter)
=> TryRemoveConcrete(typeof(TTarget), out converter);

public bool TryRemoveConcrete(Type type, out TConverter converter)
=> _concretes.TryRemove(type, out converter);

public bool TryRemoveGeneric<TTarget>(out Type converterType)
=> TryRemoveGeneric(typeof(TTarget), out converterType);

public bool TryRemoveGeneric(Type targetType, out Type converterType)
=> _generics.TryRemove(targetType, out converterType);

private Type GetMostSpecific(Type type)
{
if (_generics.TryGetValue(type, out var matching))


Loading…
Cancel
Save