Skip to content

Commit

Permalink
- async map error
Browse files Browse the repository at this point in the history
  • Loading branch information
ax0l0tl committed Feb 9, 2024
1 parent 15bf5b4 commit 3d2915a
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 22 deletions.
10 changes: 8 additions & 2 deletions Source/FunicularSwitch.Generators.Templates/ResultType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,14 @@ public static Task<MyResult<T1>> Map<T, T1>(
Func<T, Task<T1>> bind)
=> Bind(result, async v => MyResult.Ok(await bind(v).ConfigureAwait(false)));

public static MyResult<T> MapError<T>(this MyResult<T> result, Func<MyError, MyError> mapError) =>
result.Match(ok => ok, error => MyResult.Error<T>(mapError(error)));
public static MyResult<T> MapError<T>(this MyResult<T> result, Func<MyError, MyError> mapError)
{
if (result is MyResult<T>.Error_ e)
return MyResult.Error<T>(mapError(e.Details));
return result;
}

public static async Task<MyResult<T>> MapError<T>(this Task<MyResult<T>> result, Func<MyError, MyError> mapError) => (await result.ConfigureAwait(false)).MapError(mapError);

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,14 @@ public static Task<Result<T1>> Map<T, T1>(
Func<T, Task<T1>> bind)
=> Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false)));

public static Result<T> MapError<T>(this Result<T> result, Func<String, String> mapError) =>
result.Match(ok => ok, error => Result.Error<T>(mapError(error)));
public static Result<T> MapError<T>(this Result<T> result, Func<String, String> mapError)
{
if (result is Result<T>.Error_ e)
return Result.Error<T>(mapError(e.Details));
return result;
}

public static async Task<Result<T>> MapError<T>(this Task<Result<T>> result, Func<String, String> mapError) => (await result.ConfigureAwait(false)).MapError(mapError);

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,14 @@ public static Task<OperationResult<T1>> Map<T, T1>(
Func<T, Task<T1>> bind)
=> Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false)));

public static OperationResult<T> MapError<T>(this OperationResult<T> result, Func<Error, Error> mapError) =>
result.Match(ok => ok, error => OperationResult.Error<T>(mapError(error)));
public static OperationResult<T> MapError<T>(this OperationResult<T> result, Func<Error, Error> mapError)
{
if (result is OperationResult<T>.Error_ e)
return OperationResult.Error<T>(mapError(e.Details));
return result;
}

public static async Task<OperationResult<T>> MapError<T>(this Task<OperationResult<T>> result, Func<Error, Error> mapError) => (await result.ConfigureAwait(false)).MapError(mapError);

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,14 @@ public static Task<Result<T1>> Map<T, T1>(
Func<T, Task<T1>> bind)
=> Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false)));

public static Result<T> MapError<T>(this Result<T> result, Func<String, String> mapError) =>
result.Match(ok => ok, error => Result.Error<T>(mapError(error)));
public static Result<T> MapError<T>(this Result<T> result, Func<String, String> mapError)
{
if (result is Result<T>.Error_ e)
return Result.Error<T>(mapError(e.Details));
return result;
}

public static async Task<Result<T>> MapError<T>(this Task<Result<T>> result, Func<String, String> mapError) => (await result.ConfigureAwait(false)).MapError(mapError);

#endregion

Expand Down
20 changes: 19 additions & 1 deletion Source/Tests/FunicularSwitch.Generators.Consumer/ResultSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
Expand Down Expand Up @@ -60,11 +61,28 @@ public void EqualityOperators()
[TestMethod]
public void Map()
{
Result.Ok(42).Map(r => r * 2).Should().Equal(Result.Ok(84));
Blubs(() => Console.Write("Hallo"));

Result.Ok(42).Map(r =>
{
return r * 2;
}).Should().Equal(Result.Ok(84));
var error = Result.Error<int>("oh no");
error.Map(r => r * 2).Should().Equal(error);
}

[DebuggerNonUserCode]
void Blubs(Action bla)
{
Blubs2([DebuggerNonUserCode]() => bla());
}

[DebuggerNonUserCode]
void Blubs2(Action bla)
{
bla();
}

[TestMethod]
public void Bind()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,14 @@ public static Task<OperationResult<T1>> Map<T, T1>(
Func<T, Task<T1>> bind)
=> Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false)));

public static OperationResult<T> MapError<T>(this OperationResult<T> result, Func<MyError, MyError> mapError) =>
result.Match(ok => ok, error => OperationResult.Error<T>(mapError(error)));
public static OperationResult<T> MapError<T>(this OperationResult<T> result, Func<MyError, MyError> mapError)
{
if (result is OperationResult<T>.Error_ e)
return OperationResult.Error<T>(mapError(e.Details));
return result;
}

public static async Task<OperationResult<T>> MapError<T>(this Task<OperationResult<T>> result, Func<MyError, MyError> mapError) => (await result.ConfigureAwait(false)).MapError(mapError);

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,14 @@ public static Task<Result<T1>> Map<T, T1>(
Func<T, Task<T1>> bind)
=> Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false)));

public static Result<T> MapError<T>(this Result<T> result, Func<String, String> mapError) =>
result.Match(ok => ok, error => Result.Error<T>(mapError(error)));
public static Result<T> MapError<T>(this Result<T> result, Func<String, String> mapError)
{
if (result is Result<T>.Error_ e)
return Result.Error<T>(mapError(e.Details));
return result;
}

public static async Task<Result<T>> MapError<T>(this Task<Result<T>> result, Func<String, String> mapError) => (await result.ConfigureAwait(false)).MapError(mapError);

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,14 @@ public static Task<OperationResult<T1>> Map<T, T1>(
Func<T, Task<T1>> bind)
=> Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false)));

public static OperationResult<T> MapError<T>(this OperationResult<T> result, Func<MyError, MyError> mapError) =>
result.Match(ok => ok, error => OperationResult.Error<T>(mapError(error)));
public static OperationResult<T> MapError<T>(this OperationResult<T> result, Func<MyError, MyError> mapError)
{
if (result is OperationResult<T>.Error_ e)
return OperationResult.Error<T>(mapError(e.Details));
return result;
}

public static async Task<OperationResult<T>> MapError<T>(this Task<OperationResult<T>> result, Func<MyError, MyError> mapError) => (await result.ConfigureAwait(false)).MapError(mapError);

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,14 @@ public static Task<OperationResult<T1>> Map<T, T1>(
Func<T, Task<T1>> bind)
=> Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false)));

public static OperationResult<T> MapError<T>(this OperationResult<T> result, Func<MyError, MyError> mapError) =>
result.Match(ok => ok, error => OperationResult.Error<T>(mapError(error)));
public static OperationResult<T> MapError<T>(this OperationResult<T> result, Func<MyError, MyError> mapError)
{
if (result is OperationResult<T>.Error_ e)
return OperationResult.Error<T>(mapError(e.Details));
return result;
}

public static async Task<OperationResult<T>> MapError<T>(this Task<OperationResult<T>> result, Func<MyError, MyError> mapError) => (await result.ConfigureAwait(false)).MapError(mapError);

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,14 @@ public static Task<OperationResult<T1>> Map<T, T1>(
Func<T, Task<T1>> bind)
=> Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false)));

public static OperationResult<T> MapError<T>(this OperationResult<T> result, Func<MyError, MyError> mapError) =>
result.Match(ok => ok, error => OperationResult.Error<T>(mapError(error)));
public static OperationResult<T> MapError<T>(this OperationResult<T> result, Func<MyError, MyError> mapError)
{
if (result is OperationResult<T>.Error_ e)
return OperationResult.Error<T>(mapError(e.Details));
return result;
}

public static async Task<OperationResult<T>> MapError<T>(this Task<OperationResult<T>> result, Func<MyError, MyError> mapError) => (await result.ConfigureAwait(false)).MapError(mapError);

#endregion

Expand Down
18 changes: 15 additions & 3 deletions Source/Tests/FunicularSwitch.Test/ResultSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ public void OptionResultConversion()
errorLogged.Should().BeTrue();
}

class Something
{
}
class Something;

[TestMethod]
public void AsTest()
Expand Down Expand Up @@ -302,4 +300,18 @@ public async Task TryTest()
return Result.Ok(42 / zero);
}, e => e.Message)).Should().BeOfType<Result<int>.Error_>();
}

[TestMethod]
public async Task MapErrorTest()
{
Result.Error<int>("error").MapError(e => e + "2").Should().BeError().Subject.Should().Be("error2");

async Task<Result<int>> ProduceError()
{
await Task.Delay(1);
return Result.Error<int>("error");
}

(await ProduceError().MapError(e => e + "2")).Should().BeError().Subject.Should().Be("error2");
}
}

0 comments on commit 3d2915a

Please sign in to comment.