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

Add Map Extension Method to Support Mapping from Result<T> to Result #213

Merged
merged 6 commits into from
Oct 28, 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
34 changes: 34 additions & 0 deletions src/Ardalis.Result/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,39 @@ private static Result HandleNonSuccessStatus<TSource>(Result<TSource> result)
_ => throw new NotSupportedException($"Result {result.Status} conversion is not supported."),
};
}

/// <summary>
/// Transforms a Result's type from a source type to a non-generic Result type.
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <param name="result"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public static Result Map<TSource>(this Result<TSource> result)
{
switch (result.Status)
{
case ResultStatus.Ok: return Result.Success();
case ResultStatus.NotFound: return result.Errors.Any()
? Result.NotFound(result.Errors.ToArray())
: Result.NotFound();
case ResultStatus.Unauthorized: return result.Errors.Any()
? Result.Unauthorized(result.Errors.ToArray())
: Result.Unauthorized();
case ResultStatus.Forbidden: return result.Errors.Any()
? Result.Forbidden(result.Errors.ToArray())
: Result.Forbidden();
case ResultStatus.Invalid: return Result.Invalid(result.ValidationErrors);
case ResultStatus.Error: return Result.Error(new ErrorList(result.Errors.ToArray(), result.CorrelationId));
case ResultStatus.Conflict: return result.Errors.Any()
? Result.Conflict(result.Errors.ToArray())
: Result.Conflict();
case ResultStatus.CriticalError: return Result.CriticalError(result.Errors.ToArray());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need the correlation id as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CriticalError accepts a string[], so we cannot pass in the correlation id. See https://github.com/ardalis/Result/blob/main/src/Ardalis.Result/Result.cs#L223.

While Error accepts an ErrorList object that contains correlation id. See https://github.com/ardalis/Result/blob/main/src/Ardalis.Result/Result.cs#L123-L127

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we're in a bit of a transition phase to using the ErrorList class in more places but ideally we'd use the constructors that accept a CorrelationId wherever possible.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have verified the CorrelationId property is propagated through the constructors where it is available. We can open another issue to add CorrelationId to the remaining constructors and then update these helper methods as needed.

case ResultStatus.Unavailable: return Result.Unavailable(result.Errors.ToArray());
case ResultStatus.NoContent: return Result.NoContent();
default:
throw new NotSupportedException($"Result {result.Status} conversion is not supported.");
}
}
}
}
125 changes: 125 additions & 0 deletions tests/Ardalis.Result.UnitTests/ResultGenericToVoidMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using FluentAssertions;
using FluentAssertions.Execution;
using System;
using System.Linq;
using Xunit;

namespace Ardalis.Result.UnitTests;

public class ResultGenericToVoidMap
{
[Fact]
public void ShouldProduceSuccess()
{
var result = Result<int>.Success(1);

var actual = result.Map();

actual.Status.Should().Be(ResultStatus.Ok);
}

[Fact]
public void ShouldProduceNotFound()
{
var result = Result<int>.NotFound();

var actual = result.Map();

actual.Status.Should().Be(ResultStatus.NotFound);
}

[Fact]
public void ShouldProduceUnauthorized()
{
var result = Result<int>.Unauthorized();

var actual = result.Map();

actual.Status.Should().Be(ResultStatus.Unauthorized);
}

[Fact]
public void ShouldProduceForbidden()
{
var result = Result<int>.Forbidden();

var actual = result.Map();

actual.Status.Should().Be(ResultStatus.Forbidden);
}

[Fact]
public void ShouldProduceInvalid()
{
var result = Result<int>.Invalid(new ValidationError());

var actual = result.Map();

actual.Status.Should().Be(ResultStatus.Invalid);
}

[Fact]
public void ShouldProduceError()
{
var result = Result<int>.Error();

var actual = result.Map();

actual.Status.Should().Be(ResultStatus.Error);
}

[Fact]
public void ShouldProduceErrorWhenConstructedWithErrorList()
{
string correlationId = Guid.NewGuid().ToString();
string errorMessage = "Error occured 💥";
var result = Result<int>.Error(new ErrorList([errorMessage], correlationId));

var actual = result.Map();

using var assertionScope = new AssertionScope();
actual.Status.Should().Be(ResultStatus.Error);
actual.CorrelationId.Should().Be(correlationId);
actual.Errors.Single().Should().Be(errorMessage);
}

[Fact]
public void ShouldProduceConflict()
{
var result = Result<int>.Conflict();

var actual = result.Map();

actual.Status.Should().Be(ResultStatus.Conflict);
}

[Fact]
public void ShouldProduceCriticalError()
{
var result = Result<int>.CriticalError();

var actual = result.Map();

actual.Status.Should().Be(ResultStatus.CriticalError);
}

[Fact]
public void ShouldProduceUnavailable()
{
var result = Result<int>.Unavailable();

var actual = result.Map();

actual.Status.Should().Be(ResultStatus.Unavailable);
}

[Fact]
public void ShouldProduceNoContent()
{
var result = Result<int>.NoContent();

var actual = result.Map();

actual.Status.Should().Be(ResultStatus.NoContent);
}
}
Loading