Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested enums #27

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 77 additions & 97 deletions src/EnumUtilities/CodeWriters/EnumExtensionsWriter.cs

Large diffs are not rendered by default.

49 changes: 23 additions & 26 deletions src/EnumUtilities/CodeWriters/EnumExtensionsWriter.tt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ using System;
<#
if (!string.IsNullOrEmpty(Model.Namespace))
{
#>
namespace <#= Model.Namespace #>
{
<#
WriteLine($"namespace {Model.Namespace}");
WriteLine("{");
PushIndent();
}
#>
Expand All @@ -30,15 +28,15 @@ namespace <#= Model.Namespace #>
#>
/// <summary>Converts the value of this instance to its equivalent string representation.</summary>
/// <returns>The string representation of the value of this instance.</returns>
public static string ToStringFast(this <#= Model.Name #> value)
public static string ToStringFast(this <#= Model.RefName #> value)
{
return value switch
{
<#
foreach (var curr in Model.UniqueValues)
{
#>
<#= Model.Name #>.<#= curr.MemberName #> => nameof(<#= Model.Name #>.<#= curr.MemberName #>),
<#= Model.RefName #>.<#= curr.MemberName #> => nameof(<#= Model.RefName #>.<#= curr.MemberName #>),
<#
}
#>
Expand All @@ -48,7 +46,7 @@ namespace <#= Model.Namespace #>

/// <summary>Returns a boolean telling whether the value of this instance exists in the enumeration.</summary>
/// <returns><c>true</c> if the value of this instance exists in the enumeration; <c>false</c> otherwise.</returns>
public static bool IsDefined(this <#= Model.Name #> value)
public static bool IsDefined(this <#= Model.RefName #> value)
{
return <#= Model.Name #>Validation.IsDefined(value);
}
Expand All @@ -60,17 +58,18 @@ namespace <#= Model.Namespace #>
{
#>

public static string ToEnumMemberValue(this <#= Model.Name #> value)
public static string ToEnumMemberValue(this <#= Model.RefName #> value)
{
return value switch
{
<#
foreach (var curr in Model.UniqueValues)
{
#>
<#= Model.Name #>.<#= curr.MemberName #> => <#= curr.SerializationValue != null
? Append($"\"{curr.SerializationValue}\"")
: Append($"nameof({Model.Name}.{curr.MemberName})") #>,
<#= Model.RefName #>.<#= curr.MemberName #> => <#=
curr.SerializationValue != null
? Append($"\"{curr.SerializationValue}\"")
: Append($"nameof({Model.RefName}.{curr.MemberName})") #>,
<#
}
#>
Expand All @@ -88,15 +87,15 @@ namespace <#= Model.Namespace #>
{
#>

public static string? GetDescription(this <#= Model.Name #> value)
public static string? GetDescription(this <#= Model.RefName #> value)
{
return value switch
{
<#
foreach (var curr in Model.UniqueValues.Where(x => x.Description != null))
{
#>
<#= Model.Name #>.<#= curr.MemberName #> => "<#= curr.Description #>",
<#= Model.RefName #>.<#= curr.MemberName #> => "<#= curr.Description #>",
<#
}
#>
Expand All @@ -114,38 +113,38 @@ namespace <#= Model.Namespace #>
{
#>

public static string GetDisplayShortName(this <#= Model.Name #> value)
public static string GetDisplayShortName(this <#= Model.RefName #> value)
{
return value switch
{
<#
foreach (var curr in Model.UniqueValues.Where(x => x.Display?.ShortName != null))
{
#>
<#= Model.Name #>.<#= curr.MemberName #> => <#= curr.Display!.ResourceShortName != null
? Append(curr.Display.ResourceShortName)
: Append($"\"{curr.Display.ShortName}\"") #>,
<#= Model.RefName #>.<#= curr.MemberName #> => <#= curr.Display!.ResourceShortName != null
? Append(curr.Display.ResourceShortName)
: Append($"\"{curr.Display.ShortName}\"") #>,
<#
}
#>
_ => GetDisplayName(value)
};
}

public static string GetDisplayName(this <#= Model.Name #> value)
public static string GetDisplayName(this <#= Model.RefName #> value)
{
return value switch
{
<#
foreach (var curr in Model.UniqueValues)
{
#>
<#= Model.Name #>.<#= curr.MemberName #> => <#=
<#= Model.RefName #>.<#= curr.MemberName #> => <#=
curr.Display?.ResourceName != null
? Append(curr.Display.ResourceName)
: curr.Display?.Name != null
? Append($"\"{curr.Display.Name}\"")
: Append($"nameof({Model.Name}.{curr.MemberName})") #>,
: Append($"nameof({Model.RefName}.{curr.MemberName})") #>,
<#
}
#>
Expand All @@ -160,15 +159,15 @@ namespace <#= Model.Namespace #>
{
#>

public static string? GetDescription(this <#= Model.Name #> value)
public static string? GetDescription(this <#= Model.RefName #> value)
{
return value switch
{
<#
foreach (var curr in Model.UniqueValues.Where(x => x.Display?.Description != null))
{
#>
<#= Model.Name #>.<#= curr.MemberName #> => <#=
<#= Model.RefName #>.<#= curr.MemberName #> => <#=
curr.Display!.ResourceDescription != null
? Append(curr.Display.ResourceDescription)
: Append($"\"{curr.Display.Description}\"") #>,
Expand All @@ -186,15 +185,13 @@ namespace <#= Model.Namespace #>
if (!string.IsNullOrEmpty(Model.Namespace))
{
PopIndent();
#>
}
<#
WriteLine("}");
}
#>
<#+
public EnumExtensionsWriter(StringBuilder builder) : base(builder)
{
}

public override string GetFileName() => $"{Model.Namespace}.{Model.Name}Extensions.g.cs";
public override string GetFileName() => $"{Model.Namespace ?? "_"}.{Model.Name}Extensions.g.cs";
#>
Loading