-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added extensible method filtering using MethodFilterFactory
- Loading branch information
Donn Relacion
committed
Jul 4, 2022
1 parent
36ff958
commit fdfe28a
Showing
9 changed files
with
204 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) almostchristian. All rights reserved. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace ConfigurationProcessor.Core | ||
{ | ||
/// <summary> | ||
/// Contains method filter factories. | ||
/// </summary> | ||
public static class MethodFilterFactories | ||
{ | ||
private static bool DefaultMethodFilter(MethodInfo method, string name) | ||
=> true; | ||
|
||
/// <summary> | ||
/// Method filter factory that accepts methods with names like '<paramref name="name"/>' or 'Add<paramref name="name"/>'. | ||
/// </summary> | ||
/// <param name="name">The configuration name to search for.</param> | ||
/// <returns>The method filter and candidate names.</returns> | ||
public static (MethodFilter Filter, IEnumerable<string> CandidateNames) DefaultMethodFilterFactory(string name) | ||
{ | ||
var candidates = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { name, $"Add{name}" }; | ||
return (DefaultMethodFilter, candidates); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a method filter factory with suffixes. | ||
/// </summary> | ||
/// <param name="methodNameSuffixes">The method name suffixes to search for.</param> | ||
/// <returns>The method filter factory.</returns> | ||
public static MethodFilterFactory WithSuffixes(params string[] methodNameSuffixes) | ||
=> WithSuffixes(DefaultMethodFilter, methodNameSuffixes); | ||
|
||
/// <summary> | ||
/// Creates a method filter factory with suffixes. | ||
/// </summary> | ||
/// <param name="methodFilter">The default method filter factory.</param> | ||
/// <param name="methodNameSuffixes">The method name suffixes to search for.</param> | ||
/// <returns>The method filter factory.</returns> | ||
public static MethodFilterFactory WithSuffixes(MethodFilter methodFilter, params string[] methodNameSuffixes) | ||
=> WithPrefixAndSuffixes(methodFilter, new[] { "Add" }, methodNameSuffixes); | ||
|
||
/// <summary> | ||
/// Creates a method filter factory with prefixes and suffixes. | ||
/// </summary> | ||
/// <param name="methodFilter">The default method filter factory.</param> | ||
/// <param name="methodNamePrefixes">The method name suffixes to search for.</param> | ||
/// <param name="methodNameSuffixes">The method name suffixes to search for.</param> | ||
/// <returns>The method filter factory.</returns> | ||
public static MethodFilterFactory WithPrefixAndSuffixes(MethodFilter methodFilter, string[] methodNamePrefixes, string[] methodNameSuffixes) | ||
{ | ||
return name => | ||
{ | ||
var candidates = GetCandidateNames(name, methodNamePrefixes, methodNameSuffixes); | ||
return (methodFilter, candidates); | ||
}; | ||
|
||
static List<string> GetCandidateNames(string name, string[] methodNamePrefixes, string[] candidateSuffixes) | ||
{ | ||
const char GenericTypeMarker = '`'; | ||
var namesplit = name.Split(GenericTypeMarker); | ||
|
||
var result = new List<string> { name }; | ||
|
||
if (candidateSuffixes.Length > 0) | ||
{ | ||
if (namesplit.Length > 1) | ||
{ | ||
result.AddRange(candidateSuffixes.Select(x => $"{namesplit[0] + x}{GenericTypeMarker}{namesplit[1]}")); | ||
} | ||
else | ||
{ | ||
result.AddRange(candidateSuffixes.Select(x => name + x)); | ||
} | ||
} | ||
|
||
var withPrefix = result.SelectMany(y => methodNamePrefixes.Select(x => x + y)).ToList(); | ||
result.AddRange(withPrefix); | ||
|
||
return result; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) almostchristian. All rights reserved. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace ConfigurationProcessor.Core | ||
{ | ||
/// <summary> | ||
/// Delegate for filtering a candidate method. | ||
/// </summary> | ||
/// <param name="methodInfo">The method to evaluate.</param> | ||
/// <param name="name">The configuration name from the method filter factory.</param> | ||
/// <returns>True if the method is acceptable.</returns> | ||
public delegate bool MethodFilter(MethodInfo methodInfo, string name); | ||
|
||
/// <summary> | ||
/// Creates method filters. See <see cref="MethodFilterFactories"/> for generating factories. | ||
/// </summary> | ||
/// <param name="name">The configuration name.</param> | ||
/// <returns>Returns the method filter and the candidate names.</returns> | ||
public delegate (MethodFilter Filter, IEnumerable<string> CandidateNames) MethodFilterFactory(string name); | ||
} |
Oops, something went wrong.