Browse Source

Added `AuditLogEntityTypeFullNameConverter`.

pull/21873/head
maliming 1 year ago
parent
commit
1806e30b59
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 39
      modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogEntityTypeFullNameConverter.cs
  2. 18
      modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogInfoToAuditLogConverter.cs
  3. 6
      modules/audit-logging/test/Volo.Abp.AuditLogging.EntityFrameworkCore.Tests/Volo/Abp/AuditLogging/EntityFrameworkCore/AuditLogEntityTypeFullNameConverter_Tests.cs
  4. 9
      modules/audit-logging/test/Volo.Abp.AuditLogging.MongoDB.Tests/Volo/Abp/AuditLogging/MongoDB/AuditLogEntityTypeFullNameConverter_Tests.cs
  5. 46
      modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogEntityTypeFullNameConverter_Tests.cs

39
modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogEntityTypeFullNameConverter.cs

@ -0,0 +1,39 @@
using System;
using System.Text.RegularExpressions;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AuditLogging;
public class AuditLogEntityTypeFullNameConverter : ITransientDependency
{
public virtual string Convert(string typeFullName)
{
var genericType = Regex.Match(typeFullName, @"(.+?)`1\[\[");
if (!genericType.Success)
{
return ReplaceGenericSymbol(typeFullName);
}
var type = Regex.Match(typeFullName, @"`1\[\[(.+?), ");
if (!type.Success)
{
return typeFullName;
}
if (type.Groups[1].Value.Contains("System.Nullable`1[["))
{
return genericType.Groups[1].Value + "<" + type.Groups[1].Value.Replace("System.Nullable`1[[", "") + "?>";
}
return genericType.Groups[1].Value.Contains("System.Nullable")
? type.Groups[1].Value + "?"
: genericType.Groups[1].Value + "<" + ReplaceGenericSymbol(type.Groups[1].Value) + ">";
}
protected virtual string ReplaceGenericSymbol(string typeFullName)
{
return typeFullName.Contains("`1+")
? typeFullName.Substring(0, typeFullName.IndexOf("[[", StringComparison.Ordinal)).Replace("`1+", ".")
: typeFullName;
}
}

18
modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogInfoToAuditLogConverter.cs

@ -19,12 +19,19 @@ public class AuditLogInfoToAuditLogConverter : IAuditLogInfoToAuditLogConverter,
protected IExceptionToErrorInfoConverter ExceptionToErrorInfoConverter { get; }
protected IJsonSerializer JsonSerializer { get; }
protected AbpExceptionHandlingOptions ExceptionHandlingOptions { get; }
protected AuditLogEntityTypeFullNameConverter AuditLogEntityTypeFullNameConverter { get; }
public AuditLogInfoToAuditLogConverter(IGuidGenerator guidGenerator, IExceptionToErrorInfoConverter exceptionToErrorInfoConverter, IJsonSerializer jsonSerializer, IOptions<AbpExceptionHandlingOptions> exceptionHandlingOptions)
public AuditLogInfoToAuditLogConverter(
IGuidGenerator guidGenerator,
IExceptionToErrorInfoConverter exceptionToErrorInfoConverter,
IJsonSerializer jsonSerializer,
IOptions<AbpExceptionHandlingOptions> exceptionHandlingOptions,
AuditLogEntityTypeFullNameConverter auditLogEntityTypeFullNameConverter)
{
GuidGenerator = guidGenerator;
ExceptionToErrorInfoConverter = exceptionToErrorInfoConverter;
JsonSerializer = jsonSerializer;
AuditLogEntityTypeFullNameConverter = auditLogEntityTypeFullNameConverter;
ExceptionHandlingOptions = exceptionHandlingOptions.Value;
}
@ -41,6 +48,15 @@ public class AuditLogInfoToAuditLogConverter : IAuditLogInfoToAuditLogConverter,
}
}
foreach (var entityChange in auditLogInfo.EntityChanges ?? Enumerable.Empty<EntityChangeInfo>())
{
entityChange.EntityTypeFullName = AuditLogEntityTypeFullNameConverter.Convert(entityChange.EntityTypeFullName);
foreach (var propertyChange in entityChange.PropertyChanges ?? Enumerable.Empty<EntityPropertyChangeInfo>())
{
propertyChange.PropertyTypeFullName = AuditLogEntityTypeFullNameConverter.Convert(propertyChange.PropertyTypeFullName);
}
}
var entityChanges = auditLogInfo
.EntityChanges?
.Select(entityChangeInfo => new EntityChange(GuidGenerator, auditLogId, entityChangeInfo, tenantId: auditLogInfo.TenantId))

6
modules/audit-logging/test/Volo.Abp.AuditLogging.EntityFrameworkCore.Tests/Volo/Abp/AuditLogging/EntityFrameworkCore/AuditLogEntityTypeFullNameConverter_Tests.cs

@ -0,0 +1,6 @@
namespace Volo.Abp.AuditLogging.EntityFrameworkCore;
public class AuditLogEntityTypeFullNameConverter_Tests : AuditLogEntityTypeFullNameConverter_Tests<AbpAuditLoggingEntityFrameworkCoreTestModule>
{
}

9
modules/audit-logging/test/Volo.Abp.AuditLogging.MongoDB.Tests/Volo/Abp/AuditLogging/MongoDB/AuditLogEntityTypeFullNameConverter_Tests.cs

@ -0,0 +1,9 @@
using Xunit;
namespace Volo.Abp.AuditLogging.MongoDB;
[Collection(MongoTestCollection.Name)]
public class AuditLogEntityTypeFullNameConverter_Tests : AuditLogEntityTypeFullNameConverter_Tests<AbpAuditLoggingMongoDbTestModule>
{
}

46
modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogEntityTypeFullNameConverter_Tests.cs

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Shouldly;
using Volo.Abp.Modularity;
using Xunit;
namespace Volo.Abp.AuditLogging;
public abstract class AuditLogEntityTypeFullNameConverter_Tests<TStartupModule> : AuditLoggingTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
private readonly AuditLogEntityTypeFullNameConverter _typeFullNameConverter;
protected AuditLogEntityTypeFullNameConverter_Tests()
{
_typeFullNameConverter = GetRequiredService<AuditLogEntityTypeFullNameConverter>();
}
[Fact]
public void AuditLogEntityTypeFullNameConverter_Test()
{
_typeFullNameConverter.Convert("MyType").ShouldBe("MyType");
_typeFullNameConverter.Convert(typeof(string).FullName!).ShouldBe("System.String");
_typeFullNameConverter.Convert(typeof(Guid).FullName!).ShouldBe("System.Guid");
_typeFullNameConverter.Convert(typeof(Guid?).FullName!).ShouldBe("System.Guid?");
_typeFullNameConverter.Convert(typeof(int).FullName!).ShouldBe("System.Int32");
_typeFullNameConverter.Convert(typeof(long?).FullName!).ShouldBe("System.Int64?");
_typeFullNameConverter.Convert(typeof(MyClass).FullName!).ShouldBe("Volo.Abp.AuditLogging.AuditLogEntityTypeFullNameConverter_Tests.MyClass");
_typeFullNameConverter.Convert(typeof(ICollection<string>).FullName!).ShouldBe($"System.Collections.Generic.ICollection<System.String>");
_typeFullNameConverter.Convert(typeof(Collection<int>).FullName!).ShouldBe($"System.Collections.ObjectModel.Collection<System.Int32>");
_typeFullNameConverter.Convert(typeof(List<Guid>).FullName!).ShouldBe($"System.Collections.Generic.List<System.Guid>");
_typeFullNameConverter.Convert(typeof(List<MyClass>).FullName!).ShouldBe($"System.Collections.Generic.List<Volo.Abp.AuditLogging.AuditLogEntityTypeFullNameConverter_Tests.MyClass>");
_typeFullNameConverter.Convert(typeof(ICollection<long?>).FullName!).ShouldBe($"System.Collections.Generic.ICollection<System.Int64?>");
_typeFullNameConverter.Convert(typeof(Collection<int?>).FullName!).ShouldBe($"System.Collections.ObjectModel.Collection<System.Int32?>");
_typeFullNameConverter.Convert(typeof(List<Guid?>).FullName!).ShouldBe($"System.Collections.Generic.List<System.Guid?>");
}
public class MyClass
{
}
}
Loading…
Cancel
Save