-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
320 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Dgmjr.Abstractions" /> | ||
<PackageReference Include="Dgmjr.Primitives" /> | ||
<PackageReference Include="System.Collections.Immutable" /> | ||
<PackageReference Include="System.Text.Json.Usings" IncludeAssets="Build;BuildTransitive;BuildMultitargeting;Runtime;Compile" ExcludeAssets="ContentFiles;Native;Analyzers" PrivateAssets="None" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B283EBC2-E01F-412D-9339-FD56EF114549}" | ||
ProjectSection(SolutionItems) = preProject | ||
..\..\..\..\..\Directory.Build.props = ..\..\..\..\..\Directory.Build.props | ||
..\..\..\..\..\Directory.Build.targets = ..\..\..\..\..\Directory.Build.targets | ||
..\..\..\..\..\global.json = ..\..\..\..\..\global.json | ||
..\..\..\..\..\Packages\Versions.Local.props = ..\..\..\..\..\Packages\Versions.Local.props | ||
EndProjectSection | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Enumerations.CodeGenerator.Runtime", "Dgmjr.Enumerations.CodeGenerator.Runtime.csproj", "{E33DF0D9-620C-4445-82BB-6A5E88D76295}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Local|Any CPU = Local|Any CPU | ||
Debug|Any CPU = Debug|Any CPU | ||
Testing|Any CPU = Testing|Any CPU | ||
Staging|Any CPU = Staging|Any CPU | ||
Production|Any CPU = Production|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Local|Any CPU.ActiveCfg = Local|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Local|Any CPU.Build.0 = Local|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Testing|Any CPU.ActiveCfg = Testing|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Testing|Any CPU.Build.0 = Testing|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Staging|Any CPU.ActiveCfg = Staging|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Staging|Any CPU.Build.0 = Staging|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Production|Any CPU.ActiveCfg = Local|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Production|Any CPU.Build.0 = Local|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{E33DF0D9-620C-4445-82BB-6A5E88D76295}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {44E1535F-C020-4710-88C8-13B03F0C33B8} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace Dgmjr.Enumerations; | ||
|
||
using Dgmjr.Abstractions; | ||
|
||
public class EnumerationJsonConverter<TEnumeration> : JsonConverter<TEnumeration> | ||
where TEnumeration : IIdentifiable, | ||
IHaveADescription, | ||
IHaveAName, | ||
IHaveAShortName, | ||
IHaveAUri, | ||
IHaveAValue, | ||
IHaveADisplayName, | ||
IHaveAGuid | ||
{ | ||
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 | ||
) | ||
{ | ||
var dto = EnumerationSerializationDto<TEnumeration>.FromEnumeration(value); | ||
Serialize(writer, dto, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Dgmjr.Enumerations; | ||
|
||
public sealed class EnumerationJsonConverterAttribute : JConverterAttribute | ||
{ | ||
public EnumerationJsonConverterAttribute() | ||
: base(typeof(EnumerationJsonConverterFactory)) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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)]; | ||
|
||
public override bool CanConvert(type typeToConvert) => | ||
TrueForAll(Interfaces, i => i.IsAssignableFrom(typeToConvert)); | ||
|
||
public override JConverter? CreateConverter( | ||
type typeToConvert, | ||
Jso options | ||
) | ||
{ | ||
var converterType = typeof(EnumerationJsonConverter<>).MakeGenericType(typeToConvert); | ||
return (JConverter?)Activator.CreateInstance(converterType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
namespace Dgmjr.Enumerations; | ||
|
||
using System.Collections.Concurrent; | ||
using System.Collections.Immutable; | ||
using System.Text.Json.Nodes; | ||
|
||
using Dgmjr.Abstractions; | ||
|
||
public record class EnumerationSerializationDto(uri Uri, | ||
string Description, | ||
IDictionary<string, object> Ids, | ||
IStringDictionary Names | ||
) | ||
{ | ||
protected const string Id = "id"; | ||
protected const string Guid = "guid"; | ||
protected const string Name = "name"; | ||
protected const string Short = "short"; | ||
protected const string Display = "display"; | ||
public static readonly EnumerationSerializationDto Empty = | ||
new( | ||
uri.Empty, | ||
string.Empty, | ||
ImmutableDictionary<string, object>.Empty, | ||
ImmutableDictionary<string, string>.Empty | ||
); | ||
|
||
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 } }, | ||
new StringDictionary() | ||
{ | ||
{ Name, (e as IHaveAName).Name }, | ||
{ Short, (e as IHaveAShortName).ShortName }, | ||
{ Display, (e as IHaveADisplayName).DisplayName } | ||
} | ||
); | ||
|
||
public uri Uri { get; init; } = Uri; | ||
public string Description { get; init; } = Description; | ||
public IDictionary<string, object> Ids { get; init; } = | ||
new Dictionary<string, object>(Ids, StringComparer.OrdinalIgnoreCase); | ||
public IStringDictionary Names { get; init; } = | ||
new StringDictionary(Names, StringComparer.OrdinalIgnoreCase); | ||
} | ||
|
||
public record class EnumerationSerializationDto<TEnumeration>( | ||
uri Uri, | ||
string Description, | ||
IDictionary<string, object> Ids, | ||
IStringDictionary Names | ||
) : EnumerationSerializationDto(Uri, Description, Ids, Names) | ||
where TEnumeration : IIdentifiable, | ||
IHaveADescription, | ||
IHaveAName, | ||
IHaveAShortName, | ||
IHaveAUri, | ||
IHaveAValue, | ||
IHaveADisplayName, | ||
IHaveAGuid | ||
{ | ||
public static EnumerationSerializationDto<TEnumeration> FromEnumeration( | ||
TEnumeration e | ||
) => | ||
new( | ||
e.Uri.ToString(), | ||
e.Description, | ||
new Dictionary<string, object>() { { Id, e.Id }, { Guid, e.Guid } }, | ||
new StringDictionary() | ||
{ | ||
{ Name, e.Name }, | ||
{ Short, e.ShortName }, | ||
{ Display, e.DisplayName } | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
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 | ||
<!-- TODO: Write the contents of the Dgmjr.Enumerations.CodeGenerator.Runtime Readme file --> | ||
## Package Description | ||
## Getting Started | ||
## Prerequisites | ||
## Installation | ||
## Usage | ||
## Contributing | ||
## Versioning | ||
Built from [commit on branch main at ] | ||
(/tree/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
namespace Dgmjr.Enumerations; | ||
|
||
using System.Collections.Concurrent; | ||
|
||
using Dgmjr.Abstractions; | ||
|
||
public static class UniversalUriResolver | ||
{ | ||
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()); | ||
|
||
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 | ||
=> | ||
type.GetParseMethod<TEnumeration>().CreateDelegate(typeof(ParseDelegate)) as ParseDelegate; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.