forked from vkhorikov/CSharpFunctionalExtensions
-
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.
Adding Task support to TapTry and TapIfTry.
- Loading branch information
1 parent
1c2b4f0
commit 0315cb0
Showing
7 changed files
with
924 additions
and
0 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
108 changes: 108 additions & 0 deletions
108
CSharpFunctionalExtensions/Result/Methods/Extensions/TapIfTry.Task.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,108 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpFunctionalExtensions | ||
{ | ||
public static partial class AsyncResultExtensionsLeftOperand | ||
{ | ||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result> TapIfTry(this Task<Result> resultTask, bool condition, Action action) | ||
{ | ||
var result = await resultTask.DefaultAwait(); | ||
return result.TapIfTry(condition, action); | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T>> TapIfTry<T>(this Task<Result<T>> resultTask, bool condition, Action action) | ||
{ | ||
var result = await resultTask.DefaultAwait(); | ||
return result.TapIfTry(condition, action); | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T>> TapIfTry<T>(this Task<Result<T>> resultTask, bool condition, Action<T> action) | ||
{ | ||
var result = await resultTask.DefaultAwait(); | ||
return result.TapIfTry(condition, action); | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<UnitResult<E>> TapIfTry<E>(this Task<UnitResult<E>> resultTask, bool condition, Action action, Func<Exception, E> errorHandler) | ||
{ | ||
var result = await resultTask.DefaultAwait(); | ||
return result.TapIfTry(condition, action, errorHandler); | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T, E>> TapIfTry<T, E>(this Task<Result<T, E>> resultTask, bool condition, Action action, Func<Exception, E> errorHandler) | ||
{ | ||
var result = await resultTask.DefaultAwait(); | ||
return result.TapIfTry(condition, action, errorHandler); | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T, E>> TapIfTry<T, E>(this Task<Result<T, E>> resultTask, bool condition, Action<T> action, Func<Exception, E> errorHandler) | ||
{ | ||
var result = await resultTask.DefaultAwait(); | ||
return result.TapIfTry(condition, action, errorHandler); | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the predicate is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T>> TapIfTry<T>(this Task<Result<T>> resultTask, Func<T, bool> predicate, Action action) | ||
{ | ||
var result = await resultTask.DefaultAwait(); | ||
return result.TapIfTry(predicate, action); | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the predicate is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T>> TapIfTry<T>(this Task<Result<T>> resultTask, Func<T, bool> predicate, Action<T> action) | ||
{ | ||
var result = await resultTask.DefaultAwait(); | ||
return result.TapIfTry(predicate, action); | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the predicate is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T, E>> TapIfTry<T, E>(this Task<Result<T, E>> resultTask, Func<T, bool> predicate, Action action, Func<Exception, E> errorHandler) | ||
{ | ||
var result = await resultTask.DefaultAwait(); | ||
return result.TapIfTry(predicate, action, errorHandler); | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the predicate is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T, E>> TapIfTry<T, E>(this Task<Result<T, E>> resultTask, Func<T, bool> predicate, Action<T> action, Func<Exception, E> errorHandler) | ||
{ | ||
var result = await resultTask.DefaultAwait(); | ||
return result.TapIfTry(predicate, action, errorHandler); | ||
} | ||
} | ||
} |
218 changes: 218 additions & 0 deletions
218
CSharpFunctionalExtensions/Result/Methods/Extensions/TapIfTry.Task.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,218 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace CSharpFunctionalExtensions | ||
{ | ||
public static partial class AsyncResultExtensionsRightOperand | ||
{ | ||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result> TapIfTry(this Result result, bool condition, Func<Task> func, Func<Exception, string> errorHandler = null) | ||
{ | ||
errorHandler ??= Result.Configuration.DefaultTryErrorHandler; | ||
|
||
try | ||
{ | ||
if (condition && result.IsSuccess) | ||
await func(); | ||
|
||
return result; | ||
} | ||
catch (Exception exc) | ||
{ | ||
string message = errorHandler(exc); | ||
return Result.Failure(message); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T>> TapIfTry<T>(this Result<T> result, bool condition, Func<Task> func, Func<Exception, string> errorHandler = null) | ||
{ | ||
errorHandler ??= Result.Configuration.DefaultTryErrorHandler; | ||
|
||
try | ||
{ | ||
if (condition && result.IsSuccess) | ||
await func(); | ||
|
||
return result; | ||
} | ||
catch (Exception exc) | ||
{ | ||
string message = errorHandler(exc); | ||
return new Result<T>(true, message, default); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T>> TapIfTry<T>(this Result<T> result, bool condition, Func<T, Task> func, Func<Exception, string> errorHandler = null) | ||
{ | ||
errorHandler ??= Result.Configuration.DefaultTryErrorHandler; | ||
|
||
try | ||
{ | ||
if (condition && result.IsSuccess) | ||
await func(result.Value); | ||
|
||
return result; | ||
} | ||
catch (Exception exc) | ||
{ | ||
string message = errorHandler(exc); | ||
return new Result<T>(true, message, default); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<UnitResult<E>> TapIfTry<E>(this UnitResult<E> result, bool condition, Func<Task> func, Func<Exception, E> errorHandler) | ||
{ | ||
try | ||
{ | ||
if (condition && result.IsSuccess) | ||
await func(); | ||
|
||
return result; | ||
} | ||
catch (Exception exc) | ||
{ | ||
var error = errorHandler(exc); | ||
return new UnitResult<E>(true, error); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T, E>> TapIfTry<T, E>(this Result<T, E> result, bool condition, Func<Task> func, Func<Exception, E> errorHandler) | ||
{ | ||
try | ||
{ | ||
if (condition && result.IsSuccess) | ||
await func(); | ||
|
||
return result; | ||
} | ||
catch (Exception exc) | ||
{ | ||
var error = errorHandler(exc); | ||
return new Result<T, E>(true, error, default); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the condition is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T, E>> TapIfTry<T, E>(this Result<T, E> result, bool condition, Func<T, Task> func, Func<Exception, E> errorHandler) | ||
{ | ||
try | ||
{ | ||
if (condition && result.IsSuccess) | ||
await func(result.Value); | ||
|
||
return result; | ||
} | ||
catch (Exception exc) | ||
{ | ||
var error = errorHandler(exc); | ||
return new Result<T, E>(true, error, default); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the predicate is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T>> TapIfTry<T>(this Result<T> result, Func<T, bool> predicate, Func<Task> func, Func<Exception, string> errorHandler = null) | ||
{ | ||
errorHandler ??= Result.Configuration.DefaultTryErrorHandler; | ||
|
||
try | ||
{ | ||
if (result.IsSuccess && predicate(result.Value) && result.IsSuccess) | ||
await func(); | ||
|
||
return result; | ||
} | ||
catch (Exception exc) | ||
{ | ||
string message = errorHandler(exc); | ||
return new Result<T>(true, message, default); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the predicate is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T>> TapIfTry<T>(this Result<T> result, Func<T, bool> predicate, Func<T, Task> func, Func<Exception, string> errorHandler = null) | ||
{ | ||
errorHandler ??= Result.Configuration.DefaultTryErrorHandler; | ||
|
||
try | ||
{ | ||
if (result.IsSuccess && predicate(result.Value)) | ||
await func(result.Value); | ||
|
||
return result; | ||
} | ||
catch (Exception exc) | ||
{ | ||
string message = errorHandler(exc); | ||
return new Result<T>(true, message, default); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the predicate is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T, E>> TapIfTry<T, E>(this Result<T, E> result, Func<T, bool> predicate, Func<Task> func, Func<Exception, E> errorHandler) | ||
{ | ||
try | ||
{ | ||
if (result.IsSuccess && predicate(result.Value)) | ||
await func(); | ||
|
||
return result; | ||
} | ||
catch (Exception exc) | ||
{ | ||
var error = errorHandler(exc); | ||
return new Result<T, E>(true, error, default); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Executes the given action if the calling result is a success and the predicate is true. Returns the calling result. | ||
/// If there is an exception, returns a new failure Result. | ||
/// </summary> | ||
public static async Task<Result<T, E>> TapIfTry<T, E>(this Result<T, E> result, Func<T, bool> predicate, Func<T, Task> func, Func<Exception, E> errorHandler) | ||
{ | ||
try | ||
{ | ||
if (result.IsSuccess && predicate(result.Value)) | ||
await func(result.Value); | ||
|
||
return result; | ||
} | ||
catch (Exception exc) | ||
{ | ||
var error = errorHandler(exc); | ||
return new Result<T, E>(true, error, default); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.