Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

182 Add enumerable support for Merge #215

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,17 @@ var results = new List<Result> { result1, result2, result3 };
var mergedResult = results.Merge();
```

You can also merge results containing a collection of elements into a flattened collection with `MergeFlat()`. The value type and Enumerable type must be specified as generic parameters

```csharp
var result1 = Result.Ok(new string[] { "A", "B" });
var result2 = Result.Ok(new string[] { "C", "D" });
var result3 = Result.Ok(new string[] { "E", "F" });

// Will contain ["A", "B", "C", "D", "E", "F"]
var mergedResult = Result.MergeFlat<string, string[]>(result1, result2, result3);
```

### Converting and Transformation

A result object can be converted to another result object with methods `ToResult()` and `ToResult<TValue>()`.
Expand Down
56 changes: 54 additions & 2 deletions src/FluentResults.Test/MergeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using FluentAssertions;
using FluentAssertions;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace FluentResults.Test
Expand Down Expand Up @@ -54,5 +55,56 @@ public void Merge_WithSuccessResultWithValue_ShouldMergeResults()
1, 2
});
}

[Fact]
public void MergeFlat_WithSuccessResultWithListValue_ShouldMergeResults()
{
var result1 = Result.Ok(new List<string> { "A", "B" });
var result2 = Result.Ok(new List<string> { "C", "D" });
var result3 = Result.Ok(new List<string> { "E", "F" });

var mergedResult = Result.MergeFlat<string, List<string>>(result1, result2, result3);

mergedResult.IsSuccess.Should().BeTrue();
mergedResult.Value.Should().HaveCount(6);
mergedResult.Value.Should().BeEquivalentTo(new[]
{
"A", "B", "C", "D", "E", "F"
});
}

[Fact]
public void MergeFlat_WithSuccessResultWithArrayValue_ShouldMergeResults()
{
var result1 = Result.Ok(new string[] { "A", "B" });
var result2 = Result.Ok(new string[] { "C", "D" });
var result3 = Result.Ok(new string[] { "E", "F" });

var mergedResult = Result.MergeFlat<string, string[]>(result1, result2, result3);

mergedResult.IsSuccess.Should().BeTrue();
mergedResult.Value.Should().HaveCount(6);
mergedResult.Value.Should().BeEquivalentTo(new[]
{
"A", "B", "C", "D", "E", "F"
});
}

[Fact]
public void MergeFlat_WithSuccessResultWithEnumerableValue_ShouldMergeResults()
{
Result<IEnumerable<string>> result1 = Result.Ok(new string[] { "A", "B" }.Select(a => a));
Result<IEnumerable<string>> result2 = Result.Ok(new string[] { "C", "D" }.Select(a => a));
Result<IEnumerable<string>> result3 = Result.Ok(new string[] { "E", "F" }.Select(a => a));

var mergedResult = Result.MergeFlat<string, IEnumerable<string>>(result1, result2, result3);

mergedResult.IsSuccess.Should().BeTrue();
mergedResult.Value.Should().HaveCount(6);
mergedResult.Value.Should().BeEquivalentTo(new[]
{
"A", "B", "C", "D", "E", "F"
});
}
}
}
23 changes: 20 additions & 3 deletions src/FluentResults/Factories/ResultHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,37 @@ public static Result Merge(IEnumerable<ResultBase> results)
return Result.Ok().WithReasons(results.SelectMany(result => result.Reasons));
}

public static Result<IEnumerable<TValue>> MergeWithValue<TValue>(
IEnumerable<Result<TValue>> results)
private static Result<IEnumerable<TValue>> MergeWithValue<TValue, TInValue>(
IEnumerable<Result<TInValue>> results,
Func<List<Result<TInValue>>, IEnumerable<TValue>> createValue)
{
var resultList = results.ToList();

var finalResult = Result.Ok<IEnumerable<TValue>>(new List<TValue>())
.WithReasons(resultList.SelectMany(result => result.Reasons));

if (finalResult.IsSuccess)
finalResult.WithValue(resultList.Select(r => r.Value).ToList());
finalResult.WithValue(createValue(resultList));

return finalResult;
}

public static Result<IEnumerable<TValue>> MergeWithValue<TValue>(
IEnumerable<Result<TValue>> results)
{
return MergeWithValue(
results,
resultList => resultList.Select(r => r.Value).ToList());
}

public static Result<IEnumerable<TValue>> MergeWithValue<TValue, TArray>(
IEnumerable<Result<TArray>> results) where TArray : IEnumerable<TValue>
{
return MergeWithValue(
results,
resultList => resultList.SelectMany(r => r.Value).ToList());
}

public static bool HasError<TError>(
List<IError> errors,
Func<TError, bool> predicate,
Expand Down
8 changes: 8 additions & 0 deletions src/FluentResults/Factories/Results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ public static Result<IEnumerable<TValue>> Merge<TValue>(params Result<TValue>[]
return ResultHelper.MergeWithValue(results);
}

/// <summary>
/// Merge multiple result objects to one result object together. Return one result with a flattened list of merged values.
/// </summary>
public static Result<IEnumerable<TValue>> MergeFlat<TValue, TArray>(params Result<TArray>[] results) where TArray : IEnumerable<TValue>
{
return ResultHelper.MergeWithValue<TValue, TArray>(results);
}

/// <summary>
/// Create a success/failed result depending on the parameter isSuccess
/// </summary>
Expand Down