-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Switch/SwitchAsync, Match/MatchAsync extension methods
- Loading branch information
1 parent
339f45c
commit 6fc4845
Showing
9 changed files
with
416 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.