From 3c0ad8d13f9797699ee899fa073f256b19d1e999 Mon Sep 17 00:00:00 2001 From: Tim Schneider <43130816+DerStimmler@users.noreply.github.com> Date: Thu, 22 Aug 2024 12:16:13 +0200 Subject: [PATCH 1/3] feat: add Of methods --- .../Result/Methods/Of.cs | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 CSharpFunctionalExtensions/Result/Methods/Of.cs diff --git a/CSharpFunctionalExtensions/Result/Methods/Of.cs b/CSharpFunctionalExtensions/Result/Methods/Of.cs new file mode 100644 index 00000000..19b22273 --- /dev/null +++ b/CSharpFunctionalExtensions/Result/Methods/Of.cs @@ -0,0 +1,84 @@ +using System; +using System.Threading.Tasks; + +namespace CSharpFunctionalExtensions +{ + public partial struct Result + { + /// + /// Creates a successful containing the given value. + /// + public static Result Of(T value) where T : notnull + { + return new Result(false, default, value); + } + + /// + /// Creates a successful containing the given value from a . + /// + public static Result Of(Func func) where T : notnull + { + T value = func(); + + return new Result(false, default, value); + } + + /// + /// Creates a successful containing the given async value. + /// + public static async Task> Of(Task valueTask) where T : notnull + { + T value = await valueTask; + + return new Result(false, default, value); + } + + /// + /// Creates a successful containing the given value from an async . + /// + public static async Task> Of(Func> valueTaskFunc) where T : notnull + { + T value = await valueTaskFunc(); + + return new Result(false, default, value); + } + + /// + /// Creates a successful containing the given value. + /// + public static Result Of(T value) where T : notnull + { + return new Result(false, default, value); + } + + /// + /// Creates a successful containing the given value from a . + /// + public static Result Of(Func func) where T : notnull + { + T value = func(); + + return new Result(false, default, value); + } + + /// + /// Creates a successful containing the given async value. + /// + public static async Task> Of(Task valueTask) where T : notnull + { + T value = await valueTask; + + return new Result(false, default, value); + } + + /// + /// Creates a successful containing the given value from an async . + /// + public static async Task> Of(Func> valueTaskFunc) where T : notnull + { + T value = await valueTaskFunc(); + + return new Result(false, default, value); + } + } +} From a03ae86c4f5fd0e17410956603688db7501cd226 Mon Sep 17 00:00:00 2001 From: Tim Schneider <43130816+DerStimmler@users.noreply.github.com> Date: Thu, 22 Aug 2024 12:16:40 +0200 Subject: [PATCH 2/3] test: add tests for Of methods --- .../ResultTests/Methods/OfTests.cs | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 CSharpFunctionalExtensions.Tests/ResultTests/Methods/OfTests.cs diff --git a/CSharpFunctionalExtensions.Tests/ResultTests/Methods/OfTests.cs b/CSharpFunctionalExtensions.Tests/ResultTests/Methods/OfTests.cs new file mode 100644 index 00000000..bd0eaf67 --- /dev/null +++ b/CSharpFunctionalExtensions.Tests/ResultTests/Methods/OfTests.cs @@ -0,0 +1,105 @@ +using System; +using System.Threading.Tasks; +using FluentAssertions; +using Xunit; + +namespace CSharpFunctionalExtensions.Tests.ResultTests.Methods; + +public class OfTests +{ + [Fact] + public void Of_can_create_ResultT_from_value() + { + string value = "value"; + + Result result = Result.Of(value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(value); + } + + [Fact] + public void Of_can_create_ResultT_from_func() + { + string value = "value"; + Func func = () => value; + + Result result = Result.Of(func); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(value); + } + + [Fact] + public async Task Of_can_create_ResultT_from_valueTask() + { + string value = "value"; + Task valueTask = Task.FromResult(value); + + Result result = await Result.Of(valueTask); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(value); + } + + [Fact] + public async Task Of_can_create_ResultT_from_valueTaskFunc() + { + string value = "value"; + Task valueTask = Task.FromResult(value); + Func> valueTaskFunc = () => valueTask; + + Result result = await Result.Of(valueTaskFunc); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(value); + } + + [Fact] + public void Of_can_create_ResultTE_from_value() + { + string value = "value"; + + Result result = Result.Of(value); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(value); + } + + [Fact] + public void Of_can_create_ResultTE_from_func() + { + string value = "value"; + Func func = () => value; + + Result result = Result.Of(func); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(value); + } + + [Fact] + public async Task Of_can_create_ResultTE_from_valueTask() + { + string value = "value"; + Task valueTask = Task.FromResult(value); + + Result result = await Result.Of(valueTask); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(value); + } + + [Fact] + public async Task Of_can_create_ResultTE_from_valueTaskFunc() + { + string value = "value"; + Task valueTask = Task.FromResult(value); + Func> valueTaskFunc = () => valueTask; + + Result result = await Result.Of(valueTaskFunc); + + result.IsSuccess.Should().BeTrue(); + result.Value.Should().Be(value); + } +} From f55c581c7e1e7de72006868317e9b5864e6f7343 Mon Sep 17 00:00:00 2001 From: Tim Schneider <43130816+DerStimmler@users.noreply.github.com> Date: Thu, 22 Aug 2024 12:17:05 +0200 Subject: [PATCH 3/3] docs(readme): add examples for Of method to readme --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 5e908669..0b16ef44 100644 --- a/README.md +++ b/README.md @@ -487,6 +487,15 @@ Result failedOperation = Result.Failure("Could n Result successInventoryUpdate = Result.Success(); ``` +To create a success result of a value you can also use the `Of` method which has overloads for `Func` and `Task`. + +```csharp +Result something = Result.Of(_service.CreateSomething()); +Result something = await Result.Of(_service.CreateSomethingAsync()); +Result something = Result.Of(() => _service.CreateSomething()); +Result something = await Result.Of(() => _service.CreateSomethingAsync()); +``` + #### Conditional Construction: SuccessIf and FailureIf Use case: Creating successful or failed Results based on expressions or delegates instead of if/else statements or ternary expressions