Skip to content

Commit

Permalink
- add Result.Try overlaods that return Result
Browse files Browse the repository at this point in the history
  • Loading branch information
ax0l0tl committed Feb 9, 2024
1 parent 66b665f commit 12bc5df
Show file tree
Hide file tree
Showing 16 changed files with 301 additions and 42 deletions.
24 changes: 24 additions & 0 deletions Source/FunicularSwitch.Generators.Templates/ResultType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ public static async Task<MyResult<T>> Try<T>(Func<Task<T>> action, Func<Exceptio
return Error<T>(formatError(e));
}
}

public static MyResult<T> Try<T>(Func<MyResult<T>> action, Func<Exception, MyError> formatError)
{
try
{
return action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}

public static async Task<MyResult<T>> Try<T>(Func<Task<MyResult<T>>> action, Func<Exception, MyError> formatError)
{
try
{
return await action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}
}

public abstract partial class MyResult<T> : MyResult, IEnumerable<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<!--#region adapt versions here-->
<MajorVersion>3</MajorVersion>
<MinorAndPatchVersion>2.0</MinorAndPatchVersion>
<MinorAndPatchVersion>3.0</MinorAndPatchVersion>
<!--#endregion-->

<AssemblyVersion>$(MajorVersion).0.0</AssemblyVersion>
Expand Down
2 changes: 1 addition & 1 deletion Source/FunicularSwitch/FunicularSwitch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<!--#region adapt versions here-->
<MajorVersion>5</MajorVersion>
<MinorAndPatchVersion>0.1</MinorAndPatchVersion>
<MinorAndPatchVersion>1.1</MinorAndPatchVersion>
<!--#endregion-->

<AssemblyVersion>$(MajorVersion).0.0</AssemblyVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ public static async Task<Result<T>> Try<T>(Func<Task<T>> action, Func<Exception,
return Error<T>(formatError(e));
}
}

public static Result<T> Try<T>(Func<Result<T>> action, Func<Exception, String> formatError)
{
try
{
return action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}

public static async Task<Result<T>> Try<T>(Func<Task<Result<T>>> action, Func<Exception, String> formatError)
{
try
{
return await action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}
}

public abstract partial class Result<T> : Result, IEnumerable<T>
Expand Down
13 changes: 13 additions & 0 deletions Source/FunicularSwitch/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ namespace FunicularSwitch
{
public abstract partial class Result
{
public static Result<Unit> Try(Action action, Func<Exception, string> formatError)
{
try
{
action();
return No.Thing;
}
catch (Exception e)
{
return Error<Unit>(formatError(e));
}
}

public static async Task<Result<Unit>> Try(Func<Task> action, Func<Exception, string> formatError)
{
try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
Expand All @@ -10,11 +10,11 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="FunicularSwitch" Version="5.0.1" />
<PackageReference Include="FunicularSwitch.Generators" Version="3.1.2">
<PackageReference Include="FunicularSwitch.Generators" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FunicularSwitch.Generators.FluentAssertions" Version="1.1.0">
<PackageReference Include="FunicularSwitch.Generators.FluentAssertions" Version="1.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ public static async Task<OperationResult<T>> Try<T>(Func<Task<T>> action, Func<E
return Error<T>(formatError(e));
}
}

public static OperationResult<T> Try<T>(Func<OperationResult<T>> action, Func<Exception, Error> formatError)
{
try
{
return action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}

public static async Task<OperationResult<T>> Try<T>(Func<Task<OperationResult<T>>> action, Func<Exception, Error> formatError)
{
try
{
return await action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}
}

abstract partial class OperationResult<T> : OperationResult, IEnumerable<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ public static async Task<Result<T>> Try<T>(Func<Task<T>> action, Func<Exception,
return Error<T>(formatError(e));
}
}

public static Result<T> Try<T>(Func<Result<T>> action, Func<Exception, String> formatError)
{
try
{
return action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}

public static async Task<Result<T>> Try<T>(Func<Task<Result<T>>> action, Func<Exception, String> formatError)
{
try
{
return await action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}
}

public abstract partial class Result<T> : Result, IEnumerable<T>
Expand Down
66 changes: 30 additions & 36 deletions Source/Tests/FunicularSwitch.Generators.Consumer/ResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,6 @@ public void BindMany()
.BeEquivalentTo(Result.Error<List<int>>(string.Join(Environment.NewLine, errorMessage)));
}

//[TestMethod]
//public void OptionResultConversion()
//{
// const string notThere = "it's not there";
// Option.Some(42).ToResult(() => notThere).Should().Equal(Result.Ok(42));
// Option.None<int>().ToResult(() => notThere).Should().Equal(Result.Error<int>(notThere));

// var something = new Something();
// something.ToOption().Should().Equal(Option.Some(something));
// ((Something?)null).ToOption().Should().Equal(Option.None<Something>());

// var option = Result.Ok(something).ToOption();
// option.Should().Equal(Option.Some(something));
// Result.Error<Something>(notThere).ToOption().Should().Equal(Option.None<Something>());

// var errorLogged = false;
// void LogError(string error) => errorLogged = true;
// Result.Error<Something>(notThere).ToOption(LogError).Should().Equal(Option.None<Something>());
// errorLogged.Should().BeTrue();
//}

class Something
{
}

[TestMethod]
public void AsTest()
{
Expand All @@ -134,17 +109,12 @@ public void AsTest()
stringResult.IsError.Should().BeTrue();
}

//[TestMethod]
//public void ImplicitCastTest()
//{
// Result<int> result = 42;
// result.Equals(Result.Ok(42)).Should().BeTrue();
// Option<int> option = 42;
// option.Equals(Option.Some(42)).Should().BeTrue();

// var odds = Enumerable.Range(0, 10).Choose(i => i % 2 != 0 ? i * 10 : Option<int>.None).ToList();
// odds.Should().BeEquivalentTo(Enumerable.Range(0, 10).Where(i => i % 2 != 0).Select(i => i * 10));
//}
[TestMethod]
public void ImplicitCastTest()
{
Result<int> result = 42;
result.Equals(Result.Ok(42)).Should().BeTrue();
}

[TestMethod]
public void BoolConversionTest()
Expand Down Expand Up @@ -177,5 +147,29 @@ public async Task AsyncAggregateTest()
result.Should().BeOfType<Result<IReadOnlyCollection<int>>.Ok_>();
result.GetValueOrThrow().Should().BeEquivalentTo(new[] { 0, 2, 4 });
}

[TestMethod]
public async Task TryTest()
{
Result.Try(() => 42, _ => "error").Should().BeOfType<Result<int>.Ok_>();
Result.Try(() => Result.Ok(42), _ => "error").Should().BeOfType<Result<int>.Ok_>();
(await Result.Try(() => Task.FromResult(Result.Ok(42)), _ => "error")).Should().BeOfType<Result<int>.Ok_>();
(await Result.Try(() => Task.FromResult(42), _ => "error")).Should().BeOfType<Result<int>.Ok_>();

var zero = 0;
Result.Try(() => 42 / zero, e => e.Message).Should().BeOfType<Result<int>.Error_>();
(await Result.Try(async () =>
{
await Task.Delay(1);
return 42 / zero;
}, e => e.Message)).Should().BeOfType<Result<int>.Error_>();

Result.Try(() => Result.Ok(42 / zero), e => e.Message).Should().BeOfType<Result<int>.Error_>();
(await Result.Try(async () =>
{
await Task.Delay(10);
return Result.Ok(42 / zero);
}, e => e.Message)).Should().BeOfType<Result<int>.Error_>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public static async Task<OperationResult<T>> Try<T>(Func<Task<T>> action, Func<E
return Error<T>(formatError(e));
}
}

public static OperationResult<T> Try<T>(Func<OperationResult<T>> action, Func<Exception, MyError> formatError)
{
try
{
return action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}

public static async Task<OperationResult<T>> Try<T>(Func<Task<OperationResult<T>>> action, Func<Exception, MyError> formatError)
{
try
{
return await action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}
}

public abstract partial class OperationResult<T> : OperationResult, IEnumerable<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public static async Task<Result<T>> Try<T>(Func<Task<T>> action, Func<Exception,
return Error<T>(formatError(e));
}
}

public static Result<T> Try<T>(Func<Result<T>> action, Func<Exception, String> formatError)
{
try
{
return action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}

public static async Task<Result<T>> Try<T>(Func<Task<Result<T>>> action, Func<Exception, String> formatError)
{
try
{
return await action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}
}

public abstract partial class Result<T> : Result, IEnumerable<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ public static async Task<OperationResult<T>> Try<T>(Func<Task<T>> action, Func<E
return Error<T>(formatError(e));
}
}

public static OperationResult<T> Try<T>(Func<OperationResult<T>> action, Func<Exception, MyError> formatError)
{
try
{
return action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}

public static async Task<OperationResult<T>> Try<T>(Func<Task<OperationResult<T>>> action, Func<Exception, MyError> formatError)
{
try
{
return await action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}
}

public abstract partial class OperationResult<T> : OperationResult, IEnumerable<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public static async Task<OperationResult<T>> Try<T>(Func<Task<T>> action, Func<E
return Error<T>(formatError(e));
}
}

public static OperationResult<T> Try<T>(Func<OperationResult<T>> action, Func<Exception, MyError> formatError)
{
try
{
return action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}

public static async Task<OperationResult<T>> Try<T>(Func<Task<OperationResult<T>>> action, Func<Exception, MyError> formatError)
{
try
{
return await action();
}
catch (Exception e)
{
return Error<T>(formatError(e));
}
}
}

public abstract partial class OperationResult<T> : OperationResult, IEnumerable<T>
Expand Down
Loading

0 comments on commit 12bc5df

Please sign in to comment.