removed some new lang features to support net6.0 and net7.0
This commit is contained in:
@@ -60,6 +60,7 @@ public readonly struct Ensure<T>
|
|||||||
Value = value;
|
Value = value;
|
||||||
ValueExpression = valueExpression;
|
ValueExpression = valueExpression;
|
||||||
State = ResultState.Success;
|
State = ResultState.Success;
|
||||||
|
Error = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal Ensure(Error error, string valueExpression)
|
internal Ensure(Error error, string valueExpression)
|
||||||
@@ -72,7 +73,12 @@ public readonly struct Ensure<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class EnsureNotInitializedException(string variableName = "this") : InvalidOperationException("Ensure was not properly initialized.")
|
public class EnsureNotInitializedException : InvalidOperationException
|
||||||
{
|
{
|
||||||
public string VariableName { get; } = variableName;
|
public EnsureNotInitializedException(string variableName = "this")
|
||||||
|
: base("Ensure was not properly initialized.")
|
||||||
|
{
|
||||||
|
VariableName = variableName;
|
||||||
|
}
|
||||||
|
public string VariableName { get; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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([]),
|
(null, null) => new ManyErrors(new List<Error>()),
|
||||||
(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,
|
||||||
@@ -231,7 +231,7 @@ 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 ??= [];
|
values ??= new List<KeyValuePair<string, string>>(4);
|
||||||
values.Add(new(keyString, valueString));
|
values.Add(new(keyString, valueString));
|
||||||
}
|
}
|
||||||
return values?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty;
|
return values?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty;
|
||||||
@@ -377,7 +377,11 @@ public sealed class ManyErrors : Error, IEnumerable<Error>, IReadOnlyList<Error>
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public sealed class ErrorException(string type, string message) : Exception(message)
|
public sealed class ErrorException : Exception
|
||||||
{
|
{
|
||||||
public string Type { get; } = type ?? nameof(ErrorException);
|
public ErrorException(string type, string message) : base(message)
|
||||||
|
{
|
||||||
|
Type = type ?? nameof(ErrorException);
|
||||||
|
}
|
||||||
|
public string Type { get; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 = [];
|
List<Error> errors = new(4);
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
if (reader.TokenType == JsonTokenType.StartObject)
|
if (reader.TokenType == JsonTokenType.StartObject)
|
||||||
@@ -83,7 +83,7 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
|
|||||||
}
|
}
|
||||||
else if (!string.IsNullOrEmpty(propname))
|
else if (!string.IsNullOrEmpty(propname))
|
||||||
{
|
{
|
||||||
extensionData ??= [];
|
extensionData ??= new(4);
|
||||||
extensionData.Add(new(propname, propvalue));
|
extensionData.Add(new(propname, propvalue));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
|
||||||
|
<LangVersion>10.0</LangVersion>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<AssemblyName>Just.Railway</AssemblyName>
|
<AssemblyName>Just.Railway</AssemblyName>
|
||||||
@@ -13,7 +14,6 @@
|
|||||||
<Copyright>Copyright (c) 2023 JustFixMe</Copyright>
|
<Copyright>Copyright (c) 2023 JustFixMe</Copyright>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
|
|
||||||
<RepositoryUrl>https://github.com/JustFixMe/Just.Railway/</RepositoryUrl>
|
<RepositoryUrl>https://github.com/JustFixMe/Just.Railway/</RepositoryUrl>
|
||||||
|
|
||||||
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
|
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
|
||||||
|
|||||||
@@ -10,58 +10,58 @@ internal static class ReflectionHelper
|
|||||||
|
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static int Compare<T>(T? left, T? right) => TypeReflectionCache<T>.CompareFunc(left, right);
|
public static int Compare<T>(T? left, T? right) => TypeReflectionCache<T>.CompareFunc(left, right);
|
||||||
}
|
|
||||||
|
|
||||||
file static class TypeReflectionCache<T>
|
private static class TypeReflectionCache<T>
|
||||||
{
|
|
||||||
public static readonly Func<T?, T?, bool> IsEqualFunc;
|
|
||||||
public static readonly Func<T?, T?, int> CompareFunc;
|
|
||||||
|
|
||||||
static TypeReflectionCache()
|
|
||||||
{
|
{
|
||||||
var type = typeof(T);
|
public static readonly Func<T?, T?, bool> IsEqualFunc;
|
||||||
var isNullableStruct = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
|
public static readonly Func<T?, T?, int> CompareFunc;
|
||||||
var underlyingType = isNullableStruct ? type.GenericTypeArguments.First() : type;
|
|
||||||
var thisType = typeof(TypeReflectionCache<T>);
|
|
||||||
|
|
||||||
var equatableType = typeof(IEquatable<>).MakeGenericType(underlyingType);
|
static TypeReflectionCache()
|
||||||
if (equatableType.IsAssignableFrom(underlyingType))
|
|
||||||
{
|
{
|
||||||
var isEqualFunc = thisType.GetMethod(isNullableStruct ? nameof(IsEqualNullable) : nameof(IsEqual), BindingFlags.Static | BindingFlags.Public)
|
var type = typeof(T);
|
||||||
!.MakeGenericMethod(underlyingType);
|
var isNullableStruct = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
|
||||||
|
var underlyingType = isNullableStruct ? type.GenericTypeArguments.First() : type;
|
||||||
|
var thisType = typeof(TypeReflectionCache<T>);
|
||||||
|
|
||||||
IsEqualFunc = (Func<T?, T?, bool>)Delegate.CreateDelegate(typeof(Func<T?, T?, bool>), isEqualFunc);
|
var equatableType = typeof(IEquatable<>).MakeGenericType(underlyingType);
|
||||||
}
|
if (equatableType.IsAssignableFrom(underlyingType))
|
||||||
else
|
{
|
||||||
{
|
var isEqualFunc = thisType.GetMethod(isNullableStruct ? nameof(IsEqualNullable) : nameof(IsEqual), BindingFlags.Static | BindingFlags.Public)
|
||||||
IsEqualFunc = static (left, right) => left is null ? right is null : left.Equals(right);
|
!.MakeGenericMethod(underlyingType);
|
||||||
|
|
||||||
|
IsEqualFunc = (Func<T?, T?, bool>)Delegate.CreateDelegate(typeof(Func<T?, T?, bool>), isEqualFunc);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
IsEqualFunc = static (left, right) => left is null ? right is null : left.Equals(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
var comparableType = typeof(IComparable<>).MakeGenericType(underlyingType);
|
||||||
|
if (comparableType.IsAssignableFrom(underlyingType))
|
||||||
|
{
|
||||||
|
var compareFunc = thisType.GetMethod(isNullableStruct ? nameof(CompareNullable) : nameof(Compare), BindingFlags.Static | BindingFlags.Public)
|
||||||
|
!.MakeGenericMethod(underlyingType);
|
||||||
|
|
||||||
|
CompareFunc = (Func<T?, T?, int>)Delegate.CreateDelegate(typeof(Func<T?, T?, int>), compareFunc);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CompareFunc = static (left, right) => left is null
|
||||||
|
? right is null ? 0 : -1
|
||||||
|
: right is null ? 1 : left.GetHashCode().CompareTo(right.GetHashCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var comparableType = typeof(IComparable<>).MakeGenericType(underlyingType);
|
#pragma warning disable CS8604 // Possible null reference argument.
|
||||||
if (comparableType.IsAssignableFrom(underlyingType))
|
[Pure] public static bool IsEqual<R>(R? left, R? right) where R : notnull, IEquatable<R>, T => left is null ? right is null : left.Equals(right);
|
||||||
{
|
[Pure] public static bool IsEqualNullable<R>(R? left, R? right) where R : struct, IEquatable<R> => left is null ? right is null : right is not null && left.Value.Equals(right.Value);
|
||||||
var compareFunc = thisType.GetMethod(isNullableStruct ? nameof(CompareNullable) : nameof(Compare), BindingFlags.Static | BindingFlags.Public)
|
|
||||||
!.MakeGenericMethod(underlyingType);
|
|
||||||
|
|
||||||
CompareFunc = (Func<T?, T?, int>)Delegate.CreateDelegate(typeof(Func<T?, T?, int>), compareFunc);
|
[Pure] public static int Compare<R>(R? left, R? right) where R : notnull, IComparable<R>, T => left is null
|
||||||
}
|
? right is null ? 0 : -1
|
||||||
else
|
: right is null ? 1 : left.CompareTo(right);
|
||||||
{
|
[Pure] public static int CompareNullable<R>(R? left, R? right) where R : struct, IComparable<R> => left is null
|
||||||
CompareFunc = static (left, right) => left is null
|
? right is null ? 0 : -1
|
||||||
? right is null ? 0 : -1
|
: right is null ? 1 : left.Value.CompareTo(right.Value);
|
||||||
: right is null ? 1 : left.GetHashCode().CompareTo(right.GetHashCode());
|
#pragma warning restore CS8604 // Possible null reference argument.
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma warning disable CS8604 // Possible null reference argument.
|
|
||||||
[Pure] public static bool IsEqual<R>(R? left, R? right) where R : notnull, IEquatable<R>, T => left is null ? right is null : left.Equals(right);
|
|
||||||
[Pure] public static bool IsEqualNullable<R>(R? left, R? right) where R : struct, IEquatable<R> => left is null ? right is null : right is not null && left.Value.Equals(right.Value);
|
|
||||||
|
|
||||||
[Pure] public static int Compare<R>(R? left, R? right) where R : notnull, IComparable<R>, T => left is null
|
|
||||||
? right is null ? 0 : -1
|
|
||||||
: right is null ? 1 : left.CompareTo(right);
|
|
||||||
[Pure] public static int CompareNullable<R>(R? left, R? right) where R : struct, IComparable<R> => left is null
|
|
||||||
? right is null ? 0 : -1
|
|
||||||
: right is null ? 1 : left.Value.CompareTo(right.Value);
|
|
||||||
#pragma warning restore CS8604 // Possible null reference argument.
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,6 +136,7 @@ public readonly struct Result<T> : IEquatable<Result<T>>
|
|||||||
{
|
{
|
||||||
Value = value;
|
Value = value;
|
||||||
State = ResultState.Success;
|
State = ResultState.Success;
|
||||||
|
Error = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public static explicit operator Result(Result<T> result) => result.State switch
|
[Pure] public static explicit operator Result(Result<T> result) => result.State switch
|
||||||
@@ -263,7 +264,12 @@ public readonly struct SuccessUnit : IEquatable<SuccessUnit>
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class ResultNotInitializedException(string variableName = "this") : InvalidOperationException("Result was not properly initialized.")
|
public class ResultNotInitializedException : InvalidOperationException
|
||||||
{
|
{
|
||||||
public string VariableName { get; } = variableName;
|
public ResultNotInitializedException(string variableName = "this")
|
||||||
|
: base("Result was not properly initialized.")
|
||||||
|
{
|
||||||
|
VariableName = variableName;
|
||||||
|
}
|
||||||
|
public string VariableName { get; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ public static partial class ResultExtensions
|
|||||||
{
|
{
|
||||||
case ResultState.Error:
|
case ResultState.Error:
|
||||||
hasErrors = true;
|
hasErrors = true;
|
||||||
errors ??= [];
|
errors ??= new(4);
|
||||||
errors.Add(result.Error!);
|
errors.Add(result.Error!);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -123,13 +123,13 @@ public static partial class ResultExtensions
|
|||||||
{
|
{
|
||||||
case ResultState.Error:
|
case ResultState.Error:
|
||||||
hasErrors = true;
|
hasErrors = true;
|
||||||
errors ??= [];
|
errors ??= new(4);
|
||||||
errors.Add(result.Error!);
|
errors.Add(result.Error!);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ResultState.Success:
|
case ResultState.Success:
|
||||||
if (hasErrors) goto afterLoop;
|
if (hasErrors) goto afterLoop;
|
||||||
values ??= [];
|
values ??= new(4);
|
||||||
values.Add(result.Value);
|
values.Add(result.Value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user