Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Add FailIf from Task<T> #95

Merged
merged 4 commits into from
May 9, 2024
Merged
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
22 changes: 20 additions & 2 deletions src/ErrorOr.FailIf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ namespace ErrorOr;
public readonly partial record struct ErrorOr<TValue> : IErrorOr<TValue>
{
/// <summary>
/// If the state is error, the provided function <paramref name="onValue"/> is executed and its result is returned.
/// If the state is value, the provided function <paramref name="onValue"/> is invoked.
/// If <paramref name="onValue"/> returns true, the given <paramref name="error"/> will be returned, and the state will be error.
/// </summary>
/// <param name="onValue">The function to execute if the state is value.</param>
/// <param name="error">The <see cref="Error"/> to return if the given <paramref name="onValue"/> function returned true..</param>
/// <param name="error">The <see cref="Error"/> to return if the given <paramref name="onValue"/> function returned true.</param>
/// <returns>The given <paramref name="error"/> if <paramref name="onValue"/> returns true; otherwise, the original <see cref="ErrorOr"/> instance.</returns>
public ErrorOr<TValue> FailIf(Func<TValue, bool> onValue, Error error)
{
Expand All @@ -20,4 +21,21 @@ public ErrorOr<TValue> FailIf(Func<TValue, bool> onValue, Error error)

return onValue(Value) ? error : this;
}

/// <summary>
/// If the state is value, the provided function <paramref name="onValue"/> is invoked asynchronously.
/// If <paramref name="onValue"/> returns true, the given <paramref name="error"/> will be returned, and the state will be error.
/// </summary>
/// <param name="onValue">The function to execute if the statement is value.</param>
/// <param name="error">The <see cref="Error"/> to return if the given <paramref name="onValue"/> function returned true.</param>
/// <returns>The given <paramref name="error"/> if <paramref name="onValue"/> returns true; otherwise, the original <see cref="ErrorOr"/> instance.</returns>
public async Task<ErrorOr<TValue>> FailIfAsync(Func<TValue, Task<bool>> onValue, Error error)
{
if (IsError)
{
return this;
}

return await onValue(Value).ConfigureAwait(false) ? error : this;
}
}
42 changes: 42 additions & 0 deletions src/ErrorOr.FailIfExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace ErrorOr;

public static partial class ErrorOrExtensions
{
/// <summary>
/// If the state is value, the provided function <paramref name="onValue"/> is invoked asynchronously.
/// If <paramref name="onValue"/> returns true, the given <paramref name="error"/> will be returned, and the state will be error.
/// </summary>
/// <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="error">The <see cref="Error"/> to return if the given <paramref name="onValue"/> function returned true..</param>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <returns>The given <paramref name="error"/> if <paramref name="onValue"/> returns true; otherwise, the original <see cref="ErrorOr"/> instance.</returns>
public static async Task<ErrorOr<TValue>> FailIf<TValue>(
this Task<ErrorOr<TValue>> errorOr,
Func<TValue, bool> onValue,
Error error)
{
var result = await errorOr.ConfigureAwait(false);

return result.FailIf(onValue, error);
}

/// <summary>
/// If the state is value, the provided function <paramref name="onValue"/> is invoked asynchronously.
/// If <paramref name="onValue"/> returns true, the given <paramref name="error"/> will be returned, and the state will be error.
/// </summary>
/// <param name="errorOr">The <see cref="ErrorOr"/> instance.</param>
/// <param name="onValue">The function to execute if the statement is value.</param>
/// <param name="error">The <see cref="Error"/> to return if the given <paramref name="onValue"/> function returned true.</param>
/// <typeparam name="TValue">The type of the underlying value in the <paramref name="errorOr"/>.</typeparam>
/// <returns>The given <paramref name="error"/> if <paramref name="onValue"/> returns true; otherwise, the original <see cref="ErrorOr"/> instance.</returns>
public static async Task<ErrorOr<TValue>> FailIfAsync<TValue>(
this Task<ErrorOr<TValue>> errorOr,
Func<TValue, Task<bool>> onValue,
Error error)
{
var result = await errorOr.ConfigureAwait(false);

return await result.FailIfAsync(onValue, error);
}
}
70 changes: 70 additions & 0 deletions tests/ErrorOr.FailIfAsyncTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using ErrorOr;
using FluentAssertions;

namespace Tests;

public class FailIfAsyncTests
{
private record Person(string Name);

[Fact]
public async Task CallingFailIfAsync_WhenFailsIf_ShouldReturnError()
{
// Arrange
ErrorOr<int> errorOrInt = 5;

// Act
ErrorOr<int> result = await errorOrInt
.FailIfAsync(num => Task.FromResult(num > 3), Error.Failure());

// Assert
result.IsError.Should().BeTrue();
result.FirstError.Type.Should().Be(ErrorType.Failure);
}

[Fact]
public async Task CallingFailIfAsyncExtensionMethod_WhenFailsIf_ShouldReturnError()
{
// Arrange
ErrorOr<int> errorOrInt = 5;

// Act
ErrorOr<int> result = await errorOrInt
.ThenAsync(num => Task.FromResult(num))
.FailIfAsync(num => Task.FromResult(num > 3), Error.Failure());

// Assert
result.IsError.Should().BeTrue();
result.FirstError.Type.Should().Be(ErrorType.Failure);
}

[Fact]
public async Task CallingFailIfAsync_WhenDoesNotFailIf_ShouldReturnValue()
{
// Arrange
ErrorOr<int> errorOrInt = 5;

// Act
ErrorOr<int> result = await errorOrInt
.FailIfAsync(num => Task.FromResult(num > 10), Error.Failure());

// Assert
result.IsError.Should().BeFalse();
result.Value.Should().Be(5);
}

[Fact]
public async Task CallingFailIf_WhenIsError_ShouldNotInvokeFailIfFunc()
{
// Arrange
ErrorOr<string> errorOrString = Error.NotFound();

// Act
ErrorOr<string> result = await errorOrString
.FailIfAsync(str => Task.FromResult(str == string.Empty), Error.Failure());

// Assert
result.IsError.Should().BeTrue();
result.FirstError.Type.Should().Be(ErrorType.NotFound);
}
}
16 changes: 16 additions & 0 deletions tests/ErrorOr.FailIfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ public void CallingFailIf_WhenFailsIf_ShouldReturnError()
result.FirstError.Type.Should().Be(ErrorType.Failure);
}

[Fact]
public async Task CallingFailIfExtensionMethod_WhenFailsIf_ShouldReturnError()
{
// Arrange
ErrorOr<int> errorOrInt = 5;

// Act
ErrorOr<int> result = await errorOrInt
.ThenAsync(num => Task.FromResult(num))
.FailIf(num => num > 3, Error.Failure());

// Assert
result.IsError.Should().BeTrue();
result.FirstError.Type.Should().Be(ErrorType.Failure);
}

[Fact]
public void CallingFailIf_WhenDoesNotFailIf_ShouldReturnValue()
{
Expand Down
Loading