Skip to content

Commit

Permalink
ResultOrErrors deconstruct:
Browse files Browse the repository at this point in the history
Result<TValue> now has a new deconstruct (golang-style style): `Deconstruct(out TValue value, out List<IError> errors)`
Returning isSuccess and isFailed were redundant since we can infer success/failure from non-null values in `TValue value` and `List<IError> errors`.

ResultBase still has `Deconstruct(out bool isSuccess, out bool isFailed)`, so non-generic Results (without TValue) can still use that isSuccess/isFailed deconstruction,
but the breaking change is that someone using Result<bool> can't use that bool/bool deconstruct anymore.
  • Loading branch information
Drizin committed Aug 3, 2024
1 parent 0c3376f commit a07f12e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/FluentResults.Test/ResultWithValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,21 +998,21 @@ public void Implicit_conversion_Result_Value_is_converted_to_Result_object_with_
}

[Fact]
public void Can_deconstruct_generic_Ok_to_isSuccess_and_isFailed()
public void Can_deconstruct_generic_Ok_to_isSuccess_and_errors()
{
var (isSuccess, isFailed) = Result.Ok(true);
var (isSuccess, errors) = Result.Ok(true);

isSuccess.Should().Be(true);
isFailed.Should().Be(false);
errors.Should().BeNull();
}

[Fact]
public void Can_deconstruct_generic_Fail_to_isSuccess_and_isFailed()
public void Can_deconstruct_generic_Fail_to_isSuccess_and_errors()
{
var (isSuccess, isFailed) = Result.Fail<bool>("fail");
var (isSuccess, errors) = Result.Fail<bool>("fail");

isSuccess.Should().Be(false);
isFailed.Should().Be(true);
errors.Should().NotBeNullOrEmpty();
}

[Fact]
Expand All @@ -1035,6 +1035,29 @@ public void Can_deconstruct_generic_Fail_to_isSuccess_and_isFailed_and_value()
value.Should().Be(default);
}

[Fact]
public void Can_deconstruct_generic_Ok_to_value_with_errors()
{
var (value, errors) = Result.Ok(100);

value.Should().Be(100);
errors.Should().BeNull();
}

[Fact]
public void Can_deconstruct_generic_Fail_to_errors_with_value()
{
var error = new Error("fail");

var (value, errors) = Result.Fail<bool>(error);

value.Should().Be(default);

errors.Count.Should().Be(1);
errors.FirstOrDefault().Should().Be(error);
}


[Fact]
public void Can_deconstruct_generic_Ok_to_isSuccess_and_isFailed_and_value_with_errors()
{
Expand Down
11 changes: 11 additions & 0 deletions src/FluentResults/Results/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,17 @@ public void Deconstruct(out bool isSuccess, out bool isFailed, out TValue value,
errors = IsFailed ? Errors : default;
}

/// <summary>
/// Deconstruct Result
/// </summary>
/// <param name="value"></param>
/// <param name="errors"></param>
public void Deconstruct(out TValue value, out List<IError> errors)
{
value = IsSuccess ? Value : default;
errors = IsFailed ? Errors : default;
}

private void ThrowIfFailed()
{
if (IsFailed)
Expand Down

0 comments on commit a07f12e

Please sign in to comment.