Skip to content

Commit

Permalink
Add Switch/SwitchAsync, Match/MatchAsync extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
amantinband committed Jan 5, 2024
1 parent 339f45c commit 6fc4845
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 101 deletions.
60 changes: 60 additions & 0 deletions src/ErrorOr.ElseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace ErrorOr;

public static partial class ErrorOrExtensions
{
/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<TValue> Else<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<List<Error>, TValue> onError)
{
var result = await errorOr.ConfigureAwait(false);

return result.Else(onError);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<TValue> Else<TValue>(this Task<ErrorOr<TValue>> errorOr, TValue onError)
{
var result = await errorOr.ConfigureAwait(false);

return result.Else(onError);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<TValue> ElseAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<List<Error>, Task<TValue>> onError)
{
var result = await errorOr.ConfigureAwait(false);

return await result.ElseAsync(onError).ConfigureAwait(false);
}

/// <summary>
/// If the state is error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onError">The function to execute if the state is error.</param>
/// <returns>The result from calling <paramref name="onError"/> if state is error; otherwise the original value.</returns>
public static async Task<TValue> ElseAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Task<TValue> onError)
{
var result = await errorOr.ConfigureAwait(false);

return await result.ElseAsync(onError).ConfigureAwait(false);
}
}
76 changes: 76 additions & 0 deletions src/ErrorOr.MatchExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace ErrorOr;

public static partial class ErrorOrExtensions
{
/// <summary>
/// Executes the appropriate function based on the state of the <see cref="ErrorOr{TValue}"/>.
/// If the state is a value, the provided function <paramref name="onValue"/> is executed and its result is returned.
/// If the state is an error, the provided function <paramref name="onError"/> is executed and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <typeparam name="TNextValue">The type of the result from invoking the <paramref name="onError"/> and <paramref name="onValue"/> functions.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The function to execute if the state is a value.</param>
/// <param name="onError">The function to execute if the state is an error.</param>
/// <returns>The result of the executed function.</returns>
public static async Task<TNextValue> Match<TValue, TNextValue>(this Task<ErrorOr<TValue>> errorOr, Func<TValue, TNextValue> onValue, Func<List<Error>, TNextValue> onError)
{
var result = await errorOr.ConfigureAwait(false);

return result.Match(onValue, onError);
}

/// <summary>
/// Asynchronously executes the appropriate function based on the state of the <see cref="ErrorOr{TValue}"/>.
/// If the state is a value, the provided function <paramref name="onValue"/> is executed asynchronously and its result is returned.
/// If the state is an error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <typeparam name="TNextValue">The type of the result from invoking the <paramref name="onError"/> and <paramref name="onValue"/> functions.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The function to execute if the state is a value.</param>
/// <param name="onError">The function to execute if the state is an error.</param>
/// <returns>The result of the executed function.</returns>
public static async Task<TNextValue> MatchAsync<TValue, TNextValue>(this Task<ErrorOr<TValue>> errorOr, Func<TValue, Task<TNextValue>> onValue, Func<List<Error>, Task<TNextValue>> onError)
{
var result = await errorOr.ConfigureAwait(false);

return await result.MatchAsync(onValue, onError);
}

/// <summary>
/// Executes the appropriate function based on the state of the <see cref="ErrorOr{TValue}"/>.
/// If the state is a value, the provided function <paramref name="onValue"/> is executed and its result is returned.
/// If the state is an error, the provided function <paramref name="onError"/> is executed and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <typeparam name="TNextValue">The type of the result from invoking the <paramref name="onError"/> and <paramref name="onValue"/> functions.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The function to execute if the state is a value.</param>
/// <param name="onError">The function to execute if the state is an error.</param>
/// <returns>The result of the executed function.</returns>
public static async Task<TNextValue> MatchFirst<TValue, TNextValue>(this Task<ErrorOr<TValue>> errorOr, Func<TValue, TNextValue> onValue, Func<Error, TNextValue> onError)
{
var result = await errorOr.ConfigureAwait(false);

return result.MatchFirst(onValue, onError);
}

/// <summary>
/// Asynchronously executes the appropriate function based on the state of the <see cref="ErrorOr{TValue}"/>.
/// If the state is a value, the provided function <paramref name="onValue"/> is executed asynchronously and its result is returned.
/// If the state is an error, the provided function <paramref name="onError"/> is executed asynchronously and its result is returned.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <typeparam name="TNextValue">The type of the result from invoking the <paramref name="onError"/> and <paramref name="onValue"/> functions.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The function to execute if the state is a value.</param>
/// <param name="onError">The function to execute if the state is an error.</param>
/// <returns>The result of the executed function.</returns>
public static async Task<TNextValue> MatchFirstAsync<TValue, TNextValue>(this Task<ErrorOr<TValue>> errorOr, Func<TValue, Task<TNextValue>> onValue, Func<Error, Task<TNextValue>> onError)
{
var result = await errorOr.ConfigureAwait(false);

return await result.MatchFirstAsync(onValue, onError);
}
}
72 changes: 72 additions & 0 deletions src/ErrorOr.SwitchExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace ErrorOr;

public static partial class ErrorOrExtensions
{
/// <summary>
/// Executes the appropriate action based on the state of the <see cref="ErrorOr{TValue}"/>.
/// If the state is an error, the provided action <paramref name="onError"/> is executed.
/// If the state is a value, the provided action <paramref name="onValue"/> is executed.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The action to execute if the state is a value.</param>
/// <param name="onError">The action to execute if the state is an error.</param>
/// <returns>The result of the executed function.</returns>
public static async Task Switch<TValue>(this Task<ErrorOr<TValue>> errorOr, Action<TValue> onValue, Action<List<Error>> onError)
{
var result = await errorOr.ConfigureAwait(false);

result.Switch(onValue, onError);
}

/// <summary>
/// Executes the appropriate action based on the state of the <see cref="ErrorOr{TValue}"/>.
/// If the state is an error, the provided action <paramref name="onError"/> is executed.
/// If the state is a value, the provided action <paramref name="onValue"/> is executed.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The action to execute if the state is a value.</param>
/// <param name="onError">The action to execute if the state is an error.</param>
/// <returns>The result of the executed function.</returns>
public static async Task SwitchAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<TValue, Task> onValue, Func<List<Error>, Task> onError)
{
var result = await errorOr.ConfigureAwait(false);

await result.SwitchAsync(onValue, onError);
}

/// <summary>
/// Executes the appropriate action based on the state of the <see cref="ErrorOr{TValue}"/>.
/// If the state is an error, the provided action <paramref name="onError"/> is executed.
/// If the state is a value, the provided action <paramref name="onValue"/> is executed.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The action to execute if the state is a value.</param>
/// <param name="onError">The action to execute if the state is an error.</param>
/// <returns>The result of the executed function.</returns>
public static async Task SwitchFirst<TValue>(this Task<ErrorOr<TValue>> errorOr, Action<TValue> onValue, Action<Error> onError)
{
var result = await errorOr.ConfigureAwait(false);

result.SwitchFirst(onValue, onError);
}

/// <summary>
/// Executes the appropriate action based on the state of the <see cref="ErrorOr{TValue}"/>.
/// If the state is an error, the provided action <paramref name="onError"/> is executed.
/// If the state is a value, the provided action <paramref name="onValue"/> is executed.
/// </summary>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The action to execute if the state is a value.</param>
/// <param name="onError">The action to execute if the state is an error.</param>
/// <returns>The result of the executed function.</returns>
public static async Task SwitchFirstAsync<TValue>(this Task<ErrorOr<TValue>> errorOr, Func<TValue, Task> onValue, Func<Error, Task> onError)
{
var result = await errorOr.ConfigureAwait(false);

await result.SwitchFirstAsync(onValue, onError);
}
}
Loading

0 comments on commit 6fc4845

Please sign in to comment.