Commit 7ee85c4e authored by Jon Skeet's avatar Jon Skeet

Added field constant generation

parent d33bff04
......@@ -100,6 +100,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook {
get { return global::Google.ProtocolBuffers.Examples.AddressBook.AddressBookProtos.internal__static_tutorial_Person_PhoneNumber__FieldAccessorTable; }
}
public const int NumberFieldNumber = 1;
private bool hasNumber;
private string number_ = "";
public bool HasNumber {
......@@ -109,6 +110,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook {
get { return number_; }
}
public const int TypeFieldNumber = 2;
private bool hasType;
private global::Google.ProtocolBuffers.Examples.AddressBook.Person.Types.PhoneType type_ = global::Google.ProtocolBuffers.Examples.AddressBook.Person.Types.PhoneType.HOME;
public bool HasType {
......@@ -332,6 +334,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook {
}
#endregion
public const int NameFieldNumber = 1;
private bool hasName;
private string name_ = "";
public bool HasName {
......@@ -341,6 +344,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook {
get { return name_; }
}
public const int IdFieldNumber = 2;
private bool hasId;
private int id_ = 0;
public bool HasId {
......@@ -350,6 +354,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook {
get { return id_; }
}
public const int EmailFieldNumber = 3;
private bool hasEmail;
private string email_ = "";
public bool HasEmail {
......@@ -359,6 +364,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook {
get { return email_; }
}
public const int PhoneFieldNumber = 4;
private pbc::PopsicleList<global::Google.ProtocolBuffers.Examples.AddressBook.Person.Types.PhoneNumber> phone_ = new pbc::PopsicleList<global::Google.ProtocolBuffers.Examples.AddressBook.Person.Types.PhoneNumber>();
public scg::IList<global::Google.ProtocolBuffers.Examples.AddressBook.Person.Types.PhoneNumber> PhoneList {
get { return phone_; }
......@@ -685,6 +691,7 @@ namespace Google.ProtocolBuffers.Examples.AddressBook {
get { return global::Google.ProtocolBuffers.Examples.AddressBook.AddressBookProtos.internal__static_tutorial_AddressBook__FieldAccessorTable; }
}
public const int PersonFieldNumber = 1;
private pbc::PopsicleList<global::Google.ProtocolBuffers.Examples.AddressBook.Person> person_ = new pbc::PopsicleList<global::Google.ProtocolBuffers.Examples.AddressBook.Person>();
public scg::IList<global::Google.ProtocolBuffers.Examples.AddressBook.Person> PersonList {
get { return person_; }
......
......@@ -24,6 +24,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
break;
}
writer.WriteLine ("public const int {0} = {1};", GetFieldConstantName(Descriptor), Descriptor.FieldNumber);
if (Descriptor.IsRepeated) {
writer.WriteLine("{0} static readonly", ClassAccessLevel);
writer.WriteLine(" pb::GeneratedExtensionBase<scg::IList<{0}>> {1} =", type, name);
......
......@@ -107,6 +107,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
foreach(FieldDescriptor fieldDescriptor in Descriptor.Fields) {
// Rats: we lose the debug comment here :(
writer.WriteLine("public const int {0} = {1};", GetFieldConstantName(fieldDescriptor), fieldDescriptor.FieldNumber);
SourceGenerators.CreateFieldGenerator(fieldDescriptor).GenerateMembers(writer);
writer.WriteLine();
}
......@@ -186,7 +187,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
writer.WriteLine();
}
private void GenerateSerializeOneField(TextGenerator writer, FieldDescriptor fieldDescriptor) {
private static void GenerateSerializeOneField(TextGenerator writer, FieldDescriptor fieldDescriptor) {
SourceGenerators.CreateFieldGenerator(fieldDescriptor).GenerateSerializationCode(writer);
}
......
......@@ -30,6 +30,10 @@ namespace Google.ProtocolBuffers.ProtoGen {
}
}
internal static string GetFieldConstantName(FieldDescriptor field) {
return NameHelpers.UnderscoresToPascalCase(GetFieldName(field)) + "FieldNumber";
}
private static string ToCSharpName(string name, FileDescriptor file) {
string result = file.CSharpOptions.Namespace;
if (file.CSharpOptions.NestClasses) {
......
......@@ -397,5 +397,32 @@ namespace Google.ProtocolBuffers {
TestAllTypes message = builder.Build();
TestUtil.AssertAllFieldsSet(message.ToBuilder().Build());
}
[Test]
public void FieldConstantValues() {
Assert.AreEqual(TestAllTypes.Types.NestedMessage.BbFieldNumber, 1);
Assert.AreEqual(TestAllTypes.OptionalInt32FieldNumber, 1);
Assert.AreEqual(TestAllTypes.OptionalGroupFieldNumber, 16);
Assert.AreEqual(TestAllTypes.OptionalNestedMessageFieldNumber, 18);
Assert.AreEqual(TestAllTypes.OptionalNestedEnumFieldNumber, 21);
Assert.AreEqual(TestAllTypes.RepeatedInt32FieldNumber, 31);
Assert.AreEqual(TestAllTypes.RepeatedGroupFieldNumber, 46);
Assert.AreEqual(TestAllTypes.RepeatedNestedMessageFieldNumber, 48);
Assert.AreEqual(TestAllTypes.RepeatedNestedEnumFieldNumber, 51);
}
[Test]
public void ExtensionConstantValues() {
Assert.AreEqual(TestRequired.SingleFieldNumber, 1000);
Assert.AreEqual(TestRequired.MultiFieldNumber, 1001);
Assert.AreEqual(UnitTestProtoFile.OptionalInt32ExtensionFieldNumber, 1);
Assert.AreEqual(UnitTestProtoFile.OptionalGroupExtensionFieldNumber, 16);
Assert.AreEqual(UnitTestProtoFile.OptionalNestedMessageExtensionFieldNumber, 18);
Assert.AreEqual(UnitTestProtoFile.OptionalNestedEnumExtensionFieldNumber, 21);
Assert.AreEqual(UnitTestProtoFile.RepeatedInt32ExtensionFieldNumber, 31);
Assert.AreEqual(UnitTestProtoFile.RepeatedGroupExtensionFieldNumber, 46);
Assert.AreEqual(UnitTestProtoFile.RepeatedNestedMessageExtensionFieldNumber, 48);
Assert.AreEqual(UnitTestProtoFile.RepeatedNestedEnumExtensionFieldNumber, 51);
}
}
}
......@@ -60,6 +60,7 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return global::Google.ProtocolBuffers.TestProtos.UnitTestEmbedOptimizeForProtoFile.internal__static_protobuf_unittest_TestEmbedOptimizedForSize__FieldAccessorTable; }
}
public const int OptionalMessageFieldNumber = 1;
private bool hasOptionalMessage;
private global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize optionalMessage_ = global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize.DefaultInstance;
public bool HasOptionalMessage {
......@@ -69,6 +70,7 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return optionalMessage_; }
}
public const int RepeatedMessageFieldNumber = 2;
private pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize> repeatedMessage_ = new pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize>();
public scg::IList<global::Google.ProtocolBuffers.TestProtos.TestOptimizedForSize> RepeatedMessageList {
get { return repeatedMessage_; }
......
......@@ -66,6 +66,7 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return global::Google.ProtocolBuffers.TestProtos.UnitTestImportProtoFile.internal__static_protobuf_unittest_import_ImportMessage__FieldAccessorTable; }
}
public const int DFieldNumber = 1;
private bool hasD;
private int d_ = 0;
public bool HasD {
......
......@@ -257,6 +257,7 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return global::Google.ProtocolBuffers.TestProtos.UnitTestMessageSetProtoFile.internal__static_protobuf_unittest_TestMessageSetContainer__FieldAccessorTable; }
}
public const int MessageSetFieldNumber = 1;
private bool hasMessageSet;
private global::Google.ProtocolBuffers.TestProtos.TestMessageSet messageSet_ = global::Google.ProtocolBuffers.TestProtos.TestMessageSet.DefaultInstance;
public bool HasMessageSet {
......@@ -481,8 +482,10 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return global::Google.ProtocolBuffers.TestProtos.UnitTestMessageSetProtoFile.internal__static_protobuf_unittest_TestMessageSetExtension1__FieldAccessorTable; }
}
public const int MessageSetExtensionFieldNumber = 1545008;
public static readonly pb::GeneratedExtensionBase<global::Google.ProtocolBuffers.TestProtos.TestMessageSetExtension1> MessageSetExtension =
pb::GeneratedSingleExtension<global::Google.ProtocolBuffers.TestProtos.TestMessageSetExtension1>.CreateInstance(Descriptor.Extensions[0]);
public const int IFieldNumber = 15;
private bool hasI;
private int i_ = 0;
public bool HasI {
......@@ -684,8 +687,10 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return global::Google.ProtocolBuffers.TestProtos.UnitTestMessageSetProtoFile.internal__static_protobuf_unittest_TestMessageSetExtension2__FieldAccessorTable; }
}
public const int MessageSetExtensionFieldNumber = 1547769;
public static readonly pb::GeneratedExtensionBase<global::Google.ProtocolBuffers.TestProtos.TestMessageSetExtension2> MessageSetExtension =
pb::GeneratedSingleExtension<global::Google.ProtocolBuffers.TestProtos.TestMessageSetExtension2>.CreateInstance(Descriptor.Extensions[0]);
public const int StrFieldNumber = 25;
private bool hasStr;
private string str_ = "";
public bool HasStr {
......@@ -912,6 +917,7 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return global::Google.ProtocolBuffers.TestProtos.UnitTestMessageSetProtoFile.internal__static_protobuf_unittest_RawMessageSet_Item__FieldAccessorTable; }
}
public const int TypeIdFieldNumber = 2;
private bool hasTypeId;
private int typeId_ = 0;
public bool HasTypeId {
......@@ -921,6 +927,7 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return typeId_; }
}
public const int MessageFieldNumber = 3;
private bool hasMessage;
private pb::ByteString message_ = pb::ByteString.Empty;
public bool HasMessage {
......@@ -1137,6 +1144,7 @@ namespace Google.ProtocolBuffers.TestProtos {
}
#endregion
public const int ItemFieldNumber = 1;
private pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.RawMessageSet.Types.Item> item_ = new pbc::PopsicleList<global::Google.ProtocolBuffers.TestProtos.RawMessageSet.Types.Item>();
public scg::IList<global::Google.ProtocolBuffers.TestProtos.RawMessageSet.Types.Item> ItemList {
get { return item_; }
......
......@@ -75,10 +75,13 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return global::Google.ProtocolBuffers.TestProtos.UnitTestOptimizeForProtoFile.internal__static_protobuf_unittest_TestOptimizedForSize__FieldAccessorTable; }
}
public const int TestExtensionFieldNumber = 1234;
public static readonly pb::GeneratedExtensionBase<int> TestExtension =
pb::GeneratedSingleExtension<int>.CreateInstance(Descriptor.Extensions[0]);
public const int TestExtension2FieldNumber = 1235;
public static readonly pb::GeneratedExtensionBase<global::Google.ProtocolBuffers.TestProtos.TestRequiredOptimizedForSize> TestExtension2 =
pb::GeneratedSingleExtension<global::Google.ProtocolBuffers.TestProtos.TestRequiredOptimizedForSize>.CreateInstance(Descriptor.Extensions[1]);
public const int IFieldNumber = 1;
private bool hasI;
private int i_ = 0;
public bool HasI {
......@@ -88,6 +91,7 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return i_; }
}
public const int MsgFieldNumber = 19;
private bool hasMsg;
private global::Google.ProtocolBuffers.TestProtos.ForeignMessage msg_ = global::Google.ProtocolBuffers.TestProtos.ForeignMessage.DefaultInstance;
public bool HasMsg {
......@@ -242,6 +246,7 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return global::Google.ProtocolBuffers.TestProtos.UnitTestOptimizeForProtoFile.internal__static_protobuf_unittest_TestRequiredOptimizedForSize__FieldAccessorTable; }
}
public const int XFieldNumber = 1;
private bool hasX;
private int x_ = 0;
public bool HasX {
......@@ -360,6 +365,7 @@ namespace Google.ProtocolBuffers.TestProtos {
get { return global::Google.ProtocolBuffers.TestProtos.UnitTestOptimizeForProtoFile.internal__static_protobuf_unittest_TestOptionalOptimizedForSize__FieldAccessorTable; }
}
public const int OFieldNumber = 1;
private bool hasO;
private global::Google.ProtocolBuffers.TestProtos.TestRequiredOptimizedForSize o_ = global::Google.ProtocolBuffers.TestProtos.TestRequiredOptimizedForSize.DefaultInstance;
public bool HasO {
......
......@@ -31,8 +31,10 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
#endregion
#region Extensions
public const int CsharpFileOptionsFieldNumber = 1000;
public static readonly pb::GeneratedExtensionBase<global::Google.ProtocolBuffers.DescriptorProtos.CSharpFileOptions> CSharpFileOptions =
pb::GeneratedSingleExtension<global::Google.ProtocolBuffers.DescriptorProtos.CSharpFileOptions>.CreateInstance(Descriptor.Extensions[0]);
public const int CsharpFieldOptionsFieldNumber = 1000;
public static readonly pb::GeneratedExtensionBase<global::Google.ProtocolBuffers.DescriptorProtos.CSharpFieldOptions> CSharpFieldOptions =
pb::GeneratedSingleExtension<global::Google.ProtocolBuffers.DescriptorProtos.CSharpFieldOptions>.CreateInstance(Descriptor.Extensions[1]);
#endregion
......@@ -73,6 +75,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
get { return global::Google.ProtocolBuffers.DescriptorProtos.CSharpOptions.internal__static_google_protobuf_CSharpFileOptions__FieldAccessorTable; }
}
public const int NamespaceFieldNumber = 1;
private bool hasNamespace;
private string namespace_ = "";
public bool HasNamespace {
......@@ -82,6 +85,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
get { return namespace_; }
}
public const int UmbrellaClassnameFieldNumber = 2;
private bool hasUmbrellaClassname;
private string umbrellaClassname_ = "";
public bool HasUmbrellaClassname {
......@@ -91,6 +95,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
get { return umbrellaClassname_; }
}
public const int PublicClassesFieldNumber = 3;
private bool hasPublicClasses;
private bool publicClasses_ = false;
public bool HasPublicClasses {
......@@ -100,6 +105,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
get { return publicClasses_; }
}
public const int MultipleFilesFieldNumber = 4;
private bool hasMultipleFiles;
private bool multipleFiles_ = false;
public bool HasMultipleFiles {
......@@ -109,6 +115,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
get { return multipleFiles_; }
}
public const int NestClassesFieldNumber = 5;
private bool hasNestClasses;
private bool nestClasses_ = false;
public bool HasNestClasses {
......@@ -301,6 +308,7 @@ namespace Google.ProtocolBuffers.DescriptorProtos {
get { return global::Google.ProtocolBuffers.DescriptorProtos.CSharpOptions.internal__static_google_protobuf_CSharpFieldOptions__FieldAccessorTable; }
}
public const int PropertyNameFieldNumber = 1;
private bool hasPropertyName;
private string propertyName_ = "";
public bool HasPropertyName {
......
......@@ -9,6 +9,9 @@ Current task list (not in order)
- Avoid using reflection for messages which don't need it (is this
possible?)
- Add RegisterAllExtensions
- Add ToBuilder changes from Google's r92
- Add ToBuilder changes from Google's r92 (done? Need to check)
- Silverlight changes (as per email to Jon)
- Extension registration/initialization code?
From latest Java code:
r120: (Lots)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment