Skip to content

Commit

Permalink
Add ValueTask support for Match (#14)
Browse files Browse the repository at this point in the history
* Add ValueTask support for Match
  • Loading branch information
nkz-soft authored Oct 20, 2024
1 parent 7f2b69a commit 4597b23
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
</ItemGroup>
</Project>
</Project>
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);
}
}
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
<Description>It is a library that extends the popular FluentResults library and helps you write code in a more functional way.</Description>
<Copyright>Copyright © 2024</Copyright>
<RepositoryUrl>https://github.com/nkz-soft/NKZSoft.FluentResults.Extensions.Functional</RepositoryUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,78 @@ protected TValue ValueFailure(IList<IError> errors)
return TValue.Value;
}

protected Task FailureAsync(IList<IError> arg)
protected Task TaskFailure(IList<IError> arg)
{
Failure(arg);
return Task.CompletedTask;
}

protected Task SuccessEmptyAsync()
protected ValueTask ValueTaskFailure(IList<IError> arg)
{
Failure(arg);
return ValueTask.CompletedTask;
}

protected Task TaskSuccessEmpty()
{
SuccessEmpty();
return Task.CompletedTask;
}

protected Task<TValue> ValueFailureAsync(IList<IError> arg)
protected ValueTask ValueTaskSuccessEmpty()
{
SuccessEmpty();
return ValueTask.CompletedTask;
}

protected Task<TValue> TaskValueFailure(IList<IError> arg)
{
Failure(arg);
return Task.FromResult(TValue.Value);
}

protected Task<TValue> ValueSuccessAsync()
protected ValueTask<TValue> ValueTaskValueFailure(IList<IError> arg)
{
Failure(arg);
return ValueTask.FromResult(TValue.Value);
}

protected Task<TValue> TaskValueSuccess()
{
SuccessEmpty();
return Task.FromResult(TValue.Value);
}

protected Task<TValue> ValueSuccessTAsync(TValue value)
protected ValueTask<TValue> ValueTaskValueSuccess()
{
SuccessEmpty();
return ValueTask.FromResult(TValue.Value);
}

protected Task<TValue> TaskValueSuccessT(TValue value)
{
SuccessEmpty();
return Task.FromResult(TValue.Value);
}

protected Task<TValue> ValueFailureTAsync(IList<IError> arg)
protected ValueTask<TValue> ValueTaskValueSuccessT(TValue value)
{
SuccessEmpty();
return ValueTask.FromResult(TValue.Value);
}

protected Task<TValue> TaskValueFailureT(IList<IError> arg)
{
Failure(arg);
return Task.FromResult(TValue.Value);
}

protected ValueTask<TValue> ValueTaskValueFailureT(IList<IError> arg)
{
Failure(arg);
return ValueTask.FromResult(TValue.Value);
}

protected void AssertSuccess(Result<TValue> output, bool isSuccess)
{
AssertSuccess(isSuccess);
Expand Down
Loading

0 comments on commit 4597b23

Please sign in to comment.