Refactor search

This commit is contained in:
Cj 2024-06-12 22:31:41 -04:00
parent 9ab953b005
commit 19e711ebee
4 changed files with 76 additions and 41 deletions

View File

@ -31,12 +31,10 @@ internal class SearchParams
public bool? HasGenericParameters { get; set; } = null; public bool? HasGenericParameters { get; set; } = null;
public List<string> MatchMethods { get; set; } public List<string> MatchMethods { get; set; }
public List<string> IgnoreMethods { get; set; } public List<string> IgnoreMethods { get; set; }
public List<string> MatchFields { get; set; } public List<string> MatchFields { get; set; }
public List<string> IgnoreFields { get; set; } public List<string> IgnoreFields { get; set; }
public List<string> MatchProperties { get; set; } public List<string> MatchProperties { get; set; }
public List<string> IgnorePropterties { get; set; } public List<string> IgnorePropterties { get; set; }
public List<string> MatchNestedTypes { get; set; } public List<string> MatchNestedTypes { get; set; }
public List<string> IgnoreNestedTypes { get; set; } public List<string> IgnoreNestedTypes { get; set; }

View File

@ -153,9 +153,8 @@ internal class Remapper
if (type.Name != remap.OriginalTypeName) { return; } if (type.Name != remap.OriginalTypeName) { return; }
var oldName = type.Name; var oldName = type.Name;
type.Name = remap.NewTypeName;
remap.OriginalTypeName = type.Name; remap.OriginalTypeName = type.Name;
type.Name = remap.NewTypeName;
Logger.Log("-----------------------------------------------", ConsoleColor.Green); Logger.Log("-----------------------------------------------", ConsoleColor.Green);
Logger.Log($"Renamed {oldName} to {type.Name} directly", ConsoleColor.Green); Logger.Log($"Renamed {oldName} to {type.Name} directly", ConsoleColor.Green);

View File

@ -174,44 +174,38 @@ internal static class SearchExtentions
public static EMatchResult MatchMethods(this TypeDefinition type, SearchParams parms, ScoringModel score) public static EMatchResult MatchMethods(this TypeDefinition type, SearchParams parms, ScoringModel score)
{ {
// We're not searching for methods and this type contains methods // We're not searching for methods and this type contains methods
if (parms.MatchMethods.Count == 0 && parms.IgnoreMethods.Count == 0) if (parms.MatchMethods.Count is 0 && parms.IgnoreMethods.Count is 0)
{ {
return type.HasMethods return EMatchResult.Disabled;
? EMatchResult.NoMatch
: EMatchResult.Match;
} }
var skippAll = parms.IgnoreMethods.Contains("*"); var skippAll = parms.IgnoreMethods.Contains("*");
var methodCount = type.Methods.Count - type.GetConstructors().Count();
// The type has methods and we dont want any // Subtract method count from constructor count to check for real methods
if (type.HasMethods is true && skippAll is true) if (methodCount > 0 && skippAll is true)
{ {
foreach (var method in type.Methods) // Type has methods, we dont want any
{
if (method.Name == ".ctor")
{
continue;
}
score.Score--;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
score.Score++; // Handle Ignore methods
return EMatchResult.Match; foreach (var method in type.Methods)
{
if (parms.IgnoreMethods.Contains(method.Name))
{
// Contains blacklisted method, no match
score.FailureReason = EFailureReason.HasMethods;
score.Score--;
return EMatchResult.NoMatch;
}
} }
var matchCount = 0; var matchCount = 0;
// Handle match methods
foreach (var method in type.Methods) foreach (var method in type.Methods)
{ {
// Type contains a method we dont want
if (parms.IgnoreMethods.Contains(method.Name))
{
score.FailureReason = EFailureReason.HasMethods;
return EMatchResult.NoMatch;
}
foreach (var name in parms.MatchMethods) foreach (var name in parms.MatchMethods)
{ {
// Method name match // Method name match
@ -228,15 +222,17 @@ internal static class SearchExtentions
public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score) public static EMatchResult MatchFields(this TypeDefinition type, SearchParams parms, ScoringModel score)
{ {
if (parms.MatchFields.Count == 0 && parms.IgnoreFields.Count == 0) if (parms.MatchFields.Count is 0 && parms.IgnoreFields.Count is 0)
{ {
return EMatchResult.Disabled; return EMatchResult.Disabled;
} }
// `*` is the wildcard to ignore all fields that exist on types var skippAll = parms.IgnoreFields.Contains("*");
if (!type.HasFields && parms.IgnoreFields.Contains("*"))
// Type has fields, we dont want any
if (type.HasFields is true && skippAll is true)
{ {
return EMatchResult.Match; return EMatchResult.NoMatch;
} }
int matchCount = 0; int matchCount = 0;
@ -249,7 +245,10 @@ internal static class SearchExtentions
score.FailureReason = EFailureReason.HasFields; score.FailureReason = EFailureReason.HasFields;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
}
foreach (var field in type.Fields)
{
if (parms.MatchFields.Contains(field.Name)) if (parms.MatchFields.Contains(field.Name))
{ {
matchCount++; matchCount++;
@ -262,19 +261,18 @@ internal static class SearchExtentions
public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score) public static EMatchResult MatchProperties(this TypeDefinition type, SearchParams parms, ScoringModel score)
{ {
if (parms.MatchProperties.Count == 0 && parms.IgnorePropterties.Count == 0) if (parms.MatchProperties.Count is 0 && parms.IgnorePropterties.Count is 0)
{ {
return EMatchResult.Disabled; return EMatchResult.Disabled;
} }
// `*` is the wildcard to ignore all fields that exist on types var skippAll = parms.IgnorePropterties.Contains("*");
if (!type.HasProperties && parms.IgnorePropterties.Contains("*"))
{
score.Score++;
return EMatchResult.Match;
}
int matchCount = 0; // Type has fields, we dont want any
if (type.HasProperties is true && skippAll is true)
{
return EMatchResult.NoMatch;
}
foreach (var property in type.Properties) foreach (var property in type.Properties)
{ {
@ -284,7 +282,12 @@ internal static class SearchExtentions
score.FailureReason = EFailureReason.HasProperties; score.FailureReason = EFailureReason.HasProperties;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
}
int matchCount = 0;
foreach (var property in type.Properties)
{
if (parms.MatchProperties.Contains(property.Name)) if (parms.MatchProperties.Contains(property.Name))
{ {
matchCount++; matchCount++;
@ -302,15 +305,15 @@ internal static class SearchExtentions
return EMatchResult.Disabled; return EMatchResult.Disabled;
} }
var skippAll = parms.IgnorePropterties.Contains("*");
// `*` is the wildcard to ignore all fields that exist on types // `*` is the wildcard to ignore all fields that exist on types
if (type.HasNestedTypes && parms.IgnoreNestedTypes.Contains("*")) if (type.HasNestedTypes is true && skippAll is true)
{ {
score.FailureReason = EFailureReason.HasNestedTypes; score.FailureReason = EFailureReason.HasNestedTypes;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
int matchCount = 0;
foreach (var nestedType in type.NestedTypes) foreach (var nestedType in type.NestedTypes)
{ {
if (parms.IgnoreNestedTypes.Contains(nestedType.Name)) if (parms.IgnoreNestedTypes.Contains(nestedType.Name))
@ -319,7 +322,12 @@ internal static class SearchExtentions
score.FailureReason = EFailureReason.HasNestedTypes; score.FailureReason = EFailureReason.HasNestedTypes;
return EMatchResult.NoMatch; return EMatchResult.NoMatch;
} }
}
int matchCount = 0;
foreach (var nestedType in type.NestedTypes)
{
if (parms.MatchNestedTypes.Contains(nestedType.Name)) if (parms.MatchNestedTypes.Contains(nestedType.Name))
{ {
matchCount++; matchCount++;

View File

@ -53,6 +53,19 @@ internal static class DataProvider
ScoringModels = []; ScoringModels = [];
Remaps = JsonConvert.DeserializeObject<HashSet<RemapModel>>(jsonText); Remaps = JsonConvert.DeserializeObject<HashSet<RemapModel>>(jsonText);
var properties = typeof(SearchParams).GetProperties();
foreach (var remap in Remaps)
{
foreach (var property in properties)
{
if (property.PropertyType == typeof(List<string>) && property.GetValue(remap.SearchParams) is null)
{
property.SetValue(remap.SearchParams, new List<string>());
}
}
}
} }
public static void UpdateMapping() public static void UpdateMapping()
@ -68,6 +81,23 @@ internal static class DataProvider
Formatting = Formatting.Indented Formatting = Formatting.Indented
}; };
var properties = typeof(SearchParams).GetProperties();
foreach (var remap in Remaps)
{
foreach (var property in properties)
{
if (property.PropertyType == typeof(List<string>))
{
var val = property.GetValue(remap.SearchParams);
if (val is List<string> list && list.Count > 0) { continue; }
property.SetValue(remap.SearchParams, null);
}
}
}
var jsonText = JsonConvert.SerializeObject(Remaps, settings); var jsonText = JsonConvert.SerializeObject(Remaps, settings);
File.WriteAllText(AppSettings.MappingPath, jsonText); File.WriteAllText(AppSettings.MappingPath, jsonText);