Skip to content

Added optional flag for pre-pending Enum name to Enum values to Proto generation #828

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MichaelKono
Copy link

Added a new flag in SchemaGenerationFlags for applying ( EnumName_ + ValueName ) to the generation of enum values.

Google's Proto compiler requires globally unique enum field names. Adding this prefix fixes compilation issues with Google's proto library. Most generators (C# for sure) will strip the prefix because it is part of Protobuf style guidelines.

Michael Konoplyan and others added 2 commits August 17, 2021 15:33
foreach (var member in enums)
{
var parsed = member.TryGetInt32();
if (parsed.HasValue && parsed.Value == 0)
{
NewLine(builder, indent + 1).Append(member.Name).Append(" = 0;");
NewLine(builder, indent + 1).Append(enumPrefix + member.Name).Append(" = 0;");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be two separate append calls, to prevent the extra alloc? Not really going to make a huge difference in real terms, but since we have the builder...

haveWrittenZero = true;
}
}

if (syntax == ProtoSyntax.Proto3 && !haveWrittenZero)
{
NewLine(builder, indent + 1).Append("ZERO = 0; // proto3 requires a zero value as the first item (it can be named anything)");
NewLine(builder, indent + 1)
.Append(enumPrefix + "ZERO = 0; // proto3 requires a zero value as the first item (it can be named anything)");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

@@ -2047,11 +2049,11 @@ public bool IsGroup
if (parsed.HasValue)
{
if (parsed.Value == 0) continue;
NewLine(builder, indent + 1).Append(member.Name).Append(" = ").Append(parsed.Value).Append(';');
NewLine(builder, indent + 1).Append(enumPrefix + member.Name).Append(" = ").Append(parsed.Value).Append(';');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

}
else
{
NewLine(builder, indent + 1).Append("// ").Append(member.Name).Append(" = ").Append(member.Value).Append(';').Append(" // note: enums should be valid 32-bit integers");
NewLine(builder, indent + 1).Append("// ").Append(enumPrefix + member.Name).Append(" = ").Append(member.Value).Append(';').Append(" // note: enums should be valid 32-bit integers");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Member

@mgravell mgravell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good, thanks. A really small nit around append usage, but: +1

@MichaelKono MichaelKono requested a review from mgravell August 23, 2021 22:15
@@ -2025,19 +2024,21 @@ public bool IsGroup

bool haveWrittenZero = false;
// write zero values **first**

string enumPrefix = flags.HasFlag(SchemaGenerationFlags.PrefixEnumValuesWithEnumName) ? GetSchemaTypeName(callstack) + "_" : "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit: boxing, but... I don't think it is going to break the world

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants