-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: main
Are you sure you want to change the base?
Conversation
…s with enum name during *.proto schema generation
src/protobuf-net/Meta/MetaType.cs
Outdated
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;"); |
There was a problem hiding this comment.
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...
src/protobuf-net/Meta/MetaType.cs
Outdated
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)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
src/protobuf-net/Meta/MetaType.cs
Outdated
@@ -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(';'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
src/protobuf-net/Meta/MetaType.cs
Outdated
} | ||
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
There was a problem hiding this 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
@@ -2025,19 +2024,21 @@ public bool IsGroup | |||
|
|||
bool haveWrittenZero = false; | |||
// write zero values **first** | |||
|
|||
string enumPrefix = flags.HasFlag(SchemaGenerationFlags.PrefixEnumValuesWithEnumName) ? GetSchemaTypeName(callstack) + "_" : ""; |
There was a problem hiding this comment.
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
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.