Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to project to support Nullable types #155

Merged
merged 4 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/projects/EnsureThat/Annotations/NotNullAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This IF should be 'any new targets we add that natively support NotNullAttribute'. Right now, that is only 2_1.
#if !NETSTANDARD2_1
// ReSharper disable once CheckNamespace
namespace System.Diagnostics.CodeAnalysis
{
/// <summary>
/// This is JUST so we can avoid having #if regions in each usage site.
///
/// For non-supported frameworks, it may be ignored by CodeAnalysis, but allows the usage
/// sites to avoid all having
/// <code>
/// #if NETSTANDARD2_1
/// [NotNull]
/// #endif
/// [OtherAttributes]
/// public string SampleMethod() {}
/// </code>
/// </summary>
internal sealed class NotNullAttribute : Attribute
{
}
}
#endif
18 changes: 10 additions & 8 deletions src/projects/EnsureThat/Enforcers/AnyArg.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System;
using EnsureThat.Annotations;
using JetBrains.Annotations;

using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;

namespace EnsureThat.Enforcers
{
public sealed class AnyArg
Expand All @@ -18,9 +20,9 @@ public sealed class AnyArg
/// <param name="optsFn"></param>
/// <returns></returns>
/// <remarks>If you know you are dealing with e.g. a struct, the <see cref="IsNotNull{T}(T?, string, OptsFn)"/> overload is more performant.</remarks>
[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public T HasValue<T>([NoEnumeration, ValidatedNotNull] T value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
public T HasValue<T>([NoEnumeration, ValidatedNotNull, NotNull] T value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
// ReSharper disable once HeapView.BoxingAllocation
if (value == null)
Expand All @@ -29,19 +31,19 @@ public T HasValue<T>([NoEnumeration, ValidatedNotNull] T value, [InvokerParamete
return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public T IsNotNull<T>([NoEnumeration, ValidatedNotNull] T value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null) where T : class
public T IsNotNull<T>([NoEnumeration, ValidatedNotNull, NotNull] T value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null) where T : class
{
if (value == null)
throw Ensure.ExceptionFactory.ArgumentNullException(ExceptionMessages.Common_IsNotNull_Failed, paramName, optsFn);

return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public T? IsNotNull<T>([ValidatedNotNull] T? value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null) where T : struct
public T? IsNotNull<T>([ValidatedNotNull, NotNull] T? value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null) where T : struct
{
if (value == null)
throw Ensure.ExceptionFactory.ArgumentNullException(ExceptionMessages.Common_IsNotNull_Failed, paramName, optsFn);
Expand All @@ -57,4 +59,4 @@ public T IsNotDefault<T>(T value, [InvokerParameterName] string paramName = null
return value;
}
}
}
}
108 changes: 55 additions & 53 deletions src/projects/EnsureThat/Enforcers/CollectionArg.cs

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions src/projects/EnsureThat/Enforcers/EnumerableArg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using EnsureThat.Annotations;
using JetBrains.Annotations;

using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;

namespace EnsureThat.Enforcers
{
/// <summary>
Expand All @@ -14,7 +16,7 @@ namespace EnsureThat.Enforcers
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
public sealed class EnumerableArg
{
[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public IEnumerable<T> HasItems<T>([ValidatedNotNull, InstantHandle]IEnumerable<T> value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Expand All @@ -29,7 +31,7 @@ public IEnumerable<T> HasItems<T>([ValidatedNotNull, InstantHandle]IEnumerable<T
return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public IEnumerable<T> SizeIs<T>([ValidatedNotNull, InstantHandle]IEnumerable<T> value, int expected, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Expand All @@ -46,7 +48,7 @@ public IEnumerable<T> SizeIs<T>([ValidatedNotNull, InstantHandle]IEnumerable<T>
return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public IEnumerable<T> SizeIs<T>([ValidatedNotNull, InstantHandle]IEnumerable<T> value, long expected, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Expand All @@ -69,7 +71,7 @@ public IEnumerable<T> SizeIs<T>([ValidatedNotNull, InstantHandle]IEnumerable<T>
return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public IEnumerable<T> HasAny<T>([ValidatedNotNull, InstantHandle]IEnumerable<T> value, Func<T, bool> predicate, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Expand All @@ -84,4 +86,4 @@ public IEnumerable<T> HasAny<T>([ValidatedNotNull, InstantHandle]IEnumerable<T>
return value;
}
}
}
}
48 changes: 25 additions & 23 deletions src/projects/EnsureThat/Enforcers/StringArg.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
using System;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using EnsureThat.Annotations;
using JetBrains.Annotations;

using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;

namespace EnsureThat.Enforcers
{
public sealed class StringArg
{
[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public string IsNotNull([ValidatedNotNull]string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
public string IsNotNull([ValidatedNotNull, NotNull]string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Ensure.Any.IsNotNull(value, paramName, optsFn);

return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public string IsNotNullOrWhiteSpace([ValidatedNotNull]string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
public string IsNotNullOrWhiteSpace([ValidatedNotNull, NotNull]string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Ensure.Any.IsNotNull(value, paramName, optsFn);

Expand All @@ -29,8 +31,8 @@ public string IsNotNullOrWhiteSpace([ValidatedNotNull]string value, [InvokerPara
return value;
}

[NotNull]
public string IsNotNullOrEmpty([ValidatedNotNull]string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
[return: NotNull]
public string IsNotNullOrEmpty([ValidatedNotNull, NotNull]string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Ensure.Any.IsNotNull(value, paramName, optsFn);

Expand Down Expand Up @@ -69,9 +71,9 @@ public string IsNotEmpty(string value, [InvokerParameterName] string paramName =
return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public string HasLength([ValidatedNotNull]string value, int expected, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
public string HasLength([ValidatedNotNull, NotNull]string value, int expected, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Ensure.Any.IsNotNull(value, paramName, optsFn);

Expand All @@ -84,9 +86,9 @@ public string HasLength([ValidatedNotNull]string value, int expected, [InvokerPa
return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public string HasLengthBetween([ValidatedNotNull]string value, int minLength, int maxLength, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
public string HasLengthBetween([ValidatedNotNull, NotNull]string value, int minLength, int maxLength, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Ensure.Any.IsNotNull(value, paramName, optsFn);

Expand All @@ -107,11 +109,11 @@ public string HasLengthBetween([ValidatedNotNull]string value, int minLength, in
return value;
}

[NotNull]
[return: NotNull]
public string Matches(string value, [RegexPattern] string match, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
=> Matches(value, new Regex(match), paramName, optsFn);

[NotNull]
[return: NotNull]
public string Matches(string value, Regex match, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
if (!match.IsMatch(value))
Expand All @@ -123,10 +125,10 @@ public string Matches(string value, Regex match, [InvokerParameterName] string p
return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
[Obsolete("Use 'HasLength' instead. This will be removed in an upcoming version.")]
public string SizeIs([ValidatedNotNull] string value, int expected, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
public string SizeIs([ValidatedNotNull, NotNull] string value, int expected, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
=> HasLength(value, expected, paramName, optsFn);

public string Is(string value, string expected, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
Expand Down Expand Up @@ -185,9 +187,9 @@ public string IsNotEqualTo(string value, string notExpected, StringComparison co
return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public Guid IsGuid([ValidatedNotNull]string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
public Guid IsGuid([ValidatedNotNull, NotNull]string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
if (!Guid.TryParse(value, out var parsed))
throw Ensure.ExceptionFactory.ArgumentException(
Expand All @@ -198,9 +200,9 @@ public Guid IsGuid([ValidatedNotNull]string value, [InvokerParameterName] string
return parsed;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public string StartsWith([ValidatedNotNull]string value, [NotNull] string expectedStartsWith, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
public string StartsWith([ValidatedNotNull, NotNull]string value, [NotNull] string expectedStartsWith, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Ensure.Any.IsNotNull(value, paramName, optsFn);

Expand All @@ -213,9 +215,9 @@ public string StartsWith([ValidatedNotNull]string value, [NotNull] string expect
return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public string StartsWith([ValidatedNotNull]string value, [NotNull] string expectedStartsWith, StringComparison comparison, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
public string StartsWith([ValidatedNotNull, NotNull]string value, [NotNull] string expectedStartsWith, StringComparison comparison, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Ensure.Any.IsNotNull(value, paramName, optsFn);

Expand Down Expand Up @@ -277,9 +279,9 @@ public string IsInRange(string value, string min, string max, StringComparison c
return value;
}

[NotNull]
[return: NotNull]
[ContractAnnotation("value:null => halt")]
public string IsAllLettersOrDigits([ValidatedNotNull] string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
public string IsAllLettersOrDigits([ValidatedNotNull, NotNull] string value, [InvokerParameterName] string paramName = null, OptsFn optsFn = null)
{
Ensure.Any.IsNotNull(value, paramName, optsFn);

Expand Down
Loading