-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ValueTask support for Match (#14)
* Add ValueTask support for Match
- Loading branch information
Showing
11 changed files
with
491 additions
and
16 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
99 changes: 99 additions & 0 deletions
99
src/NKZSoft.FluentResults.Extensions.Functional/Methods/Match.ValueTask.Left.cs
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,99 @@ | ||
namespace NKZSoft.FluentResults.Extensions.Functional; | ||
|
||
public static partial class ResultExtensions | ||
{ | ||
/// <summary> | ||
/// Matches a Task of Result to either a success or failure action. | ||
/// </summary> | ||
/// <param name="resultTask">The Task of Result to match.</param> | ||
/// <param name="onSuccess">The action to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The action to execute if the Result is failed.</param> | ||
/// <exception cref="ArgumentNullException">Thrown if onSuccess or onFailure is null.</exception> | ||
public static async ValueTask MatchAsync(this ValueTask<Result> resultTask, Action onSuccess, Action<IList<IError>> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
var result = await resultTask.ConfigureAwait(false); | ||
|
||
if (result.IsSuccess) | ||
{ | ||
onSuccess(); | ||
} | ||
else | ||
{ | ||
onFailure(result.Errors); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Matches a Task of Result to either a success or failure function. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the value returned by the functions.</typeparam> | ||
/// <param name="resultTask">The Task of Result to match.</param> | ||
/// <param name="onSuccess">The function to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The function to execute if the Result is failed.</param> | ||
/// <returns>A Task representing the asynchronous operation. The task result contains the value returned by the executed function.</returns> | ||
public static async ValueTask<T> MatchAsync<T>(this ValueTask<Result> resultTask, | ||
Func<T> onSuccess, | ||
Func<IList<IError>, T> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
var result = await resultTask.ConfigureAwait(false); | ||
|
||
return result.IsSuccess | ||
? onSuccess() | ||
: onFailure(result.Errors); | ||
} | ||
|
||
/// <summary> | ||
/// Matches a Task of Result to either a success or failure action. | ||
/// </summary> | ||
/// <typeparam name="TValue">The type of the value in the result.</typeparam> | ||
/// <param name="resultTask">The Task of Result to match.</param> | ||
/// <param name="onSuccess">The action to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The action to execute if the Result is failed.</param> | ||
public static async ValueTask MatchAsync<TValue>(this ValueTask<Result<TValue>> resultTask, | ||
Action<TValue> onSuccess, | ||
Action<IList<IError>> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
var result = await resultTask.ConfigureAwait(false); | ||
|
||
if (result.IsSuccess) | ||
{ | ||
onSuccess(result.Value); | ||
} | ||
else | ||
{ | ||
onFailure(result.Errors); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Matches a Task of Result to either a success or failure function. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the value returned by the success or failure function.</typeparam> | ||
/// <typeparam name="TValue">The type of the value in the result.</typeparam> | ||
/// <param name="resultTask">The Task of Result to match.</param> | ||
/// <param name="onSuccess">The function to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The function to execute if the Result is failed.</param> | ||
/// <returns>A Task representing the asynchronous operation, returning the result of the success or failure function.</returns> | ||
public static async ValueTask<T> MatchAsync<T, TValue>(this ValueTask<Result<TValue>> resultTask, | ||
Func<TValue, T> onSuccess, | ||
Func<IList<IError>, T> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
var result = await resultTask.ConfigureAwait(false); | ||
|
||
return result.IsSuccess | ||
? onSuccess(result.Value) | ||
: onFailure(result.Errors); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/NKZSoft.FluentResults.Extensions.Functional/Methods/Match.ValueTask.Right.cs
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,94 @@ | ||
namespace NKZSoft.FluentResults.Extensions.Functional; | ||
|
||
public static partial class ResultExtensions | ||
{ | ||
/// <summary> | ||
/// Matches a Result to either a success or failure action asynchronously. | ||
/// </summary> | ||
/// <param name="result">The Result to match.</param> | ||
/// <param name="onSuccess">The action to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The action to execute if the Result is failed.</param> | ||
/// <returns>A Task representing the asynchronous operation.</returns> | ||
public static async ValueTask MatchAsync(this Result result, | ||
Func<ValueTask> onSuccess, | ||
Func<IList<IError>, ValueTask> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
if (result.IsSuccess) | ||
{ | ||
await onSuccess().ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
await onFailure(result.Errors).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Matches a Result to either a success or failure function asynchronously. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the value returned by the functions.</typeparam> | ||
/// <param name="result">The Result to match.</param> | ||
/// <param name="onSuccess">The function to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The function to execute if the Result is failed.</param> | ||
/// <returns>A Task representing the asynchronous operation, returning the result of the executed function.</returns> | ||
public static async ValueTask<T> MatchAsync<T>(this Result result, | ||
Func<ValueTask<T>> onSuccess, | ||
Func<IList<IError>, ValueTask<T>> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
return result.IsSuccess | ||
? await onSuccess().ConfigureAwait(false) | ||
: await onFailure(result.Errors).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Matches a Result to either a success or failure action asynchronously. | ||
/// </summary> | ||
/// <typeparam name="TValue">The type of the value in the result.</typeparam> | ||
/// <param name="result">The Result to match.</param> | ||
/// <param name="onSuccess">The action to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The action to execute if the Result is failed.</param> | ||
/// <returns>A Task representing the asynchronous operation.</returns> | ||
public static async ValueTask MatchAsync<TValue>(this Result<TValue> result, | ||
Func<ValueTask<TValue>> onSuccess, | ||
Func<IList<IError>, ValueTask> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
if (result.IsSuccess) | ||
{ | ||
await onSuccess().ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
await onFailure(result.Errors).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Matches a Result to either a success or failure function asynchronously. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the value returned by the success or failure function.</typeparam> | ||
/// <typeparam name="TValue">The type of the value in the result.</typeparam> | ||
/// <param name="result">The Result to match.</param> | ||
/// <param name="onSuccess">The function to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The function to execute if the Result is failed.</param> | ||
/// <returns>A Task representing the asynchronous operation, returning the result of the success or failure function.</returns> | ||
public static async ValueTask<T> MatchAsync<T, TValue>(this Result<TValue> result, | ||
Func<TValue, ValueTask<T>> onSuccess, | ||
Func<IList<IError>, ValueTask<T>> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
return result.IsSuccess | ||
? await onSuccess(result.Value).ConfigureAwait(false) | ||
: await onFailure(result.Errors).ConfigureAwait(false); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/NKZSoft.FluentResults.Extensions.Functional/Methods/Match.ValueTask.cs
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,102 @@ | ||
namespace NKZSoft.FluentResults.Extensions.Functional; | ||
|
||
public static partial class ResultExtensions | ||
{ | ||
/// <summary> | ||
/// Matches a Task of Result to either a success or failure function. | ||
/// </summary> | ||
/// <param name="resultTask">The Task of Result to match.</param> | ||
/// <param name="onSuccess">The function to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The function to execute if the Result is failed.</param> | ||
/// <returns>A Task representing the asynchronous operation.</returns> | ||
public static async ValueTask MatchAsync(this ValueTask<Result> resultTask, | ||
Func<ValueTask> onSuccess, | ||
Func<IList<IError>, ValueTask> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
var result = await resultTask.ConfigureAwait(false); | ||
|
||
if (result.IsSuccess) | ||
{ | ||
await onSuccess().ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
await onFailure(result.Errors).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Matches a Task of Result to either a success or failure function, returning a value of type T. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the value returned by the functions.</typeparam> | ||
/// <param name="resultTask">The Task of Result to match.</param> | ||
/// <param name="onSuccess">The function to execute if the Result is successful, returning a value of type T.</param> | ||
/// <param name="onFailure">The function to execute if the Result is failed, returning a value of type T.</param> | ||
/// <returns>A Task representing the asynchronous operation, returning a value of type T.</returns> | ||
public static async ValueTask<T> MatchAsync<T>(this ValueTask<Result> resultTask, | ||
Func<ValueTask<T>> onSuccess, | ||
Func<IList<IError>, ValueTask<T>> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
var result = await resultTask.ConfigureAwait(false); | ||
|
||
return result.IsSuccess | ||
? await onSuccess().ConfigureAwait(false) | ||
: await onFailure(result.Errors).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Matches a Task of Result to either a success or failure function. | ||
/// </summary> | ||
/// <typeparam name="TValue">The type of the value in the result.</typeparam> | ||
/// <param name="resultTask">The Task of Result to match.</param> | ||
/// <param name="onSuccess">The function to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The function to execute if the Result is failed.</param> | ||
/// <returns>A Task representing the asynchronous operation.</returns> | ||
public static async ValueTask MatchAsync<TValue>(this ValueTask<Result<TValue>> resultTask, | ||
Func<ValueTask<TValue>> onSuccess, | ||
Func<IList<IError>, ValueTask> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
var result = await resultTask.ConfigureAwait(false); | ||
|
||
if (result.IsSuccess) | ||
{ | ||
await onSuccess().ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
await onFailure(result.Errors).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Matches a Task of Result to either a success or failure function. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the value returned by the functions.</typeparam> | ||
/// <typeparam name="TValue">The type of the value in the result.</typeparam> | ||
/// <param name="resultTask">The Task of Result to match.</param> | ||
/// <param name="onSuccess">The function to execute if the Result is successful.</param> | ||
/// <param name="onFailure">The function to execute if the Result is failed.</param> | ||
/// <returns>A Task representing the asynchronous operation. The task result contains the value returned by the executed function.</returns> | ||
public static async ValueTask<T> MatchAsync<T, TValue>(this ValueTask<Result<TValue>> resultTask, | ||
Func<TValue, ValueTask<T>> onSuccess, | ||
Func<IList<IError>, ValueTask<T>> onFailure) | ||
{ | ||
ArgumentNullException.ThrowIfNull(onSuccess); | ||
ArgumentNullException.ThrowIfNull(onFailure); | ||
|
||
var result = await resultTask.ConfigureAwait(false); | ||
|
||
return result.IsSuccess | ||
? await onSuccess(result.Value).ConfigureAwait(false) | ||
: await onFailure(result.Errors).ConfigureAwait(false); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ft.FluentResults.Extensions.Functional/NKZSoft.FluentResults.Extensions.Functional.csproj
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
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
Oops, something went wrong.