From 6f334bd21b825aa7b0447e0c5cd668b86fde9187 Mon Sep 17 00:00:00 2001 From: "David G. Moore, Jr." Date: Fri, 19 Jan 2024 19:27:19 -0500 Subject: [PATCH] minor refactorings --- LICENSE.md | 35 +++++++ Runtime/EnumerationJsonConverter.cs | 12 +-- Runtime/EnumerationJsonConverterFactory.cs | 29 +++++- Runtime/EnumerationSerializationDto.cs | 27 +++--- Runtime/LICENSE.md | 35 +++++++ Runtime/README.md | 27 ------ Runtime/UniversalUriResolver.cs | 92 ++++++++++++------- src/Resources/Enumeration.scriban | 1 + .../EnumerationJsonConverter.scriban | 2 + src/Resources/IEnumeration.scriban | 5 +- src/Resources/NestedEnumerationType.scriban | 18 +--- 11 files changed, 177 insertions(+), 106 deletions(-) create mode 100644 LICENSE.md create mode 100644 Runtime/LICENSE.md delete mode 100644 Runtime/README.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..4f592f8 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,35 @@ +--- +date: 2023-07-13T05:44:46:00-05:00Z +description: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, yadda, yadda, yadda... +keywords: +- IP +- copyright +- license +- mit +permissions: +- commercial-use +- modifications +- distribution +- private-use +conditions: +- include-copyright +limitations: +- liability +- warranty +lastmod: 2024-01-0T00:39:00.0000+05:00Z +license: MIT +slug: mit-license +title: MIT License +type: license +--- + +# MIT License + +## Copyright © 2022-2024 [David G. Moore, Jr.](mailto:david@dgmjr.io "Send Dr. Moore") ([@dgmjr](https://github.com/dgmjr "Contact Dr. Moore on GitHub")), All Rights Reserved + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Runtime/EnumerationJsonConverter.cs b/Runtime/EnumerationJsonConverter.cs index a8d16c1..d162259 100644 --- a/Runtime/EnumerationJsonConverter.cs +++ b/Runtime/EnumerationJsonConverter.cs @@ -12,21 +12,13 @@ public class EnumerationJsonConverter : JsonConverter>(reader.ValueSpan, options); return UniversalUriResolver.ResolveUri(dto.Uri); } - public override void Write( - Utf8JsonWriter writer, - TEnumeration value, - Jso options - ) + public override void Write(Utf8JsonWriter writer, TEnumeration value, Jso options) { var dto = EnumerationSerializationDto.FromEnumeration(value); Serialize(writer, dto, options); diff --git a/Runtime/EnumerationJsonConverterFactory.cs b/Runtime/EnumerationJsonConverterFactory.cs index 7b357e1..a5d6a5f 100644 --- a/Runtime/EnumerationJsonConverterFactory.cs +++ b/Runtime/EnumerationJsonConverterFactory.cs @@ -1,18 +1,37 @@ +/* + * EnumerationJsonConverterFactory.cs + * + * Created: 2023-47-28T19:47:35-05:00 + * Modified: 2024-46-19T07:46:10-05:00 + * + * Author: David G. Moore, Jr. + * + * Copyright © 2024 David G. Moore, Jr., All Rights Reserved + * License: MIT (https://opensource.org/licenses/MIT) + */ + namespace Dgmjr.Enumerations; using Dgmjr.Abstractions; public class EnumerationJsonConverterFactory : JsonConverterFactory { - private static readonly type[] Interfaces = [typeof(IIdentifiable), typeof(IHaveAGuid), typeof(IHaveAUri), typeof(IHaveAValue), typeof(IHaveAName), typeof(IHaveAShortName), typeof(IHaveADisplayName), typeof(IHaveADescription)]; + private static readonly type[] Interfaces = + [ + typeof(IIdentifiable), + typeof(IHaveAGuid), + typeof(IHaveAUri), + typeof(IHaveAValue), + typeof(IHaveAName), + typeof(IHaveAShortName), + typeof(IHaveADisplayName), + typeof(IHaveADescription) + ]; public override bool CanConvert(type typeToConvert) => TrueForAll(Interfaces, i => i.IsAssignableFrom(typeToConvert)); - public override JConverter? CreateConverter( - type typeToConvert, - Jso options - ) + public override JConverter? CreateConverter(type typeToConvert, Jso options) { var converterType = typeof(EnumerationJsonConverter<>).MakeGenericType(typeToConvert); return (JConverter?)Activator.CreateInstance(converterType); diff --git a/Runtime/EnumerationSerializationDto.cs b/Runtime/EnumerationSerializationDto.cs index fba866c..b182c89 100644 --- a/Runtime/EnumerationSerializationDto.cs +++ b/Runtime/EnumerationSerializationDto.cs @@ -6,7 +6,8 @@ namespace Dgmjr.Enumerations; using Dgmjr.Abstractions; -public record class EnumerationSerializationDto(uri Uri, +public record class EnumerationSerializationDto( + uri Uri, string Description, IDictionary Ids, IStringDictionary Names @@ -25,18 +26,20 @@ IStringDictionary Names ImmutableDictionary.Empty ); - public static EnumerationSerializationDto FromEnumeration( - object e - ) => + public static EnumerationSerializationDto FromEnumeration(object e) => new( - (e as IHaveAUri).Uri.ToString(), - (e as IHaveADescription).Description, - new Dictionary() { { Id, (e as IIdentifiable).Id }, { Guid, (e as IHaveAGuid).Guid } }, + (e as IHaveAUri)?.Uri.ToString(), + (e as IHaveADescription)?.Description, + new Dictionary() + { + { Id, (e as IIdentifiable)?.Id }, + { Guid, (e as IHaveAGuid)?.Guid } + }, new StringDictionary() { - { Name, (e as IHaveAName).Name }, - { Short, (e as IHaveAShortName).ShortName }, - { Display, (e as IHaveADisplayName).DisplayName } + { Name, (e as IHaveAName)?.Name }, + { Short, (e as IHaveAShortName)?.ShortName }, + { Display, (e as IHaveADisplayName)?.DisplayName } } ); @@ -63,9 +66,7 @@ IStringDictionary Names IHaveADisplayName, IHaveAGuid { - public static EnumerationSerializationDto FromEnumeration( - TEnumeration e - ) => + public static EnumerationSerializationDto FromEnumeration(TEnumeration e) => new( e.Uri.ToString(), e.Description, diff --git a/Runtime/LICENSE.md b/Runtime/LICENSE.md new file mode 100644 index 0000000..376423d --- /dev/null +++ b/Runtime/LICENSE.md @@ -0,0 +1,35 @@ +--- +date: 2023-07-13T05:44:46:00+05:00Z +description: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, yadda, yadda, yadda... +keywords: +- IP +- copyright +- license +- mit +permissions: +- commercial-use +- modifications +- distribution +- private-use +conditions: +- include-copyright +limitations: +- liability +- warranty +lastmod: 2024-01-0T00:39:00.0000+05:00Z +license: MIT +slug: mit-license +title: MIT License +type: license +--- + +# MIT License + +## Copyright © 2022-2023 [David G. Moore, Jr.](mailto:david@dgmjr.io "Send Dr. Moore an email") ([@dgmjr](https://github.com/dgmjr "Contact Dr. Moore on GitHub")), All Rights Reserved + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Runtime/README.md b/Runtime/README.md deleted file mode 100644 index 31cf793..0000000 --- a/Runtime/README.md +++ /dev/null @@ -1,27 +0,0 @@ ---- -title: -lastmod: 2023-26-28T19:26:44.3950-05:00Z -date: 2023-26-28T19:26:44.3952-05:00Z -license: MIT -slug: Dgmjr.Enumerations.CodeGenerator.Runtime-readme -version: -authors: - - dgmjr; -description: Dgmjr.Enumerations.CodeGenerator.Runtime Readme #TODO: write description for Dgmjr.Enumerations.CodeGenerator.Runtime Readme -keywords: -- Dgmjr.Enumerations.CodeGenerator.Runtime - - dgmjr - - dgmjr-io -type: readme ---- -# Dgmjr.Enumerations.CodeGenerator.Runtime Readme - -## Package Description -## Getting Started -## Prerequisites -## Installation -## Usage -## Contributing -## Versioning -Built from [commit on branch main at ] -(/tree/) diff --git a/Runtime/UniversalUriResolver.cs b/Runtime/UniversalUriResolver.cs index f961e5a..0f0cfe3 100644 --- a/Runtime/UniversalUriResolver.cs +++ b/Runtime/UniversalUriResolver.cs @@ -6,45 +6,69 @@ namespace Dgmjr.Enumerations; public static class UniversalUriResolver { - private static readonly IDictionary _cache = new ConcurrentDictionary(); + private static readonly IDictionary _cache = + new ConcurrentDictionary(); const string Parse = nameof(Parse); delegate object ParseDelegate(string s); - public static TEnumeration ResolveUri( - uri uri - ) - where TEnumeration : IIdentifiable, - IHaveADescription, - IHaveAName, - IHaveAShortName, - IHaveAUri, - IHaveAValue, - IHaveADisplayName, - IHaveAGuid - => (TEnumeration)(_cache[uri] = _cache.TryGetValue(uri, out var value) ? (TEnumeration)value : CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Select(GetParseDelegate).WhereNotNull().Select(d => { try { return d(uri.ToString()); } catch { return null; } }).WhereNotNull().FirstOrDefault()); + public static TEnumeration ResolveUri(uri uri) + where TEnumeration : IIdentifiable, + IHaveADescription, + IHaveAName, + IHaveAShortName, + IHaveAUri, + IHaveAValue, + IHaveADisplayName, + IHaveAGuid => + (TEnumeration)( + _cache[uri] = _cache.TryGetValue(uri, out var value) + ? (TEnumeration)value + : CurrentDomain + .GetAssemblies() + .SelectMany(a => a.GetTypes()) + .Select(GetParseDelegate) + .WhereNotNull() + .Select(d => + { + try + { + return d(uri.ToString()); + } + catch + { + return null; + } + }) + .WhereNotNull() + .FirstOrDefault() + ); - static MethodInfo? GetParseMethod( - this type type - ) - where TEnumeration : IIdentifiable, - IHaveADescription, - IHaveAName, - IHaveAShortName, - IHaveAUri, - IHaveAValue, - IHaveADisplayName, - IHaveAGuid => - type.GetRuntimeMethods().FirstOrDefault(m => m.Name == Parse && typeof(TEnumeration).IsAssignableFrom(m.ReturnType) && m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType == typeof(string)); + static MethodInfo? GetParseMethod(this type type) + where TEnumeration : IIdentifiable, + IHaveADescription, + IHaveAName, + IHaveAShortName, + IHaveAUri, + IHaveAValue, + IHaveADisplayName, + IHaveAGuid => + type.GetRuntimeMethods() + .FirstOrDefault( + m => + m.Name == Parse + && typeof(TEnumeration).IsAssignableFrom(m.ReturnType) + && m.GetParameters().Length == 1 + && m.GetParameters()[0].ParameterType == typeof(string) + ); static ParseDelegate? GetParseDelegate(this type type) - where TEnumeration : IIdentifiable, - IHaveADescription, - IHaveAName, - IHaveAShortName, - IHaveAUri, - IHaveAValue, - IHaveADisplayName, - IHaveAGuid - => + where TEnumeration : IIdentifiable, + IHaveADescription, + IHaveAName, + IHaveAShortName, + IHaveAUri, + IHaveAValue, + IHaveADisplayName, + IHaveAGuid => type.GetParseMethod().CreateDelegate(typeof(ParseDelegate)) as ParseDelegate; } diff --git a/src/Resources/Enumeration.scriban b/src/Resources/Enumeration.scriban index 3a4d157..7febe6f 100644 --- a/src/Resources/Enumeration.scriban +++ b/src/Resources/Enumeration.scriban @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Linq; using System.Text; using static System.StringComparison; +using Dgmjr.Enumerations; {{ compiler_generated_attributes }} public {{ if base_type == "" || base_type == null }} static {{ end }} partial {{ data_structure_type }} {{ dto_type_name }} {{ if base_type != "" && base_type != null }} : {{ base_type }}{{ end }} diff --git a/src/Resources/EnumerationJsonConverter.scriban b/src/Resources/EnumerationJsonConverter.scriban index d0108d7..02c6741 100644 --- a/src/Resources/EnumerationJsonConverter.scriban +++ b/src/Resources/EnumerationJsonConverter.scriban @@ -1,3 +1,4 @@ +#if NO namespace {{ dto_namespace }}; using {{ dto_namespace }}.Abstractions; @@ -29,3 +30,4 @@ public partial {{ data_structure_type }} @{{ dto_type_name }} public {{ field_name }}JsonConverterAttribute() : base(typeof({{ field_name }}JsonConverter)) { } } } +#endif diff --git a/src/Resources/IEnumeration.scriban b/src/Resources/IEnumeration.scriban index b8d3bf1..ca45f57 100644 --- a/src/Resources/IEnumeration.scriban +++ b/src/Resources/IEnumeration.scriban @@ -1,8 +1,9 @@ namespace {{ dto_namespace }}.Abstractions; using {{ dto_namespace }}; +using Dgmjr.Enumerations; {{ compiler_generated_attributes }} -[EnumerationJsonConverter] +[Dgmjr.Enumerations.EnumerationJsonConverter] public partial interface I{{ dto_type_name }} : IHaveAName, IHaveAValue, @@ -10,7 +11,6 @@ IHaveAName, IHaveAValue<{{ enum_namespace }}.{{ enum_type_name }}>, IHaveADescription, IHaveAUri, - IHaveAuri, IHaveAUriString, IHaveSynonyms, IHaveAShortName, @@ -23,6 +23,5 @@ IHaveAName, IEquatable<{{ enum_underlying_type }}>, IEquatable { - new uri Uri { get; } int Order { get; } } diff --git a/src/Resources/NestedEnumerationType.scriban b/src/Resources/NestedEnumerationType.scriban index 07222f5..d05edf9 100644 --- a/src/Resources/NestedEnumerationType.scriban +++ b/src/Resources/NestedEnumerationType.scriban @@ -1,5 +1,7 @@ namespace {{ dto_namespace }}; using {{ dto_namespace }}.Abstractions; +using DTO_TYPE = @{{ dto_type_name }}; +using ENUM_TYPE = {{ enum_namespace }}.{{ enum_type_name }}; public partial {{ data_structure_type }} @{{ dto_type_name }} {{ if base_type != "" && base_type != null }} : {{ base_type }}{{ end }} { @@ -122,31 +124,21 @@ public partial {{ data_structure_type }} @{{ dto_type_name }} {{ if base_type != [JsonIgnore, XmlIgnore] string IHaveAUriString.UriString => UriString; - /// The {{ enumeration_name }}'s URI - /// {{ uri_string }} - [JsonIgnore, XmlIgnore] - System.uri IHaveAuri.Uri => System.uri.From(Uri.ToString()); - /// The order in which the {{ enumeration_name }} should appear /// {{ order }} /// 0 [JsonIgnore, XmlIgnore] int I{{ enumeration_name }}.Order => Order; - /// The {{ enumeration_name }}'s URI - /// {{ uri_string }} - [JsonIgnore, XmlIgnore] - uri I{{ dto_type_name }}.Uri => uri.From(Uri.ToString()); - /// The {{ enumeration_name }}'s Id as an /// {{ id }} [JsonIgnore, XmlIgnore] object IIdentifiable.Id => Id; - /// The {{ enumeration_name }}'s Id as a(n) + /// The {{ enumeration_name }}'s Id as an /// {{ id }} [JsonIgnore, XmlIgnore] - {{ enum_underlying_type }} IIdentifiable<{{ enum_underlying_type }}>.Id => Id; + {{ enum_underlying_type }} IIdentifiable<{{ enum_underlying_type }}>.Id => ({{ enum_underlying_type }})Id; /// The {{ enumeration_name }}'s synonyms /// {{ synonyms }} @@ -159,8 +151,6 @@ public partial {{ data_structure_type }} @{{ dto_type_name }} {{ if base_type != bool IEquatable<{{ enum_underlying_type }}>.Equals({{ enum_underlying_type }} value) => (({{ enum_namespace }}.{{ enum_type }})Convert.ChangeType(value, typeof({{ enum_namespace }}.{{ enum_type }}))) == Value; - public int CompareTo(object? other) => other is IEnumeration ie ? CompareTo(ie) : -1; - public int CompareTo(IEnumeration other) => Order.CompareTo(other.Order); public override int GetHashCode() => Id.GetHashCode(); public TypeCode GetTypeCode() => Convert.GetTypeCode(Value);