-
-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathCheck.cs
More file actions
149 lines (121 loc) · 4.49 KB
/
Check.cs
File metadata and controls
149 lines (121 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) Pomelo Foundation. All rights reserved.
// Licensed under the MIT. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using CA = System.Diagnostics.CodeAnalysis;
namespace Microsoft.EntityFrameworkCore.Utilities
{
[DebuggerStepThrough]
internal static class Check
{
[ContractAnnotation("value:null => halt")]
public static T NotNull<T>([NoEnumeration] T value, [InvokerParameterName] [NotNull] string parameterName)
{
#pragma warning disable IDE0041 // Use 'is null' check
if (ReferenceEquals(value, null))
#pragma warning restore IDE0041 // Use 'is null' check
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentNullException(parameterName);
}
return value;
}
[ContractAnnotation("value:null => halt")]
public static IReadOnlyList<T> NotEmpty<T>(IReadOnlyList<T> value, [InvokerParameterName] [NotNull] string parameterName)
{
NotNull(value, parameterName);
if (value.Count == 0)
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentException(AbstractionsStrings.CollectionArgumentIsEmpty);
}
return value;
}
[ContractAnnotation("value:null => halt")]
public static string NotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName)
{
Exception e = null;
if (value is null)
{
e = new ArgumentNullException(parameterName);
}
else if (value.Trim().Length == 0)
{
e = new ArgumentException(AbstractionsStrings.ArgumentIsEmpty);
}
if (e != null)
{
NotEmpty(parameterName, nameof(parameterName));
throw e;
}
return value;
}
public static string NullButNotEmpty(string value, [InvokerParameterName] [NotNull] string parameterName)
{
if (!(value is null)
&& value.Length == 0)
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentException(AbstractionsStrings.ArgumentIsEmpty);
}
return value;
}
public static IReadOnlyList<T> HasNoNulls<T>(IReadOnlyList<T> value, [InvokerParameterName] [NotNull] string parameterName)
where T : class
{
NotNull(value, parameterName);
if (value.Any(e => e == null))
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentException(parameterName);
}
return value;
}
public static IReadOnlyList<string> HasNoEmptyElements(
IReadOnlyList<string> value,
[InvokerParameterName] [NotNull] string parameterName)
{
NotNull(value, parameterName);
if (value.Any(s => string.IsNullOrWhiteSpace(s)))
{
NotEmpty(parameterName, nameof(parameterName));
throw new ArgumentException(AbstractionsStrings.CollectionArgumentHasEmptyElements);
}
return value;
}
public static TEnum? EnumValue<TEnum>(
TEnum? value,
[InvokerParameterName] [NotNull] string parameterName)
where TEnum : struct
{
NotNull(value, parameterName);
return NullOrEnumValue(value, parameterName);
}
public static TEnum? NullOrEnumValue<TEnum>(
TEnum? value,
[InvokerParameterName] [NotNull] string parameterName)
where TEnum : struct
{
if (value is not null)
{
if (!Enum.IsDefined(typeof(TEnum), value))
{
throw new ArgumentOutOfRangeException(parameterName, value, null);
}
}
return value;
}
[Conditional("DEBUG")]
public static void DebugAssert([CA.DoesNotReturnIfAttribute(false)] bool condition, string message)
{
if (!condition)
{
throw new Exception($"Check.DebugAssert failed: {message}");
}
}
}
}