-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
206 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
namespace Fluxera.ValueObject | ||
{ | ||
using JetBrains.Annotations; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
|
||
internal static class Guard | ||
{ | ||
public static T ThrowIfNull<T>(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
ArgumentNullException.ThrowIfNull(argument, parameterName); | ||
|
||
return argument; | ||
} | ||
|
||
public static string ThrowIfNullOrEmpty(string argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
argument = ThrowIfNull(argument, parameterName); | ||
|
||
if(string.IsNullOrEmpty(argument)) | ||
{ | ||
throw new ArgumentException("Value cannot be empty.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
|
||
public static string ThrowIfNullOrWhiteSpace(string argument, [InvokerParameterName][CallerArgumentExpression("argument")] string parameterName = null) | ||
{ | ||
argument = ThrowIfNull(argument, parameterName); | ||
|
||
if(string.IsNullOrWhiteSpace(argument)) | ||
{ | ||
throw new ArgumentException("Value cannot be whitespace-only.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
|
||
public static bool ThrowIfFalse(bool argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null, string message = null) | ||
{ | ||
if(!argument) | ||
{ | ||
throw new ArgumentException(message ?? "Value cannot be false.", parameterName); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static IEnumerable<T> ThrowIfNullOrEmpty<T>(IEnumerable<T> argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
argument = ThrowIfNull(argument, parameterName); | ||
|
||
// ReSharper disable PossibleMultipleEnumeration | ||
if(!argument.Any()) | ||
{ | ||
throw new ArgumentException("Enumerable cannot be empty.", parameterName); | ||
} | ||
|
||
return argument; | ||
// ReSharper enable PossibleMultipleEnumeration | ||
} | ||
|
||
#if NET7_0_OR_GREATER | ||
public static T ThrowIfNegative<T>(T argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
where T : INumber<T> | ||
{ | ||
if(T.IsNegative(argument)) | ||
{ | ||
throw new ArgumentException("Value cannot be negative.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
#endif | ||
|
||
#if NET6_0 | ||
public static byte ThrowIfNegative(byte argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
if(argument < 0) | ||
{ | ||
throw new ArgumentException("Value cannot be negative.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
|
||
public static short ThrowIfNegative(short argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
if(argument < 0) | ||
{ | ||
throw new ArgumentException("Value cannot be negative.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
|
||
public static int ThrowIfNegative(int argument, [InvokerParameterName] [CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
if(argument < 0) | ||
{ | ||
throw new ArgumentException("Value cannot be negative.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
|
||
public static long ThrowIfNegative(long argument, [InvokerParameterName][CallerArgumentExpression(nameof(argument))] string parameterName = null) | ||
{ | ||
if(argument < 0) | ||
{ | ||
throw new ArgumentException("Value cannot be negative.", parameterName); | ||
} | ||
|
||
return argument; | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
namespace Fluxera.ValueObject | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
internal static class TypeExtensions | ||
{ | ||
private static readonly HashSet<Type> ExtraPrimitiveTypes = | ||
[ | ||
typeof(string), | ||
typeof(decimal), | ||
typeof(DateOnly), | ||
typeof(TimeOnly), | ||
typeof(DateTime), | ||
typeof(DateTimeOffset), | ||
typeof(TimeSpan), | ||
typeof(Guid) | ||
]; | ||
|
||
/// <summary> | ||
/// Determines whether the specified type is a primitive. It automatically | ||
/// unwraps the wrapped type of the nullable. | ||
/// </summary> | ||
/// <param name="type">The type.</param> | ||
/// <param name="includeEnums">If set to <c>true</c> include enums.</param> | ||
/// <returns> | ||
/// <c>true</c> if the specified type is a primitive; otherwise, <c>false</c>. | ||
/// </returns> | ||
public static bool IsPrimitive(this Type type, bool includeEnums = false) | ||
{ | ||
type = type.UnwrapNullableType(); | ||
|
||
if(type.IsPrimitive) | ||
{ | ||
return true; | ||
} | ||
|
||
if(includeEnums && type.IsEnum) | ||
{ | ||
return true; | ||
} | ||
|
||
return ExtraPrimitiveTypes.Contains(type); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the type without nullable if the type is a <see cref="Nullable{T}" />. | ||
/// </summary> | ||
/// <param name="type">The type.</param> | ||
/// <returns></returns> | ||
private static Type UnwrapNullableType(this Type type) | ||
{ | ||
return Nullable.GetUnderlyingType(type) ?? type; | ||
} | ||
} | ||
} |