-
Notifications
You must be signed in to change notification settings - Fork 5
[WIP] Implement AggregateResult #17
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
Draft
TheBrambleShark
wants to merge
3
commits into
Remora:main
Choose a base branch
from
TheBrambleShark:feat/AggregateResult
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // | ||
| // AggregateResult.cs | ||
| // | ||
| // Author: | ||
| // Jarl Gullberg <jarl.gullberg@gmail.com> | ||
| // | ||
| // Copyright (c) Jarl Gullberg | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Lesser General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Lesser General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Lesser General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| // | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace Remora.Results; | ||
|
|
||
| /// <inheritdoc /> | ||
| [PublicAPI] | ||
| public readonly struct AggregateResult : IResult | ||
| { | ||
| /// <summary> | ||
| /// Gets a value indicating whether all the contained results were successful. | ||
| /// </summary> | ||
| public bool IsSuccess { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets an <see cref="AggregateError"/> containing the failed results. | ||
| /// </summary> | ||
| public IResultError? Error => new AggregateError(_lookup[false].ToArray()); | ||
|
|
||
| /// <inheritdoc /> | ||
| public IResult? Inner { get; } = null; | ||
|
|
||
| /// <summary> | ||
| /// Gets a collection of successful results. | ||
| /// </summary> | ||
| public IEnumerable<IResult> SuccessfulResults => _lookup[true]; | ||
|
|
||
| /// <summary> | ||
| /// Gets a collection of failed results. | ||
| /// </summary> | ||
| public IEnumerable<IResult> FailedResults => _lookup[false]; | ||
|
|
||
| /// <summary> | ||
| /// Gets a readonly collection of the results contained in this collection. | ||
| /// </summary> | ||
| public IReadOnlyCollection<IResult> Results => _results; | ||
|
|
||
| private readonly IResult[] _results; | ||
| private readonly ILookup<bool, IResult> _lookup; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AggregateResult"/> struct. | ||
| /// </summary> | ||
| /// <param name="results">The results to use.</param> | ||
| public AggregateResult(params IResult[] results) | ||
| : this(results.All(it => it.IsSuccess), results) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AggregateResult"/> struct. | ||
| /// </summary> | ||
| /// <param name="isSuccess">A value indicating whether all the results are successful.</param> | ||
| /// <param name="results">The results.</param> | ||
| internal AggregateResult(bool isSuccess, IResult[] results) | ||
| { | ||
| this.IsSuccess = isSuccess; | ||
| _results = results; | ||
| _lookup = this.Results.ToLookup(it => it.IsSuccess); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // | ||
| // AggregateResultBuilder.cs | ||
| // | ||
| // Author: | ||
| // Jarl Gullberg <jarl.gullberg@gmail.com> | ||
| // | ||
| // Copyright (c) Jarl Gullberg | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Lesser General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Lesser General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Lesser General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| // | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using JetBrains.Annotations; | ||
|
|
||
| namespace Remora.Results; | ||
|
|
||
| /// <summary> | ||
| /// Allows for the construction of an AggregateResult. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This is intended for use within a foreach loop or in situations where results are added on their own | ||
| /// rather than as a group. If you have all the results available together, use <see cref="AggregateResult"/>. | ||
| /// </remarks> | ||
| [PublicAPI] | ||
| public class AggregateResultBuilder | ||
| { | ||
| private List<IResult> _results = new(); | ||
|
|
||
| /// <summary> | ||
| /// Adds the result to the collection. | ||
| /// </summary> | ||
| /// <param name="result">The result to add.</param> | ||
| public void Add(IResult result) => _results.Add(result); | ||
|
|
||
| /// <summary> | ||
| /// Builds the <see cref="AggregateResult"/>. | ||
| /// </summary> | ||
| /// <returns>A new AggregateResult.</returns> | ||
| public AggregateResult Build() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.