Skip to content

Commit

Permalink
Refactoring generator
Browse files Browse the repository at this point in the history
  • Loading branch information
usercode committed Jul 26, 2024
1 parent d87f1e7 commit 798efad
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 26 deletions.
13 changes: 0 additions & 13 deletions src/ImageWizard.Core/Middlewares/ImageWizardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public static class ImageWizardExtensions
/// <summary>
/// Maps ImageWizard endpoint with specified base path.
/// </summary>
/// <param name="endpoints"></param>
/// <param name="path"></param>
/// <returns></returns>
public static IEndpointConventionBuilder MapImageWizard(this IEndpointRouteBuilder endpoints, string path = ImageWizardDefaults.BasePath)
{
return endpoints
Expand All @@ -30,9 +27,6 @@ public static IEndpointConventionBuilder MapImageWizard(this IEndpointRouteBuild
/// <summary>
/// Use ImageWizard middleware with default base path. ("/image")
/// </summary>
/// <param name="builder"></param>
/// <param name="endpointsHandler"></param>
/// <returns></returns>
public static IApplicationBuilder UseImageWizard(this IApplicationBuilder builder, Action<IImageWizardEndpointBuilder>? endpointsHandler = null)
{
return UseImageWizard(builder, ImageWizardDefaults.BasePath, endpointsHandler);
Expand All @@ -41,10 +35,6 @@ public static IApplicationBuilder UseImageWizard(this IApplicationBuilder builde
/// <summary>
/// Use ImageWizard middleware with specified base path.
/// </summary>
/// <param name="builder"></param>
/// <param name="path"></param>
/// <param name="endpointsHandler"></param>
/// <returns></returns>
public static IApplicationBuilder UseImageWizard(this IApplicationBuilder builder, string path, Action<IImageWizardEndpointBuilder>? endpointsHandler = null)
{
builder.Map(path, x =>
Expand Down Expand Up @@ -72,9 +62,6 @@ public static IApplicationBuilder UseImageWizard(this IApplicationBuilder builde
/// <see cref="IUrlSignature"/> -> <see cref="HMACSHA256UrlSignature"/><br/>
/// <see cref="IStreamPool"/> -> <see cref="RecyclableMemoryStreamPool"/><br/>
/// </summary>
/// <param name="services"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IImageWizardBuilder AddImageWizard(this IServiceCollection services, Action<ImageWizardOptions>? options = null)
{
if (options != null)
Expand Down
9 changes: 4 additions & 5 deletions src/ImageWizard.Core/Pipelines/Factory/FilterAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
// MIT License

using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;

namespace ImageWizard.Processing;

public class FilterAction<TFilter> : IFilterAction
where TFilter : IFilter
{
public delegate void FilterItemHandler(TFilter filer, GroupCollection groups);
public delegate void FilterActionHandler(TFilter filer, GroupCollection groups);

public FilterAction(string name, [StringSyntax(StringSyntaxAttribute.Regex)]string pattern, FilterItemHandler handler)
public FilterAction(string name, Regex pattern, FilterActionHandler handler)
{
Name = name;
Regex = new Regex(pattern);
Regex = pattern;
Handler = handler;
}

Expand All @@ -33,7 +32,7 @@ public FilterAction(string name, [StringSyntax(StringSyntaxAttribute.Regex)]stri
/// <summary>
/// Handler
/// </summary>
public FilterItemHandler Handler { get; }
public FilterActionHandler Handler { get; }

public bool TryExecute(IServiceProvider serviceProvider, string input, FilterContext filterContext)
{
Expand Down
4 changes: 1 addition & 3 deletions src/ImageWizard.Core/Pipelines/Pipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,8 @@ public async Task<DataResult> StartAsync(PipelineContext context)
preProcessing?.Invoke(filterContext);

//execute filters
while (context.UrlFilters.Count > 0)
while (context.UrlFilters.TryPeek(out FilterSegment segment))
{
FilterSegment segment = context.UrlFilters.Peek();

if (FilterFactories.TryGetValue(segment.Name, out var filters))
{
//find and execute filter
Expand Down
10 changes: 5 additions & 5 deletions src/ImageWizard.Generators/FilterGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void Generate(SourceProductionContext context, ImmutableArray<GeneratorA
string pattern = CreateParameterRegex(methodSymbol);
string parser = CreateParameterParser(methodSymbol);
x.AppendLine($"new FilterAction<{group.Key.Identifier.Text}>(\"{method.TargetSymbol.Name.ToLower()}\", \"{pattern}\", (filter, group) => {{ {parser} }}),");
x.AppendLine($"new FilterAction<{group.Key.Identifier.Text}>(\"{method.TargetSymbol.Name.ToLower()}\", new Regex(@\"{pattern}\"), (filter, group) => {{ {parser} }}),");
}
x.AppendLine("];");
});
Expand Down Expand Up @@ -99,15 +99,15 @@ private string CreateParameterRegex(IMethodSymbol methodSymbol)
or SpecialType.System_Int16
or SpecialType.System_Int32
or SpecialType.System_Int64
=> @"-?\\d+",
=> @"-?\d+",

SpecialType.System_Single
or SpecialType.System_Double
or SpecialType.System_Decimal
=> @"-?\\d+\\.\\d+",
=> @"-?\d+\.\d+",

SpecialType.System_Boolean => "True|False",
SpecialType.System_String => @"(('[^']*')|([A-Za-z0-9-_\\s]+))",
SpecialType.System_String => @"(('[^']*')|([A-Za-z0-9-_\s]+))",

_ => throw new Exception()
};
Expand All @@ -120,7 +120,7 @@ or SpecialType.System_Decimal
});
}

return $@"^\\({string.Join(",", parameterItems.Select(x => x.Pattern).ToArray())}\\)$";
return $@"^\({string.Join(",", parameterItems.Select(x => x.Pattern).ToArray())}\)$";
}

private string CreateParameterParser(IMethodSymbol methodSymbol)
Expand Down

0 comments on commit 798efad

Please sign in to comment.