From 9768aa7713826d49a6c2ea1025caa4b87d62e5e8 Mon Sep 17 00:00:00 2001 From: vnugent Date: Thu, 2 Nov 2023 01:50:05 -0400 Subject: also carried away --- .../src/RoutingExtensions.cs | 87 ++++++++++++---------- 1 file changed, 47 insertions(+), 40 deletions(-) (limited to 'lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs') diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs b/lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs index 7731bcf..1061dda 100644 --- a/lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs +++ b/lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs @@ -54,58 +54,65 @@ namespace VNLib.Plugins.Extensions.Loading.Routing Type endpointType = typeof(T); T endpoint; - - //If the config attribute is not set, then ignore the config variables - if (string.IsNullOrWhiteSpace(pluginConfigPathName)) - { - ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase) }); - - _ = constructor ?? throw new EntryPointNotFoundException($"No constructor t(PluginBase p) found for {endpointType.Name}"); - - //Create the new endpoint and pass the plugin instance - endpoint = (T)constructor.Invoke(new object[] { plugin }); - - //Register event handlers for the endpoint - ScheduleIntervals(plugin, endpoint, endpointType, null); - } - else + try { - //Try to get config but allow null if not required - IConfigScope? config = plugin.TryGetConfig(pluginConfigPathName); - - if(configRequired && config == null) - { - ConfigurationExtensions.ThrowConfigNotFoundForType(endpointType); - return default; - } - - //Choose constructor based on config - if (config != null) + //If the config attribute is not set, then ignore the config variables + if (string.IsNullOrWhiteSpace(pluginConfigPathName)) { - ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase), typeof(IConfigScope) }); + ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase) }); - //Make sure the constructor exists - _ = constructor ?? throw new EntryPointNotFoundException($"No constructor t(PluginBase p, IConfigScope cs) found for {endpointType.Name}"); + _ = constructor ?? throw new EntryPointNotFoundException($"No constructor t(PluginBase p) found for {endpointType.Name}"); - //Create the new endpoint and pass the plugin instance along with the configuration object - endpoint = (T)constructor.Invoke(new object[] { plugin, config }); + //Create the new endpoint and pass the plugin instance + endpoint = (T)constructor.Invoke(new object[] { plugin }); //Register event handlers for the endpoint - ScheduleIntervals(plugin, endpoint, endpointType, config); + ScheduleIntervals(plugin, endpoint, endpointType, null); } else { - //Config does not exist, so use the default constructor - ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase) }); + //Try to get config but allow null if not required + IConfigScope? config = plugin.TryGetConfig(pluginConfigPathName); - _ = constructor ?? throw new EntryPointNotFoundException($"No constructor t(PluginBase p) found for {endpointType.Name}"); + if (configRequired && config == null) + { + ConfigurationExtensions.ThrowConfigNotFoundForType(endpointType); + return default; + } - //Create the new endpoint and pass the plugin instance - endpoint = (T)constructor.Invoke(new object[] { plugin }); + //Choose constructor based on config + if (config != null) + { + ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase), typeof(IConfigScope) }); - //Register event handlers for the endpoint - ScheduleIntervals(plugin, endpoint, endpointType, null); - } + //Make sure the constructor exists + _ = constructor ?? throw new EntryPointNotFoundException($"No constructor t(PluginBase p, IConfigScope cs) found for {endpointType.Name}"); + + //Create the new endpoint and pass the plugin instance along with the configuration object + endpoint = (T)constructor.Invoke(new object[] { plugin, config }); + + //Register event handlers for the endpoint + ScheduleIntervals(plugin, endpoint, endpointType, config); + } + else + { + //Config does not exist, so use the default constructor + ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase) }); + + _ = constructor ?? throw new EntryPointNotFoundException($"No constructor t(PluginBase p) found for {endpointType.Name}"); + + //Create the new endpoint and pass the plugin instance + endpoint = (T)constructor.Invoke(new object[] { plugin }); + + //Register event handlers for the endpoint + ScheduleIntervals(plugin, endpoint, endpointType, null); + } + } + } + catch(TargetInvocationException te) + { + LoadingExtensions.FindAndThrowInnerException(te); + throw; } //Route the endpoint -- cgit