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

refactor: refactor dotnet and cryptography #378

Merged
merged 16 commits into from
Dec 21, 2022
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
22 changes: 22 additions & 0 deletions Masa.Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Service.Caller
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Utils.Extensions.Validations.FluentValidation.Tests", "src\Utils\Extensions\Validations\Tests\Masa.Utils.Extensions.Validations.FluentValidation.Tests\Masa.Utils.Extensions.Validations.FluentValidation.Tests.csproj", "{351755B9-4C7F-4B51-B813-3203DC9A8284}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Utils.Extensions.Enums.Tests", "src\Utils\Extensions\Tests\Masa.Utils.Extensions.Enums.Tests\Masa.Utils.Extensions.Enums.Tests.csproj", "{3AAD9D87-BC2D-446C-B56B-60F8A1492C30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Utils.Extensions.Expressions.Tests", "src\Utils\Extensions\Tests\Masa.Utils.Extensions.Expressions.Tests\Masa.Utils.Extensions.Expressions.Tests.csproj", "{FCD6DF86-4E3B-4CDC-9849-28884B3B40D8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -2263,6 +2267,14 @@ Global
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|Any CPU.Build.0 = Release|Any CPU
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|x64.ActiveCfg = Release|Any CPU
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1}.Release|x64.Build.0 = Release|Any CPU
{3AAD9D87-BC2D-446C-B56B-60F8A1492C30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3AAD9D87-BC2D-446C-B56B-60F8A1492C30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3AAD9D87-BC2D-446C-B56B-60F8A1492C30}.Debug|x64.ActiveCfg = Debug|Any CPU
{3AAD9D87-BC2D-446C-B56B-60F8A1492C30}.Debug|x64.Build.0 = Debug|Any CPU
{3AAD9D87-BC2D-446C-B56B-60F8A1492C30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3AAD9D87-BC2D-446C-B56B-60F8A1492C30}.Release|Any CPU.Build.0 = Release|Any CPU
{3AAD9D87-BC2D-446C-B56B-60F8A1492C30}.Release|x64.ActiveCfg = Release|Any CPU
{3AAD9D87-BC2D-446C-B56B-60F8A1492C30}.Release|x64.Build.0 = Release|Any CPU
{351755B9-4C7F-4B51-B813-3203DC9A8284}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{351755B9-4C7F-4B51-B813-3203DC9A8284}.Debug|Any CPU.Build.0 = Debug|Any CPU
{351755B9-4C7F-4B51-B813-3203DC9A8284}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand All @@ -2271,6 +2283,14 @@ Global
{351755B9-4C7F-4B51-B813-3203DC9A8284}.Release|Any CPU.Build.0 = Release|Any CPU
{351755B9-4C7F-4B51-B813-3203DC9A8284}.Release|x64.ActiveCfg = Release|Any CPU
{351755B9-4C7F-4B51-B813-3203DC9A8284}.Release|x64.Build.0 = Release|Any CPU
{FCD6DF86-4E3B-4CDC-9849-28884B3B40D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FCD6DF86-4E3B-4CDC-9849-28884B3B40D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FCD6DF86-4E3B-4CDC-9849-28884B3B40D8}.Debug|x64.ActiveCfg = Debug|Any CPU
{FCD6DF86-4E3B-4CDC-9849-28884B3B40D8}.Debug|x64.Build.0 = Debug|Any CPU
{FCD6DF86-4E3B-4CDC-9849-28884B3B40D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FCD6DF86-4E3B-4CDC-9849-28884B3B40D8}.Release|Any CPU.Build.0 = Release|Any CPU
{FCD6DF86-4E3B-4CDC-9849-28884B3B40D8}.Release|x64.ActiveCfg = Release|Any CPU
{FCD6DF86-4E3B-4CDC-9849-28884B3B40D8}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2586,6 +2606,8 @@ Global
{9FEE5178-557E-4694-A088-F157267C989C} = {B1B09684-A842-44FA-9D35-68BE66838A5C}
{BC1E63D5-C997-4269-BCE9-A5FBE7BA7CA1} = {E8681596-D4BF-484A-A428-06D749FD4C5D}
{351755B9-4C7F-4B51-B813-3203DC9A8284} = {9FEE5178-557E-4694-A088-F157267C989C}
{3AAD9D87-BC2D-446C-B56B-60F8A1492C30} = {C2FAC276-9D6E-498A-BBA2-F3F14ADF4D0D}
{FCD6DF86-4E3B-4CDC-9849-28884B3B40D8} = {C2FAC276-9D6E-498A-BBA2-F3F14ADF4D0D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public bool TrySetVariable(string key, string variable, string defaultValue)
return true;
}

public void SetVariableAndDefaultValue(string key, string variable, string defaultValue)
public void SetVariable(string key, string variable, string defaultValue)
{
var variableInfo = VariableInfos.FirstOrDefault(v => v.Key.Equals(key, StringComparison.OrdinalIgnoreCase));
if (variableInfo != null) VariableInfos.Remove(variableInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static ErrorCode()
var fields = classType.GetFields(BindingFlags.Static | BindingFlags.Public);
foreach (var field in fields)
{
var errorMessage = AttributeUtils.GetDescriptionByField(field);
var errorMessage = AttributeUtils.GetDescriptionValueByField(field);

_errorCodeMessageDictionary.Add(field.GetRawConstantValue()!.ToString()!, errorMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void TestInitializeAppConfiguration()
var services = new ServiceCollection();
services.Configure<MasaAppConfigureOptions>(options =>
{
options.SetVariableAndDefaultValue(nameof(MasaAppConfigureOptions.Environment), "Env", "test1");
options.SetVariable(nameof(MasaAppConfigureOptions.Environment), "Env", "test1");
});
services.AddMasaConfiguration(masaConfigurationBuilder =>
{
Expand All @@ -34,7 +34,7 @@ public void TestInitializeAppConfiguration2()
var services = new ServiceCollection();
services.Configure<MasaAppConfigureOptions>(options =>
{
options.SetVariableAndDefaultValue(nameof(MasaAppConfigureOptions.Environment), "Env", "test1");
options.SetVariable(nameof(MasaAppConfigureOptions.Environment), "Env", "test1");
});
services.AddMasaConfiguration();
var serviceProvider = services.BuildServiceProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ public DefaultMapper(IMappingConfigProvider provider)

public TDestination Map<TSource, TDestination>(TSource source, MapOptions? options = null)
{
ArgumentNullException.ThrowIfNull(source, nameof(source));
MasaArgumentException.ThrowIfNull(source);

return source.Adapt<TSource, TDestination>(_provider.GetConfig(source.GetType(), typeof(TDestination), options));
}

public TDestination Map<TDestination>(object source, MapOptions? options = null)
{
ArgumentNullException.ThrowIfNull(source, nameof(source));
MasaArgumentException.ThrowIfNull(source);

return source.Adapt<TDestination>(_provider.GetConfig(source.GetType(), typeof(TDestination), options));
}

public TDestination Map<TSource, TDestination>(TSource source, TDestination destination, MapOptions? options = null)
{
ArgumentNullException.ThrowIfNull(source, nameof(source));
MasaArgumentException.ThrowIfNull(source);

Type destinationType = destination?.GetType() ?? typeof(TDestination);
return source.Adapt<TSource, TDestination>(destination, _provider.GetConfig(source.GetType(), destinationType, options));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace System;

public static class ByteExtensions
{
public static string ToBase64String(this byte[] inArray)
=> Convert.ToBase64String(inArray);

public static string ConvertToString(this byte[] inArray, Encoding encoding)
=> encoding.GetString(inArray);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Utils.Security.Cryptography;
// ReSharper disable once CheckNamespace

namespace System;

public enum FillType
{
NoFile = 1,

/// <summary>
/// left fill
/// </summary>
Left = 2,

/// <summary>
/// right fill
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace System.Text.Json;
/// <remarks>
/// It requires a reference to the "System.Linq.Expressions" assembly.
/// </remarks>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[ExcludeFromCodeCoverage]
public static class JsonSerializerExtensions
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// ReSharper disable once CheckNamespace

namespace System;
namespace System.Reflection;

public static class MethodInfoExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace System;

public static class ObjectExtensions
{
public static string GetGenericTypeName(this object @object)
=> @object.GetType().GetGenericTypeName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace System.IO;

public static class StreamExtensions
{
public static byte[] ConvertToBytes(this Stream stream)
{
if (!stream.CanRead) return Array.Empty<byte>();

if (!stream.CanSeek)
{
stream.Seek(0, SeekOrigin.Begin);
}

var bytes = new byte[stream.Length];
_ = stream.Read(bytes, 0, bytes.Length);
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}

public static async Task<byte[]> ConvertToBytesAsync(this Stream stream)
{
if (!stream.CanRead) return Array.Empty<byte>();

if (!stream.CanSeek)
{
stream.Seek(0, SeekOrigin.Begin);
}

var bytes = new Memory<byte>(new byte[stream.Length]);
_ = await stream.ReadAsync(bytes);
stream.Seek(0, SeekOrigin.Begin);
return bytes.ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? value)
public static bool IsNullOrEmpty([NotNullWhen(false)] this string? value)
=> string.IsNullOrEmpty(value);

public static void CheckIsNullOrWhiteSpace(this string? value, [CallerArgumentExpression("value")] string? paramName = null)
{
if (value.IsNullOrWhiteSpace())
throw new ArgumentException($"{paramName} cannot be WhiteSpace or Null");
}

public static void CheckIsNullOrEmpty(this string? value, [CallerArgumentExpression("value")] string? paramName = null)
{
if (value.IsNullOrEmpty())
throw new ArgumentException($"{paramName} cannot be Empty or Null");
}

public static string TrimStart(this string value, string trimParameter)
=> value.TrimStart(trimParameter, StringComparison.CurrentCulture);

Expand All @@ -50,4 +38,32 @@ public static string TrimEnd(this string value,

return value.Substring(0, value.Length - trimParameter.Length);
}

public static byte[] ConvertToBytes(this string value, Encoding encoding)
=> encoding.GetBytes(value);

public static byte[] FromBase64String(this string value)
=> Convert.FromBase64String(value);

public static string GetSpecifiedLengthString(
this string value,
int length,
Action action,
FillType fillType = FillType.NoFile,
char fillCharacter = ' ')
{
if (fillType == FillType.NoFile && value.Length < length)
action.Invoke();

var keyLength = value.Length;
if (keyLength == length) return value;

if (keyLength > length) return value.Substring(0, length);

if (fillType == FillType.Left) return value.PadLeft(length, fillCharacter);

if (fillType == FillType.Right) return value.PadRight(length, fillCharacter);

throw new NotSupportedException($"... Unsupported {nameof(fillType)}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,47 @@ public static string GetGenericTypeName(this Type type)
return typeName;
}

public static string GetGenericTypeName(this object @object)
=> @object.GetType().GetGenericTypeName();

public static bool IsNullableType(this Type type)
=> type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);

/// <summary>
/// Determine if a class is derived from a generic class
/// todo: The restrictions on type and genericType will be removed in the future
/// </summary>
/// <param name="type">common class, Generic classes are not supported</param>
/// <param name="genericType">generic class, Only interface generics are supported</param>
/// <param name="type"></param>
/// <param name="genericType">base class type, Only interface generics are supported</param>
/// <returns></returns>
public static bool IsImplementerOfGeneric(this Type type, Type genericType)
{
if (!genericType.GetTypeInfo().IsGenericType || !type.IsClass)
if (!genericType.GetTypeInfo().IsGenericType)
return false;

if (genericType.IsInterface)
return type.IsImplementerOfGenericInterface(genericType);

return type.IsImplementerOfGenericClass(genericType);
}

private static bool IsImplementerOfGenericInterface(this Type type, Type genericType)
{
return type.GetInterfaces().Any(interfaceType =>
{
var current = interfaceType.GetTypeInfo().IsGenericType ?
interfaceType.GetGenericTypeDefinition() : interfaceType;
return current == genericType;
});
}

private static bool IsImplementerOfGenericClass(this Type type, Type genericType)
{
var currentType = type.GetTypeInfo().IsGenericType ?
type.GetGenericTypeDefinition() : type;
if (currentType == genericType)
return true;

var baseType = currentType.BaseType;
if (baseType == null)
return false;

return baseType.IsImplementerOfGenericClass(genericType);
}
}
Loading