Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Apr 28, 2024
2 parents f3032ba + 4babddf commit 3e5673a
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 103 deletions.
8 changes: 4 additions & 4 deletions Results.Tests/DotNetThoughts.Results.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="xunit.core" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit.core" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
283 changes: 269 additions & 14 deletions Results/BindAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ public static Result<Unit> BindAll<T>(this Result<IEnumerable<T>> source, Func<T
}
}

public static Result<Unit> BindAll<T>(this Result<IEnumerable<T>> source, Func<T, int, Result<Unit>> next)
{
if (source.Success)
{
var results = new List<Result<Unit>>();
var index = 0;
foreach (var s in source.Value)
{
results.Add(next(s, index));
index++;
}
return results.Any(x => !x.Success)
? UnitResult.Error(results.SelectMany(x => x.Errors))
: UnitResult.Ok;
}
else
{
return Result<Unit>.Error(source.Errors);
}
}

public static async Task<Result<Unit>> BindAll<T>(this Result<IEnumerable<T>> source, Func<T, Task<Result<Unit>>> next)
{
if (source.Success)
Expand All @@ -42,6 +63,27 @@ public static async Task<Result<Unit>> BindAll<T>(this Result<IEnumerable<T>> so
}
}

public static async Task<Result<Unit>> BindAll<T>(this Result<IEnumerable<T>> source, Func<T, int, Task<Result<Unit>>> next)
{
if (source.Success)
{
var results = new List<Result<Unit>>();
var index = 0;
foreach (var s in source.Value)
{
results.Add(await next(s, index));
index++;
}
return results.Any(x => !x.Success)
? UnitResult.Error(results.SelectMany(x => x.Errors))
: UnitResult.Ok;
}
else
{
return Result<Unit>.Error(source.Errors);
}
}

/// <summary>
/// Does not short circuit. Returns all errors or all results
/// </summary>
Expand All @@ -65,29 +107,242 @@ public static Result<List<TResult>> BindAll<T, TResult>(this Result<IEnumerable<
}
}

public static Result<List<TResult>> BindAll<T, TResult>(this IEnumerable<T> source, Func<T, Result<TResult>> next)
public static Result<List<TResult>> BindAll<T, TResult>(this Result<IEnumerable<T>> source, Func<T, int, Result<TResult>> next)
{
if (source.Success)
{
var results = new List<Result<TResult>>();
var index = 0;
foreach (var s in source.Value)
{
results.Add(next(s, index));
index++;
}
return results.Any(x => !x.Success)
? Result<List<TResult>>.Error(results.SelectMany(x => x.Errors))
: Result<List<TResult>>.Ok(results.Select(x => x.Value).ToList());
}
else
{
return Result<List<TResult>>.Error(source.Errors);
}
}

/// <summary>
/// Does not short circuit. Returns all errors or all results
/// </summary>
public static async Task<Result<List<TResult>>> BindAll<T, TResult>(this Result<IEnumerable<T>> source, Func<T, Task<Result<TResult>>> next)
{
var results = new List<Result<TResult>>();
if (source.Success)
{
var results = new List<Result<TResult>>();

foreach (var s in source.Value)
{
results.Add(await next(s));
}
return results.Any(x => !x.Success)
? Result<List<TResult>>.Error(results.SelectMany(x => x.Errors))
: Result<List<TResult>>.Ok(results.Select(x => x.Value).ToList());
}
else
{
return Result<List<TResult>>.Error(source.Errors);
}
}

foreach (var s in source)
public static async Task<Result<List<TResult>>> BindAll<T, TResult>(this Result<IEnumerable<T>> source, Func<T, int, Task<Result<TResult>>> next)
{
if (source.Success)
{
var results = new List<Result<TResult>>();
var index = 0;
foreach (var s in source.Value)
{
results.Add(await next(s, index));
index++;
}
return results.Any(x => !x.Success)
? Result<List<TResult>>.Error(results.SelectMany(x => x.Errors))
: Result<List<TResult>>.Ok(results.Select(x => x.Value).ToList());
}
else
{
results.Add(next(s));
return Result<List<TResult>>.Error(source.Errors);
}
return results.Any(x => !x.Success)
? Result<List<TResult>>.Error(results.SelectMany(x => x.Errors))
: Result<List<TResult>>.Ok(results.Select(x => x.Value).ToList());
}

public static async Task<Result<List<TResult>>> BindAll<T, TResult>(this IEnumerable<T> source, Func<T, Task<Result<TResult>>> next)
////// Same as above, but from tasks


/// <summary>
/// Does not short circuit. Returns all errors or one unit
/// </summary>
public static async Task<Result<Unit>> BindAll<T>(this Task<Result<IEnumerable<T>>> source, Func<T, Result<Unit>> next)
{
var results = new List<Result<TResult>>();
if ((await source).Success)
{
var results = new List<Result<Unit>>();
foreach (var s in (await source).Value)
{
results.Add(next(s));
}
return results.Any(x => !x.Success)
? UnitResult.Error(results.SelectMany(x => x.Errors))
: UnitResult.Ok;
}
else
{
return Result<Unit>.Error((await source).Errors);
}
}

foreach (var s in source)
public static async Task<Result<Unit>> BindAll<T>(this Task<Result<IEnumerable<T>>> source, Func<T, int, Result<Unit>> next)
{
if ((await source).Success)
{
var results = new List<Result<Unit>>();
var index = 0;
foreach (var s in (await source).Value)
{
results.Add(next(s, index));
index++;
}
return results.Any(x => !x.Success)
? UnitResult.Error(results.SelectMany(x => x.Errors))
: UnitResult.Ok;
}
else
{
return Result<Unit>.Error((await source).Errors);
}
}

public static async Task<Result<Unit>> BindAll<T>(this Task<Result<IEnumerable<T>>> source, Func<T, Task<Result<Unit>>> next)
{
if ((await source).Success)
{
var results = new List<Result<Unit>>();
foreach (var s in (await source).Value)
{
results.Add(await next(s));
}
return results.Any(x => !x.Success)
? UnitResult.Error(results.SelectMany(x => x.Errors))
: UnitResult.Ok;
}
else
{
return Result<Unit>.Error((await source).Errors);
}
}

public static async Task<Result<Unit>> BindAll<T>(this Task<Result<IEnumerable<T>>> source, Func<T, int, Task<Result<Unit>>> next)
{
if ((await source).Success)
{
var results = new List<Result<Unit>>();
var index = 0;
foreach (var s in (await source).Value)
{
results.Add(await next(s, index));
index++;
}
return results.Any(x => !x.Success)
? UnitResult.Error(results.SelectMany(x => x.Errors))
: UnitResult.Ok;
}
else
{
return Result<Unit>.Error((await source).Errors);
}
}

/// <summary>
/// Does not short circuit. Returns all errors or all results
/// </summary>
public static async Task<Result<List<TResult>>> BindAll<T, TResult>(this Task<Result<IEnumerable<T>>> source, Func<T, Result<TResult>> next)
{
if ((await source).Success)
{
var results = new List<Result<TResult>>();

foreach (var s in (await source).Value)
{
results.Add(next(s));
}
return results.Any(x => !x.Success)
? Result<List<TResult>>.Error(results.SelectMany(x => x.Errors))
: Result<List<TResult>>.Ok(results.Select(x => x.Value).ToList());
}
else
{
return Result<List<TResult>>.Error((await source).Errors);
}
}

public static async Task<Result<List<TResult>>> BindAll<T, TResult>(this Task<Result<IEnumerable<T>>> source, Func<T, int, Result<TResult>> next)
{
if ((await source).Success)
{
var results = new List<Result<TResult>>();
var index = 0;
foreach (var s in (await source).Value)
{
results.Add(next(s, index));
index++;
}
return results.Any(x => !x.Success)
? Result<List<TResult>>.Error(results.SelectMany(x => x.Errors))
: Result<List<TResult>>.Ok(results.Select(x => x.Value).ToList());
}
else
{
return Result<List<TResult>>.Error((await source).Errors);
}
}

/// <summary>
/// Does not short circuit. Returns all errors or all results
/// </summary>
public static async Task<Result<List<TResult>>> BindAll<T, TResult>(this Task<Result<IEnumerable<T>>> source, Func<T, Task<Result<TResult>>> next)
{
if ((await source).Success)
{
var results = new List<Result<TResult>>();

foreach (var s in (await source).Value)
{
results.Add(await next(s));
}
return results.Any(x => !x.Success)
? Result<List<TResult>>.Error(results.SelectMany(x => x.Errors))
: Result<List<TResult>>.Ok(results.Select(x => x.Value).ToList());
}
else
{
return Result<List<TResult>>.Error((await source).Errors);
}
}

public static async Task<Result<List<TResult>>> BindAll<T, TResult>(this Task<Result<IEnumerable<T>>> source, Func<T, int, Task<Result<TResult>>> next)
{
if ((await source).Success)
{
var results = new List<Result<TResult>>();
var index = 0;
foreach (var s in (await source).Value)
{
results.Add(await next(s, index));
index++;
}
return results.Any(x => !x.Success)
? Result<List<TResult>>.Error(results.SelectMany(x => x.Errors))
: Result<List<TResult>>.Ok(results.Select(x => x.Value).ToList());
}
else
{
results.Add(await next(s));
return Result<List<TResult>>.Error((await source).Errors);
}
return results.Any(x => !x.Success)
? Result<List<TResult>>.Error(results.SelectMany(x => x.Errors))
: Result<List<TResult>>.Ok(results.Select(x => x.Value).ToList());
}
}
Loading

0 comments on commit 3e5673a

Please sign in to comment.