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

Boolean operators and TryGet method #45

Merged
merged 3 commits into from Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/OperationResults/IGenericResult{OfT}.cs
Expand Up @@ -3,4 +3,6 @@ namespace OperationResults;
public interface IGenericResult<T> : IGenericResult
{
public T? Content { get; }

public bool TryGet(out T? value);
}
9 changes: 9 additions & 0 deletions src/OperationResults/Result.cs
Expand Up @@ -52,4 +52,13 @@ public static Result Fail(int failureReason, string message, string detail, IEnu

public static Result Fail(int failureReason, Exception? error, IEnumerable<ValidationError>? validationErrors = null)
=> new(false, failureReason: failureReason, error: error, validationErrors: validationErrors);

public static bool operator true(Result result)
=> result.Success;

public static bool operator false(Result result)
=> !result.Success;

public static implicit operator bool(Result result)
=> result.Success;
}
15 changes: 15 additions & 0 deletions src/OperationResults/Result{OfT}.cs
Expand Up @@ -29,6 +29,12 @@ internal Result(bool success = true, T? content = default, int failureReason = F
ValidationErrors = validationErrors;
}

public bool TryGet(out T? value)
{
value = Content;
return Success;
}

public static Result<T> Ok(T? content = default)
=> new(success: true, content);

Expand Down Expand Up @@ -67,4 +73,13 @@ public static Result<T> Fail(int failureReason, Exception? error, IEnumerable<Va

public static implicit operator Result<T>(Result result)
=> new(result.Success, default, result.FailureReason, result.ErrorMessage, result.ErrorDetail, result.Error, result.ValidationErrors);

public static bool operator true(Result<T> result)
=> result.Success;

public static bool operator false(Result<T> result)
=> !result.Success;

public static implicit operator bool(Result<T> result)
=> result.Success;
}