Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added non-append Success variants #57

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions TransactionHelpers/Interface/IResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ public interface IResult
/// </summary>
void ThrowIfError();

/// <summary>
/// Checks if the result has an error.
/// </summary>
/// <returns>True if the result has an error, otherwise false.</returns>
[MemberNotNullWhen(false, nameof(Error))]
bool Success();

/// <summary>
/// Checks if the result has an error or no value.
/// </summary>
/// <returns>True if the result has an error, otherwise false.</returns>
[MemberNotNullWhen(false, nameof(Error))]
bool SuccessAndHasValue();

/// <summary>
/// Appends the specified result and checks if the appended result has an error.
/// </summary>
Expand Down Expand Up @@ -174,4 +188,29 @@ public interface IResult<TValue> : IResult
/// <exception cref="EmptyResultException">the <see cref="IResult{TValue}.Value"/> has no value.</exception>
[MemberNotNull(nameof(Value))]
TValue GetValueOrThrow();

/// <summary>
/// Checks if the result has an error.
/// </summary>
/// <param name="value">The out result value.</param>
/// <returns>false if the result has an error or no value, otherwise true.</returns>
[MemberNotNullWhen(false, nameof(Error))]
bool Success(out TValue? value);

/// <summary>
/// Checks if the result has an error or no value.
/// </summary>
/// <returns>True if the result has an error, otherwise false.</returns>
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Value))]
new bool SuccessAndHasValue();

/// <summary>
/// Checks if the result has an error.
/// </summary>
/// <param name="value">The out result value.</param>
/// <returns>false if the result has an error or no value, otherwise true.</returns>
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Value))]
bool SuccessAndHasValue([NotNullWhen(true)] out TValue? value);
}
43 changes: 43 additions & 0 deletions TransactionHelpers/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public virtual void ThrowIfError()
}
}

/// <inheritdoc/>
[MemberNotNullWhen(false, nameof(Error))]
public bool Success()
{
return !IsError;
}

/// <inheritdoc/>
[MemberNotNullWhen(false, nameof(Error))]
public bool SuccessAndHasValue()
{
return !IsError;
}

/// <inheritdoc/>
[MemberNotNullWhen(false, nameof(Error))]
public bool Success<TAppend>(TAppend resultAppend, bool appendResultValues)
Expand Down Expand Up @@ -188,6 +202,17 @@ public class Result<TValue> : Result, IResult<TValue>
{
internal TValue? InternalValue;

/// <inheritdoc/>
[JsonIgnore]
public override Error? Error => base.Error;

/// <inheritdoc/>
public override IReadOnlyList<Error> Errors
{
get => base.Errors;
init => base.Errors = value.ToList();
}

/// <inheritdoc/>
public virtual TValue? Value
{
Expand Down Expand Up @@ -226,6 +251,24 @@ public virtual TValue GetValueOrThrow()
return Value;
}

/// <inheritdoc/>
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Value))]
public bool Success(out TValue? value)
{
value = Value;
return !IsError;
}

/// <inheritdoc/>
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Value))]
public bool SuccessAndHasValue([NotNullWhen(true)] out TValue? value)
{
value = Value;
return !IsError && !HasNoValue;
}

/// <summary>
/// Implicit operator for <see cref="Error"/> conversion.
/// </summary>
Expand Down