Skip to content

Commit

Permalink
v3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Tynab committed Nov 29, 2023
1 parent 574733e commit 713783f
Show file tree
Hide file tree
Showing 23 changed files with 120 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class JsonDeserializeBenchmark
public void Setup() => _json = new SampleModel
{
Id = NewGuid()
}.CamelSerialize();
}.Serialize();

[Benchmark(Baseline = true)]
public void YANLib_Json() => For(0, Size, index => _json!.Deserialize<SampleModel>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class JsonSerializeBenchmark
};

[Benchmark(Baseline = true)]
public void YANLib_Json() => For(0, Size, index => _model.Serialize());
public void YANLib_Json() => For(0, Size, index => _model.StandardSerialize());

[Benchmark]
public void Newtonsoft_Json() => For(0, Size, index => SerializeObject(_model));
Expand Down
4 changes: 2 additions & 2 deletions lib/YANLib/Ultimate/YANJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static IEnumerable<string> Serializes<T>(this IEnumerable<T> mdls)

foreach (var mdl in mdls)
{
yield return mdl.Serialize();
yield return mdl.StandardSerialize();
}
}

Expand All @@ -24,7 +24,7 @@ public static IEnumerable<string> CamelSerializes<T>(this IEnumerable<T> mdls)

foreach (var mdl in mdls)
{
yield return mdl.CamelSerialize();
yield return mdl.Serialize();
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/YANLib/YANBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace YANLib;

public static partial class YANBytes
{
public static byte[]? ToByteArray<T>(this T obj) => obj is null ? default : UTF8.GetBytes(obj.Serialize());
public static byte[]? ToByteArray<T>(this T obj) => obj is null ? default : UTF8.GetBytes(obj.StandardSerialize());

public static IEnumerable<byte[]?>? ToByteArray<T>(this IEnumerable<T> objs) => objs.IsEmptyOrNull() ? default : objs.Select(o => o.ToByteArray());

Expand Down
10 changes: 10 additions & 0 deletions lib/YANLib/YANEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using static System.Enum;

namespace YANLib;

public static partial class YANEnum
{
public static T? ToEnum<T>(this string value) where T : struct => value.IsWhiteSpaceOrNull() ? default : TryParse<T>(value, out var result) ? result : default;

public static IEnumerable<T?>? ToEnum<T>(this IEnumerable<string> values) where T : struct => values.IsEmptyOrNull() ? default : values.Select(s => s.ToEnum<T>());
}
11 changes: 6 additions & 5 deletions lib/YANLib/YANEnumerable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using static System.Math;
using System.Diagnostics.CodeAnalysis;
using static System.Math;
using static System.Nullable;
using static YANLib.YANNum;

Expand Down Expand Up @@ -68,13 +69,13 @@ public static IEnumerable<string> Clean(this IEnumerable<string> srcs)
}
}

public static bool IsEmptyOrNull<T>(this IEnumerable<T> srcs) => srcs is null || !srcs.Any();
public static bool IsEmptyOrNull<T>([NotNullWhen(false)] this IEnumerable<T> srcs) => srcs is null || !srcs.Any();

public static bool IsEmptyOrNull<T>(this ICollection<T> srcs) => srcs is null || srcs.Count < 1;
public static bool IsEmptyOrNull<T>([NotNullWhen(false)] this ICollection<T> srcs) => srcs is null || srcs.Count < 1;

public static bool IsNotEmptyAndNull<T>(this IEnumerable<T> srcs) => srcs is not null && srcs.Any();
public static bool IsNotEmptyAndNull<T>([NotNullWhen(true)] this IEnumerable<T> srcs) => srcs is not null && srcs.Any();

public static bool IsNotEmptyAndNull<T>(this ICollection<T> srcs) => srcs is not null && srcs.Count > 0;
public static bool IsNotEmptyAndNull<T>([NotNullWhen(true)] this ICollection<T> srcs) => srcs is not null && srcs.Count > 0;

public static bool AllEmptyOrNull<T>(this IEnumerable<T?> srcs) where T : class => !srcs.Any(x => x is not null || x.AnyPropertiesNotDefault());

Expand Down
58 changes: 46 additions & 12 deletions lib/YANLib/YANJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,83 @@ namespace YANLib;

public static partial class YANJson
{
private static readonly JsonSerializerOptions IsPropNameCaseInsJsonSerializerOpt = new()
private static readonly JsonSerializerOptions IsPropertyNameCaseInsensitive = new()
{
PropertyNameCaseInsensitive = true
};

private static readonly JsonSerializerOptions PropNamingPolWkCaseJsonSerializerOpt = new()
private static readonly JsonSerializerOptions CamelCasePropertyNamingPolicy = new()
{
PropertyNamingPolicy = CamelCase
};

public static string Serialize<T>(this T mdl) => JsonSerializer.Serialize(mdl);
public static string StandardSerialize<T>(this T value, JsonSerializerOptions? options = null) => options is null ? JsonSerializer.Serialize(value) : JsonSerializer.Serialize(value, options);

public static IEnumerable<string>? Serializes<T>(this IEnumerable<T> mdls) => mdls.IsEmptyOrNull() ? default : mdls.Select(m => m.Serialize());
public static IEnumerable<string>? StandardSerializes<T>(this IEnumerable<T> values, JsonSerializerOptions? options = null) => values.IsEmptyOrNull()
? default
: options is null
? values.Select(m => m.StandardSerialize())
: values.Select(m => m.StandardSerialize(options));

public static string CamelSerialize<T>(this T mdl) => JsonSerializer.Serialize(mdl, PropNamingPolWkCaseJsonSerializerOpt);
public static string Serialize<T>(this T value, JsonSerializerOptions? options = null) => options is null ? JsonSerializer.Serialize(value, CamelCasePropertyNamingPolicy) : JsonSerializer.Serialize(value, options);

public static IEnumerable<string>? CamelSerializes<T>(this IEnumerable<T> mdls) => mdls.IsEmptyOrNull() ? default : mdls.Select(m => m.CamelSerialize());
public static IEnumerable<string>? Serializes<T>(this IEnumerable<T> values, JsonSerializerOptions? options = null) => values.IsEmptyOrNull()
? default
: options is null
? values.Select(m => m.Serialize())
: values.Select(m => m.Serialize(options));

public static T? StandardDeserialize<T>(this string str)
public static T? StandardDeserialize<T>(this string json, JsonSerializerOptions? options = null)
{
try
{
return JsonSerializer.Deserialize<T>(str);
return options is null ? JsonSerializer.Deserialize<T>(json) : JsonSerializer.Deserialize<T>(json, options);
}
catch
{
return default;
}
}

public static IEnumerable<T?>? StandardDeserializes<T>(this IEnumerable<string> strs) => strs.IsEmptyOrNull() ? default : strs.Select(s => s.StandardDeserialize<T>());
public static IEnumerable<T?>? StandardDeserializes<T>(this IEnumerable<string> jsons, JsonSerializerOptions? options = null) => jsons.IsEmptyOrNull()
? default
: options is null
? jsons.Select(s => s.StandardDeserialize<T>())
: jsons.Select(s => s.StandardDeserialize<T>(options));

public static T? Deserialize<T>(this string str)
public static T? Deserialize<T>(this string json, JsonSerializerOptions? options = null)
{
try
{
return JsonSerializer.Deserialize<T>(str, IsPropNameCaseInsJsonSerializerOpt);
return options is null ? JsonSerializer.Deserialize<T>(json, IsPropertyNameCaseInsensitive) : JsonSerializer.Deserialize<T>(json, options);
}
catch
{
return default;
}
}

public static IEnumerable<T?>? Deserializes<T>(this IEnumerable<string> strs) => strs.IsEmptyOrNull() ? default : strs.Select(s => s.Deserialize<T>());
public static IEnumerable<T?>? Deserializes<T>(this IEnumerable<string> jsons, JsonSerializerOptions? options = null) => jsons.IsEmptyOrNull()
? default
: options is null
? jsons.Select(s => s.Deserialize<T>())
: jsons.Select(s => s.Deserialize<T>(options));

public static T? Deserialize<T>(this byte[] utf8Json, JsonSerializerOptions? options = null)
{
try
{
return options is null ? JsonSerializer.Deserialize<T>(utf8Json, IsPropertyNameCaseInsensitive) : JsonSerializer.Deserialize<T>(utf8Json, options);
}
catch
{
return default;
}
}

public static IEnumerable<T?>? Deserializes<T>(this IEnumerable<byte[]> jsons, JsonSerializerOptions? options = null) => jsons.IsEmptyOrNull()
? default
: options is null
? jsons.Select(s => s.Deserialize<T>())
: jsons.Select(s => s.Deserialize<T>(options));
}
16 changes: 14 additions & 2 deletions lib/YANLib/YANLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@
<PackAsTool>False</PackAsTool>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Update model</PackageReleaseNotes>
<PackageReleaseNotes>Add:
- Enum

Update
- Text: check null attribute
- Json:
- byte[]
- Deserialize
- Deserializes
- StandardDeserialize
- StandardDeserializes
- CamelSerialize -&gt; StandardSerialize
- CamelSerializes -&gt; StandardSerializes</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<PackageId>Tynab.YANLib</PackageId>
<Version>3.1.9</Version>
<Version>3.2.0</Version>
<LangVersion>preview</LangVersion>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions lib/YANLib/YANProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static async Task KillAllProcessesByName(params string[] names)
{
if (names.AllNotWhiteSpaceAndNull())
{
await WhenAll(names.SelectMany(name => GetProcessesByName(name)).Select(p =>
await WhenAll(names.SelectMany(GetProcessesByName).Select(p =>
{
if (!p.CloseMainWindow())
{
Expand All @@ -42,7 +42,7 @@ public static async Task KillAllProcessesByName(this IEnumerable<string> names)
{
if (names.AllNotWhiteSpaceAndNull())
{
await WhenAll(names.SelectMany(name => GetProcessesByName(name)).Select(p =>
await WhenAll(names.SelectMany(GetProcessesByName).Select(p =>
{
if (!p.CloseMainWindow())
{
Expand Down
15 changes: 8 additions & 7 deletions lib/YANLib/YANText.String.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using static System.StringComparison;

namespace YANLib;

public static partial class YANText
{

public static bool IsNull(this string str) => str is null;
public static bool IsNull([NotNullWhen(false)] this string str) => str is null;

public static bool AllNull(params string[] strs) => strs is not null && !strs.Any(s => s.IsNotNull());

Expand All @@ -16,7 +17,7 @@ public static partial class YANText

public static bool AnyNull(this IEnumerable<string> strs) => strs is not null && strs.Any(s => s.IsNull());

public static bool IsEmptyOrNull(this string str) => string.IsNullOrEmpty(str);
public static bool IsEmptyOrNull([NotNullWhen(false)] this string str) => string.IsNullOrEmpty(str);

public static bool AllEmptyOrNull(params string[] strs) => strs is not null && !strs.Any(s => s.IsNotEmptyAndNull());

Expand All @@ -26,7 +27,7 @@ public static partial class YANText

public static bool AnyEmptyOrNull(this IEnumerable<string> strs) => strs is not null && strs.Any(s => s.IsEmptyOrNull());

public static bool IsWhiteSpaceOrNull(this string str) => string.IsNullOrWhiteSpace(str);
public static bool IsWhiteSpaceOrNull([NotNullWhen(false)] this string str) => string.IsNullOrWhiteSpace(str);

public static bool AllWhiteSpaceOrNull(params string[] strs) => strs is not null && !strs.Any(s => s.IsNotWhiteSpaceAndNull());

Expand All @@ -46,7 +47,7 @@ public static partial class YANText

public static bool AnyEqualsIgnoreCase(this IEnumerable<string> strs) => !strs.AllNotEqualsIgnoreCase();

public static bool IsNotNull(this string str) => str is not null;
public static bool IsNotNull([NotNullWhen(true)] this string str) => str is not null;

public static bool AllNotNull(params string[] strs) => strs is not null && !strs.Any(s => s.IsNull());

Expand All @@ -56,7 +57,7 @@ public static partial class YANText

public static bool AnyNotNull(this IEnumerable<string> strs) => strs is not null && strs.Any(s => s.IsNotNull());

public static bool IsNotEmptyAndNull(this string str) => !string.IsNullOrEmpty(str);
public static bool IsNotEmptyAndNull([NotNullWhen(true)] this string str) => !string.IsNullOrEmpty(str);

public static bool AllNotEmptyAndNull(params string[] strs) => strs is not null && !strs.Any(s => s.IsEmptyOrNull());

Expand All @@ -66,7 +67,7 @@ public static partial class YANText

public static bool AnyNotEmptyAndNull(this IEnumerable<string> strs) => strs is not null && strs.Any(s => s.IsNotEmptyAndNull());

public static bool IsNotWhiteSpaceAndNull(this string str) => !string.IsNullOrWhiteSpace(str);
public static bool IsNotWhiteSpaceAndNull([NotNullWhen(true)] this string str) => !string.IsNullOrWhiteSpace(str);

public static bool AllNotWhiteSpaceAndNull(params string[] strs) => strs is not null && !strs.Any(s => s.IsWhiteSpaceOrNull());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ await WhenAll((await _database.HashGetAllAsync(group.ToLowerInvariant())).Where(

public async ValueTask<bool> Set(string group, string key, DeveloperTypeRedisDto value)
{
var jsonVal = value.CamelSerialize();
var jsonVal = value.Serialize();

try
{
Expand All @@ -156,13 +156,13 @@ public async ValueTask<bool> SetBulk(string group, IDictionary<string, Developer
throw new BusinessException(BAD_REQUEST);
}

await _database.HashSetAsync(group.ToLowerInvariant(), fields.Select(p => new HashEntry(p.Key.ToLowerInvariant(), p.Value.CamelSerialize())).ToArray());
await _database.HashSetAsync(group.ToLowerInvariant(), fields.Select(p => new HashEntry(p.Key.ToLowerInvariant(), p.Value.Serialize())).ToArray());

return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "SetBulkDeveloperTypeRedisService-Exception: {Group} - {Fields}", group, fields.CamelSerialize());
_logger.LogError(ex, "SetBulkDeveloperTypeRedisService-Exception: {Group} - {Fields}", group, fields.Serialize());
throw;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/YANLib.Application/EsServices/DeveloperEsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async ValueTask<bool> Set(DeveloperIndex data)
}
catch (Exception ex)
{
_logger.LogError(ex, "SetDeveloperEsService-Exception: {Data}", data.CamelSerialize());
_logger.LogError(ex, "SetDeveloperEsService-Exception: {Data}", data.Serialize());
throw;
}
}
Expand Down Expand Up @@ -75,7 +75,7 @@ public async ValueTask<bool> SetBulk(List<DeveloperIndex> datas)
}
catch (Exception ex)
{
_logger.LogError(ex, "SetBulkDeveloperEsService-Exception: {Datas}", datas.CamelSerialize());
_logger.LogError(ex, "SetBulkDeveloperEsService-Exception: {Datas}", datas.Serialize());
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ICertificateService certificateService
#region Implements
public async Task HandleEventAsync(CertificateAdjustEto eventData)
{
_logger.LogInformation("AdjustCertificateHandler-Subscribe: {EventData}", eventData.CamelSerialize());
_logger.LogInformation("AdjustCertificateHandler-Subscribe: {EventData}", eventData.Serialize());
_logger.LogInformation("AdjustCertificateHandler-UpdateCertificateService: {Responses}", await _certificateService.Update(ObjectMapper.Map<CertificateAdjustEto, CertificateRequest>(eventData)));
}
#endregion
Expand Down
4 changes: 2 additions & 2 deletions src/YANLib.Application/Services/CertificateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async ValueTask<bool> Create(CertificateRequest request)
}
catch (Exception ex)
{
_logger.LogError(ex, "CreateCertificateService-Exception: {Request}", request.CamelSerialize());
_logger.LogError(ex, "CreateCertificateService-Exception: {Request}", request.Serialize());
throw;
}
}
Expand All @@ -39,7 +39,7 @@ public async ValueTask<bool> Update(CertificateRequest request)
}
catch (Exception ex)
{
_logger.LogError(ex, "UpdateCertificateService-Exception: {Request}", request.CamelSerialize());
_logger.LogError(ex, "UpdateCertificateService-Exception: {Request}", request.Serialize());
throw;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/YANLib.Application/Services/DeveloperService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public async ValueTask<DeveloperResponse> Create(DeveloperCreateRequest request)
var eto = ObjectMapper.Map<Certificate, CertificateCreateEto>(cert);

await _capPublisher.PublishAsync(CERT_CRT, eto);
_logger.LogInformation("InsertDeveloperService-Publish: {ETO}", eto.CamelSerialize());
_logger.LogInformation("InsertDeveloperService-Publish: {ETO}", eto.Serialize());
});

rslt.Certificates = new List<CertificateResponse>(ObjectMapper.Map<List<Certificate>, List<CertificateResponse>>(certs));
Expand All @@ -107,7 +107,7 @@ public async ValueTask<DeveloperResponse> Create(DeveloperCreateRequest request)
}
catch (Exception ex)
{
_logger.LogError(ex, "CreateDeveloperService-Exception: {Request}", request.CamelSerialize());
_logger.LogError(ex, "CreateDeveloperService-Exception: {Request}", request.Serialize());
throw;
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ public async ValueTask<DeveloperResponse> Adjust(string idCard, DeveloperUpdateR
var eto = ObjectMapper.Map<Certificate, CertificateCreateEto>(cert);

await _capPublisher.PublishAsync(CERT_CRT, eto);
_logger.LogInformation("AdjustDeveloperService-Publish: {ETO}", eto.CamelSerialize());
_logger.LogInformation("AdjustDeveloperService-Publish: {ETO}", eto.Serialize());
});

rslt.Certificates = new List<CertificateResponse>(ObjectMapper.Map<List<Certificate>, List<CertificateResponse>>(certs));
Expand All @@ -164,7 +164,7 @@ public async ValueTask<DeveloperResponse> Adjust(string idCard, DeveloperUpdateR
var eto = ObjectMapper.Map<CertificateResponse, CertificateAdjustEto>(x);

await _distributedEventBus.PublishAsync(eto);
_logger.LogInformation("AdjustDeveloperService-Publish: {ETO}", eto.CamelSerialize());
_logger.LogInformation("AdjustDeveloperService-Publish: {ETO}", eto.Serialize());
});

rslt.Certificates = new List<CertificateResponse>(mdl.Certificates);
Expand All @@ -174,7 +174,7 @@ public async ValueTask<DeveloperResponse> Adjust(string idCard, DeveloperUpdateR
}
catch (Exception ex)
{
_logger.LogError(ex, "AdjustDeveloperService-Exception: {IdCard} - {DTO}", idCard, dto.CamelSerialize());
_logger.LogError(ex, "AdjustDeveloperService-Exception: {IdCard} - {DTO}", idCard, dto.Serialize());
throw;
}
}
Expand Down
Loading

0 comments on commit 713783f

Please sign in to comment.