Skip to content

Commit

Permalink
Added the new Runtime project
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmjr committed Dec 29, 2023
1 parent d6dbd60 commit b8d3576
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 33 deletions.
11 changes: 11 additions & 0 deletions Runtime/Dgmjr.Enumerations.CodeGenerator.Runtime.csproj
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>
42 changes: 42 additions & 0 deletions Runtime/Dgmjr.Enumerations.CodeGenerator.Runtime.sln
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
34 changes: 34 additions & 0 deletions Runtime/EnumerationJsonConverter.cs
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);
}
}
7 changes: 7 additions & 0 deletions Runtime/EnumerationJsonConverterAttribute.cs
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)) { }
}
20 changes: 20 additions & 0 deletions Runtime/EnumerationJsonConverterFactory.cs
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);
}
}
80 changes: 80 additions & 0 deletions Runtime/EnumerationSerializationDto.cs
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 }
}
);
}
27 changes: 27 additions & 0 deletions Runtime/README.md
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/)
50 changes: 50 additions & 0 deletions Runtime/UniversalUriResolver.cs
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;
}
1 change: 1 addition & 0 deletions Samples/Dgmjr.Enumerations.CodeGenerator.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dgmjr.Enumerations.CodeGenerator" IncludeAssets="Analyzers;Build" ExcludeAssets="Native;BuildTransitive;BuildMultitargeting;ContentFiles;Compile;Runtime" PrivateAssets="All" />
<PackageReference Include="Dgmjr.Enumerations.CodeGenerator.Runtime" />
<!-- <PackageReference Include="Dgmjr.Enumerations.CodeGenerator" IncludeAssets="Analyzers;Build" ExcludeAssets="Native;BuildTransitive;ContentFiles" PrivateAssets="All" /> -->
<!-- <ProjectReference Include="../src/Dgmjr.Enumerations.CodeGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" IncludeAssets="Analyzers;Build" ExcludeAssets="Native;BuildTransitive;ContentFiles" PrivateAssets="None" /> -->
<PackageReference Include="Dgmjr.Enumerations.Enumeration" />
Expand Down
26 changes: 13 additions & 13 deletions Samples/Dgmjr.Enumerations.CodeGenerator.Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\..\..\..\..\Packages\Versions.Local.props = ..\..\..\..\..\Packages\Versions.Local.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Enumerations.CodeGenerator.Samples", "Dgmjr.Enumerations.CodeGenerator.Samples.csproj", "{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Enumerations.CodeGenerator.Samples", "Dgmjr.Enumerations.CodeGenerator.Samples.csproj", "{D33298F0-5910-47AB-A268-803BACB636B9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -20,18 +20,18 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Local|Any CPU.ActiveCfg = Local|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Local|Any CPU.Build.0 = Local|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Testing|Any CPU.ActiveCfg = Testing|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Testing|Any CPU.Build.0 = Testing|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Staging|Any CPU.ActiveCfg = Staging|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Staging|Any CPU.Build.0 = Staging|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Production|Any CPU.ActiveCfg = Local|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Production|Any CPU.Build.0 = Local|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72AF42ED-1B8A-46BA-BDB6-E666F7019AF7}.Release|Any CPU.Build.0 = Release|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Local|Any CPU.ActiveCfg = Local|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Local|Any CPU.Build.0 = Local|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Testing|Any CPU.ActiveCfg = Testing|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Testing|Any CPU.Build.0 = Testing|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Staging|Any CPU.ActiveCfg = Staging|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Staging|Any CPU.Build.0 = Staging|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Production|Any CPU.ActiveCfg = Local|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Production|Any CPU.Build.0 = Local|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D33298F0-5910-47AB-A268-803BACB636B9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 5 additions & 0 deletions src/Dgmjr.Enumerations.CodeGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<PackageReference Include="ThisAssembly.Project" />
<PackageReference Include="System.Text.Json.Usings" IncludeAssets="Build;BuildTransitive;BuildMultitargeting;Runtime;Compile" ExcludeAssets="ContentFiles;Native;Analyzers" PrivateAssets="None" />
<PackageReference Include="System.Private.CoreLib.Polyfills" IncludeAssets="ContentFiles;Build;BuildTransitive;BuildMultitargeting" ExcludeAssets="Analyzers;Runtime;Native" PrivateAssets="All" />
<ProjectReference Include="../Runtime/Dgmjr.Enumerations.CodeGenerator.Runtime.csproj" />
</ItemGroup>

<ItemGroup>
Expand All @@ -54,6 +55,10 @@
<!-- <PackageFile Include="./Runtime/bin/**/*.dll" PackagePath="lib/$(TargetFramework)/%(Filename)%(Extension)" Visible="false" /> -->
</ItemGroup>

<ItemGroup>
<Using Include="System.Collections.Immutable" />
</ItemGroup>

<!-- <Target Name="BuildRuntime" BeforeTargets="Build">
<MSBuild Projects="./Runtime/Dgmjr.Enumerations.CodeGenerator.Runtime.csproj" Properties="Configuration=$(Configuration);TargetFramework=$(TargetFramework)" Targets="Restore;Build" />
</Target> -->
Expand Down
14 changes: 14 additions & 0 deletions src/Dgmjr.Enumerations.CodeGenerator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\..\..\..\..\Packages\Versions.Local.props = ..\..\..\..\..\Packages\Versions.Local.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Enumerations.CodeGenerator.Runtime", "..\Runtime\Dgmjr.Enumerations.CodeGenerator.Runtime.csproj", "{C259423F-CBF0-4111-9B9F-356907207E00}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Enumerations.CodeGenerator", "Dgmjr.Enumerations.CodeGenerator.csproj", "{C4622771-2A7B-42E6-9B3C-8F71CDD7E65B}"
EndProject
Global
Expand All @@ -20,6 +22,18 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C259423F-CBF0-4111-9B9F-356907207E00}.Local|Any CPU.ActiveCfg = Local|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Local|Any CPU.Build.0 = Local|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Testing|Any CPU.ActiveCfg = Testing|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Testing|Any CPU.Build.0 = Testing|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Staging|Any CPU.ActiveCfg = Staging|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Staging|Any CPU.Build.0 = Staging|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Production|Any CPU.ActiveCfg = Local|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Production|Any CPU.Build.0 = Local|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C259423F-CBF0-4111-9B9F-356907207E00}.Release|Any CPU.Build.0 = Release|Any CPU
{C4622771-2A7B-42E6-9B3C-8F71CDD7E65B}.Local|Any CPU.ActiveCfg = Local|Any CPU
{C4622771-2A7B-42E6-9B3C-8F71CDD7E65B}.Local|Any CPU.Build.0 = Local|Any CPU
{C4622771-2A7B-42E6-9B3C-8F71CDD7E65B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down
Loading

0 comments on commit b8d3576

Please sign in to comment.