Skip to content

Commit

Permalink
Merge pull request #26 from I-RzR-I/feature/AddAndReviewValidation
Browse files Browse the repository at this point in the history
add and review validation in the DataTypeExtensions folder
  • Loading branch information
I-RzR-I authored May 29, 2024
2 parents 1cd61e7 + 3749789 commit cbad95d
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 17 deletions.
6 changes: 5 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,8 @@
-> Add new typeparam extensions: `IfIsNull`, `IfIsNotNull`, `IfIsNullOrFuncIsTrue`, `IfIsNullAndFuncIsTrue`, `IfFuncIsTrue`, `IfFuncIsFalse`, `IfFunc`, `IfNull`, `IfNotNull`.<br />

### **v1.1.2.3434**
-> Add new string extensions: `AsRedacted`, `TrimPrefix`, `TrimSuffix`.<br />
-> Add new string extensions: `AsRedacted`, `TrimPrefix`, `TrimSuffix`.<br />

### **v1.2.0.0**
-> Add/adjust input validations in the `DataTypeExtensions` foler with extensions;<br />
-> Add new string extensions: `IfNullThenEmpty`.<br />
11 changes: 11 additions & 0 deletions src/DomainCommonExtensions/DataTypeExtensions/ByteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public static string GetHashSha1String(this byte[] bytes)
if (bytes.IsNull())
throw new ArgumentNullException(nameof(bytes));

#pragma warning disable SCS0006
using var sha1 = new SHA1Managed();
#pragma warning restore SCS0006
var hash = sha1.ComputeHash(bytes);

return Convert.ToBase64String(hash);
Expand Down Expand Up @@ -122,6 +124,9 @@ public static string GetString(this byte[] bytes)
/// <remarks></remarks>
public static string ToStringFromByteUnicode(this byte[] input)
{
if (input.IsNull())
throw new ArgumentNullException(nameof(input));

return Encoding.Unicode.GetString(input);
}

Expand All @@ -135,6 +140,9 @@ public static string ToStringFromByteUnicode(this byte[] input)
/// <remarks></remarks>
public static string ToHexByte(this byte[] clearText, bool withSpace = false)
{
if (clearText.IsNull())
throw new ArgumentNullException(nameof(clearText));

var hex = new StringBuilder(clearText.Length * 2);
foreach (var b in clearText)
hex.AppendFormat("{0:X2}{1}", b, withSpace ? " " : "");
Expand Down Expand Up @@ -176,6 +184,9 @@ public static bool CompareTo(this byte[] a, byte[] b)
/// <remarks></remarks>
public static byte[] GZipCompress(this byte[] source, CompressionLevel compressionLevel)
{
if (source.IsNull())
throw new ArgumentNullException(nameof(source));

using var memory = new MemoryStream();
using (var gzip = new GZipStream(memory, compressionLevel, true))
{
Expand Down
14 changes: 11 additions & 3 deletions src/DomainCommonExtensions/DataTypeExtensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public static Dictionary<int, string> GetEnumDefinition(this Type enumType)
/// <returns></returns>
public static string GetEnumMemberValue<T>(this T value) where T : struct, Enum, IConvertible
{
if (value.IsNull()) return null;

var element = typeof(T).GetTypeInfo().DeclaredMembers
.SingleOrDefault(x => x.Name == value.ToString());

return (object)element == null ? null : element.GetCustomAttribute<EnumMemberAttribute>(false)?.Value;
return ((object)element).IsNull() ? null : element!.GetCustomAttribute<EnumMemberAttribute>(false)?.Value;
}
#endif

Expand All @@ -75,6 +77,8 @@ public static string GetEnumMemberValue<T>(this T value) where T : struct, Enum,
/// <returns></returns>
public static T ToEnumMemberValue<T>(this string str) where T : struct, Enum, IConvertible
{
if (str.IsNull()) return default;

var enumType = typeof(T);
foreach (var name in Enum.GetNames(enumType))
{
Expand All @@ -98,6 +102,8 @@ public static T ToEnumMemberValue<T>(this string str) where T : struct, Enum, IC
/// <remarks></remarks>
public static int ToInt<T>(this T source) where T : Enum, IConvertible
{
if (source.IsNull())
throw new ArgumentNullException(nameof(source));
if (typeof(T).IsEnum.IsFalse())
throw new ArgumentException("T must be an enumerated type");

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

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

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

return attribute != null ? attribute.Description : (returnEmpty.Equals(true) ? "" : value.ToString());
return attribute.IsNotNull() ? attribute.Description : (returnEmpty.Equals(true) ? "" : value.ToString());
}
#endif

Expand Down
10 changes: 10 additions & 0 deletions src/DomainCommonExtensions/DataTypeExtensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public static bool TryCast<T>(this object obj, out T result)
/// <param name="xmlDocument">The xml document</param>
public static void GetXmlParams(this object obj, ref XmlDocument xmlDocument)
{
if (obj.IsNull())
throw new ArgumentNullException(nameof(obj));

foreach (var prop in obj.GetType().GetProperties())
{
var xmlNode = xmlDocument.SelectSingleNode("//" + prop.Name);
Expand All @@ -103,6 +106,11 @@ public static void GetXmlParams(this object obj, ref XmlDocument xmlDocument)
/// <remarks></remarks>
public static void CopyPropertiesTo(this object fromObject, object toObject)
{
if (fromObject.IsNull())
throw new ArgumentNullException(nameof(fromObject));
if (toObject.IsNull())
throw new ArgumentNullException(nameof(toObject));

var toObjectProperties = toObject.GetType().GetProperties();
foreach (var propTo in toObjectProperties)
{
Expand Down Expand Up @@ -240,6 +248,8 @@ public static T To<T>(this object source)
[CodeSource("https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=netstandard-2.0", "", "MS", "2022-12-28", "Reference source")]
public static string SerializeToString(this object obj)
{
if (obj.IsNull()) return null;

using var memoryStream = new MemoryStream();
using var reader = new StreamReader(memoryStream);
var serializer = new DataContractSerializer(obj.GetType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using DomainCommonExtensions.CommonExtensions;

#endregion

Expand All @@ -39,6 +40,9 @@ public static class SocketExtensions
/// <param name="keepAliveInterval">The keep alive interval. (ms)</param>
public static void SetSocketKeepAliveValues(this TcpClient tcpClient, int keepAliveTime, int keepAliveInterval)
{
if (tcpClient.IsNull())
throw new ArgumentNullException(nameof(tcpClient));

//KeepAliveTime: default value is 2hr
//KeepAliveInterval: default value is 1s and Detect 5 times

Expand Down
Loading

0 comments on commit cbad95d

Please sign in to comment.