Skip to content

Commit

Permalink
minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmjr committed Jan 20, 2024
1 parent b8d3576 commit 6f334bd
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 106 deletions.
35 changes: 35 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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:[email protected] "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.

12 changes: 2 additions & 10 deletions Runtime/EnumerationJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,13 @@ public class EnumerationJsonConverter<TEnumeration> : JsonConverter<TEnumeration
IHaveADisplayName,
IHaveAGuid
{
public override TEnumeration Read(
ref Utf8JsonReader reader,
type typeToConvert,
Jso options
)
public override TEnumeration Read(ref Utf8JsonReader reader, type typeToConvert, Jso options)
{
var dto = Deserialize<EnumerationSerializationDto<TEnumeration>>(reader.ValueSpan, options);
return UniversalUriResolver.ResolveUri<TEnumeration>(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<TEnumeration>.FromEnumeration(value);
Serialize(writer, dto, options);
Expand Down
29 changes: 24 additions & 5 deletions Runtime/EnumerationJsonConverterFactory.cs
Original file line number Diff line number Diff line change
@@ -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. <[email protected]>
*
* 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);
Expand Down
27 changes: 14 additions & 13 deletions Runtime/EnumerationSerializationDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> Ids,
IStringDictionary Names
Expand All @@ -25,18 +26,20 @@ IStringDictionary Names
ImmutableDictionary<string, string>.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<string, object>() { { Id, (e as IIdentifiable).Id }, { Guid, (e as IHaveAGuid).Guid } },
(e as IHaveAUri)?.Uri.ToString(),
(e as IHaveADescription)?.Description,
new Dictionary<string, object>()
{
{ 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 }
}
);

Expand All @@ -63,9 +66,7 @@ IStringDictionary Names
IHaveADisplayName,
IHaveAGuid
{
public static EnumerationSerializationDto<TEnumeration> FromEnumeration(
TEnumeration e
) =>
public static EnumerationSerializationDto<TEnumeration> FromEnumeration(TEnumeration e) =>
new(
e.Uri.ToString(),
e.Description,
Expand Down
35 changes: 35 additions & 0 deletions Runtime/LICENSE.md
Original file line number Diff line number Diff line change
@@ -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:[email protected] "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.

27 changes: 0 additions & 27 deletions Runtime/README.md

This file was deleted.

92 changes: 58 additions & 34 deletions Runtime/UniversalUriResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,69 @@ namespace Dgmjr.Enumerations;

public static class UniversalUriResolver
{
private static readonly IDictionary<uri, object> _cache = new ConcurrentDictionary<uri, object>();
private static readonly IDictionary<uri, object> _cache =
new ConcurrentDictionary<uri, object>();
const string Parse = nameof(Parse);
delegate object ParseDelegate(string s);

public static TEnumeration ResolveUri<TEnumeration>(
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<TEnumeration>).WhereNotNull().Select(d => { try { return d(uri.ToString()); } catch { return null; } }).WhereNotNull().FirstOrDefault());
public static TEnumeration ResolveUri<TEnumeration>(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<TEnumeration>)
.WhereNotNull()
.Select(d =>
{
try
{
return d(uri.ToString());
}
catch
{
return null;
}
})
.WhereNotNull()
.FirstOrDefault()
);

static MethodInfo? GetParseMethod<TEnumeration>(
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<TEnumeration>(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<TEnumeration>(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<TEnumeration>().CreateDelegate(typeof(ParseDelegate)) as ParseDelegate;
}
1 change: 1 addition & 0 deletions src/Resources/Enumeration.scriban
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/EnumerationJsonConverter.scriban
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if NO
namespace {{ dto_namespace }};
using {{ dto_namespace }}.Abstractions;

Expand Down Expand Up @@ -29,3 +30,4 @@ public partial {{ data_structure_type }} @{{ dto_type_name }}
public {{ field_name }}JsonConverterAttribute() : base(typeof({{ field_name }}JsonConverter)) { }
}
}
#endif
5 changes: 2 additions & 3 deletions src/Resources/IEnumeration.scriban
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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,
IHaveAValue<{{ enum_underlying_type }}>,
IHaveAValue<{{ enum_namespace }}.{{ enum_type_name }}>,
IHaveADescription,
IHaveAUri,
IHaveAuri,
IHaveAUriString,
IHaveSynonyms,
IHaveAShortName,
Expand All @@ -23,6 +23,5 @@ IHaveAName,
IEquatable<{{ enum_underlying_type }}>,
IEquatable<I{{ dto_type_name }}>
{
new uri Uri { get; }
int Order { get; }
}
Loading

0 comments on commit 6f334bd

Please sign in to comment.