diff options
author | vnugent <public@vaughnnugent.com> | 2024-09-14 16:09:23 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-09-14 16:09:23 -0400 |
commit | 11fa713e33a1a5cc6f2262c25f1ab69567aa6530 (patch) | |
tree | 8e052c9a77733aa08abc00045f6ee7db3ab6114c /lib/VNLib.Plugins.Extensions.Loading | |
parent | 641bdbe75cb0128c09e27f1b92709c86574026ac (diff) |
commit 181f499c92ac7b772307e40d48d2a26ea45e156b
Author: vnugent <public@vaughnnugent.com>
Date: Sat Sep 14 16:01:28 2024 -0400
taskfile prettier
commit 0d47b4d25cf61d29c44a17ae491d775cf84938ab
Author: vnugent <public@vaughnnugent.com>
Date: Wed Sep 11 16:43:36 2024 -0400
Managed library & package updates
commit 940f151f2f4e708282deb5569cffe5b0935f5c3b
Author: vnugent <public@vaughnnugent.com>
Date: Mon Sep 2 13:54:16 2024 -0400
expand config extensions, and update some sql provider usage
commit b269019103acce18e0df8debd9f7bfdf203083b8
Merge: 87887c0 641bdbe
Author: vnugent <public@vaughnnugent.com>
Date: Sun Jul 28 18:53:41 2024 -0400
Merge branch 'master' into develop
Diffstat (limited to 'lib/VNLib.Plugins.Extensions.Loading')
3 files changed, 211 insertions, 28 deletions
diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/AssemblyLoader.cs b/lib/VNLib.Plugins.Extensions.Loading/src/AssemblyLoader.cs index 49ef4b3..2d47af5 100644 --- a/lib/VNLib.Plugins.Extensions.Loading/src/AssemblyLoader.cs +++ b/lib/VNLib.Plugins.Extensions.Loading/src/AssemblyLoader.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Extensions.Loading @@ -48,19 +48,19 @@ namespace VNLib.Plugins.Extensions.Loading public sealed class AssemblyLoader<T> : ManagedLibrary, IDisposable { private readonly CancellationTokenRegistration _reg; - private readonly Lazy<T> _instance; + private readonly LazyInitializer<T> _instance; private bool disposedValue; /// <summary> /// The instance of the loaded type /// </summary> - public T Resource => _instance.Value; + public T Resource => _instance.Instance; private AssemblyLoader(string assemblyPath, AssemblyLoadContext parentContext, CancellationToken unloadToken) :base(assemblyPath, parentContext) { //Init lazy type loader - _instance = new(LoadTypeFromAssembly<T>, LazyThreadSafetyMode.PublicationOnly); + _instance = new(LoadTypeFromAssembly<T>); //Register dispose _reg = unloadToken.Register(Dispose); } @@ -96,7 +96,7 @@ namespace VNLib.Plugins.Extensions.Loading if (disposing) { //If the instance is disposable, call its dispose method on unload - if (_instance.IsValueCreated && _instance.Value is IDisposable disposable) + if (_instance.IsLoaded && _instance.Instance is IDisposable disposable) { disposable.Dispose(); } @@ -138,7 +138,7 @@ namespace VNLib.Plugins.Extensions.Loading /// <exception cref="FileNotFoundException"></exception> internal static AssemblyLoader<T> Load(string assemblyName, AssemblyLoadContext loadContext, CancellationToken unloadToken) { - _ = loadContext ?? throw new ArgumentNullException(nameof(loadContext)); + ArgumentNullException.ThrowIfNull(loadContext); //Make sure the file exists if (!FileOperations.FileExists(assemblyName)) diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/ConfigurationExtensions.cs b/lib/VNLib.Plugins.Extensions.Loading/src/ConfigurationExtensions.cs index e838822..30711fa 100644 --- a/lib/VNLib.Plugins.Extensions.Loading/src/ConfigurationExtensions.cs +++ b/lib/VNLib.Plugins.Extensions.Loading/src/ConfigurationExtensions.cs @@ -198,7 +198,6 @@ namespace VNLib.Plugins.Extensions.Loading return value; } - /// <summary> /// Gets a required configuration property from the specified configuration scope /// and deserializes the json type. @@ -211,7 +210,11 @@ namespace VNLib.Plugins.Extensions.Loading /// <exception cref="ConfigurationException"></exception> public static T GetRequiredProperty<T>(this IConfigScope config, string property) { - return GetRequiredProperty(config, property, static p => p.Deserialize<T>()!); + return GetRequiredProperty( + config, + property, + static p => p.Deserialize<T>()! + ); } /// <summary> @@ -245,6 +248,107 @@ namespace VNLib.Plugins.Extensions.Loading } /// <summary> + /// Attempts to get a configuration property from the specified configuration scope + /// and deserializes the json type. + /// output value + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="config"></param> + /// <param name="property">The name of the configuration element to get</param> + /// <param name="value">The output value to set</param> + /// <returns>A value that indicates if the property was found</returns> + /// <exception cref="ArgumentNullException"></exception> + public static bool TryGetProperty<T>(this IConfigScope config, string property, out T? value) + { + return TryGetProperty( + config, + property, + static p => p.Deserialize<T>(), + out value + ); + } + + /// <summary> + /// Attempts to get a configuration property from the specified configuration scope + /// and returns the string value if found, or null if not found. + /// output value + /// </summary> + /// <param name="config"></param> + /// <param name="property">The name of the configuration element to get</param> + /// <param name="value">The output value to set</param> + /// <returns>A value that indicates if the property was found</returns> + /// <exception cref="ArgumentNullException"></exception> + public static bool TryGetProperty(this IConfigScope config, string property, out string? value) + { + return TryGetProperty( + config, + property, + static p => p.GetString(), + out value + ); + } + + /// <summary> + /// Attempts to get a configuration property from the specified configuration scope + /// and returns the int32 value if found, or null if not found. + /// output value + /// </summary> + /// <param name="config"></param> + /// <param name="property">The name of the configuration element to get</param> + /// <param name="value">The output value to set</param> + /// <returns>A value that indicates if the property was found</returns> + /// <exception cref="ArgumentNullException"></exception> + public static bool TryGetProperty(this IConfigScope config, string property, out int? value) + { + return TryGetProperty( + config, + property, + static p => p.GetInt32(), + out value + ); + } + + /// <summary> + /// Attempts to get a configuration property from the specified configuration scope + /// and returns the uint32 value if found, or null if not found. + /// output value + /// </summary> + /// <param name="config"></param> + /// <param name="property">The name of the configuration element to get</param> + /// <param name="value">The output value to set</param> + /// <returns>A value that indicates if the property was found</returns> + /// <exception cref="ArgumentNullException"></exception> + public static bool TryGetProperty(this IConfigScope config, string property, out uint? value) + { + return TryGetProperty( + config, + property, + static p => p.GetUInt32(), + out value + ); + } + + /// <summary> + /// Attempts to get a configuration property from the specified configuration scope + /// and returns the boolean value if found, or null if not found. + /// output value + /// </summary> + /// <param name="config"></param> + /// <param name="property">The name of the configuration element to get</param> + /// <param name="value">The output value to set</param> + /// <returns>A value that indicates if the property was found</returns> + /// <exception cref="ArgumentNullException"></exception> + public static bool TryGetProperty(this IConfigScope config, string property, out bool? value) + { + return TryGetProperty( + config, + property, + static p => p.GetBoolean(), + out value + ); + } + + /// <summary> /// Gets a configuration property from the specified configuration scope /// and invokes your callback function on the element if found to transform the /// output value, or returns the default value if the property is not found. @@ -285,6 +389,84 @@ namespace VNLib.Plugins.Extensions.Loading } /// <summary> + /// Gets a configuration property from the specified configuration scope + /// and deserializes the json element if found, or returns the default value + /// if the property is not found. + /// </summary> + /// <param name="config"></param> + /// <param name="property">The name of the configuration element to get</param> + /// <param name="defaultValue">The default value to return</param> + /// <returns>The property value returned from your getter callback, or the default value if not found</returns> + /// <exception cref="ArgumentNullException"></exception> + [return: NotNullIfNotNull(nameof(defaultValue))] + public static string? GetValueOrDefault(this IConfigScope config, string property, string defaultValue) + { + return GetValueOrDefault( + config, + property, + static p => p.GetString(), + defaultValue + ); + } + + /// <summary> + /// Gets a configuration property of type int32 from the specified configuration + /// scope and, or returns the default value if the property is not found. + /// </summary> + /// <param name="config"></param> + /// <param name="property">The name of the configuration element to get</param> + /// <param name="defaultValue">The default value to return</param> + /// <returns>The property value returned from your getter callback, or the default value if not found</returns> + /// <exception cref="ArgumentNullException"></exception> + public static int GetValueOrDefault(this IConfigScope config, string property, int defaultValue) + { + return GetValueOrDefault( + config, + property, + static p => p.GetInt32(), + defaultValue + ); + } + + /// <summary> + /// Gets a configuration property of type uint32 from the specified configuration + /// scope and, or returns the default value if the property is not found. + /// </summary> + /// <param name="config"></param> + /// <param name="property">The name of the configuration element to get</param> + /// <param name="defaultValue">The default value to return</param> + /// <returns>The property value returned from your getter callback, or the default value if not found</returns> + /// <exception cref="ArgumentNullException"></exception> + public static uint GetValueOrDefault(this IConfigScope config, string property, uint defaultValue) + { + return GetValueOrDefault( + config, + property, + static p => p.GetUInt32(), + defaultValue + ); + } + + /// <summary> + /// Gets a configuration property of type boolean from the specified configuration + /// scope and, or returns the default value if the property is not found. + /// </summary> + /// <param name="config"></param> + /// <param name="property">The name of the configuration element to get</param> + /// <param name="defaultValue">The default value to return</param> + /// <returns>The property value returned from your getter callback, or the default value if not found</returns> + /// <exception cref="ArgumentNullException"></exception> + public static bool GetValueOrDefault(this IConfigScope config, string property, bool defaultValue) + { + return GetValueOrDefault( + config, + property, + static p => p.GetBoolean(), + defaultValue + ); + } + + /// <summary> /// Gets the configuration property name for the type /// </summary> /// <param name="type">The type to get the configuration name for</param> @@ -522,22 +704,22 @@ namespace VNLib.Plugins.Extensions.Loading return []; } - if (searchPaths.ValueKind == JsonValueKind.Array) - { - //Get the plugins path or throw because it should ALWAYS be defined if this method is called - return searchPaths.EnumerateArray() - .Select(static p => p.GetString()!) - .Select(Path.GetFullPath) //Get absolute file paths - .ToArray(); - } - else if (searchPaths.ValueKind == JsonValueKind.String) + switch (searchPaths.ValueKind) { - return [Path.GetFullPath(searchPaths.GetString()!)]; - } - else - { - return []; - } + case JsonValueKind.Array: + + //Get the plugins path or throw because it should ALWAYS be defined if this method is called + return searchPaths.EnumerateArray() + .Select(static p => p.GetString()!) + .Select(Path.GetFullPath) //Get absolute file paths + .ToArray(); + + case JsonValueKind.String: + return [ Path.GetFullPath(searchPaths.GetString()!) ]; + + default: + return []; + } } } } diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/IAsyncBackgroundWork.cs b/lib/VNLib.Plugins.Extensions.Loading/src/IAsyncBackgroundWork.cs index 9fb66a2..a86129c 100644 --- a/lib/VNLib.Plugins.Extensions.Loading/src/IAsyncBackgroundWork.cs +++ b/lib/VNLib.Plugins.Extensions.Loading/src/IAsyncBackgroundWork.cs @@ -1,12 +1,12 @@ /* -* Copyright (c) 2022 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Extensions.Loading -* File: LoadingExtensions.cs +* File: IAsyncBackgroundWork.cs * -* LoadingExtensions.cs is part of VNLib.Plugins.Extensions.Loading which is part of the larger -* VNLib collection of libraries and utilities. +* IAsyncBackgroundWork.cs is part of VNLib.Plugins.Extensions.Loading which is +* part of the larger VNLib collection of libraries and utilities. * * VNLib.Plugins.Extensions.Loading is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -22,9 +22,10 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ +using System.Threading; using System.Threading.Tasks; + using VNLib.Utils.Logging; -using System.Threading; namespace VNLib.Plugins.Extensions.Loading { |