Skip to content

Commit

Permalink
Adding Task support to TapTry and TapIfTry.
Browse files Browse the repository at this point in the history
  • Loading branch information
tinytownsoftware committed Jun 6, 2023
1 parent 1c2b4f0 commit 0315cb0
Show file tree
Hide file tree
Showing 7 changed files with 924 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,24 @@
<Compile Update="Result\Methods\Extensions\TapIf.ValueTask.cs">
<DependentUpon>TapIf.cs</DependentUpon>
</Compile>
<Compile Update="Result\Methods\Extensions\TapTry.Task.cs">
<DependentUpon>TapTry.cs</DependentUpon>
</Compile>
<Compile Update="Result\Methods\Extensions\TapTry.Task.Left.cs">
<DependentUpon>TapTry.Task.cs</DependentUpon>
</Compile>
<Compile Update="Result\Methods\Extensions\TapTry.Task.Right.cs">
<DependentUpon>TapTry.Task.cs</DependentUpon>
</Compile>
<Compile Update="Result\Methods\Extensions\TapIfTry.Task.cs">
<DependentUpon>TapIfTry.cs</DependentUpon>
</Compile>
<Compile Update="Result\Methods\Extensions\TapIfTry.Task.Left.cs">
<DependentUpon>TapIfTry.Task.cs</DependentUpon>
</Compile>
<Compile Update="Result\Methods\Extensions\TapIfTry.Task.Right.cs">
<DependentUpon>TapIfTry.Task.cs</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
Expand Down
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);
}
}
}
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);
}
}
}
}
Loading

0 comments on commit 0315cb0

Please sign in to comment.