Skip to content

Commit

Permalink
Merge pull request #2 from deveel/support-1
Browse files Browse the repository at this point in the history
Support Package 1
  • Loading branch information
tsutomi authored Jul 6, 2024
2 parents 1ba8362 + 88a2cbb commit fac5069
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 55 deletions.
28 changes: 28 additions & 0 deletions src/Deveel.Results/OperationErrorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Deveel
{
/// <summary>
/// Extends the <see cref="IOperationError"/> contract to provide
/// additional functionality.
/// </summary>
public static class OperationErrorExtensions
{
/// <summary>
/// Converts the error to an operation exception.
/// </summary>
/// <param name="error">
/// The error to convert to an exception.
/// </param>
/// <returns>
/// Returns an instance of <see cref="OperationException"/> that
/// represents the error.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when the <paramref name="error"/> is <see langword="null"/>.
/// </exception>
public static OperationException AsException(this IOperationError error)
{
ArgumentNullException.ThrowIfNull(error, nameof(error));
return new OperationException(error.Code, error.Domain, error.Message, error.InnerError?.AsException());
}
}
}
5 changes: 0 additions & 5 deletions src/Deveel.Results/OperationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ private OperationResult(OperationResultType resultType, IOperationError? error)
/// </summary>
public static readonly OperationResult NotChanged = new(OperationResultType.Unchanged, null);

/// <summary>
/// The result of an operation that has been cancelled.
/// </summary>
public static readonly OperationResult Cancelled = new(OperationResultType.Cancelled, null);

/// <summary>
/// Creates a new instance of an operation result that has failed.
/// </summary>
Expand Down
138 changes: 125 additions & 13 deletions src/Deveel.Results/OperationResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ public static bool IsSuccess(this IOperationResult result)
public static bool IsError(this IOperationResult result)
=> result.ResultType == OperationResultType.Error;

/// <summary>
/// Determines if the operation result is cancelled.
/// </summary>
/// <param name="result">
/// The operation result to check.
/// </param>
/// <returns>
/// Returns <see langword="true"/> if the operation result is cancelled,
/// otherwise <see langword="false"/>.
/// </returns>
public static bool IsCancelled(this IOperationResult result)
=> result.ResultType == OperationResultType.Cancelled;

/// <summary>
/// Determines if the operation has caused no changes
/// to the state of an object.
Expand Down Expand Up @@ -197,5 +184,130 @@ public static Task<TResult> MatchAsync<TResult>(this IOperationResult result,

throw new InvalidOperationException("The operation result is in an unknown state.");
}

/// <summary>
/// Attempts to match the operation result to a specific state
/// that can be handled by the caller.
/// </summary>
/// <typeparam name="TResult">
/// The type of the result that is returned by the match.
/// </typeparam>
/// <param name="result">
/// The operation result to match.
/// </param>
/// <param name="ifSuccess">
/// A function that is called when the operation result was a success.
/// </param>
/// <param name="ifError">
/// A function that is called when the operation result was an error.
/// </param>
/// <param name="ifUnchanged">
/// A function that is called when the operation result caused no changed
/// to the object.
/// </param>
/// <returns>
/// Returns the result of the function that was called based on the state
/// of the operation result.
/// </returns>
/// <exception cref="InvalidOperationException">
/// Thrown when the operation result is in an unknown state.
/// </exception>
public static TResult Match<T, TResult>(this IOperationResult<T> result,

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Publish Package

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Publish Package

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Publish Package

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / publish

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / publish

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)

Check warning on line 215 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / publish

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.Match<T, TResult>(IOperationResult<T>, Func<T?, TResult>?, Func<IOperationError?, TResult>?, Func<T?, TResult>?)' (but other type parameters do)
Func<T?, TResult>? ifSuccess = null,
Func<IOperationError?, TResult>? ifError = null,
Func<T?, TResult>? ifUnchanged = null)
{
ArgumentNullException.ThrowIfNull(result, nameof(result));

if (result.IsSuccess())
{
ArgumentNullException.ThrowIfNull(ifSuccess, nameof(ifSuccess));
return ifSuccess(result.Value);
}

if (result.IsError())
{
ArgumentNullException.ThrowIfNull(ifError, nameof(ifError));
return ifError(result.Error);
}

if (result.IsUnchanged())
{
ArgumentNullException.ThrowIfNull(ifUnchanged, nameof(ifUnchanged));
return ifUnchanged(result.Value);
}

throw new InvalidOperationException("The operation result is in an unknown state.");
}

/// <summary>
/// Attempts to match the operation result to a specific state
/// that can be handled by the caller.
/// </summary>
/// <typeparam name="TResult">
/// The type of the result that is returned by the match.
/// </typeparam>
/// <param name="result">
/// The operation result to match.
/// </param>
/// <param name="ifSuccess">
/// A function that is called when the operation result was a success.
/// </param>
/// <param name="ifError">
/// A function that is called when the operation result was an error.
/// </param>
/// <param name="ifUnchanged">
/// A function that is called when the operation result caused no changed
/// to the object.
/// </param>
/// <returns>
/// Returns the result of the function that was called based on the state
/// of the operation result.
/// </returns>
/// <exception cref="InvalidOperationException">
/// Thrown when the operation result is in an unknown state.
/// </exception>
public static Task<TResult> MatchAsync<T, TResult>(this IOperationResult<T> result,

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Publish Package

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Publish Package

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / Publish Package

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / publish

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / publish

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)

Check warning on line 270 in src/Deveel.Results/OperationResultExtensions.cs

View workflow job for this annotation

GitHub Actions / publish

Type parameter 'T' has no matching typeparam tag in the XML comment on 'OperationResultExtensions.MatchAsync<T, TResult>(IOperationResult<T>, Func<T?, Task<TResult>>?, Func<IOperationError?, Task<TResult>>?, Func<T?, Task<TResult>>?)' (but other type parameters do)
Func<T?, Task<TResult>>? ifSuccess = null,
Func<IOperationError?, Task<TResult>>? ifError = null,
Func<T?, Task<TResult>>? ifUnchanged = null)
{
ArgumentNullException.ThrowIfNull(result, nameof(result));

if (result.IsSuccess())
{
ArgumentNullException.ThrowIfNull(ifSuccess, nameof(ifSuccess));
return ifSuccess(result.Value);
}

if (result.IsError())
{
ArgumentNullException.ThrowIfNull(ifError, nameof(ifError));
return ifError(result.Error);
}

if (result.IsUnchanged())
{
ArgumentNullException.ThrowIfNull(ifUnchanged, nameof(ifUnchanged));
return ifUnchanged(result.Value);
}

throw new InvalidOperationException("The operation result is in an unknown state.");
}



/// <summary>
/// Converts an error result to an exception.
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
public static OperationException? AsException(this IOperationResult result)
{
if (!result.IsError() || result.Error == null)
return null;

return result.Error.AsException();
}
}
}
7 changes: 1 addition & 6 deletions src/Deveel.Results/OperationResultType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public enum OperationResultType
/// <summary>
/// The operation caused no changes.
/// </summary>
Unchanged = 3,

/// <summary>
/// The operation was cancelled.
/// </summary>
Cancelled = 4
Unchanged = 3
}
}
40 changes: 29 additions & 11 deletions src/Deveel.Results/OperationResult_T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@ private OperationResult(OperationResultType resultType, T? value, IOperationErro
/// <inheritdoc/>
public IOperationError? Error { get; }

/// <summary>
/// A result of an operation that has not changed the state
/// of an object.
/// </summary>
public static readonly OperationResult<T> NotChanged = new(OperationResultType.Unchanged, default, null);

/// <summary>
/// A result of an operation that has been cancelled.
/// </summary>
public static readonly OperationResult<T> Cancelled = new(OperationResultType.Cancelled, default, null);

/// <summary>
/// Creates a new instance of an operation result that has succeeded
/// with the given value.
Expand All @@ -50,6 +39,21 @@ public static OperationResult<T> Success(T? value)
return new OperationResult<T>(OperationResultType.Success, value, null);
}

/// <summary>
/// Creates a new instance of an operation result that has caused
/// no change in the state of an object.
/// </summary>
/// <param name="value">
/// The value that represents the object that was attempted
/// to be changed.
/// </param>
/// <returns>
/// Returns an instance of <see cref="OperationResult{T}"/> that
/// represents an unchanged operation.
/// </returns>
public static OperationResult<T> NotChanged(T? value = default)
=> new(OperationResultType.Unchanged, value, null);

/// <summary>
/// Creates a new instance of an operation result that has failed
/// because of an error.
Expand Down Expand Up @@ -153,5 +157,19 @@ public static implicit operator OperationResult<T>(OperationResult result)
/// </param>
/// <seealso cref="Fail(IOperationError)"/>
public static implicit operator OperationResult<T>(OperationException error) => Fail(error);

/// <summary>
/// Implicitly converts the result to is value
/// </summary>
/// <param name="result">
/// The operation result that encapsulates the value to convert to.
/// </param>
public static implicit operator T?(OperationResult<T> result)
{
if (result.IsError())
throw result.AsException()!;

return result.Value;
}
}
}
Loading

0 comments on commit fac5069

Please sign in to comment.