From cf1e382ebce291f6da5d1025372135fd5a0827c9 Mon Sep 17 00:00:00 2001 From: Marvin Brouwer Date: Wed, 18 Oct 2023 20:28:09 +0200 Subject: [PATCH] 291 Added backwards compatibility --- .../CallerArgumentExpressionAttribute.cs | 20 +++++++++++++ .../GuardAgainstNegativeExtensions.cs | 28 +++++++++---------- .../GuardAgainstNotFoundExtensions.cs | 4 +-- .../GuardAgainstNullExtensions.cs | 16 +++++------ .../GuardAgainstOutOfRangeExtensions.cs | 8 +++--- .../GuardAgainstZeroExtensions.cs | 12 ++++---- src/GuardClauses/GuardClauses.csproj | 15 ++-------- .../NamespaceSeparate/ExtendingGuard.cs | 2 +- 8 files changed, 58 insertions(+), 47 deletions(-) create mode 100644 src/GuardClauses/CompilerFixes/CallerArgumentExpressionAttribute.cs diff --git a/src/GuardClauses/CompilerFixes/CallerArgumentExpressionAttribute.cs b/src/GuardClauses/CompilerFixes/CallerArgumentExpressionAttribute.cs new file mode 100644 index 00000000..b8bc2296 --- /dev/null +++ b/src/GuardClauses/CompilerFixes/CallerArgumentExpressionAttribute.cs @@ -0,0 +1,20 @@ +#if NETSTANDARD2_1 + +namespace System.Runtime.CompilerServices; + +/// +/// This a special exception for backwards compatibility. +/// Please upgrade your dependent package to the latest supported netN.N version rather than using netstandard2.1 +/// +[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] +internal sealed class CallerArgumentExpressionAttribute : Attribute +{ + public CallerArgumentExpressionAttribute(string parameterName) + { + ParameterName = parameterName; + } + + public string ParameterName { get; } +} + +#endif diff --git a/src/GuardClauses/GuardAgainstNegativeExtensions.cs b/src/GuardClauses/GuardAgainstNegativeExtensions.cs index 421d3e69..8ebc2d38 100644 --- a/src/GuardClauses/GuardAgainstNegativeExtensions.cs +++ b/src/GuardClauses/GuardAgainstNegativeExtensions.cs @@ -14,7 +14,7 @@ public static partial class GuardClauseExtensions /// Optional. Custom error message /// if the value is not negative. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static int Negative(this IGuardClause guardClause, int input, string parameterName, @@ -38,7 +38,7 @@ public static int Negative(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not negative. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static long Negative(this IGuardClause guardClause, long input, string parameterName, @@ -62,7 +62,7 @@ public static long Negative(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not negative. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static decimal Negative(this IGuardClause guardClause, decimal input, string parameterName, @@ -86,7 +86,7 @@ public static decimal Negative(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not negative. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static float Negative(IGuardClause guardClause, float input, string parameterName, @@ -110,7 +110,7 @@ public static float Negative(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not negative. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static double Negative(this IGuardClause guardClause, double input, string parameterName, @@ -134,7 +134,7 @@ public static double Negative(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not negative. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static TimeSpan Negative(this IGuardClause guardClause, TimeSpan input, string parameterName, @@ -158,7 +158,7 @@ public static TimeSpan Negative(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not negative. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 private static T Negative(this IGuardClause guardClause, T input, string parameterName, @@ -186,7 +186,7 @@ private static T Negative(this IGuardClause guardClause, /// /// Optional. Custom error message /// if the value is not negative or zero. -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static int NegativeOrZero(this IGuardClause guardClause, int input, string parameterName, @@ -209,7 +209,7 @@ public static int NegativeOrZero(this IGuardClause guardClause, /// /// Optional. Custom error message /// if the value is not negative or zero. -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static long NegativeOrZero(this IGuardClause guardClause, long input, string parameterName, @@ -232,7 +232,7 @@ public static long NegativeOrZero(this IGuardClause guardClause, /// /// Optional. Custom error message /// if the value is not negative or zero. -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static decimal NegativeOrZero(this IGuardClause guardClause, decimal input, string parameterName, @@ -255,7 +255,7 @@ public static decimal NegativeOrZero(this IGuardClause guardClause, /// /// Optional. Custom error message /// if the value is not negative or zero. -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static float NegativeOrZero(this IGuardClause guardClause, float input, string parameterName, @@ -278,7 +278,7 @@ public static float NegativeOrZero(this IGuardClause guardClause, /// /// Optional. Custom error message /// if the value is not negative or zero. -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static double NegativeOrZero(this IGuardClause guardClause, double input, string parameterName, @@ -301,7 +301,7 @@ public static double NegativeOrZero(this IGuardClause guardClause, /// /// Optional. Custom error message /// if the value is not negative or zero. -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static TimeSpan NegativeOrZero(this IGuardClause guardClause, TimeSpan input, string parameterName, @@ -325,7 +325,7 @@ public static TimeSpan NegativeOrZero(this IGuardClause guardClause, /// /// Optional. Custom error message /// if the value is not negative or zero. -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 private static T NegativeOrZero(this IGuardClause guardClause, T input, string parameterName, diff --git a/src/GuardClauses/GuardAgainstNotFoundExtensions.cs b/src/GuardClauses/GuardAgainstNotFoundExtensions.cs index 6010f04f..28c4425d 100644 --- a/src/GuardClauses/GuardAgainstNotFoundExtensions.cs +++ b/src/GuardClauses/GuardAgainstNotFoundExtensions.cs @@ -15,7 +15,7 @@ public static partial class GuardClauseExtensions /// /// if the value is not null. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static T NotFound(this IGuardClause guardClause, [NotNull][ValidatedNotNull] string key, [NotNull][ValidatedNotNull] T? input, @@ -48,7 +48,7 @@ public static T NotFound(this IGuardClause guardClause, /// /// if the value is not null. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static T NotFound(this IGuardClause guardClause, [NotNull][ValidatedNotNull] TKey key, [NotNull][ValidatedNotNull] T? input, diff --git a/src/GuardClauses/GuardAgainstNullExtensions.cs b/src/GuardClauses/GuardAgainstNullExtensions.cs index eeb79d82..ada9165b 100644 --- a/src/GuardClauses/GuardAgainstNullExtensions.cs +++ b/src/GuardClauses/GuardAgainstNullExtensions.cs @@ -23,7 +23,7 @@ public static partial class GuardClauseExtensions /// /// Optional. Custom error message /// if the value is not null. -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static T Null(this IGuardClause guardClause, [NotNull][ValidatedNotNull] T? input, string parameterName, @@ -56,7 +56,7 @@ public static T Null(this IGuardClause guardClause, /// /// Optional. Custom error message /// if the value is not null. -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static T Null(this IGuardClause guardClause, [NotNull][ValidatedNotNull] T? input, string parameterName, @@ -91,7 +91,7 @@ public static T Null(this IGuardClause guardClause, /// if the value is not an empty string or null. /// /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static string NullOrEmpty(this IGuardClause guardClause, [NotNull][ValidatedNotNull] string? input, string parameterName, @@ -123,7 +123,7 @@ public static string NullOrEmpty(this IGuardClause guardClause, /// if the value is not an empty guid or null. /// /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static Guid NullOrEmpty(this IGuardClause guardClause, [NotNull][ValidatedNotNull] Guid? input, string parameterName, @@ -155,7 +155,7 @@ public static Guid NullOrEmpty(this IGuardClause guardClause, /// if the value is not an empty enumerable or null. /// /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static IEnumerable NullOrEmpty(this IGuardClause guardClause, [NotNull][ValidatedNotNull] IEnumerable? input, string parameterName, @@ -187,7 +187,7 @@ public static IEnumerable NullOrEmpty(this IGuardClause guardClause, /// if the value is not an empty or whitespace string. /// /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static string NullOrWhiteSpace(this IGuardClause guardClause, [NotNull][ValidatedNotNull] string? input, string parameterName, @@ -217,7 +217,7 @@ public static string NullOrWhiteSpace(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not default for that type. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static T Default(this IGuardClause guardClause, [AllowNull, NotNull] T input, string parameterName, @@ -251,7 +251,7 @@ public static T Default(this IGuardClause guardClause, /// /// /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static T NullOrInvalidInput(this IGuardClause guardClause, [NotNull] T? input, string parameterName, diff --git a/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs b/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs index 27bdc359..c4b55bc1 100644 --- a/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs +++ b/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs @@ -19,7 +19,7 @@ public static partial class GuardClauseExtensions /// Optional. Custom error message /// if the value is not out of range. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static int EnumOutOfRange(this IGuardClause guardClause, int input, string parameterName, @@ -53,7 +53,7 @@ public static int EnumOutOfRange(this IGuardClause guardClause, /// /// Optional. Custom error message /// if the value is not out of range. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static T EnumOutOfRange(this IGuardClause guardClause, T input, string parameterName, @@ -123,7 +123,7 @@ public static IEnumerable OutOfRange(this IGuardClause guardClause, /// if the value is in the range of valid SqlDateTime values. /// /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static DateTime NullOrOutOfSQLDateRange(this IGuardClause guardClause, [NotNull][ValidatedNotNull] DateTime? input, string parameterName, @@ -148,7 +148,7 @@ public static DateTime NullOrOutOfSQLDateRange(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is in the range of valid SqlDateTime values. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static DateTime OutOfSQLDateRange(this IGuardClause guardClause, DateTime input, string parameterName, diff --git a/src/GuardClauses/GuardAgainstZeroExtensions.cs b/src/GuardClauses/GuardAgainstZeroExtensions.cs index 1c263443..195d5279 100644 --- a/src/GuardClauses/GuardAgainstZeroExtensions.cs +++ b/src/GuardClauses/GuardAgainstZeroExtensions.cs @@ -15,7 +15,7 @@ public static partial class GuardClauseExtensions /// Optional. Custom error message /// if the value is not zero. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static int Zero(this IGuardClause guardClause, int input, string parameterName, @@ -39,7 +39,7 @@ public static int Zero(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not zero. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static long Zero(this IGuardClause guardClause, long input, string parameterName, @@ -63,7 +63,7 @@ public static long Zero(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not zero. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static decimal Zero(this IGuardClause guardClause, decimal input, string parameterName, @@ -87,7 +87,7 @@ public static decimal Zero(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not zero. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static float Zero(this IGuardClause guardClause, float input, string parameterName, @@ -111,7 +111,7 @@ public static float Zero(this IGuardClause guardClause, /// Optional. Custom error message /// if the value is not zero. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static double Zero(this IGuardClause guardClause, double input, string parameterName, @@ -134,7 +134,7 @@ public static double Zero(this IGuardClause guardClause, /// /// if the value is not zero. /// -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static TimeSpan Zero(this IGuardClause guardClause, TimeSpan input, string parameterName) diff --git a/src/GuardClauses/GuardClauses.csproj b/src/GuardClauses/GuardClauses.csproj index 22748e1c..1ed5949c 100644 --- a/src/GuardClauses/GuardClauses.csproj +++ b/src/GuardClauses/GuardClauses.csproj @@ -6,7 +6,7 @@ - netstandard2.0;net7.0 + net7.0;netstandard2.1;netstandard2.0 Ardalis.GuardClauses Ardalis.GuardClauses true @@ -18,18 +18,9 @@ https://github.com/ardalis/guardclauses guard clause clauses assert assertion - * Implemented NullOrInvalidInput method by @tiagojsrios in https://github.com/ardalis/GuardClauses/pull/178 - * Embed license in nuget package by @jafin in https://github.com/ardalis/GuardClauses/pull/208 - * Remove JetBrains Annotations by @Coop56 in https://github.com/ardalis/GuardClauses/pull/236 - * Add NullOrEmpty and NullOrWhiteSpace overloads for ReadOnlySpan by @KonH in https://github.com/ardalis/GuardClauses/pull/237 - * Created async overload for clausules that use a predicate by @danny-bos-developer in https://github.com/ardalis/GuardClauses/pull/228 - * Working through build issues for new contributors by @sadukie in https://github.com/ardalis/GuardClauses/pull/265 - * Make type parameter T of the Null() method nullable and add overload for value types. by @amal-stack in https://github.com/ardalis/GuardClauses/pull/261 - * Upgrade to dotnet 7.0 by @amal-stack in https://github.com/ardalis/GuardClauses/pull/282 - * Adds `NullOrOutOfRange` clauses, adding support for nullable classes/structs to `OutOfRange` by @rafaelsc in https://github.com/ardalis/GuardClauses/pull/143 - * Add README file to package + * Added netstandard2.1 to TargetFrameworks for backwards compatibility - 4.1.1 + 4.1.2 Ardalis.GuardClauses icon.png true diff --git a/test/GuardClauses.UnitTests/NamespaceSeparate/ExtendingGuard.cs b/test/GuardClauses.UnitTests/NamespaceSeparate/ExtendingGuard.cs index ce64a202..255f49ef 100644 --- a/test/GuardClauses.UnitTests/NamespaceSeparate/ExtendingGuard.cs +++ b/test/GuardClauses.UnitTests/NamespaceSeparate/ExtendingGuard.cs @@ -9,7 +9,7 @@ namespace Ardalis.GuardClauses; /// public static class FooGuard { -#if NETSTANDARD || NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0 public static void Foo(this IGuardClause guardClause, string input, string parameterName) #else public static void Foo(this IGuardClause guardClause, string input, [CallerArgumentExpression("input")] string? parameterName = null)