Skip to content

Commit cbad95d

Browse files
authored
Merge pull request #26 from I-RzR-I/feature/AddAndReviewValidation
add and review validation in the DataTypeExtensions folder
2 parents 1cd61e7 + 3749789 commit cbad95d

File tree

10 files changed

+122
-17
lines changed

10 files changed

+122
-17
lines changed

docs/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,8 @@
107107
-> Add new typeparam extensions: `IfIsNull`, `IfIsNotNull`, `IfIsNullOrFuncIsTrue`, `IfIsNullAndFuncIsTrue`, `IfFuncIsTrue`, `IfFuncIsFalse`, `IfFunc`, `IfNull`, `IfNotNull`.<br />
108108

109109
### **v1.1.2.3434**
110-
-> Add new string extensions: `AsRedacted`, `TrimPrefix`, `TrimSuffix`.<br />
110+
-> Add new string extensions: `AsRedacted`, `TrimPrefix`, `TrimSuffix`.<br />
111+
112+
### **v1.2.0.0**
113+
-> Add/adjust input validations in the `DataTypeExtensions` foler with extensions;<br />
114+
-> Add new string extensions: `IfNullThenEmpty`.<br />

src/DomainCommonExtensions/DataTypeExtensions/ByteExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public static string GetHashSha1String(this byte[] bytes)
7979
if (bytes.IsNull())
8080
throw new ArgumentNullException(nameof(bytes));
8181

82+
#pragma warning disable SCS0006
8283
using var sha1 = new SHA1Managed();
84+
#pragma warning restore SCS0006
8385
var hash = sha1.ComputeHash(bytes);
8486

8587
return Convert.ToBase64String(hash);
@@ -122,6 +124,9 @@ public static string GetString(this byte[] bytes)
122124
/// <remarks></remarks>
123125
public static string ToStringFromByteUnicode(this byte[] input)
124126
{
127+
if (input.IsNull())
128+
throw new ArgumentNullException(nameof(input));
129+
125130
return Encoding.Unicode.GetString(input);
126131
}
127132

@@ -135,6 +140,9 @@ public static string ToStringFromByteUnicode(this byte[] input)
135140
/// <remarks></remarks>
136141
public static string ToHexByte(this byte[] clearText, bool withSpace = false)
137142
{
143+
if (clearText.IsNull())
144+
throw new ArgumentNullException(nameof(clearText));
145+
138146
var hex = new StringBuilder(clearText.Length * 2);
139147
foreach (var b in clearText)
140148
hex.AppendFormat("{0:X2}{1}", b, withSpace ? " " : "");
@@ -176,6 +184,9 @@ public static bool CompareTo(this byte[] a, byte[] b)
176184
/// <remarks></remarks>
177185
public static byte[] GZipCompress(this byte[] source, CompressionLevel compressionLevel)
178186
{
187+
if (source.IsNull())
188+
throw new ArgumentNullException(nameof(source));
189+
179190
using var memory = new MemoryStream();
180191
using (var gzip = new GZipStream(memory, compressionLevel, true))
181192
{

src/DomainCommonExtensions/DataTypeExtensions/EnumExtensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ public static Dictionary<int, string> GetEnumDefinition(this Type enumType)
5959
/// <returns></returns>
6060
public static string GetEnumMemberValue<T>(this T value) where T : struct, Enum, IConvertible
6161
{
62+
if (value.IsNull()) return null;
63+
6264
var element = typeof(T).GetTypeInfo().DeclaredMembers
6365
.SingleOrDefault(x => x.Name == value.ToString());
6466

65-
return (object)element == null ? null : element.GetCustomAttribute<EnumMemberAttribute>(false)?.Value;
67+
return ((object)element).IsNull() ? null : element!.GetCustomAttribute<EnumMemberAttribute>(false)?.Value;
6668
}
6769
#endif
6870

@@ -75,6 +77,8 @@ public static string GetEnumMemberValue<T>(this T value) where T : struct, Enum,
7577
/// <returns></returns>
7678
public static T ToEnumMemberValue<T>(this string str) where T : struct, Enum, IConvertible
7779
{
80+
if (str.IsNull()) return default;
81+
7882
var enumType = typeof(T);
7983
foreach (var name in Enum.GetNames(enumType))
8084
{
@@ -98,6 +102,8 @@ public static T ToEnumMemberValue<T>(this string str) where T : struct, Enum, IC
98102
/// <remarks></remarks>
99103
public static int ToInt<T>(this T source) where T : Enum, IConvertible
100104
{
105+
if (source.IsNull())
106+
throw new ArgumentNullException(nameof(source));
101107
if (typeof(T).IsEnum.IsFalse())
102108
throw new ArgumentException("T must be an enumerated type");
103109

@@ -113,6 +119,8 @@ public static int ToInt<T>(this T source) where T : Enum, IConvertible
113119
/// <remarks></remarks>
114120
public static string ToString<T>(this T source) where T : Enum, IConvertible
115121
{
122+
if (source.IsNull())
123+
throw new ArgumentNullException(nameof(source));
116124
if (typeof(T).IsEnum.IsFalse())
117125
throw new ArgumentException("T must be an enumerated type");
118126

@@ -130,11 +138,11 @@ public static string ToString<T>(this T source) where T : Enum, IConvertible
130138
public static string GetDescription(this Enum value, bool returnEmpty = false)
131139
{
132140
var fieldInfo = value.GetType().GetField(value.ToString());
133-
if (fieldInfo == null) return null;
141+
if (fieldInfo.IsNull()) return null;
134142

135143
var attribute = (DescriptionAttribute)fieldInfo.GetCustomAttribute(typeof(DescriptionAttribute));
136144

137-
return attribute != null ? attribute.Description : (returnEmpty.Equals(true) ? "" : value.ToString());
145+
return attribute.IsNotNull() ? attribute.Description : (returnEmpty.Equals(true) ? "" : value.ToString());
138146
}
139147
#endif
140148

src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public static bool TryCast<T>(this object obj, out T result)
8686
/// <param name="xmlDocument">The xml document</param>
8787
public static void GetXmlParams(this object obj, ref XmlDocument xmlDocument)
8888
{
89+
if (obj.IsNull())
90+
throw new ArgumentNullException(nameof(obj));
91+
8992
foreach (var prop in obj.GetType().GetProperties())
9093
{
9194
var xmlNode = xmlDocument.SelectSingleNode("//" + prop.Name);
@@ -103,6 +106,11 @@ public static void GetXmlParams(this object obj, ref XmlDocument xmlDocument)
103106
/// <remarks></remarks>
104107
public static void CopyPropertiesTo(this object fromObject, object toObject)
105108
{
109+
if (fromObject.IsNull())
110+
throw new ArgumentNullException(nameof(fromObject));
111+
if (toObject.IsNull())
112+
throw new ArgumentNullException(nameof(toObject));
113+
106114
var toObjectProperties = toObject.GetType().GetProperties();
107115
foreach (var propTo in toObjectProperties)
108116
{
@@ -240,6 +248,8 @@ public static T To<T>(this object source)
240248
[CodeSource("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=netstandard-2.0", "", "MS", "2022-12-28", "Reference source")]
241249
public static string SerializeToString(this object obj)
242250
{
251+
if (obj.IsNull()) return null;
252+
243253
using var memoryStream = new MemoryStream();
244254
using var reader = new StreamReader(memoryStream);
245255
var serializer = new DataContractSerializer(obj.GetType());

src/DomainCommonExtensions/DataTypeExtensions/SocketExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System;
2020
using System.Net.Sockets;
2121
using System.Runtime.InteropServices;
22+
using DomainCommonExtensions.CommonExtensions;
2223

2324
#endregion
2425

@@ -39,6 +40,9 @@ public static class SocketExtensions
3940
/// <param name="keepAliveInterval">The keep alive interval. (ms)</param>
4041
public static void SetSocketKeepAliveValues(this TcpClient tcpClient, int keepAliveTime, int keepAliveInterval)
4142
{
43+
if (tcpClient.IsNull())
44+
throw new ArgumentNullException(nameof(tcpClient));
45+
4246
//KeepAliveTime: default value is 2hr
4347
//KeepAliveInterval: default value is 1s and Detect 5 times
4448

0 commit comments

Comments
 (0)