switched to ImmutableArray as underlying type for ManyErrors
All checks were successful
.NET Test / test (push) Successful in 3m32s
All checks were successful
.NET Test / test (push) Successful in 3m32s
This commit is contained in:
123
Railway/Error.cs
123
Railway/Error.cs
@@ -52,7 +52,7 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
|||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Error Many(Error error1, Error error2) => (error1, error2) switch
|
public static Error Many(Error error1, Error error2) => (error1, error2) switch
|
||||||
{
|
{
|
||||||
(null, null) => new ManyErrors(new List<Error>()),
|
(null, null) => new ManyErrors(ImmutableArray<Error>.Empty),
|
||||||
(Error err, null) => err,
|
(Error err, null) => err,
|
||||||
(Error err, { IsEmpty: true }) => err,
|
(Error err, { IsEmpty: true }) => err,
|
||||||
(null, Error err) => err,
|
(null, Error err) => err,
|
||||||
@@ -137,12 +137,14 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
|||||||
return string.Compare(Message, other.Message);
|
return string.Compare(Message, other.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public override string ToString() => Message;
|
[Pure] public sealed override string ToString() => Message;
|
||||||
[Pure] public void Deconstruct(out string type, out string message)
|
[Pure] public void Deconstruct(out string type, out string message)
|
||||||
{
|
{
|
||||||
type = Type;
|
type = Type;
|
||||||
message = Message;
|
message = Message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Pure] internal virtual Error AccessUnsafe(int position) => this;
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConverter(typeof(ExpectedErrorJsonConverter))]
|
[JsonConverter(typeof(ExpectedErrorJsonConverter))]
|
||||||
@@ -221,7 +223,7 @@ public sealed class ExceptionalError : Error
|
|||||||
if (!(exception.Data?.Count > 0))
|
if (!(exception.Data?.Count > 0))
|
||||||
return ImmutableDictionary<string, string>.Empty;
|
return ImmutableDictionary<string, string>.Empty;
|
||||||
|
|
||||||
List<KeyValuePair<string, string>>? values = null;
|
ImmutableDictionary<string, string>.Builder? values = null;
|
||||||
|
|
||||||
foreach (var key in exception.Data.Keys)
|
foreach (var key in exception.Data.Keys)
|
||||||
{
|
{
|
||||||
@@ -234,63 +236,70 @@ public sealed class ExceptionalError : Error
|
|||||||
var valueString = value.ToString();
|
var valueString = value.ToString();
|
||||||
if (string.IsNullOrEmpty(keyString) || string.IsNullOrEmpty(valueString)) continue;
|
if (string.IsNullOrEmpty(keyString) || string.IsNullOrEmpty(valueString)) continue;
|
||||||
|
|
||||||
values ??= new List<KeyValuePair<string, string>>(4);
|
values ??= ImmutableDictionary.CreateBuilder<string, string>();
|
||||||
values.Add(new(keyString, valueString));
|
values.Add(keyString, valueString);
|
||||||
}
|
}
|
||||||
return values?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty;
|
return values is not null ? values.ToImmutable() : ImmutableDictionary<string, string>.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConverter(typeof(ManyErrorsJsonConverter))]
|
[JsonConverter(typeof(ManyErrorsJsonConverter))]
|
||||||
public sealed class ManyErrors : Error, IEnumerable<Error>, IReadOnlyList<Error>
|
public sealed class ManyErrors : Error, IEnumerable<Error>, IReadOnlyList<Error>
|
||||||
{
|
{
|
||||||
private readonly List<Error> _errors;
|
private readonly ImmutableArray<Error> _errors;
|
||||||
[Pure] public IEnumerable<Error> Errors { get => _errors; }
|
[Pure] public IEnumerable<Error> Errors { get => _errors; }
|
||||||
|
|
||||||
internal ManyErrors(List<Error> errors) => _errors = errors;
|
internal ManyErrors(ImmutableArray<Error> errors) => _errors = errors;
|
||||||
internal ManyErrors(Error head, Error tail)
|
internal ManyErrors(Error head, Error tail)
|
||||||
{
|
{
|
||||||
_errors = new List<Error>(head.Count + tail.Count);
|
var headCount = head.Count;
|
||||||
|
var tailCount = tail.Count;
|
||||||
|
var errors = ImmutableArray.CreateBuilder<Error>(headCount + tailCount);
|
||||||
|
|
||||||
if (head.Count == 1)
|
if (headCount > 0)
|
||||||
_errors.Add(head);
|
AppendSanitized(errors, head);
|
||||||
else if (head.Count > 1)
|
|
||||||
_errors.AddRange(head.ToEnumerable());
|
|
||||||
|
|
||||||
if (tail.Count == 1)
|
if (tailCount > 0)
|
||||||
_errors.Add(tail);
|
AppendSanitized(errors, tail);
|
||||||
else if (tail.Count > 1)
|
|
||||||
_errors.AddRange(tail.ToEnumerable());
|
_errors = errors.MoveToImmutable();
|
||||||
}
|
}
|
||||||
public ManyErrors(IEnumerable<Error> errors)
|
public ManyErrors(IEnumerable<Error> errors)
|
||||||
{
|
{
|
||||||
_errors = errors.SelectMany(x => x.ToEnumerable())
|
var unpackedErrors = ImmutableArray.CreateBuilder<Error>();
|
||||||
.Where(x => !x.IsEmpty)
|
|
||||||
.ToList();
|
foreach (var err in errors)
|
||||||
|
{
|
||||||
|
if (err.IsEmpty) continue;
|
||||||
|
|
||||||
|
AppendSanitized(unpackedErrors, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
_errors = unpackedErrors.ToImmutable();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public override string Type => "many_errors";
|
[Pure] public override string Type => "many_errors";
|
||||||
[Pure] public override string Message => ToFullArrayString();
|
|
||||||
[Pure] public override string ToString() => ToFullArrayString();
|
|
||||||
|
|
||||||
[Pure] private string ToFullArrayString()
|
private string? _lazyMessage = null;
|
||||||
|
[Pure] public override string Message => _lazyMessage ??= ToFullArrayString(_errors);
|
||||||
|
|
||||||
|
[Pure] private static string ToFullArrayString(in ImmutableArray<Error> errors)
|
||||||
{
|
{
|
||||||
var separator = Environment.NewLine;
|
var separator = Environment.NewLine;
|
||||||
var lastIndex = _errors.Count - 1;
|
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
for (int i = 0; i < _errors.Count; i++)
|
for (int i = 0; i < errors.Length; i++)
|
||||||
{
|
{
|
||||||
sb.Append(_errors[i]);
|
sb.Append(errors[i]);
|
||||||
if (i < lastIndex)
|
sb.Append(separator);
|
||||||
sb.Append(separator);
|
|
||||||
}
|
}
|
||||||
|
sb.Remove(sb.Length - separator.Length, separator.Length);
|
||||||
|
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public override int Count => _errors.Count;
|
[Pure] public override int Count => _errors.Length;
|
||||||
[Pure] public override bool IsEmpty => _errors.Count == 0;
|
[Pure] public override bool IsEmpty => _errors.IsEmpty;
|
||||||
[Pure] public override bool IsExpected => _errors.All(static x => x.IsExpected);
|
[Pure] public override bool IsExpected => _errors.All(static x => x.IsExpected);
|
||||||
[Pure] public override bool IsExeptional => _errors.Any(static x => x.IsExeptional);
|
[Pure] public override bool IsExeptional => _errors.Any(static x => x.IsExeptional);
|
||||||
|
|
||||||
@@ -303,22 +312,19 @@ public sealed class ManyErrors : Error, IEnumerable<Error>, IReadOnlyList<Error>
|
|||||||
{
|
{
|
||||||
if (other is null)
|
if (other is null)
|
||||||
return -1;
|
return -1;
|
||||||
if (other.Count != _errors.Count)
|
if (other.Count != _errors.Length)
|
||||||
return _errors.Count.CompareTo(other.Count);
|
return _errors.Length.CompareTo(other.Count);
|
||||||
|
|
||||||
var compareResult = 0;
|
for (int i = 0; i < _errors.Length; i++)
|
||||||
int i = 0;
|
|
||||||
foreach (var otherErr in other.ToEnumerable())
|
|
||||||
{
|
{
|
||||||
var thisErr = _errors[i++];
|
var compareResult = _errors[i].CompareTo(other.AccessUnsafe(i));
|
||||||
compareResult = thisErr.CompareTo(otherErr);
|
|
||||||
if (compareResult != 0)
|
if (compareResult != 0)
|
||||||
{
|
{
|
||||||
return compareResult;
|
return compareResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return compareResult;
|
return 0;
|
||||||
}
|
}
|
||||||
[Pure] public override bool IsSimilarTo([NotNullWhen(true)] Error? other)
|
[Pure] public override bool IsSimilarTo([NotNullWhen(true)] Error? other)
|
||||||
{
|
{
|
||||||
@@ -326,15 +332,13 @@ public sealed class ManyErrors : Error, IEnumerable<Error>, IReadOnlyList<Error>
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (_errors.Count != other.Count)
|
if (_errors.Length != other.Count)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int i = 0;
|
for (int i = 0; i < _errors.Length; i++)
|
||||||
foreach (var otherErr in other.ToEnumerable())
|
|
||||||
{
|
{
|
||||||
var thisErr = _errors[i++];
|
if (!_errors[i].IsSimilarTo(other.AccessUnsafe(i)))
|
||||||
if (!thisErr.IsSimilarTo(otherErr))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -347,36 +351,49 @@ public sealed class ManyErrors : Error, IEnumerable<Error>, IReadOnlyList<Error>
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (_errors.Count != other.Count)
|
if (_errors.Length != other.Count)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int i = 0;
|
for (int i = 0; i < _errors.Length; i++)
|
||||||
foreach (var otherErr in other.ToEnumerable())
|
|
||||||
{
|
{
|
||||||
var thisErr = _errors[i++];
|
if (!_errors[i].Equals(other.AccessUnsafe(i)))
|
||||||
if (!thisErr.Equals(otherErr))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
[Pure] public override int GetHashCode()
|
|
||||||
|
private int? _lazyHashCode = null;
|
||||||
|
[Pure] public override int GetHashCode() => _lazyHashCode ??= CalcHashCode(_errors);
|
||||||
|
private static int CalcHashCode(in ImmutableArray<Error> errors)
|
||||||
{
|
{
|
||||||
if (_errors.Count == 0)
|
if (errors.IsEmpty)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
var hash = new HashCode();
|
var hash = new HashCode();
|
||||||
foreach (var err in _errors)
|
foreach (var err in errors)
|
||||||
{
|
{
|
||||||
hash.Add(err);
|
hash.Add(err);
|
||||||
}
|
}
|
||||||
return hash.ToHashCode();
|
return hash.ToHashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public IEnumerator<Error> GetEnumerator() => _errors.GetEnumerator();
|
|
||||||
[Pure] IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
[Pure] public ImmutableArray<Error>.Enumerator GetEnumerator() => _errors.GetEnumerator();
|
||||||
|
[Pure] IEnumerator<Error> IEnumerable<Error>.GetEnumerator() => Errors.GetEnumerator();
|
||||||
|
[Pure] IEnumerator IEnumerable.GetEnumerator() => Errors.GetEnumerator();
|
||||||
|
|
||||||
|
internal static void AppendSanitized(ImmutableArray<Error>.Builder errors, Error error)
|
||||||
|
{
|
||||||
|
if (error is ManyErrors many)
|
||||||
|
errors.AddRange(many._errors);
|
||||||
|
else
|
||||||
|
errors.Add(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Pure] internal override Error AccessUnsafe(int position) => _errors[position];
|
||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
|
|||||||
|
|
||||||
internal static ManyErrors ReadMany(ref Utf8JsonReader reader)
|
internal static ManyErrors ReadMany(ref Utf8JsonReader reader)
|
||||||
{
|
{
|
||||||
List<Error> errors = new(4);
|
var errors = ImmutableArray.CreateBuilder<Error>();
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
if (reader.TokenType == JsonTokenType.StartObject)
|
if (reader.TokenType == JsonTokenType.StartObject)
|
||||||
@@ -43,7 +43,7 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
|
|||||||
errors.Add(ToExpectedError(ReadOne(ref reader)));
|
errors.Add(ToExpectedError(ReadOne(ref reader)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new ManyErrors(errors);
|
return new ManyErrors(errors.ToImmutable());
|
||||||
}
|
}
|
||||||
internal static ExpectedError ToExpectedError(in (string Type, string Message, ImmutableDictionary<string, string> ExtensionData) errorInfo)
|
internal static ExpectedError ToExpectedError(in (string Type, string Message, ImmutableDictionary<string, string> ExtensionData) errorInfo)
|
||||||
=> new(errorInfo.Type, errorInfo.Message) { ExtensionData = errorInfo.ExtensionData };
|
=> new(errorInfo.Type, errorInfo.Message) { ExtensionData = errorInfo.ExtensionData };
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using System.Collections.Immutable;
|
||||||
|
|
||||||
namespace Just.Railway;
|
namespace Just.Railway;
|
||||||
|
|
||||||
public static partial class ResultExtensions
|
public static partial class ResultExtensions
|
||||||
@@ -80,7 +82,7 @@ public static partial class ResultExtensions
|
|||||||
|
|
||||||
public static Result Merge(this IEnumerable<Result> results)
|
public static Result Merge(this IEnumerable<Result> results)
|
||||||
{
|
{
|
||||||
List<Error>? errors = null;
|
ImmutableArray<Error>.Builder? errors = null;
|
||||||
bool hasErrors = false;
|
bool hasErrors = false;
|
||||||
|
|
||||||
foreach (var result in results.OrderBy(x => x.State))
|
foreach (var result in results.OrderBy(x => x.State))
|
||||||
@@ -89,8 +91,8 @@ public static partial class ResultExtensions
|
|||||||
{
|
{
|
||||||
case ResultState.Error:
|
case ResultState.Error:
|
||||||
hasErrors = true;
|
hasErrors = true;
|
||||||
errors ??= new(4);
|
errors ??= ImmutableArray.CreateBuilder<Error>();
|
||||||
errors.Add(result.Error!);
|
ManyErrors.AppendSanitized(errors, result.Error!);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ResultState.Success:
|
case ResultState.Success:
|
||||||
@@ -102,7 +104,7 @@ public static partial class ResultExtensions
|
|||||||
}
|
}
|
||||||
afterLoop:
|
afterLoop:
|
||||||
return hasErrors
|
return hasErrors
|
||||||
? new(new ManyErrors(errors!))
|
? new(new ManyErrors(errors!.ToImmutable()))
|
||||||
: new(null);
|
: new(null);
|
||||||
}
|
}
|
||||||
public static async Task<Result> Merge(this IEnumerable<Task<Result>> tasks)
|
public static async Task<Result> Merge(this IEnumerable<Task<Result>> tasks)
|
||||||
@@ -113,8 +115,8 @@ public static partial class ResultExtensions
|
|||||||
|
|
||||||
public static Result<IEnumerable<T>> Merge<T>(this IEnumerable<Result<T>> results)
|
public static Result<IEnumerable<T>> Merge<T>(this IEnumerable<Result<T>> results)
|
||||||
{
|
{
|
||||||
List<T>? values = null;
|
ImmutableList<T>.Builder? values = null;
|
||||||
List<Error>? errors = null;
|
ImmutableArray<Error>.Builder? errors = null;
|
||||||
bool hasErrors = false;
|
bool hasErrors = false;
|
||||||
|
|
||||||
foreach (var result in results.OrderBy(x => x.State))
|
foreach (var result in results.OrderBy(x => x.State))
|
||||||
@@ -123,13 +125,13 @@ public static partial class ResultExtensions
|
|||||||
{
|
{
|
||||||
case ResultState.Error:
|
case ResultState.Error:
|
||||||
hasErrors = true;
|
hasErrors = true;
|
||||||
errors ??= new(4);
|
errors ??= ImmutableArray.CreateBuilder<Error>();
|
||||||
errors.Add(result.Error!);
|
ManyErrors.AppendSanitized(errors, result.Error!);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ResultState.Success:
|
case ResultState.Success:
|
||||||
if (hasErrors) goto afterLoop;
|
if (hasErrors) goto afterLoop;
|
||||||
values ??= new(4);
|
values ??= ImmutableList.CreateBuilder<T>();
|
||||||
values.Add(result.Value);
|
values.Add(result.Value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -138,8 +140,8 @@ public static partial class ResultExtensions
|
|||||||
}
|
}
|
||||||
afterLoop:
|
afterLoop:
|
||||||
return hasErrors
|
return hasErrors
|
||||||
? new(new ManyErrors(errors!))
|
? new(new ManyErrors(errors!.ToImmutable()))
|
||||||
: new((IEnumerable<T>?)values ?? Array.Empty<T>());
|
: new(values is not null ? values.ToImmutable() : ImmutableList<T>.Empty);
|
||||||
}
|
}
|
||||||
public static async Task<Result<IEnumerable<T>>> Merge<T>(this IEnumerable<Task<Result<T>>> tasks)
|
public static async Task<Result<IEnumerable<T>>> Merge<T>(this IEnumerable<Task<Result<T>>> tasks)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user