Skip to content

Commit 7c8c000

Browse files
ThaTechMaestroabuyusuf
andauthored
Adds New SchemaGeneration Flag for Generating Prefixed Enum Members (#1133)
* adds support for EnumNamePrefix * update tests * resolve comments --------- Co-authored-by: abuyusuf <[email protected]>
1 parent 99eb2c3 commit 7c8c000

3 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎src/protobuf-net.Core/Meta/SchemaGenerationOptions.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public enum SchemaGenerationFlags
6868
/// Record the sub-type relationship formally in schemas
6969
/// </summary>
7070
PreserveSubType = 1 << 1,
71+
72+
/// <summary>
73+
/// Provides support for adding Prefix to names of Enum members in schemas
74+
/// </summary>
75+
IncludeEnumNamePrefix = 1 << 2,
7176
}
7277

7378

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Xunit;
2+
using ProtoBuf.Meta;
3+
4+
namespace ProtoBuf.Test.Issues
5+
{
6+
public class Issue323
7+
{
8+
const string ExpectedProtoWithPrefixedEnumMembers = @"syntax = ""proto3"";
9+
package ProtoBuf.Test.Issues;
10+
11+
enum TokenType {
12+
TokenType_Temporary = 0;
13+
TokenType_Persistent = 1;
14+
}
15+
";
16+
17+
const string ExpectedProtoWithoutPrefixedEnumMembers = @"syntax = ""proto3"";
18+
package ProtoBuf.Test.Issues;
19+
20+
enum TokenType {
21+
Temporary = 0;
22+
Persistent = 1;
23+
}
24+
";
25+
26+
[Fact]
27+
public void GeneratesProtoWithPrefixedEnumMembers()
28+
{
29+
string proto = Serializer.GetProto(new SchemaGenerationOptions { Types = { typeof(TokenType) },
30+
Syntax = ProtoSyntax.Proto3, Flags = SchemaGenerationFlags.IncludeEnumNamePrefix });
31+
32+
Assert.Equal(ExpectedProtoWithPrefixedEnumMembers, proto, ignoreLineEndingDifferences: true);
33+
}
34+
35+
[Fact]
36+
public void GeneratesProtoWithoutPrefixedEnumMembers()
37+
{
38+
string proto = Serializer.GetProto(new SchemaGenerationOptions
39+
{
40+
Types = { typeof(TokenType) },
41+
Syntax = ProtoSyntax.Proto3,
42+
Flags = SchemaGenerationFlags.None,
43+
});
44+
45+
Assert.Equal(ExpectedProtoWithoutPrefixedEnumMembers, proto, ignoreLineEndingDifferences: true);
46+
}
47+
48+
[ProtoContract]
49+
public enum TokenType
50+
{
51+
[ProtoEnum]
52+
Temporary,
53+
[ProtoEnum]
54+
Persistent,
55+
}
56+
}
57+
}

‎src/protobuf-net/Meta/MetaType.cs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,8 @@ internal void WriteSchema(HashSet<Type> callstack, StringBuilder builder, int in
19721972
if (surrogateType is not null) return; // nothing to write
19731973

19741974
bool multipleNamespaceSupport = (flags & SchemaGenerationFlags.MultipleNamespaceSupport) != 0;
1975+
bool isEnumNamePrefixSupported = (flags & SchemaGenerationFlags.IncludeEnumNamePrefix) != 0;
1976+
19751977
var repeated = model.TryGetRepeatedProvider(Type);
19761978

19771979
if (repeated is not null)
@@ -2028,6 +2030,7 @@ internal void WriteSchema(HashSet<Type> callstack, StringBuilder builder, int in
20282030
else if (Type.IsEnum)
20292031
{
20302032
var enums = GetEnumValues();
2033+
string enumNamePrefix = isEnumNamePrefixSupported ? $"{GetSchemaTypeName(callstack)}_" : "";
20312034

20322035
bool allValid = IsValidEnum(enums);
20332036
if (!allValid) NewLine(builder, indent).Append("/* for context only");
@@ -2063,14 +2066,15 @@ internal void WriteSchema(HashSet<Type> callstack, StringBuilder builder, int in
20632066
var parsed = member.TryGetInt32();
20642067
if (parsed.HasValue && parsed.Value == 0)
20652068
{
2066-
NewLine(builder, indent + 1).Append(member.Name).Append(" = 0;");
2069+
NewLine(builder, indent + 1).Append(enumNamePrefix).Append(member.Name).Append(" = 0;");
20672070
haveWrittenZero = true;
20682071
}
20692072
}
20702073

20712074
if (syntax == ProtoSyntax.Proto3 && !haveWrittenZero)
20722075
{
2073-
NewLine(builder, indent + 1).Append("ZERO = 0; // proto3 requires a zero value as the first item (it can be named anything)");
2076+
NewLine(builder, indent + 1).Append(enumNamePrefix).Append("ZERO").Append(" = 0;")
2077+
.Append(" // proto3 requires a zero value as the first item (it can be named anything)");
20742078
}
20752079

20762080
// note array is already sorted, so zero would already be first
@@ -2080,11 +2084,12 @@ internal void WriteSchema(HashSet<Type> callstack, StringBuilder builder, int in
20802084
if (parsed.HasValue)
20812085
{
20822086
if (parsed.Value == 0) continue;
2083-
NewLine(builder, indent + 1).Append(member.Name).Append(" = ").Append(parsed.Value).Append(';');
2087+
NewLine(builder, indent + 1).Append(enumNamePrefix).Append(member.Name).Append(" = ").Append(parsed.Value).Append(';');
20842088
}
20852089
else
20862090
{
2087-
NewLine(builder, indent + 1).Append("// ").Append(member.Name).Append(" = ").Append(member.Value).Append(';').Append(" // note: enums should be valid 32-bit integers");
2091+
NewLine(builder, indent + 1).Append("// ").Append(enumNamePrefix).Append(member.Name)
2092+
.Append(" = ").Append(member.Value).Append(';').Append(" // note: enums should be valid 32-bit integers");
20882093
}
20892094
}
20902095
if (HasReservations) AppendReservations();

0 commit comments

Comments
 (0)