Skip to content

Commit

Permalink
Try'n'a add support for base types; not there yet
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmjr committed Oct 22, 2023
1 parent 6e31d45 commit 780b457
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 69 deletions.
35 changes: 35 additions & 0 deletions Samples/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
date: 2023-07-13T05:44:46.048Z
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
liimitations:
- liability
- warranty
lastmod: 2023-08-29T17:13:51.216Z
license: MIT
slug: mit-license
title: MIT License
type: license
---

# MIT License

## Copyright © 2022-2023 [David G. Moore, Jr.](mailto:david@dgmjr.io "Send David an email") ([@dgmjr](https://github.com/dgmjr "Contact david 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: 27 additions & 0 deletions Samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title:
lastmod: 2023-47-20T21:47:50.3282-04:00Z
date: 2023-47-20T21:47:50.3284-04:00Z
license: MIT
slug: Dgmjr.Enumerations.CodeGeneration.Samples-readme
version:
authors:
- dgmjr;
description: Dgmjr.Enumerations.CodeGeneration.Samples Readme #TODO: write description for Dgmjr.Enumerations.CodeGeneration.Samples Readme
keywords:
- Dgmjr.Enumerations.CodeGeneration.Samples
- dgmjr
- dgmjr-io
type: readme
---
# Dgmjr.Enumerations.CodeGeneration.Samples Readme
<!-- TODO: Write the contents of the Dgmjr.Enumerations.CodeGeneration.Samples Readme file -->
## Package Description
## Getting Started
## Prerequisites
## Installation
## Usage
## Contributing
## Versioning
Built from [commit on branch at ]
(/tree/)
29 changes: 0 additions & 29 deletions samples/Dgmjr.Enumerations.CodeGeneration.Samples.csproj

This file was deleted.

13 changes: 0 additions & 13 deletions samples/Dgmjr.Enumerations.CodeGeneration.Samples.sln

This file was deleted.

32 changes: 25 additions & 7 deletions samples/YesNoIdc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@
*/
namespace System;

[GenerateEnumerationRecordClass]
[GenerateEnumerationRecordClass("YesNoIdc", "System", typeof(YesNo))]
public enum YesNoIdcEnum
{
/// <summary>I don't care</summary>
[Display(Name = "I don't care", ShortName = "IDC", Description = "I don't care", Prompt = "Yes/No/IDC?")]
[Display(
Name = "I don't care",
ShortName = "IDC",
Description = "I don't care",
Prompt = "Yes/No/IDC?"
)]
Idc = -1,

/// <summary>I don't give a fuck</summary>
[Display(Name = "I don't give a fuck", ShortName = "IDGAF", Description = "I don't give a fuck", Prompt = "Yes/No/IDC?")]
[Display(
Name = "I don't give a fuck",
ShortName = "IDGAF",
Description = "I don't give a fuck",
Prompt = "Yes/No/IDC?"
)]
Idgaf = Idc,
}

[GenerateEnumerationRecordClass]
[GenerateEnumerationRecordClass("YesNo", "System")]
public enum YesNoEnum
{
/// <summary>No, negative, nope, false, etc.</summary>
[Display(Name = "No", ShortName = "No", Description = "Negative", Prompt = "Yes or no?")]
No = 0,

/// <summary>Yes, affirmative, yep, true, etc.</summary>
[Display(Name = "Yes", ShortName = "Yes", Description = "Affirmative", Prompt = "Yes or no?")]
Yes = 1,
Expand All @@ -37,12 +49,18 @@ public enum YesNoEnum
public partial record class YesNo
{
// public YesNo() : this((YesNoEnum)No.Id) { }
public static implicit operator YesNo(YesNoEnum value) => FromValue(value);
public static implicit operator YesNo(YesNoEnum value) => FromValue(value) as YesNo;

// public static implicit operator YesNo(YesNoIdc yesNoIdc) => yesNoIdc.
}

[Display(Name = "Yes/No/IDC", ShortName = "Yes/No/IDC", Description = "Yes/No/IDC", Prompt = "Yes/No/IDC?")]
[Display(
Name = "Yes/No/IDC",
ShortName = "Yes/No/IDC",
Description = "Yes/No/IDC",
Prompt = "Yes/No/IDC?"
)]
public partial record class YesNoIdc : YesNo
{
// public YesNoIdc() : base() { }

}
17 changes: 17 additions & 0 deletions src/Dgmjr.Enumerations.CodeGenerator.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
* Dgmjr.Enumerations.CodeGenerator.csproj
*
* Created: 2023-10-10-09:00:48
* Modified: 2023-10-12-08:28:19
*
* Author: David G. Moore, Jr. <david@dgmjr.io>
*
* Copyright © 2022 - 2023 David G. Moore, Jr., All Rights Reserved
* License: MIT (https://opensource.org/licenses/MIT)
-->

<Project>
<ItemGroup>
<PackageReference Include="Dgmjr.Primitives" />
</ItemGroup>
</Project>
5 changes: 3 additions & 2 deletions src/EnumerationDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ internal record struct EnumerationDto(
get
{
var @this = this;
#pragma warning disable S2365
return EnumType
.GetMembers()
.OfType<IFieldSymbol>()
Expand All @@ -99,11 +100,11 @@ internal record struct EnumerationDto(
@this.DtoTypeName,
@this.DtoNamespace,
@this.EnumType?.EnumUnderlyingType?.MetadataName ?? "int",
@this.EnumType?.MetadataName,
@this.BaseType
@this.EnumType?.MetadataName
)
)
.ToArray();
#pragma warning restore S2365
}
}
}
6 changes: 5 additions & 1 deletion src/EnumerationFieldDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ internal record struct EnumerationFieldDto(
string DtoNamespace,
string EnumUnderlyingType,
string EnumType,
string? BaseType = default
type? BaseTypeType = default
)
{
private static readonly MD5 MD5 = MD5.Create();

public const string CompilerGeneratedAttributes = Constants.CompilerGeneratedAttributes;
public readonly string BaseType =>
IsClass ? (BaseTypeType?.FullName ?? string.Empty) : string.Empty;
public readonly string EnumTypeName => EnumType;
public readonly string EnumerationName => DtoTypeName;
public readonly string EnumNamespace => EnumSymbol.Type.ContainingNamespace.ToDisplayString();
public readonly string? Value => EnumSymbol.ConstantValue?.ToString();
public readonly bool IsRecord => DataStructureType.Contains(@record, OrdinalIgnoreCase);
public readonly bool IsClass => DataStructureType.Contains(@class, OrdinalIgnoreCase);
private readonly AttributeData? DisplayAttribute =>
EnumSymbol
.GetAttributes()
Expand Down
13 changes: 8 additions & 5 deletions src/EnumerationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public void Initialize(IigInitCtx context)

using (
_logger = (
new LoggerProvider(context).CreateLogger<EnumerationGenerator>()
as IDisposable
new LoggerProvider(context).CreateLogger<EnumerationGenerator>() as IDisposable
)!
)
{
Expand Down Expand Up @@ -209,6 +208,7 @@ ImmutableArray<GeneratorAttributeSyntaxContext> values
dataStructureType,
baseType
);
fileName = $"{dtoTypeName}.g.cs";
context.AddSource(
fileName,
$"""
Expand All @@ -234,7 +234,8 @@ ImmutableArray<GeneratorAttributeSyntaxContext> values
fieldName,
fieldValue,
dataStructureType,
dtoNamespace
dtoNamespace,
baseType
);

fileName = $"{dtoTypeName}.{fieldName}.g.cs";
Expand Down Expand Up @@ -286,7 +287,8 @@ type baseType
string fieldName,
object fieldValue,
string dataStructureType,
string dtoNamespace
string dtoNamespace,
type? baseType = default
)
{
Logger?.LogInformation($"Generating nested class for {enumSymbol.Name}.{fieldValue}");
Expand All @@ -299,7 +301,8 @@ string dtoNamespace
dtoTypeName,
dtoNamespace,
enumSymbol.EnumUnderlyingType.ToDisplayString(),
enumSymbol.MetadataName
enumSymbol.MetadataName,
baseType
)
)
);
Expand Down
22 changes: 10 additions & 12 deletions src/Resources/NestedEnumerationType.scriban
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace {{ dto_namespace }};
using {{ dto_namespace }}.Abstractions;

public partial {{ data_structure_type }} {{ dto_type_name }} {{ if base_type != "" && base_type != null }} : {{ base_type }}{{ end }}
public partial {{ data_structure_type }} @{{ dto_type_name }} {{ if base_type != "" && base_type != null }} : {{ base_type }}{{ end }}
{
/// <summary>{{ description }}</summary>
/// <value><see cref="{{ enum_namespace }}.{{ enum_type }}.{{ field_name }}" /><value>
{{ compiler_generated_attributes }}
public partial {{ data_structure_type }} {{ field_name }} :
public partial {{ data_structure_type }} @{{ field_name }} :
{{ if base_type != "" && base_type != null }}{{ dto_type_name }}, {{ end }}
I{{ enumeration_name }},
IHaveAName,
Expand All @@ -13,6 +15,7 @@ public partial {{ data_structure_type }} {{ dto_type_name }} {{ if base_type !=
IHaveAValue<{{ enum_namespace }}.{{ enum_type }}>,
IHaveADescription,
IHaveAUri,
IHaveAuri,
IHaveAUriString,
IHaveSynonyms,
IHaveAShortName,
Expand Down Expand Up @@ -126,26 +129,17 @@ public partial {{ data_structure_type }} {{ dto_type_name }} {{ if base_type !=

/// <summary>The {{ enumeration_name }}'s URI</summary>
/// <value>{{ uri_string }}</value>
uri I{{ dto_type_name }}.Uri => uri.From(Uri.ToString());
System.uri IHaveAuri.Uri => System.uri.From(Uri.ToString());

/// <summary>The order in which the {{ enumeration_name }} should appear</summary>
/// <value>{{ order }}</value>
/// <default>0</default>
int I{{ enumeration_name }}.Order => Order;

/// <summary>The {{ enumeration_name }}'s URI</summary>
/// <value>{{ uri_string }}</value>
string IHaveAUriString.UriString => UriString;

/// <summary>The {{ enumeration_name }}'s URI</summary>
/// <value>{{ uri_string }}</value>
uri I{{ dto_type_name }}.Uri => uri.From(Uri.ToString());

/// <summary>The order in which the {{ enumeration_name }} should appear</summary>
/// <value>{{ order }}</value>
/// <default>0</default>
int I{{ enumeration_name }}.Order => Order;

/// <summary>The {{ enumeration_name }}'s Id as an <see langword="object" /></summary>
/// <value>{{ id }}</value>
object IIdentifiable.Id => Id;
Expand Down Expand Up @@ -273,5 +267,9 @@ public partial {{ data_structure_type }} {{ dto_type_name }} {{ if base_type !=

/// <summary>An instance of the {{ enumeration_name }} as an I{{ enumeration_name }}</summary>
public static readonly I{{ enumeration_name }} Instance = new {{ field_name }}();

{{ if !is_record }}
public {{ if is_class }}override{{ end }} bool Equals(object other) => other is IHaveAValue<{{ enum_namespace }}.{{ enum_type }}> ihav ? this == ihav : false;
{{ end }}
}
}

0 comments on commit 780b457

Please sign in to comment.