diff --git a/README.md b/README.md index 3d2cd06..b3b25d7 100644 --- a/README.md +++ b/README.md @@ -509,12 +509,18 @@ var outcome = result switch }; ``` -### Deconstruct Operators +### Golang-style Deconstruct Operators ```csharp -var (isSuccess, isFailed, value, errors) = Result.Fail("Failure 1"); +// For Result you get TValue and Errors +var (result, errors) = Result.Ok(1); +var (result, errors) = Result.Ok(1); +// beware that on error you will get "result==0" (since TValue is a non-nullable int), so you probably want to check errors first! +var (result, errors) = Result.Fail("fail"); -var (isSuccess, isFailed, errors) = Result.Fail("Failure 1"); +// For Result (without underlying value) you get bool isSuccess and Errors +var (isSuccess, errors) = Result.Fail("Failure 1"); +var (isSuccess, errors) = Result.Ok(); ``` ### Logging diff --git a/src/FluentResults.Test/ResultWithValueTests.cs b/src/FluentResults.Test/ResultWithValueTests.cs index 803f016..a1ea53c 100644 --- a/src/FluentResults.Test/ResultWithValueTests.cs +++ b/src/FluentResults.Test/ResultWithValueTests.cs @@ -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("fail"); + var (isSuccess, errors) = Result.Fail("fail"); isSuccess.Should().Be(false); - isFailed.Should().Be(true); + errors.Should().NotBeNullOrEmpty(); } [Fact] @@ -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_value_with_errors() + { + var error = new Error("fail"); + + var (value, errors) = Result.Fail(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() { diff --git a/src/FluentResults/Results/Result.cs b/src/FluentResults/Results/Result.cs index 0b03a0b..21a6f96 100644 --- a/src/FluentResults/Results/Result.cs +++ b/src/FluentResults/Results/Result.cs @@ -196,7 +196,31 @@ public async ValueTask Bind(Func> action) return result; } - + + /// + /// Deconstruct Result + /// + /// + /// + public void Deconstruct(out bool isSuccess, out bool isFailed) + { + isSuccess = IsSuccess; + isFailed = IsFailed; + } + + /// + /// Deconstruct Result + /// + /// + /// + /// + public void Deconstruct(out bool isSuccess, out bool isFailed, out List errors) + { + isSuccess = IsSuccess; + isFailed = IsFailed; + errors = IsFailed ? Errors : default; + } + /// /// Implict conversion from to a /// @@ -535,6 +559,17 @@ public static implicit operator Result(List errors) return Result.Fail(errors); } + /// + /// Deconstruct Result + /// + /// + /// + public void Deconstruct(out TValue value, out List errors) + { + value = IsSuccess ? Value : default; + errors = IsFailed ? Errors : default; + } + /// /// Deconstruct Result /// diff --git a/src/FluentResults/Results/ResultBase.cs b/src/FluentResults/Results/ResultBase.cs index 00ae293..c2fa773 100644 --- a/src/FluentResults/Results/ResultBase.cs +++ b/src/FluentResults/Results/ResultBase.cs @@ -213,30 +213,6 @@ public bool HasSuccess(Func predicate) { return ResultHelper.HasSuccess(Successes, predicate, out _); } - - /// - /// Deconstruct Result - /// - /// - /// - public void Deconstruct(out bool isSuccess, out bool isFailed) - { - isSuccess = IsSuccess; - isFailed = IsFailed; - } - - /// - /// Deconstruct Result - /// - /// - /// - /// - public void Deconstruct(out bool isSuccess, out bool isFailed, out List errors) - { - isSuccess = IsSuccess; - isFailed = IsFailed; - errors = IsFailed ? Errors : default; - } } ///