Current auto mapper work

This commit is contained in:
Cj 2024-06-15 19:13:52 -04:00
parent bfd86d7f2f
commit 88373b084e
3 changed files with 51 additions and 23 deletions

View File

@ -9,26 +9,10 @@ public class ReCodeItAutoMapper
{
private List<MappingPair> MappingPairs { get; set; } = [];
private List<string> CompilerGeneratedClasses = [];
private AutoMapperSettings Settings => DataProvider.Settings.AutoMapper;
private static readonly List<string> SystemTypeIgnoreList = new()
{
"Boolean",
"List",
"Dictionary",
"Byte",
"Int16",
"Int36",
"Func",
"Action"
};
private List<string> TokensToMatch = new()
{
"Class",
"GClass"
};
public void InitializeAutoMapping()
{
Logger.ClearLog();
@ -36,8 +20,15 @@ public class ReCodeItAutoMapper
// Clear any previous pairs
MappingPairs = [];
CompilerGeneratedClasses = [];
foreach (var type in DataProvider.ModuleDefinition.Types)
FindCompilerGeneratedObjects(DataProvider.ModuleDefinition.Types);
Logger.Log($"Found {CompilerGeneratedClasses.Count} Compiler generated objects");
var types = DataProvider.ModuleDefinition.Types;
foreach (var type in types)
{
MappingPairs.AddRange(FilterFieldNames(type));
MappingPairs.AddRange(FilterPropertyNames(type));
@ -46,6 +37,28 @@ public class ReCodeItAutoMapper
FilterTypeNames();
}
private void FindCompilerGeneratedObjects(Mono.Collections.Generic.Collection<TypeDefinition> types)
{
foreach (var typeDefinition in types)
{
if (typeDefinition.IsClass || typeDefinition.IsInterface || typeDefinition.IsValueType) // Check for class or struct
{
if (typeDefinition.HasCustomAttributes &&
typeDefinition.CustomAttributes.Any(attr => attr.AttributeType.FullName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute"))
{
string typeName = typeDefinition.Name;
CompilerGeneratedClasses.Add(typeName);
//Logger.Log($"Compiler Generated object found: {typeName}", ConsoleColor.Yellow);
}
}
if (typeDefinition.NestedTypes.Count > 0)
{
FindCompilerGeneratedObjects(typeDefinition.NestedTypes);
}
}
}
/// <summary>
/// Pair field declaring types with their names
/// </summary>
@ -55,6 +68,12 @@ public class ReCodeItAutoMapper
{
var fieldsWithTypes = new List<MappingPair>();
if (CompilerGeneratedClasses.Contains(type.Name))
{
//Logger.Log($"Skipping over compiler generated object: {type.Name}");
return fieldsWithTypes;
}
// Handle nested types recursively
foreach (var nestedType in type.NestedTypes)
{
@ -91,6 +110,12 @@ public class ReCodeItAutoMapper
{
var propertiesWithTypes = new List<MappingPair>();
if (CompilerGeneratedClasses.Contains(type.Name))
{
//Logger.Log($"Skipping over compiler generated object: {type.Name}");
return propertiesWithTypes;
}
// Handle nested types recursively
foreach (var nestedType in type.NestedTypes)
{

View File

@ -1,8 +1,8 @@
using ReCodeIt.Enums;
using Mono.Cecil;
using Mono.Cecil.Rocks;
using ReCodeIt.Enums;
using ReCodeIt.Models;
using ReCodeIt.Utils;
using Mono.Cecil;
using Mono.Cecil.Rocks;
namespace ReCodeIt.ReMapper.Search;
@ -177,6 +177,7 @@ internal static class TypeDefExtensions
{
return EMatchResult.Disabled;
}
var attrs = type.CustomAttributes;
if (type.HasCustomAttributes == parms.HasAttribute)
{

View File

@ -37,7 +37,9 @@
],
"TokensToMatch": [ // The auto mapper will look for these tokens in class names and prioritize those
"Class",
"GClass"
"GClass",
"GStruct",
"GInterface"
],
"PropertyFieldBlackList": [ // Property or fields names to ignore in the automap, these are case sanitized so case does not matter
"Columns",