Skip to content

Commit

Permalink
Generate const values if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahStolk committed May 31, 2024
1 parent c32c024 commit 64f0830
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Intellenum/Generators/ClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public string BuildClass(VoWorkItem item, TypeDeclarationSyntax tds, bool isNetF
global::System.IComparable,
global::System.IComparable<{className}>
{{
{MemberGeneration.GenerateConstValuesIfPossible(item)}
{Util.GenerateLazyLookupsIfNeeded(item)}
#if DEBUG
Expand Down Expand Up @@ -230,4 +232,4 @@ private static string GenerateNullCheckIfNeeded(VoWorkItem voWorkItem) =>
throw new {nameof(IntellenumCreationFailedException)}(""Cannot create an Intellenum member with a null."");
}}
";
}
}
19 changes: 18 additions & 1 deletion src/Intellenum/MemberGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,23 @@ public static BuildResult TryBuildMemberValueAsText(string propertyName, object
$"Member '{propertyName}' has a value type '{propertyValue.GetType()}' of '{propertyValue}' which cannot be converted to the underlying type of '{underlyingType}' - {e.Message}");
}
}

public static string GenerateConstValuesIfPossible(VoWorkItem item)
{
if (!item.IsConstant || item.MemberProperties.Count == 0)
{
return string.Empty;
}

StringBuilder sb = new StringBuilder("// const fields...");
sb.AppendLine();
foreach (var memberProperties in item.MemberProperties)
{
sb.AppendLine($"public const {item.UnderlyingTypeFullName} {memberProperties.FieldName}Const = {memberProperties.ValueAsText};");
}

return sb.ToString();
}

public static string GeneratePrivateConstructionInitialisationIfNeeded(VoWorkItem item)
{
Expand Down Expand Up @@ -226,4 +243,4 @@ public static string GeneratePrivateConstructionInitialisationIfNeeded(VoWorkIte
""");
return sb.ToString();
}
}
}

0 comments on commit 64f0830

Please sign in to comment.