From 92134b1ed93c29b248b453f75e07191eb7259894 Mon Sep 17 00:00:00 2001 From: Foxtrek_64 Date: Tue, 4 Jun 2024 15:16:22 -0500 Subject: [PATCH 1/3] Initial implementation of AggregateResult and AggregateResultBuilder. --- Remora.Results.sln.DotSettings | 9 ++++ Remora.Results/AggregateResult.cs | 62 ++++++++++++++++++++++++ Remora.Results/AggregateResultBuilder.cs | 34 +++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 Remora.Results/AggregateResult.cs create mode 100644 Remora.Results/AggregateResultBuilder.cs diff --git a/Remora.Results.sln.DotSettings b/Remora.Results.sln.DotSettings index cc1f393..cc4671d 100644 --- a/Remora.Results.sln.DotSettings +++ b/Remora.Results.sln.DotSettings @@ -122,10 +122,19 @@ <Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> + <Policy><Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static readonly fields (private)"><ElementKinds><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /></Policy> + <Policy><Descriptor Staticness="Any" AccessRightKinds="Private" Description="Constant fields (private)"><ElementKinds><Kind Name="CONSTANT_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /></Policy> + <Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /></Policy> + <Policy><Descriptor Staticness="Instance" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Instance fields (not private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></Policy> + <Policy><Descriptor Staticness="Any" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Constant fields (not private)"><ElementKinds><Kind Name="CONSTANT_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></Policy> + <Policy><Descriptor Staticness="Static" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Static fields (not private)"><ElementKinds><Kind Name="FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></Policy> + <Policy><Descriptor Staticness="Static" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Static readonly fields (not private)"><ElementKinds><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></Policy> + <Policy><Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"><ElementKinds><Kind Name="FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /></Policy> True True True True + True True True True diff --git a/Remora.Results/AggregateResult.cs b/Remora.Results/AggregateResult.cs new file mode 100644 index 0000000..7504564 --- /dev/null +++ b/Remora.Results/AggregateResult.cs @@ -0,0 +1,62 @@ +using System.Collections.Generic; +using System.Linq; +using JetBrains.Annotations; + +namespace Remora.Results; + +/// +[PublicAPI] +public readonly struct AggregateResult : IResult +{ + /// + /// Gets a value indicating whether all the contained results were successful. + /// + public bool IsSuccess { get; } + + /// + /// Gets an containing the failed results. + /// + public IResultError? Error => new AggregateError(_lookup[false].ToArray()); + + /// + public IResult? Inner { get; } = null; + + /// + /// Gets a collection of successful results. + /// + public IEnumerable SuccessfulResults => _lookup[true]; + + /// + /// Gets a collection of failed results. + /// + public IEnumerable FailedResults => _lookup[false]; + + /// + /// Gets a readonly collection of the results contained in this collection. + /// + public IReadOnlyCollection Results => _results; + + private readonly IResult[] _results; + private readonly ILookup _lookup; + + /// + /// Initializes a new instance of the struct. + /// + /// The results to use. + public AggregateResult(params IResult[] results) + : this(results.All(it => it.IsSuccess), results) + { + } + + /// + /// Initializes a new instance of the struct. + /// + /// A value indicating whether all the results are successful. + /// The results. + internal AggregateResult(bool isSuccess, IResult[] results) + { + this.IsSuccess = isSuccess; + _results = results; + _lookup = this.Results.ToLookup(it => it.IsSuccess); + } +} diff --git a/Remora.Results/AggregateResultBuilder.cs b/Remora.Results/AggregateResultBuilder.cs new file mode 100644 index 0000000..0430679 --- /dev/null +++ b/Remora.Results/AggregateResultBuilder.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using JetBrains.Annotations; + +namespace Remora.Results; + +/// +/// Allows for the construction of an AggregateResult. +/// +/// +/// 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 of the results available together, use +/// +[PublicAPI] +public class AggregateResultBuilder +{ + private List _results = new(); + + /// + /// Adds the result to the collection. + /// + /// The result to add. + public void Add(IResult result) => _results.Add(result); + + /// + /// Builds the . + /// + /// A new AggregateResult. + /// + public AggregateResult Build() + { + throw new NotImplementedException(); + } +} From 076224da2148977e512bd0f0f14fa2914520b945 Mon Sep 17 00:00:00 2001 From: Foxtrek_64 Date: Tue, 4 Jun 2024 15:17:50 -0500 Subject: [PATCH 2/3] Fix errors --- Remora.Results/AggregateResult.cs | 24 ++++++++++++++++++++- Remora.Results/AggregateResultBuilder.cs | 27 +++++++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Remora.Results/AggregateResult.cs b/Remora.Results/AggregateResult.cs index 7504564..da9830f 100644 --- a/Remora.Results/AggregateResult.cs +++ b/Remora.Results/AggregateResult.cs @@ -1,4 +1,26 @@ -using System.Collections.Generic; +// +// AggregateResult.cs +// +// Author: +// Jarl Gullberg +// +// 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 . +// + +using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; diff --git a/Remora.Results/AggregateResultBuilder.cs b/Remora.Results/AggregateResultBuilder.cs index 0430679..b222939 100644 --- a/Remora.Results/AggregateResultBuilder.cs +++ b/Remora.Results/AggregateResultBuilder.cs @@ -1,4 +1,26 @@ -using System; +// +// AggregateResultBuilder.cs +// +// Author: +// Jarl Gullberg +// +// 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 . +// + +using System; using System.Collections.Generic; using JetBrains.Annotations; @@ -9,7 +31,7 @@ namespace Remora.Results; /// /// /// 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 of the results available together, use +/// rather than as a group. If you have all the results available together, use . /// [PublicAPI] public class AggregateResultBuilder @@ -26,7 +48,6 @@ public class AggregateResultBuilder /// Builds the . /// /// A new AggregateResult. - /// public AggregateResult Build() { throw new NotImplementedException(); From fbb47857867050ed1b78babe29923a42a63b34bd Mon Sep 17 00:00:00 2001 From: Foxtrek_64 Date: Tue, 4 Jun 2024 15:36:20 -0500 Subject: [PATCH 3/3] Finish builder Build(), accept suggestion from github --- Remora.Results/AggregateResult.cs | 5 ++++- Remora.Results/AggregateResultBuilder.cs | 14 +++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Remora.Results/AggregateResult.cs b/Remora.Results/AggregateResult.cs index da9830f..e0d3450 100644 --- a/Remora.Results/AggregateResult.cs +++ b/Remora.Results/AggregateResult.cs @@ -38,9 +38,12 @@ namespace Remora.Results; /// /// Gets an containing the failed results. /// - public IResultError? Error => new AggregateError(_lookup[false].ToArray()); + public IResultError Error => new AggregateError(_lookup[false].ToArray()); /// + /// + /// Always returns null. + /// public IResult? Inner { get; } = null; /// diff --git a/Remora.Results/AggregateResultBuilder.cs b/Remora.Results/AggregateResultBuilder.cs index b222939..2f70be9 100644 --- a/Remora.Results/AggregateResultBuilder.cs +++ b/Remora.Results/AggregateResultBuilder.cs @@ -20,7 +20,6 @@ // along with this program. If not, see . // -using System; using System.Collections.Generic; using JetBrains.Annotations; @@ -37,12 +36,21 @@ namespace Remora.Results; public class AggregateResultBuilder { private List _results = new(); + private bool _allSuccessful = true; /// /// Adds the result to the collection. /// /// The result to add. - public void Add(IResult result) => _results.Add(result); + public void Add(IResult result) + { + if (!result.IsSuccess) + { + _allSuccessful = false; + } + + _results.Add(result); + } /// /// Builds the . @@ -50,6 +58,6 @@ public class AggregateResultBuilder /// A new AggregateResult. public AggregateResult Build() { - throw new NotImplementedException(); + return new AggregateResult(_allSuccessful, _results.ToArray()); } }