Skip to content

Commit

Permalink
Merge pull request #576 from marcinjahn/toresult-with-error-func
Browse files Browse the repository at this point in the history
feat: add lazy overload of Maybe.ToResult
  • Loading branch information
vkhorikov authored Dec 23, 2024
2 parents 0d8259f + 6896bd3 commit b0b37fe
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 120 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task ToResult_Task_returns_success_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsTask().ToResult("Error".AsTask());
var result = await maybe.AsTask().ToResult("Error");

result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
Expand All @@ -44,10 +44,32 @@ public async Task ToResult_Task_custom_failure_returns_success_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsTask().ToResult(E.Value.AsTask());
var result = await maybe.AsTask().ToResult(E.Value);

result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
}

[Fact]
public async Task ToResult_Task_returns_custom_failure_via_error_function_if_has_no_value()
{
Maybe<T> maybe = null;

var result = await maybe.AsTask().ToResult(() => E.Value);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
}

[Fact]
public async Task ToResult_Task_custom_failure_with_error_function_returns_success_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsTask().ToResult(() => E.Value);

result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task ToResult_ValueTask_returns_success_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsValueTask().ToResult("Error".AsValueTask());
var result = await maybe.AsValueTask().ToResult("Error");

result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
Expand All @@ -45,10 +45,32 @@ public async Task ToResult_ValueTask_custom_failure_returns_success_if_has_value
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsValueTask().ToResult(E.Value.AsValueTask());
var result = await maybe.AsValueTask().ToResult(E.Value);

result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
}

[Fact]
public async Task ToResult_ValueTask_returns_custom_failure_via_error_function_if_has_no_value()
{
Maybe<T> maybe = null;

var result = await maybe.AsValueTask().ToResult(() => E.Value);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
}

[Fact]
public async Task ToResult_ValueTask_custom_failure_with_error_function_returns_success_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = await maybe.AsValueTask().ToResult(() => E.Value);

result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using FluentAssertions;
using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.MaybeTests.Extensions
Expand Down Expand Up @@ -49,5 +48,27 @@ public void ToResult_custom_failure_returns_success_if_has_value()
result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
}

[Fact]
public void ToResult_returns_custom_failure_via_error_function_if_has_no_value()
{
Maybe<T> maybe = null;

var result = maybe.ToResult(() => E.Value);

result.IsSuccess.Should().BeFalse();
result.Error.Should().Be(E.Value);
}

[Fact]
public void ToResult_custom_failure_with_error_function_returns_success_if_has_value()
{
var maybe = Maybe<T>.From(T.Value);

var result = maybe.ToResult(() => E.Value);

result.IsSuccess.Should().BeTrue();
result.Value.Should().Be(T.Value);
}
}
}
11 changes: 9 additions & 2 deletions CSharpFunctionalExtensions/Maybe/Extensions/ToResult.Task.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;

namespace CSharpFunctionalExtensions
{
Expand All @@ -15,5 +16,11 @@ public static async Task<Result<T, E>> ToResult<T, E>(this Task<Maybe<T>> maybeT
var maybe = await maybeTask.DefaultAwait();
return maybe.ToResult(error);
}

public static async Task<Result<T, E>> ToResult<T, E>(this Task<Maybe<T>> maybeTask, Func<E> errorFunc)
{
var maybe = await maybeTask.DefaultAwait();
return maybe.ToResult(errorFunc);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public static async ValueTask<Result<T, E>> ToResult<T, E>(this ValueTask<Maybe<
Maybe<T> maybe = await maybeTask;
return maybe.ToResult(error);
}

public static async ValueTask<Result<T, E>> ToResult<T, E>(this ValueTask<Maybe<T>> maybeTask, Func<E> errorFunc)
{
Maybe<T> maybe = await maybeTask;
return maybe.ToResult(errorFunc);
}
}
}
#endif
#endif
14 changes: 12 additions & 2 deletions CSharpFunctionalExtensions/Maybe/Extensions/ToResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace CSharpFunctionalExtensions
using System;

namespace CSharpFunctionalExtensions
{
public static partial class MaybeExtensions
{
Expand All @@ -17,5 +19,13 @@ public static Result<T, E> ToResult<T, E>(in this Maybe<T> maybe, E error)

return Result.Success<T, E>(maybe.GetValueOrThrow());
}

public static Result<T, E> ToResult<T, E>(in this Maybe<T> maybe, Func<E> errorFunc)
{
if (maybe.HasNoValue)
return Result.Failure<T, E>(errorFunc());

return Result.Success<T, E>(maybe.GetValueOrThrow());
}
}
}
}

0 comments on commit b0b37fe

Please sign in to comment.