Skip to content

Commit

Permalink
Add Map Extension Method to Support Mapping from Result<T> to Result (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrtnz94 authored Oct 28, 2024
1 parent 2ebaed8 commit 74c1a48
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
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());
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);
}
}

0 comments on commit 74c1a48

Please sign in to comment.