Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions DSharpPlus/EventArgs/Interaction/SelectMenuModalSubmission.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;

using DSharpPlus.Entities;
using DSharpPlus.Extensions;

namespace DSharpPlus.EventArgs;

Expand All @@ -25,4 +27,67 @@ internal SelectMenuModalSubmission(string customId, IReadOnlyList<string> values
this.CustomId = customId;
this.Values = values;
}

/// <summary>
/// Parse all <see cref="SelectMenuModalSubmission.Values"/> as <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T">Supplied data type you wish to convert the <see cref="SelectMenuModalSubmission.Values"/> to.</typeparam>
/// <exception cref="AggregateException"></exception>
/// <returns>All parsable <see cref="SelectMenuModalSubmission.Values"/></returns>
public IEnumerable<T> GetValuesAs<T>()
where T : IParsable<T>
{
List<FormatException> errors = new(this.Values.Count);
List<T> results = new(this.Values.Count);

foreach (string i in this.Values)
{
if (i.TryGetValueAs<T>(out T? value))
{
results.Add(value);
}
else
{
errors.Add(new FormatException(
$"Value \"{i}\" cannot be parsed as {typeof(T).Name}."));
}
}

return errors.Count > 0
? throw new AggregateException(
$"One or more values cannot be parsed as {typeof(T).Name}.", errors)
: (IEnumerable<T>)results;
}

/// <summary>
/// Parse all <see cref="SelectMenuModalSubmission.Values"/> as <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T">Supplied data type you wish to convert the <see cref="SelectMenuModalSubmission.Values"/> to.</typeparam>
/// <returns>All parsable <see cref="SelectMenuModalSubmission.Values"/>, where each are <![CDATA[default(T)]]> if not parsable.</returns>
public IEnumerable<T> GetValuesAsOrDefault<T>()
where T : IParsable<T>
{
foreach (string i in this.Values)
{
yield return i.TryGetValueAs<T>(out T? value) ? value : default!;
}
}

/// <summary>
/// Parse all <see cref="SelectMenuModalSubmission.Values"/> as <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">Supplied data type you wish to convert the <see cref="SelectMenuModalSubmission.Values"/> to.</typeparam>
/// <param name="defaultValue">Will return this if cannot parse <see cref="SelectMenuModalSubmission.Values"/> to <typeparamref name="T"/></param>
/// <returns><see cref="SelectMenuModalSubmission.Values"/> as <typeparamref name="T"/>, else return your supplied <paramref name="defaultValue"/></returns>
public IEnumerable<T> GetValueAsOrDefault<T>
(
T defaultValue
)
where T : IParsable<T>
{
foreach (string i in this.Values)
{
yield return i.TryGetValueAs<T>(out T? value) ? value : defaultValue;
}
}
}
63 changes: 63 additions & 0 deletions DSharpPlus/EventArgs/Interaction/TextInputModalSubmission.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Diagnostics.CodeAnalysis;

using DSharpPlus.Entities;
using DSharpPlus.Extensions;

namespace DSharpPlus.EventArgs;

Expand All @@ -21,4 +25,63 @@ internal TextInputModalSubmission(string customId, string value)
this.CustomId = customId;
this.Value = value;
}

/// <summary>
/// Parse <see cref="TextInputModalSubmission.Value"/> as <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">Supplied data type you wish to convert the <see cref="TextInputModalSubmission.Value"/> to.</typeparam>
/// <returns><see cref="TextInputModalSubmission.Value"/> as <typeparamref name="T"/></returns>
/// <exception cref="FormatException"></exception>
public T GetValueAs<T>()
where T : IParsable<T>
{
return TryGetValueAs<T>(out T? result)
? result
: throw new FormatException(
$"Value \"{this.Value}\" cannot be parsed as {typeof(T).Name}.");
}

/// <summary>
/// Try Parse <see cref="TextInputModalSubmission.Value"/> as <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">Supplied data type you wish to convert the <see cref="TextInputModalSubmission.Value"/> to.</typeparam>
/// <param name="value">The converted value when able to parse, else <![CDATA[null]]>.</param>
/// <returns><![CDATA[true]]> when able to parse.</returns>
public bool TryGetValueAs<T>
(
[NotNullWhen(true)]
out T? value
)
where T : IParsable<T>
=> this.Value.TryGetValueAs(out value);

/// <summary>
/// Parse <see cref="TextInputModalSubmission.Value"/> as <typeparamref name="T"/>, else <![CDATA[default(T)]]>.
/// </summary>
/// <typeparam name="T">Supplied data type you wish to convert the <see cref="TextInputModalSubmission.Value"/> to.</typeparam>
/// <returns><see cref="TextInputModalSubmission.Value"/> as <typeparamref name="T"/>, else <![CDATA[default(T)]]>.</returns>
public T GetValueAsOrDefault<T>()
where T : IParsable<T>
{
return TryGetValueAs<T>(out T? result)
? result
: default!;
}

/// <summary>
/// Parse <see cref="TextInputModalSubmission.Value"/> as <typeparamref name="T"/>, else return your supplied <paramref name="defaultValue"/>.
/// </summary>
/// <typeparam name="T">Supplied data type you wish to convert the <see cref="TextInputModalSubmission.Value"/> to.</typeparam>
/// <param name="defaultValue">Will return this if cannot parse <see cref="TextInputModalSubmission.Value"/> to <typeparamref name="T"/></param>.
/// <returns><see cref="TextInputModalSubmission.Value"/> as <typeparamref name="T"/>, else return your supplied <paramref name="defaultValue"/></returns>
public T GetValueAsOrDefault<T>
(
T defaultValue
)
where T : IParsable<T>
{
return TryGetValueAs<T>(out T? result)
? result
: defaultValue;
}
}
25 changes: 25 additions & 0 deletions DSharpPlus/Extensions/StringExtentions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace DSharpPlus.Extensions;

internal static class StringExtentions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upon thinking about this further, i think we absolutely should not extend string even internally, since it'll pollute the symbol space for every string in the entire project. use a static helper method instead, please

{
/// <summary>
/// Method for trying to convert a string to a caller provided data type that implements <see cref="IParsable{TSelf}"/>.
/// </summary>
/// <typeparam name="T">The destination data type</typeparam>
/// <param name="value"></param>
/// <param name="parsedValue"></param>
/// <returns></returns>
internal static bool TryGetValueAs<T>
(
this string value,

[NotNullWhen(true)]
out T? parsedValue
)
where T : IParsable<T>
=> T.TryParse(value, CultureInfo.InvariantCulture, out parsedValue);
}
Loading