-
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.
* split `.ThenAsync` extension methods into separate file * added missing `.Then` and `.ThenAsync` extension methods * added unit tests for `.Then` and `.ThenAsync` extension methods * added extension methods for unit testing (`.ToResultTask`)
- Loading branch information
1 parent
cbbb798
commit fea0d50
Showing
27 changed files
with
1,455 additions
and
53 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
130 changes: 130 additions & 0 deletions
130
src/RailwayResult.FunctionalExtensions/ResultExtensions.ThenAsync.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,130 @@ | ||
namespace RailwayResult; | ||
|
||
public static partial class ResultExtensions | ||
{ | ||
public static async Task<Result> ThenAsync<TValue>(this Result<TValue> result, Func<TValue, Task<Result>> asyncMapper) | ||
{ | ||
if (result.IsFailure) | ||
return result.Error; | ||
|
||
return await asyncMapper(result.Value); | ||
} | ||
|
||
public static async Task<Result> ThenAsync<TValue>(this Task<Result<TValue>> resultTask, Func<TValue, Task<Result>> asyncMapper) | ||
{ | ||
var result = await resultTask; | ||
return await result.ThenAsync(asyncMapper); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TValue, TOut>(this Result<TValue> result, Func<TValue, Task<TOut>> asyncMapper) | ||
{ | ||
if (result.IsFailure) | ||
return result.Error; | ||
|
||
return await asyncMapper(result.Value); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TValue, TOut>(this Task<Result<TValue>> resultTask, Func<TValue, Task<TOut>> asyncMapper) | ||
{ | ||
var result = await resultTask; | ||
return await result.ThenAsync(asyncMapper); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TValue, TOut>(this Result<TValue> result, Func<TValue, Task<Result<TOut>>> asyncMapper) | ||
{ | ||
if (result.IsFailure) | ||
return result.Error; | ||
|
||
return await asyncMapper(result.Value); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TValue, TOut>(this Task<Result<TValue>> resultTask, Func<TValue, Task<Result<TOut>>> asyncMapper) | ||
{ | ||
var result = await resultTask; | ||
return await result.ThenAsync(asyncMapper); | ||
} | ||
|
||
public static async Task<Result> ThenAsync<TFirst, TSecond>(this Result<(TFirst, TSecond)> result, Func<TFirst, TSecond, Task<Result>> asyncMapper) | ||
{ | ||
if (result.IsFailure) | ||
return result.Error; | ||
|
||
return await asyncMapper(result.Value.Item1, result.Value.Item2); | ||
} | ||
|
||
public static async Task<Result> ThenAsync<TFirst, TSecond>(this Task<Result<(TFirst, TSecond)>> resultTask, Func<TFirst, TSecond, Task<Result>> asyncMapper) | ||
{ | ||
var result = await resultTask; | ||
return await result.ThenAsync(asyncMapper); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TFirst, TSecond, TOut>(this Result<(TFirst, TSecond)> result, Func<TFirst, TSecond, Task<TOut>> asyncMapper) | ||
{ | ||
if (result.IsFailure) | ||
return result.Error; | ||
|
||
return await asyncMapper(result.Value.Item1, result.Value.Item2); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TFirst, TSecond, TOut>(this Task<Result<(TFirst, TSecond)>> selfTask, Func<TFirst, TSecond, Task<TOut>> asyncMapper) | ||
{ | ||
var self = await selfTask; | ||
return await self.ThenAsync(asyncMapper); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TFirst, TSecond, TOut>(this Result<(TFirst, TSecond)> result, Func<TFirst, TSecond, Task<Result<TOut>>> asyncMapper) | ||
{ | ||
if (result.IsFailure) | ||
return result.Error; | ||
|
||
return await asyncMapper(result.Value.Item1, result.Value.Item2); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TFirst, TSecond, TOut>(this Task<Result<(TFirst, TSecond)>> resultTask, Func<TFirst, TSecond, Task<Result<TOut>>> asyncMapper) | ||
{ | ||
var result = await resultTask; | ||
return await result.ThenAsync(asyncMapper); | ||
} | ||
|
||
public static async Task<Result> ThenAsync<TFirst, TSecond, TThird>(this Result<(TFirst, TSecond, TThird)> result, Func<TFirst, TSecond, TThird, Task<Result>> asyncMapper) | ||
{ | ||
if (result.IsFailure) | ||
return result.Error; | ||
|
||
return await asyncMapper(result.Value.Item1, result.Value.Item2, result.Value.Item3); | ||
} | ||
|
||
public static async Task<Result> ThenAsync<TFirst, TSecond, TThird>(this Task<Result<(TFirst, TSecond, TThird)>> resultTask, Func<TFirst, TSecond, TThird, Task<Result>> asyncMapper) | ||
{ | ||
var result = await resultTask; | ||
return await result.ThenAsync(asyncMapper); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TFirst, TSecond, TThird, TOut>(this Result<(TFirst, TSecond, TThird)> result, Func<TFirst, TSecond, TThird, Task<TOut>> asyncMapper) | ||
{ | ||
if (result.IsFailure) | ||
return result.Error; | ||
|
||
return await asyncMapper(result.Value.Item1, result.Value.Item2, result.Value.Item3); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TFirst, TSecond, TThird, TOut>(this Task<Result<(TFirst, TSecond, TThird)>> resultTask, Func<TFirst, TSecond, TThird, Task<TOut>> asyncMapper) | ||
{ | ||
var result = await resultTask; | ||
return await result.ThenAsync(asyncMapper); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TFirst, TSecond, TThird, TOut>(this Result<(TFirst, TSecond, TThird)> result, Func<TFirst, TSecond, TThird, Task<Result<TOut>>> asyncMapper) | ||
{ | ||
if (result.IsFailure) | ||
return result.Error; | ||
|
||
return await asyncMapper(result.Value.Item1, result.Value.Item2, result.Value.Item3); | ||
} | ||
|
||
public static async Task<Result<TOut>> ThenAsync<TFirst, TSecond, TThird, TOut>(this Task<Result<(TFirst, TSecond, TThird)>> resultTask, Func<TFirst, TSecond, TThird, Task<Result<TOut>>> asyncMapper) | ||
{ | ||
var result = await resultTask; | ||
return await result.ThenAsync(asyncMapper); | ||
} | ||
} |
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
18 changes: 0 additions & 18 deletions
18
tests/RailwayResult.FunctionalExtensions.Tests/ComparerExtension.cs
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
tests/RailwayResult.FunctionalExtensions.Tests/ErrorExtensions.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,20 @@ | ||
namespace RailwayResult.FunctionalExtensions.Tests; | ||
|
||
public static class ErrorExtensions | ||
{ | ||
public static Result ToResult<TError>(this TError error) where TError : Error => error; | ||
|
||
public static Result<T> ToResult<T, TError>(this TError error) where TError : Error => error; | ||
|
||
public static Task<Result> ToResultTask<TError>(this TError error) where TError : Error | ||
{ | ||
Result r = error; | ||
return Task.FromResult(r); | ||
} | ||
|
||
public static Task<Result<T>> ToResultTask<T, TError>(this TError error) where TError : Error | ||
{ | ||
Result<T> r = error; | ||
return Task.FromResult(r); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
tests/RailwayResult.FunctionalExtensions.Tests/ResultExtensions.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,35 @@ | ||
namespace RailwayResult.FunctionalExtensions.Tests; | ||
|
||
public static class ResultExtensions | ||
{ | ||
public static void ShouldBe(this Result self, Result expected) | ||
{ | ||
if (self.IsSuccess) | ||
{ | ||
expected.IsSuccess.Should().BeTrue(); | ||
} | ||
else | ||
{ | ||
expected.IsSuccess.Should().BeFalse(); | ||
self.Error.Should().BeEquivalentTo(expected.Error); | ||
} | ||
} | ||
|
||
public static void ShouldBe<T>(this Result<T> self, Result<T> expected) | ||
{ | ||
if (self.IsSuccess) | ||
{ | ||
expected.IsSuccess.Should().BeTrue(); | ||
self.Value.Should().BeEquivalentTo(expected.Value); | ||
} | ||
else | ||
{ | ||
expected.IsSuccess.Should().BeFalse(); | ||
self.Error.Should().BeEquivalentTo(expected.Error); | ||
} | ||
} | ||
|
||
public static Task<Result> ToResultTask(this Result result) => Task.FromResult(result); | ||
|
||
public static Task<Result<T>> ToResultTask<T>(this Result<T> result) => Task.FromResult(result); | ||
} |
Oops, something went wrong.