From cda0fd65d4a84226fc546795f5c89596a678d31b Mon Sep 17 00:00:00 2001 From: David Ferneding Date: Fri, 15 Nov 2024 15:53:45 +0100 Subject: [PATCH] feat: add methods to append errors from list of ErrorOr to current wip, this is only the basic implementation. ToDo: - AppendErrors with ErrorOrs of same type using generics - AppendErrors to `Task` - Tests :) Refs: #120 --- src/ErrorOr/ErrorOr.AppendErrors.cs | 20 ++++++++++++++++++ src/ErrorOr/ErrorOr.AppendErrorsExtensions.cs | 21 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/ErrorOr/ErrorOr.AppendErrors.cs create mode 100644 src/ErrorOr/ErrorOr.AppendErrorsExtensions.cs diff --git a/src/ErrorOr/ErrorOr.AppendErrors.cs b/src/ErrorOr/ErrorOr.AppendErrors.cs new file mode 100644 index 0000000..136a460 --- /dev/null +++ b/src/ErrorOr/ErrorOr.AppendErrors.cs @@ -0,0 +1,20 @@ +namespace ErrorOr; + +public readonly partial record struct ErrorOr : IErrorOr +{ + /// + /// If the state is error or any of the given has errors, an instance with all errors of the given instances will be returned. + /// + /// The instances which errors should be added to the result. + /// The original if all states are value, otherwise a list of all errors. + public ErrorOr AppendErrors(params IErrorOr[] errorOrs) + { + List combinedErrors = ErrorsOrEmptyList; + foreach (IErrorOr errorOr in errorOrs) + { + combinedErrors.AddRange(errorOr.Errors ?? []); + } + + return combinedErrors.Count == 0 ? this : combinedErrors; + } +} diff --git a/src/ErrorOr/ErrorOr.AppendErrorsExtensions.cs b/src/ErrorOr/ErrorOr.AppendErrorsExtensions.cs new file mode 100644 index 0000000..c61f269 --- /dev/null +++ b/src/ErrorOr/ErrorOr.AppendErrorsExtensions.cs @@ -0,0 +1,21 @@ +namespace ErrorOr; + +public static partial class ErrorOrExtensions +{ + /// + /// If the state of is error or any of the given has errors, an instance with all errors of the given instances will be returned. + /// + /// The instance which value should be returned on success. + /// The instances which errors should be added to the result. + /// The value of if all states are value, otherwise a list of all errors. + public static ErrorOr AppendErrors(this ErrorOr original, params IErrorOr[] errorOrs) + { + List combinedErrors = original.ErrorsOrEmptyList; + foreach (IErrorOr errorOr in errorOrs) + { + combinedErrors.AddRange(errorOr.Errors ?? []); + } + + return combinedErrors.Count == 0 ? original : combinedErrors; + } +}