Skip to content

Commit

Permalink
Rename method extensions to registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Oct 15, 2024
1 parent e6c9fcc commit 690ccbb
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Modules/Controllers/Provider/ControllerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public IHandler Build(IHandler parent)

var instance = _Instance ?? throw new BuilderMissingPropertyException("Instance or Type");

var extensions = new MethodExtensions(serializers, injectors, formatters);
var extensions = new MethodRegistry(serializers, injectors, formatters);

return Concerns.Chain(parent, _Concerns, p => new ControllerHandler(p, instance, extensions));
}
Expand Down
18 changes: 9 additions & 9 deletions Modules/Controllers/Provider/ControllerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ public sealed partial class ControllerHandler : IHandler

private ResponseProvider ResponseProvider { get; }

private MethodExtensions Extensions { get; }
private MethodRegistry Registry { get; }

private object Instance { get; }

#endregion

#region Initialization

public ControllerHandler(IHandler parent, object instance, MethodExtensions extensions)
public ControllerHandler(IHandler parent, object instance, MethodRegistry registry)
{
Parent = parent;
Extensions = extensions;
Registry = registry;

Instance = instance;

ResponseProvider = new ResponseProvider(extensions);
ResponseProvider = new ResponseProvider(registry);

Provider = new MethodCollection(this, AnalyzeMethods(instance.GetType(), extensions));
Provider = new MethodCollection(this, AnalyzeMethods(instance.GetType(), registry));
}

private IEnumerable<Func<IHandler, MethodHandler>> AnalyzeMethods(Type type, MethodExtensions extensions)
private IEnumerable<Func<IHandler, MethodHandler>> AnalyzeMethods(Type type, MethodRegistry registry)
{
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
Expand All @@ -51,7 +51,7 @@ private IEnumerable<Func<IHandler, MethodHandler>> AnalyzeMethods(Type type, Met

var operation = CreateOperation(method, arguments);

yield return parent => new MethodHandler(parent, operation, () => Instance, annotation, ResponseProvider.GetResponseAsync, extensions);
yield return parent => new MethodHandler(parent, operation, () => Instance, annotation, ResponseProvider.GetResponseAsync, registry);
}
}

Expand All @@ -61,14 +61,14 @@ private Operation CreateOperation(MethodInfo method, List<string> arguments)

if (method.Name == "Index")
{
return OperationBuilder.Create(pathArguments.Length > 0 ? $"/{pathArguments}/" : null, method, Extensions,true);
return OperationBuilder.Create(pathArguments.Length > 0 ? $"/{pathArguments}/" : null, method, Registry,true);
}

var name = HypenCase(method.Name);

var path = $"/{name}";

return OperationBuilder.Create(pathArguments.Length > 0 ? $"{path}/{pathArguments}/" : $"{path}/", method, Extensions, true);
return OperationBuilder.Create(pathArguments.Length > 0 ? $"{path}/{pathArguments}/" : $"{path}/", method, Registry, true);
}

private List<string> FindPathArguments(MethodInfo method)
Expand Down
2 changes: 1 addition & 1 deletion Modules/Functional/Provider/InlineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public IHandler Build(IHandler parent)

var formatters = (_Formatters ?? Formatting.Default()).Build();

var extensions = new MethodExtensions(serializers, injectors, formatters);
var extensions = new MethodRegistry(serializers, injectors, formatters);

return Concerns.Chain(parent, _Concerns, p => new InlineHandler(p, _Functions, extensions));
}
Expand Down
12 changes: 6 additions & 6 deletions Modules/Functional/Provider/InlineHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ public class InlineHandler : IHandler

#region Initialization

public InlineHandler(IHandler parent, List<InlineFunction> functions, MethodExtensions extensions)
public InlineHandler(IHandler parent, List<InlineFunction> functions, MethodRegistry registry)
{
Parent = parent;

ResponseProvider = new ResponseProvider(extensions);
ResponseProvider = new ResponseProvider(registry);

Methods = new MethodCollection(this, AnalyzeMethods(functions, extensions));
Methods = new MethodCollection(this, AnalyzeMethods(functions, registry));
}

private IEnumerable<Func<IHandler, MethodHandler>> AnalyzeMethods(List<InlineFunction> functions, MethodExtensions extensions)
private IEnumerable<Func<IHandler, MethodHandler>> AnalyzeMethods(List<InlineFunction> functions, MethodRegistry registry)
{
foreach (var function in functions)
{
var method = function.Delegate.Method;

var operation = OperationBuilder.Create(function.Path, method, extensions);
var operation = OperationBuilder.Create(function.Path, method, registry);

var target = function.Delegate.Target ?? throw new InvalidOperationException("Delegate target must not be null");

yield return parent => new MethodHandler(parent, operation, () => target, function.Configuration, ResponseProvider.GetResponseAsync, extensions);
yield return parent => new MethodHandler(parent, operation, () => target, function.Configuration, ResponseProvider.GetResponseAsync, registry);
}
}

Expand Down
16 changes: 8 additions & 8 deletions Modules/Reflection/MethodHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public sealed class MethodHandler : IHandler

private Func<IRequest, IHandler, object?, Action<IResponseBuilder>?, ValueTask<IResponse?>> ResponseProvider { get; }

public MethodExtensions Extensions { get; }
public MethodRegistry Registry { get; }

#endregion

#region Initialization

public MethodHandler(IHandler parent, Operation operation, Func<object> instanceProvider, IMethodConfiguration metaData,
Func<IRequest, IHandler, object?, Action<IResponseBuilder>?, ValueTask<IResponse?>> responseProvider,
MethodExtensions extensions)
MethodRegistry registry)
{
Parent = parent;

Expand All @@ -55,7 +55,7 @@ public MethodHandler(IHandler parent, Operation operation, Func<object> instance
ResponseProvider = responseProvider;

Operation = operation;
Extensions = extensions;
Registry = registry;
}

#endregion
Expand Down Expand Up @@ -102,11 +102,11 @@ public MethodHandler(IHandler parent, Operation operation, Func<object> instance
{
targetArguments[i] = arg.Source switch
{
OperationArgumentSource.Injected => ArgumentProvider.GetInjectedArgument(request, this, arg, Extensions),
OperationArgumentSource.Path => ArgumentProvider.GetPathArgument(arg, sourceParameters, Extensions),
OperationArgumentSource.Body => await ArgumentProvider.GetBodyArgumentAsync(request, arg, Extensions),
OperationArgumentSource.Query => ArgumentProvider.GetQueryArgument(request, bodyArguments, arg, Extensions),
OperationArgumentSource.Content => await ArgumentProvider.GetContentAsync(request, arg, Extensions),
OperationArgumentSource.Injected => ArgumentProvider.GetInjectedArgument(request, this, arg, Registry),
OperationArgumentSource.Path => ArgumentProvider.GetPathArgument(arg, sourceParameters, Registry),
OperationArgumentSource.Body => await ArgumentProvider.GetBodyArgumentAsync(request, arg, Registry),
OperationArgumentSource.Query => ArgumentProvider.GetQueryArgument(request, bodyArguments, arg, Registry),
OperationArgumentSource.Content => await ArgumentProvider.GetContentAsync(request, arg, Registry),
_ => throw new ProviderException(ResponseStatus.InternalServerError, $"Unable to map argument '{arg.Name}' of type '{arg.Type}' because source '{arg.Source}' is not supported")
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using GenHTTP.Modules.Conversion.Formatters;
using GenHTTP.Modules.Conversion.Serializers;
using GenHTTP.Modules.Reflection.Injectors;

namespace GenHTTP.Modules.Reflection;

public record MethodExtensions
(

SerializationRegistry Serialization,

InjectionRegistry Injection,

FormatterRegistry Formatting

);
using GenHTTP.Modules.Conversion.Formatters;
using GenHTTP.Modules.Conversion.Serializers;
using GenHTTP.Modules.Reflection.Injectors;

namespace GenHTTP.Modules.Reflection;

public record MethodRegistry
(

SerializationRegistry Serialization,

InjectionRegistry Injection,

FormatterRegistry Formatting

);
22 changes: 11 additions & 11 deletions Modules/Reflection/Operations/ArgumentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace GenHTTP.Modules.Reflection.Operations;
public static class ArgumentProvider
{

public static object? GetInjectedArgument(IRequest request, IHandler handler, OperationArgument argument, MethodExtensions extensions)
public static object? GetInjectedArgument(IRequest request, IHandler handler, OperationArgument argument, MethodRegistry registry)
{
foreach (var injector in extensions.Injection)
foreach (var injector in registry.Injection)
{
if (injector.Supports(argument.Type))
{
Expand All @@ -23,22 +23,22 @@ public static class ArgumentProvider
return null;
}

public static object? GetPathArgument(OperationArgument argument, Match? matchedPath, MethodExtensions extensions)
public static object? GetPathArgument(OperationArgument argument, Match? matchedPath, MethodRegistry registry)
{
if (matchedPath != null)
{
var sourceArgument = matchedPath.Groups[argument.Name];

if (sourceArgument.Success)
{
return sourceArgument.Value.ConvertTo(argument.Type, extensions.Formatting);
return sourceArgument.Value.ConvertTo(argument.Type, registry.Formatting);
}
}

return null;
}

public static async ValueTask<object?> GetBodyArgumentAsync(IRequest request, OperationArgument argument, MethodExtensions extensions)
public static async ValueTask<object?> GetBodyArgumentAsync(IRequest request, OperationArgument argument, MethodRegistry registry)
{
if (request.Content == null)
{
Expand All @@ -53,35 +53,35 @@ public static class ArgumentProvider

if (!string.IsNullOrWhiteSpace(body))
{
result = body.ConvertTo(argument.Type, extensions.Formatting);
result = body.ConvertTo(argument.Type, registry.Formatting);
}

request.Content.Seek(0, SeekOrigin.Begin);

return result;
}

public static object? GetQueryArgument(IRequest request, Dictionary<string, string>? formArguments, OperationArgument argument, MethodExtensions extensions)
public static object? GetQueryArgument(IRequest request, Dictionary<string, string>? formArguments, OperationArgument argument, MethodRegistry registry)
{
if (request.Query.TryGetValue(argument.Name, out var queryValue))
{
return queryValue.ConvertTo(argument.Type, extensions.Formatting);
return queryValue.ConvertTo(argument.Type, registry.Formatting);
}

if (formArguments is not null)
{
if (formArguments.TryGetValue(argument.Name, out var bodyValue))
{
return bodyValue.ConvertTo(argument.Type, extensions.Formatting);
return bodyValue.ConvertTo(argument.Type, registry.Formatting);
}
}

return null;
}

public static async ValueTask<object?> GetContentAsync(IRequest request, OperationArgument argument, MethodExtensions extensions)
public static async ValueTask<object?> GetContentAsync(IRequest request, OperationArgument argument, MethodRegistry registry)
{
var deserializer = extensions.Serialization.GetDeserialization(request);
var deserializer = registry.Serialization.GetDeserialization(request);

if (deserializer is null)
{
Expand Down
4 changes: 2 additions & 2 deletions Modules/Reflection/Operations/OperationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static partial class OperationBuilder

#region Functionality

public static Operation Create(string? definition, MethodInfo method, MethodExtensions extensions, bool forceTrailingSlash = false)
public static Operation Create(string? definition, MethodInfo method, MethodRegistry registry, bool forceTrailingSlash = false)
{
var isWildcard = CheckWildcardRoute(method.ReturnType);

Expand Down Expand Up @@ -76,7 +76,7 @@ public static Operation Create(string? definition, MethodInfo method, MethodExte
path = new OperationPath(nameBuilder.ToString(), matcher, false, isWildcard);
}

var arguments = SignatureAnalyzer.GetArguments(method, pathArguments, extensions);
var arguments = SignatureAnalyzer.GetArguments(method, pathArguments, registry);

return new Operation(method, path, arguments);
}
Expand Down
10 changes: 5 additions & 5 deletions Modules/Reflection/Operations/SignatureAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GenHTTP.Modules.Reflection.Operations;
public static class SignatureAnalyzer
{

public static Dictionary<string, OperationArgument> GetArguments(MethodInfo method, HashSet<string> pathArguments, MethodExtensions extensions)
public static Dictionary<string, OperationArgument> GetArguments(MethodInfo method, HashSet<string> pathArguments, MethodRegistry registry)
{
var result = new Dictionary<string, OperationArgument>(StringComparer.OrdinalIgnoreCase);

Expand All @@ -20,13 +20,13 @@ public static Dictionary<string, OperationArgument> GetArguments(MethodInfo meth
continue;
}

if (TryInject(param, extensions, out var injectedArg))
if (TryInject(param, registry, out var injectedArg))
{
result.Add(param.Name, injectedArg);
continue;
}

if (param.CanFormat(extensions.Formatting))
if (param.CanFormat(registry.Formatting))
{
if (TryFromBody(param, out var bodyArg))
{
Expand All @@ -46,9 +46,9 @@ public static Dictionary<string, OperationArgument> GetArguments(MethodInfo meth
return result;
}

private static bool TryInject(ParameterInfo param, MethodExtensions extensions, [NotNullWhen(true)] out OperationArgument? argument)
private static bool TryInject(ParameterInfo param, MethodRegistry registry, [NotNullWhen(true)] out OperationArgument? argument)
{
foreach (var injector in extensions.Injection)
foreach (var injector in registry.Injection)
{
if (injector.Supports(param.ParameterType))
{
Expand Down
12 changes: 6 additions & 6 deletions Modules/Reflection/ResponseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class ResponseProvider

#region Get-/Setters

private MethodExtensions Extensions { get; }
private MethodRegistry Registry { get; }

#endregion

#region Initialization

public ResponseProvider(MethodExtensions extensions)
public ResponseProvider(MethodRegistry registry)
{
Extensions = extensions;
Registry = registry;
}

#endregion
Expand Down Expand Up @@ -91,17 +91,17 @@ public ResponseProvider(MethodExtensions extensions)
}

// format the value if possible
if (Extensions.Formatting.CanHandle(type))
if (Registry.Formatting.CanHandle(type))
{
return request.Respond()
.Content(Extensions.Formatting.Write(result, type) ?? string.Empty)
.Content(Registry.Formatting.Write(result, type) ?? string.Empty)
.Type(ContentType.TextPlain)
.Adjust(adjustments)
.Build();
}

// serialize the result
var serializer = Extensions.Serialization.GetSerialization(request);
var serializer = Registry.Serialization.GetSerialization(request);

if (serializer is null)
{
Expand Down
2 changes: 1 addition & 1 deletion Modules/Webservices/Provider/ServiceResourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public IHandler Build(IHandler parent)

var instance = _Instance ?? throw new BuilderMissingPropertyException("instance");

var extensions = new MethodExtensions(serializers, injectors, formatters);
var extensions = new MethodRegistry(serializers, injectors, formatters);

return Concerns.Chain(parent, _Concerns, p => new ServiceResourceRouter(p, instance, extensions));
}
Expand Down
Loading

0 comments on commit 690ccbb

Please sign in to comment.