Commit 7b5c3967 authored by Jan Tattermusch's avatar Jan Tattermusch

Merge pull request #625 from jskeet/reflection-refactor

Reflection refactor
parents 2ee4b566 43d64b4f
......@@ -117,10 +117,10 @@ csharp_EXTRA_DIST= \
csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs \
csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs \
csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs \
csharp/src/Google.Protobuf/Reflection/FieldAccessorTable.cs \
csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs \
csharp/src/Google.Protobuf/Reflection/FieldType.cs \
csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs \
csharp/src/Google.Protobuf/Reflection/GeneratedCodeInfo.cs \
csharp/src/Google.Protobuf/Reflection/IDescriptor.cs \
csharp/src/Google.Protobuf/Reflection/IFieldAccessor.cs \
csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs \
......
......@@ -12,11 +12,6 @@ namespace Google.Protobuf.Examples.AddressBook {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class Addressbook {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_tutorial_Person__FieldAccessorTable;
internal static pbr::FieldAccessorTable internal__static_tutorial_Person_PhoneNumber__FieldAccessorTable;
internal static pbr::FieldAccessorTable internal__static_tutorial_AddressBook__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -36,17 +31,11 @@ namespace Google.Protobuf.Examples.AddressBook {
"aWFsQhFBZGRyZXNzQm9va1Byb3Rvc6oCJEdvb2dsZS5Qcm90b2J1Zi5FeGFt",
"cGxlcy5BZGRyZXNzQm9va2IGcHJvdG8z"));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
});
internal__static_tutorial_Person__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.Examples.AddressBook.Person), descriptor.MessageTypes[0],
new string[] { "Name", "Id", "Email", "Phones", }, new string[] { });
internal__static_tutorial_Person_PhoneNumber__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneNumber), descriptor.MessageTypes[0].NestedTypes[0],
new string[] { "Number", "Type", }, new string[] { });
internal__static_tutorial_AddressBook__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.Examples.AddressBook.AddressBook), descriptor.MessageTypes[1],
new string[] { "People", }, new string[] { });
new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Examples.AddressBook.Person), new[]{ "Name", "Id", "Email", "Phones" }, null, new[]{ typeof(global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType) }, new pbr::GeneratedCodeInfo[] { new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneNumber), new[]{ "Number", "Type" }, null, null, null)}),
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.Examples.AddressBook.AddressBook), new[]{ "People" }, null, null, null)
}));
}
#endregion
......@@ -57,14 +46,12 @@ namespace Google.Protobuf.Examples.AddressBook {
private static readonly pb::MessageParser<Person> _parser = new pb::MessageParser<Person>(() => new Person());
public static pb::MessageParser<Person> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "email", "id", "name", "phones" };
private static readonly uint[] _fieldTags = new uint[] { 26, 16, 10, 34 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.Examples.AddressBook.Addressbook.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.Examples.AddressBook.Addressbook.internal__static_tutorial_Person__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......@@ -256,14 +243,12 @@ namespace Google.Protobuf.Examples.AddressBook {
private static readonly pb::MessageParser<PhoneNumber> _parser = new pb::MessageParser<PhoneNumber>(() => new PhoneNumber());
public static pb::MessageParser<PhoneNumber> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "number", "type" };
private static readonly uint[] _fieldTags = new uint[] { 10, 16 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.Examples.AddressBook.Person.Descriptor.NestedTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.Examples.AddressBook.Addressbook.internal__static_tutorial_Person_PhoneNumber__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......@@ -407,14 +392,12 @@ namespace Google.Protobuf.Examples.AddressBook {
private static readonly pb::MessageParser<AddressBook> _parser = new pb::MessageParser<AddressBook>(() => new AddressBook());
public static pb::MessageParser<AddressBook> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "people" };
private static readonly uint[] _fieldTags = new uint[] { 10 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.Examples.AddressBook.Addressbook.Descriptor.MessageTypes[1]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.Examples.AddressBook.Addressbook.internal__static_tutorial_AddressBook__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -604,7 +604,7 @@ namespace Google.Protobuf
public void Reflection_GetValue()
{
var message = SampleMessages.CreateFullTestAllTypes();
var fields = ((IReflectedMessage) message).Fields;
var fields = TestAllTypes.Descriptor.FieldAccessorsByFieldNumber;
Assert.AreEqual(message.SingleBool, fields[TestAllTypes.SingleBoolFieldNumber].GetValue(message));
Assert.AreEqual(message.SingleBytes, fields[TestAllTypes.SingleBytesFieldNumber].GetValue(message));
Assert.AreEqual(message.SingleDouble, fields[TestAllTypes.SingleDoubleFieldNumber].GetValue(message));
......@@ -639,7 +639,7 @@ namespace Google.Protobuf
// Just a single map field, for the same reason
var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } };
fields = ((IReflectedMessage) mapMessage).Fields;
fields = TestMap.Descriptor.FieldAccessorsByFieldNumber;
var dictionary = (IDictionary) fields[TestMap.MapStringStringFieldNumber].GetValue(mapMessage);
Assert.AreEqual(mapMessage.MapStringString, dictionary);
Assert.AreEqual("value1", dictionary["key1"]);
......@@ -648,8 +648,8 @@ namespace Google.Protobuf
[Test]
public void Reflection_Clear()
{
IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Fields;
var message = SampleMessages.CreateFullTestAllTypes();
var fields = TestAllTypes.Descriptor.FieldAccessorsByFieldNumber;
fields[TestAllTypes.SingleBoolFieldNumber].Clear(message);
fields[TestAllTypes.SingleInt32FieldNumber].Clear(message);
fields[TestAllTypes.SingleStringFieldNumber].Clear(message);
......@@ -673,7 +673,7 @@ namespace Google.Protobuf
// Separately, maps.
var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } };
fields = ((IReflectedMessage) mapMessage).Fields;
fields = TestMap.Descriptor.FieldAccessorsByFieldNumber;
fields[TestMap.MapStringStringFieldNumber].Clear(mapMessage);
Assert.AreEqual(0, mapMessage.MapStringString.Count);
}
......@@ -682,8 +682,8 @@ namespace Google.Protobuf
public void Reflection_SetValue_SingleFields()
{
// Just a sample (primitives, messages, enums, strings, byte strings)
IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Fields;
var message = SampleMessages.CreateFullTestAllTypes();
var fields = TestAllTypes.Descriptor.FieldAccessorsByFieldNumber;
fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, false);
fields[TestAllTypes.SingleInt32FieldNumber].SetValue(message, 500);
fields[TestAllTypes.SingleStringFieldNumber].SetValue(message, "It's a string");
......@@ -709,51 +709,52 @@ namespace Google.Protobuf
[Test]
public void Reflection_SetValue_SingleFields_WrongType()
{
IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Fields;
IMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Descriptor.FieldAccessorsByFieldNumber;
Assert.Throws<InvalidCastException>(() => fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, "This isn't a bool"));
}
[Test]
public void Reflection_SetValue_MapFields()
{
IReflectedMessage message = new TestMap();
var fields = message.Fields;
IMessage message = new TestMap();
var fields = message.Descriptor.FieldAccessorsByFieldNumber;
Assert.Throws<InvalidOperationException>(() => fields[TestMap.MapStringStringFieldNumber].SetValue(message, new Dictionary<string, string>()));
}
[Test]
public void Reflection_SetValue_RepeatedFields()
{
IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Fields;
IMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Descriptor.FieldAccessorsByFieldNumber;
Assert.Throws<InvalidOperationException>(() => fields[TestAllTypes.RepeatedDoubleFieldNumber].SetValue(message, new double[10]));
}
[Test]
public void Reflection_GetValue_IncorrectType()
{
IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
Assert.Throws<InvalidCastException>(() => message.Fields[TestAllTypes.SingleBoolFieldNumber].GetValue(new TestMap()));
IMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Descriptor.FieldAccessorsByFieldNumber;
Assert.Throws<InvalidCastException>(() => fields[TestAllTypes.SingleBoolFieldNumber].GetValue(new TestMap()));
}
[Test]
public void Reflection_Oneof()
{
var message = new TestAllTypes();
var fields = ((IReflectedMessage) message).Fields;
Assert.AreEqual(1, fields.Oneofs.Count);
var oneof = fields.Oneofs[0];
Assert.AreEqual("oneof_field", oneof.Descriptor.Name);
Assert.IsNull(oneof.GetCaseFieldDescriptor(message));
var descriptor = TestAllTypes.Descriptor;
Assert.AreEqual(1, descriptor.Oneofs.Count);
var oneof = descriptor.Oneofs[0];
Assert.AreEqual("oneof_field", oneof.Name);
Assert.IsNull(oneof.Accessor.GetCaseFieldDescriptor(message));
message.OneofString = "foo";
Assert.AreSame(fields[TestAllTypes.OneofStringFieldNumber].Descriptor, oneof.GetCaseFieldDescriptor(message));
Assert.AreSame(descriptor.FieldAccessorsByFieldNumber[TestAllTypes.OneofStringFieldNumber].Descriptor, oneof.Accessor.GetCaseFieldDescriptor(message));
message.OneofUint32 = 10;
Assert.AreSame(fields[TestAllTypes.OneofUint32FieldNumber].Descriptor, oneof.GetCaseFieldDescriptor(message));
Assert.AreSame(descriptor.FieldAccessorsByFieldNumber[TestAllTypes.OneofUint32FieldNumber].Descriptor, oneof.Accessor.GetCaseFieldDescriptor(message));
oneof.Clear(message);
oneof.Accessor.Clear(message);
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
}
}
......
......@@ -62,6 +62,7 @@ namespace Google.Protobuf.Reflection
Assert.AreEqual(UnittestImportProto3.Descriptor, file.Dependencies[0]);
MessageDescriptor messageType = TestAllTypes.Descriptor;
Assert.AreSame(typeof(TestAllTypes), messageType.GeneratedType);
Assert.AreEqual(messageType, file.MessageTypes[0]);
Assert.AreEqual(messageType, file.FindTypeByName<MessageDescriptor>("TestAllTypes"));
Assert.Null(file.FindTypeByName<MessageDescriptor>("NoSuchType"));
......
......@@ -12,9 +12,6 @@ namespace Google.Protobuf.TestProtos {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class UnittestImportProto3 {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_protobuf_unittest_import_ImportMessage__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -33,12 +30,10 @@ namespace Google.Protobuf.TestProtos {
"c3RIAfgBAaoCGkdvb2dsZS5Qcm90b2J1Zi5UZXN0UHJvdG9zUABiBnByb3Rv",
"Mw=="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
global::Google.Protobuf.TestProtos.UnittestImportPublicProto3.Descriptor,
});
internal__static_protobuf_unittest_import_ImportMessage__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.ImportMessage), descriptor.MessageTypes[0],
new string[] { "D", }, new string[] { });
new pbr::FileDescriptor[] { global::Google.Protobuf.TestProtos.UnittestImportPublicProto3.Descriptor, },
new pbr::GeneratedCodeInfo(new[] {typeof(global::Google.Protobuf.TestProtos.ImportEnum), }, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.TestProtos.ImportMessage), new[]{ "D" }, null, null, null)
}));
}
#endregion
......@@ -59,14 +54,12 @@ namespace Google.Protobuf.TestProtos {
private static readonly pb::MessageParser<ImportMessage> _parser = new pb::MessageParser<ImportMessage>(() => new ImportMessage());
public static pb::MessageParser<ImportMessage> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "d" };
private static readonly uint[] _fieldTags = new uint[] { 8 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.TestProtos.UnittestImportProto3.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.TestProtos.UnittestImportProto3.internal__static_protobuf_unittest_import_ImportMessage__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -12,9 +12,6 @@ namespace Google.Protobuf.TestProtos {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class UnittestImportPublicProto3 {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_protobuf_unittest_import_PublicImportMessage__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -29,11 +26,10 @@ namespace Google.Protobuf.TestProtos {
"bXBvcnRNZXNzYWdlEgkKAWUYASABKAVCNwoYY29tLmdvb2dsZS5wcm90b2J1",
"Zi50ZXN0qgIaR29vZ2xlLlByb3RvYnVmLlRlc3RQcm90b3NiBnByb3RvMw=="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
});
internal__static_protobuf_unittest_import_PublicImportMessage__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.PublicImportMessage), descriptor.MessageTypes[0],
new string[] { "E", }, new string[] { });
new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.TestProtos.PublicImportMessage), new[]{ "E" }, null, null, null)
}));
}
#endregion
......@@ -44,14 +40,12 @@ namespace Google.Protobuf.TestProtos {
private static readonly pb::MessageParser<PublicImportMessage> _parser = new pb::MessageParser<PublicImportMessage>(() => new PublicImportMessage());
public static pb::MessageParser<PublicImportMessage> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "e" };
private static readonly uint[] _fieldTags = new uint[] { 8 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.TestProtos.UnittestImportPublicProto3.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.TestProtos.UnittestImportPublicProto3.internal__static_protobuf_unittest_import_PublicImportMessage__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -192,7 +192,7 @@ namespace Google.Protobuf.WellKnownTypes
Uint32Field = 3,
Uint64Field = 4
};
var fields = ((IReflectedMessage) message).Fields;
var fields = TestWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber;
Assert.AreEqual("x", fields[TestWellKnownTypes.StringFieldFieldNumber].GetValue(message));
Assert.AreEqual(ByteString.CopyFrom(1, 2, 3), fields[TestWellKnownTypes.BytesFieldFieldNumber].GetValue(message));
......@@ -216,7 +216,7 @@ namespace Google.Protobuf.WellKnownTypes
{
// Just a single example... note that we can't have a null value here
var message = new RepeatedWellKnownTypes { Int32Field = { 1, 2 } };
var fields = ((IReflectedMessage) message).Fields;
var fields = RepeatedWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber;
var list = (IList) fields[RepeatedWellKnownTypes.Int32FieldFieldNumber].GetValue(message);
CollectionAssert.AreEqual(new[] { 1, 2 }, list);
}
......@@ -226,7 +226,7 @@ namespace Google.Protobuf.WellKnownTypes
{
// Just a single example... note that we can't have a null value here
var message = new MapWellKnownTypes { Int32Field = { { 1, 2 }, { 3, null } } };
var fields = ((IReflectedMessage) message).Fields;
var fields = MapWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber;
var dictionary = (IDictionary) fields[MapWellKnownTypes.Int32FieldFieldNumber].GetValue(message);
Assert.AreEqual(2, dictionary[1]);
Assert.IsNull(dictionary[3]);
......
......@@ -30,6 +30,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using Google.Protobuf.Reflection;
using System;
using System.Collections;
using System.Collections.Generic;
......@@ -559,6 +560,8 @@ namespace Google.Protobuf.Collections
{
return codec.keyCodec.CalculateSizeWithTag(Key) + codec.valueCodec.CalculateSizeWithTag(Value);
}
MessageDescriptor IMessage.Descriptor { get { return null; } }
}
}
}
......
......@@ -78,10 +78,10 @@
<Compile Include="Reflection\EnumDescriptor.cs" />
<Compile Include="Reflection\EnumValueDescriptor.cs" />
<Compile Include="Reflection\FieldAccessorBase.cs" />
<Compile Include="Reflection\FieldAccessorTable.cs" />
<Compile Include="Reflection\FieldDescriptor.cs" />
<Compile Include="Reflection\FieldType.cs" />
<Compile Include="Reflection\FileDescriptor.cs" />
<Compile Include="Reflection\GeneratedCodeInfo.cs" />
<Compile Include="Reflection\IDescriptor.cs" />
<Compile Include="Reflection\IFieldAccessor.cs" />
<Compile Include="Reflection\MapFieldAccessor.cs" />
......
......@@ -39,15 +39,6 @@ namespace Google.Protobuf
// TODO(jonskeet): Do we want a "weak" (non-generic) version of IReflectedMessage?
// TODO(jonskeet): Split these interfaces into separate files when we're happy with them.
/// <summary>
/// Reflection support for accessing field values.
/// </summary>
public interface IReflectedMessage : IMessage
{
FieldAccessorTable Fields { get; }
// TODO(jonskeet): Descriptor? Or a single property which has "all you need for reflection"?
}
/// <summary>
/// Interface for a Protocol Buffers message, supporting
/// basic operations required for serialization.
......@@ -73,6 +64,13 @@ namespace Google.Protobuf
/// <returns>The number of bytes required to write this message
/// to a coded output stream.</returns>
int CalculateSize();
/// <summary>
/// Descriptor for this message. All instances are expected to return the same descriptor,
/// and for generated types this will be an explicitly-implemented member, returning the
/// same value as the static property declared on the type.
/// </summary>
MessageDescriptor Descriptor { get; }
}
/// <summary>
......@@ -81,7 +79,7 @@ namespace Google.Protobuf
/// the implementation class.
/// </summary>
/// <typeparam name="T">The message type.</typeparam>
public interface IMessage<T> : IReflectedMessage, IEquatable<T>, IDeepCloneable<T>, IFreezable where T : IMessage<T>
public interface IMessage<T> : IMessage, IEquatable<T>, IDeepCloneable<T>, IFreezable where T : IMessage<T>
{
/// <summary>
/// Merges the given message into this one.
......
......@@ -118,7 +118,7 @@ namespace Google.Protobuf
this.settings = settings;
}
public string Format(IReflectedMessage message)
public string Format(IMessage message)
{
ThrowHelper.ThrowIfNull(message, "message");
StringBuilder builder = new StringBuilder();
......@@ -129,7 +129,7 @@ namespace Google.Protobuf
return builder.ToString();
}
private void WriteMessage(StringBuilder builder, IReflectedMessage message)
private void WriteMessage(StringBuilder builder, IMessage message)
{
if (message == null)
{
......@@ -137,15 +137,15 @@ namespace Google.Protobuf
return;
}
builder.Append("{ ");
var fields = message.Fields;
var fields = message.Descriptor.Fields;
bool first = true;
// First non-oneof fields
foreach (var accessor in fields.Accessors)
foreach (var field in fields)
{
var descriptor = accessor.Descriptor;
var accessor = field.Accessor;
// Oneofs are written later
// TODO: Change to write out fields in order, interleaving oneofs appropriately (as per binary format)
if (descriptor.ContainingOneof != null)
if (field.ContainingOneof != null)
{
continue;
}
......@@ -156,7 +156,7 @@ namespace Google.Protobuf
continue;
}
// Omit awkward (single) values such as unknown enum values
if (!descriptor.IsRepeated && !descriptor.IsMap && !CanWriteSingleValue(accessor.Descriptor, value))
if (!field.IsRepeated && !field.IsMap && !CanWriteSingleValue(accessor.Descriptor, value))
{
continue;
}
......@@ -173,15 +173,15 @@ namespace Google.Protobuf
}
// Now oneofs
foreach (var accessor in fields.Oneofs)
foreach (var oneof in message.Descriptor.Oneofs)
{
var accessor = oneof.Accessor;
var fieldDescriptor = accessor.GetCaseFieldDescriptor(message);
if (fieldDescriptor == null)
{
continue;
}
var fieldAccessor = fields[fieldDescriptor.FieldNumber];
object value = fieldAccessor.GetValue(message);
object value = fieldDescriptor.Accessor.GetValue(message);
// Omit awkward (single) values such as unknown enum values
if (!fieldDescriptor.IsRepeated && !fieldDescriptor.IsMap && !CanWriteSingleValue(fieldDescriptor, value))
{
......@@ -194,7 +194,7 @@ namespace Google.Protobuf
}
WriteString(builder, ToCamelCase(fieldDescriptor.Name));
builder.Append(": ");
WriteValue(builder, fieldAccessor, value);
WriteValue(builder, fieldDescriptor.Accessor, value);
first = false;
}
builder.Append(first ? "}" : " }");
......@@ -385,7 +385,7 @@ namespace Google.Protobuf
}
else
{
WriteMessage(builder, (IReflectedMessage) value);
WriteMessage(builder, (IMessage) value);
}
break;
default:
......@@ -406,7 +406,7 @@ namespace Google.Protobuf
WriteSingleValue(builder, descriptor.MessageType.FindFieldByNumber(1), value);
return;
}
WriteMessage(builder, (IReflectedMessage) value);
WriteMessage(builder, (IMessage) value);
}
private void WriteList(StringBuilder builder, IFieldAccessor accessor, IList list)
......
......@@ -50,9 +50,8 @@ namespace Google.Protobuf.Reflection
/// Converts the given array into a read-only list, applying the specified conversion to
/// each input element.
/// </summary>
internal static IList<TOutput> ConvertAndMakeReadOnly<TInput, TOutput>(IList<TInput> input,
IndexedConverter<TInput, TOutput>
converter)
internal static IList<TOutput> ConvertAndMakeReadOnly<TInput, TOutput>
(IList<TInput> input, IndexedConverter<TInput, TOutput> converter)
{
TOutput[] array = new TOutput[input.Count];
for (int i = 0; i < array.Length; i++)
......
......@@ -30,6 +30,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.Collections.Generic;
namespace Google.Protobuf.Reflection
......@@ -42,11 +43,13 @@ namespace Google.Protobuf.Reflection
private readonly EnumDescriptorProto proto;
private readonly MessageDescriptor containingType;
private readonly IList<EnumValueDescriptor> values;
private readonly Type generatedType;
internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index)
internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, Type generatedType)
: base(file, file.ComputeFullName(parent, proto.Name), index)
{
this.proto = proto;
this.generatedType = generatedType;
containingType = parent;
if (proto.Value.Count == 0)
......@@ -69,6 +72,11 @@ namespace Google.Protobuf.Reflection
/// </summary>
public override string Name { get { return proto.Name; } }
/// <summary>
/// The generated type for this enum, or <c>null</c> if the descriptor does not represent a generated type.
/// </summary>
public Type GeneratedType { get { return generatedType; } }
/// <value>
/// If this is a nested type, get the outer descriptor, otherwise null.
/// </value>
......
......@@ -43,13 +43,8 @@ namespace Google.Protobuf.Reflection
private readonly Func<object, object> getValueDelegate;
private readonly FieldDescriptor descriptor;
internal FieldAccessorBase(Type type, string propertyName, FieldDescriptor descriptor)
internal FieldAccessorBase(PropertyInfo property, FieldDescriptor descriptor)
{
PropertyInfo property = type.GetProperty(propertyName);
if (property == null || !property.CanRead)
{
throw new ArgumentException("Not all required properties/methods available");
}
this.descriptor = descriptor;
getValueDelegate = ReflectionUtil.CreateFuncObjectObject(property.GetGetMethod());
}
......
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.Collections.ObjectModel;
namespace Google.Protobuf.Reflection
{
/// <summary>
/// Provides access to fields in generated messages via reflection.
/// </summary>
public sealed class FieldAccessorTable
{
private readonly ReadOnlyCollection<IFieldAccessor> accessors;
private readonly ReadOnlyCollection<OneofAccessor> oneofs;
private readonly MessageDescriptor descriptor;
/// <summary>
/// Constructs a FieldAccessorTable for a particular message class.
/// Only one FieldAccessorTable should be constructed per class.
/// </summary>
/// <param name="type">The CLR type for the message.</param>
/// <param name="descriptor">The type's descriptor</param>
/// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param>
public FieldAccessorTable(Type type, MessageDescriptor descriptor, string[] propertyNames, string[] oneofPropertyNames)
{
this.descriptor = descriptor;
var accessorsArray = new IFieldAccessor[descriptor.Fields.Count];
for (int i = 0; i < accessorsArray.Length; i++)
{
var field = descriptor.Fields[i];
var name = propertyNames[i];
accessorsArray[i] =
field.IsMap ? new MapFieldAccessor(type, name, field)
: field.IsRepeated ? new RepeatedFieldAccessor(type, name, field)
: (IFieldAccessor) new SingleFieldAccessor(type, name, field);
}
accessors = new ReadOnlyCollection<IFieldAccessor>(accessorsArray);
var oneofsArray = new OneofAccessor[descriptor.Oneofs.Count];
for (int i = 0; i < oneofsArray.Length; i++)
{
var oneof = descriptor.Oneofs[i];
oneofsArray[i] = new OneofAccessor(type, oneofPropertyNames[i], oneof);
}
oneofs = new ReadOnlyCollection<OneofAccessor>(oneofsArray);
}
// TODO: Validate the name here... should possibly make this type a more "general reflection access" type,
// bearing in mind the oneof parts to come as well.
/// <summary>
/// Returns all of the field accessors for the message type.
/// </summary>
public ReadOnlyCollection<IFieldAccessor> Accessors { get { return accessors; } }
public ReadOnlyCollection<OneofAccessor> Oneofs { get { return oneofs; } }
// TODO: Review this, as it's easy to get confused between FieldNumber and Index.
// Currently only used to get an accessor related to a oneof... maybe just make that simpler?
public IFieldAccessor this[int fieldNumber]
{
get
{
FieldDescriptor field = descriptor.FindFieldByNumber(fieldNumber);
return accessors[field.Index];
}
}
}
}
\ No newline at end of file
......@@ -31,6 +31,7 @@
#endregion
using System;
using System.Linq;
namespace Google.Protobuf.Reflection
{
......@@ -45,9 +46,11 @@ namespace Google.Protobuf.Reflection
private readonly MessageDescriptor containingType;
private readonly OneofDescriptor containingOneof;
private FieldType fieldType;
private readonly string propertyName; // Annoyingly, needed in Crosslink.
private IFieldAccessor accessor;
internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file,
MessageDescriptor parent, int index)
MessageDescriptor parent, int index, string propertyName)
: base(file, file.ComputeFullName(parent, proto.Name), index)
{
this.proto = proto;
......@@ -74,6 +77,12 @@ namespace Google.Protobuf.Reflection
}
file.DescriptorPool.AddSymbol(this);
// We can't create the accessor until we've cross-linked, unfortunately, as we
// may not know whether the type of the field is a map or not. Remember the property name
// for later.
// We could trust the generated code and check whether the type of the property is
// a MapField, but that feels a tad nasty.
this.propertyName = propertyName;
}
/// <summary>
......@@ -83,6 +92,8 @@ namespace Google.Protobuf.Reflection
internal FieldDescriptorProto Proto { get { return proto; } }
public IFieldAccessor Accessor { get { return accessor; } }
/// <summary>
/// Maps a field type as included in the .proto file to a FieldType.
/// </summary>
......@@ -287,6 +298,23 @@ namespace Google.Protobuf.Reflection
{
throw new DescriptorValidationException(this, "MessageSet format is not supported.");
}
accessor = CreateAccessor(propertyName);
}
private IFieldAccessor CreateAccessor(string propertyName)
{
if (containingType.GeneratedType == null || propertyName == null)
{
return null;
}
var property = containingType.GeneratedType.GetProperty(propertyName);
if (property == null)
{
throw new DescriptorValidationException(this, "Property " + propertyName + " not found in " + containingType.GeneratedType);
}
return IsMap ? new MapFieldAccessor(property, this)
: IsRepeated ? new RepeatedFieldAccessor(property, this)
: (IFieldAccessor) new SingleFieldAccessor(property, this);
}
}
}
\ No newline at end of file
......@@ -62,7 +62,7 @@ namespace Google.Protobuf.Reflection
get { return proto.Syntax == "proto3" ? ProtoSyntax.Proto3 : ProtoSyntax.Proto2; }
}
private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies)
private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo)
{
this.pool = pool;
this.proto = proto;
......@@ -74,11 +74,11 @@ namespace Google.Protobuf.Reflection
messageTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.MessageType,
(message, index) =>
new MessageDescriptor(message, this, null, index));
new MessageDescriptor(message, this, null, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedTypes[index]));
enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType,
(enumType, index) =>
new EnumDescriptor(enumType, this, null, index));
new EnumDescriptor(enumType, this, null, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedEnums[index]));
services = DescriptorUtil.ConvertAndMakeReadOnly(proto.Service,
(service, index) =>
......@@ -262,10 +262,11 @@ namespace Google.Protobuf.Reflection
/// file's dependencies, in the exact order listed in the .proto file. May be null,
/// in which case it is treated as an empty array.</param>
/// <param name="allowUnknownDependencies">Whether unknown dependencies are ignored (true) or cause an exception to be thrown (false).</param>
/// <param name="generatedCodeInfo">Reflection information, if any. May be null, specifically for non-generated code.</param>
/// <exception cref="DescriptorValidationException">If <paramref name="proto"/> is not
/// a valid descriptor. This can occur for a number of reasons, such as a field
/// having an undefined type or because two messages were defined with the same name.</exception>
private static FileDescriptor BuildFrom(FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies)
private static FileDescriptor BuildFrom(FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo)
{
// Building descriptors involves two steps: translating and linking.
// In the translation step (implemented by FileDescriptor's
......@@ -282,7 +283,7 @@ namespace Google.Protobuf.Reflection
}
DescriptorPool pool = new DescriptorPool(dependencies);
FileDescriptor result = new FileDescriptor(proto, dependencies, pool, allowUnknownDependencies);
FileDescriptor result = new FileDescriptor(proto, dependencies, pool, allowUnknownDependencies, generatedCodeInfo);
// TODO(jonskeet): Reinstate these checks, or get rid of them entirely. They aren't in the Java code,
// and fail for the CustomOptions test right now. (We get "descriptor.proto" vs "google/protobuf/descriptor.proto".)
......@@ -319,8 +320,17 @@ namespace Google.Protobuf.Reflection
}
}
/// <summary>
/// Creates an instance for generated code.
/// </summary>
/// <remarks>
/// The <paramref name="generatedCodeInfo"/> parameter should be null for descriptors which don't correspond to
/// generated types. Otherwise, it should be a <see cref="GeneratedCodeInfo"/> with nested types and nested
/// enums corresponding to the types and enums contained within the file descriptor.
/// </remarks>
public static FileDescriptor InternalBuildGeneratedFileFrom(byte[] descriptorData,
FileDescriptor[] dependencies)
FileDescriptor[] dependencies,
GeneratedCodeInfo generatedCodeInfo)
{
FileDescriptorProto proto;
try
......@@ -336,7 +346,7 @@ namespace Google.Protobuf.Reflection
{
// When building descriptors for generated code, we allow unknown
// dependencies by default.
return BuildFrom(proto, dependencies, true);
return BuildFrom(proto, dependencies, true, generatedCodeInfo);
}
catch (DescriptorValidationException e)
{
......
using System;
namespace Google.Protobuf.Reflection
{
/// <summary>
/// Extra information provided by generated code when initializing a message or file descriptor.
/// These are constructed as required, and are not long-lived. Hand-written code should
/// never need to use this type.
/// </summary>
public sealed class GeneratedCodeInfo
{
private static readonly string[] EmptyNames = new string[0];
private static readonly GeneratedCodeInfo[] EmptyCodeInfo = new GeneratedCodeInfo[0];
/// <summary>
/// Irrelevant for file descriptors; the CLR type for the message for message descriptors.
/// </summary>
public Type ClrType { get; private set; }
/// <summary>
/// Irrelevant for file descriptors; the CLR property names (in message descriptor field order)
/// for fields in the message for message descriptors.
/// </summary>
public string[] PropertyNames { get; private set; }
/// <summary>
/// Irrelevant for file descriptors; the CLR property "base" names (in message descriptor oneof order)
/// for oneofs in the message for message descriptors. It is expected that for a oneof name of "Foo",
/// there will be a "FooCase" property and a "ClearFoo" method.
/// </summary>
public string[] OneofNames { get; private set; }
/// <summary>
/// The reflection information for types within this file/message descriptor. Elements may be null
/// if there is no corresponding generated type, e.g. for map entry types.
/// </summary>
public GeneratedCodeInfo[] NestedTypes { get; private set; }
/// <summary>
/// The CLR types for enums within this file/message descriptor.
/// </summary>
public Type[] NestedEnums { get; private set; }
/// <summary>
/// Creates a GeneratedCodeInfo for a message descriptor, with nested types, nested enums, the CLR type, property names and oneof names.
/// Each array parameter may be null, to indicate a lack of values.
/// The parameter order is designed to make it feasible to format the generated code readably.
/// </summary>
public GeneratedCodeInfo(Type clrType, string[] propertyNames, string[] oneofNames, Type[] nestedEnums, GeneratedCodeInfo[] nestedTypes)
{
NestedTypes = nestedTypes ?? EmptyCodeInfo;
NestedEnums = nestedEnums ?? ReflectionUtil.EmptyTypes;
ClrType = clrType;
PropertyNames = propertyNames ?? EmptyNames;
OneofNames = oneofNames ?? EmptyNames;
}
/// <summary>
/// Creates a GeneratedCodeInfo for a file descriptor, with only types and enums.
/// </summary>
public GeneratedCodeInfo(Type[] nestedEnums, GeneratedCodeInfo[] nestedTypes)
: this(null, null, null, nestedEnums, nestedTypes)
{
}
}
}
\ No newline at end of file
......@@ -32,6 +32,7 @@
using System;
using System.Collections;
using System.Reflection;
namespace Google.Protobuf.Reflection
{
......@@ -40,7 +41,7 @@ namespace Google.Protobuf.Reflection
/// </summary>
internal sealed class MapFieldAccessor : FieldAccessorBase
{
internal MapFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor)
internal MapFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor)
{
}
......
......@@ -30,8 +30,10 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using Google.Protobuf.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Google.Protobuf.Reflection
{
......@@ -60,32 +62,48 @@ namespace Google.Protobuf.Reflection
private readonly IList<EnumDescriptor> enumTypes;
private readonly IList<FieldDescriptor> fields;
private readonly IList<OneofDescriptor> oneofs;
// CLR representation of the type described by this descriptor, if any.
private readonly Type generatedType;
private IDictionary<int, IFieldAccessor> fieldAccessorsByFieldNumber;
internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex)
internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex, GeneratedCodeInfo generatedCodeInfo)
: base(file, file.ComputeFullName(parent, proto.Name), typeIndex)
{
this.proto = proto;
generatedType = generatedCodeInfo == null ? null : generatedCodeInfo.ClrType;
containingType = parent;
oneofs = DescriptorUtil.ConvertAndMakeReadOnly(proto.OneofDecl,
oneofs = DescriptorUtil.ConvertAndMakeReadOnly(
proto.OneofDecl,
(oneof, index) =>
new OneofDescriptor(oneof, file, this, index));
new OneofDescriptor(oneof, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.OneofNames[index]));
nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.NestedType,
nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(
proto.NestedType,
(type, index) =>
new MessageDescriptor(type, file, this, index));
new MessageDescriptor(type, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedTypes[index]));
enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType,
enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(
proto.EnumType,
(type, index) =>
new EnumDescriptor(type, file, this, index));
new EnumDescriptor(type, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedEnums[index]));
// TODO(jonskeet): Sort fields first?
fields = DescriptorUtil.ConvertAndMakeReadOnly(proto.Field,
fields = DescriptorUtil.ConvertAndMakeReadOnly(
proto.Field,
(field, index) =>
new FieldDescriptor(field, file, this, index));
new FieldDescriptor(field, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.PropertyNames[index]));
file.DescriptorPool.AddSymbol(this);
}
/// <summary>
/// Returns the total number of nested types and enums, recursively.
/// </summary>
private int CountTotalGeneratedTypes()
{
return nestedTypes.Sum(nested => nested.CountTotalGeneratedTypes()) + enumTypes.Count;
}
/// <summary>
/// The brief name of the descriptor's target.
/// </summary>
......@@ -93,6 +111,11 @@ namespace Google.Protobuf.Reflection
internal DescriptorProto Proto { get { return proto; } }
/// <summary>
/// The generated type for this message, or <c>null</c> if the descriptor does not represent a generated type.
/// </summary>
public Type GeneratedType { get { return generatedType; } }
/// <summary>
/// Returns whether this message is one of the "well known types" which may have runtime/protoc support.
/// </summary>
......@@ -141,6 +164,13 @@ namespace Google.Protobuf.Reflection
get { return oneofs; }
}
/// <summary>
/// Returns a map from field number to accessor.
/// TODO: Revisit this. It's mostly in place to make the transition from FieldAccessorTable
/// to descriptor-based reflection simple in terms of tests. Work out what we really want.
/// </summary>
public IDictionary<int, IFieldAccessor> FieldAccessorsByFieldNumber { get { return fieldAccessorsByFieldNumber; } }
/// <summary>
/// Finds a field by field name.
/// </summary>
......@@ -192,6 +222,8 @@ namespace Google.Protobuf.Reflection
{
oneof.CrossLink();
}
fieldAccessorsByFieldNumber = new ReadOnlyDictionary<int, IFieldAccessor>(fields.ToDictionary(field => field.FieldNumber, field => field.Accessor));
}
}
}
\ No newline at end of file
......@@ -44,18 +44,16 @@ namespace Google.Protobuf.Reflection
private readonly Action<object> clearDelegate;
private OneofDescriptor descriptor;
internal OneofAccessor(Type type, string propertyName, OneofDescriptor descriptor)
internal OneofAccessor(PropertyInfo caseProperty, MethodInfo clearMethod, OneofDescriptor descriptor)
{
PropertyInfo property = type.GetProperty(propertyName + "Case");
if (property == null || !property.CanRead)
if (!caseProperty.CanRead)
{
throw new ArgumentException("Not all required properties/methods available");
throw new ArgumentException("Cannot read from property");
}
this.descriptor = descriptor;
caseDelegate = ReflectionUtil.CreateFuncObjectT<int>(property.GetGetMethod());
caseDelegate = ReflectionUtil.CreateFuncObjectT<int>(caseProperty.GetGetMethod());
this.descriptor = descriptor;
MethodInfo clearMethod = type.GetMethod("Clear" + propertyName);
clearDelegate = ReflectionUtil.CreateActionObject(clearMethod);
}
......
......@@ -32,6 +32,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Google.Protobuf.Reflection
{
......@@ -40,14 +41,16 @@ namespace Google.Protobuf.Reflection
private readonly OneofDescriptorProto proto;
private MessageDescriptor containingType;
private IList<FieldDescriptor> fields;
private readonly OneofAccessor accessor;
internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index)
internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, string clrName)
: base(file, file.ComputeFullName(parent, proto.Name), index)
{
this.proto = proto;
containingType = parent;
file.DescriptorPool.AddSymbol(this);
accessor = CreateAccessor(clrName);
}
/// <summary>
......@@ -62,6 +65,8 @@ namespace Google.Protobuf.Reflection
public IList<FieldDescriptor> Fields { get { return fields; } }
public OneofAccessor Accessor { get { return accessor; } }
internal void CrossLink()
{
List<FieldDescriptor> fieldCollection = new List<FieldDescriptor>();
......@@ -74,5 +79,25 @@ namespace Google.Protobuf.Reflection
}
fields = new ReadOnlyCollection<FieldDescriptor>(fieldCollection);
}
private OneofAccessor CreateAccessor(string clrName)
{
if (containingType.GeneratedType == null || clrName == null)
{
return null;
}
var caseProperty = containingType.GeneratedType.GetProperty(clrName + "Case");
if (caseProperty == null)
{
throw new DescriptorValidationException(this, "Property " + clrName + "Case not found in " + containingType.GeneratedType);
}
var clearMethod = containingType.GeneratedType.GetMethod("Clear" + clrName, ReflectionUtil.EmptyTypes);
if (clearMethod == null)
{
throw new DescriptorValidationException(this, "Method Clear" + clrName + " not found in " + containingType.GeneratedType);
}
return new OneofAccessor(caseProperty, clearMethod, this);
}
}
}
......@@ -31,6 +31,7 @@
#endregion
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
......
......@@ -32,6 +32,7 @@
using System;
using System.Collections;
using System.Reflection;
namespace Google.Protobuf.Reflection
{
......@@ -40,7 +41,7 @@ namespace Google.Protobuf.Reflection
/// </summary>
internal sealed class RepeatedFieldAccessor : FieldAccessorBase
{
internal RepeatedFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor)
internal RepeatedFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor)
{
}
......
......@@ -48,11 +48,8 @@ namespace Google.Protobuf.Reflection
private readonly Action<object, object> setValueDelegate;
private readonly Action<object> clearDelegate;
internal SingleFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor)
internal SingleFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor)
{
PropertyInfo property = type.GetProperty(propertyName);
// We know there *is* such a property, or the base class constructor would have thrown. We should be able to write
// to it though.
if (!property.CanWrite)
{
throw new ArgumentException("Not all required properties/methods available");
......
......@@ -14,9 +14,6 @@ namespace Google.Protobuf.WellKnownTypes {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class Any {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Any__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -31,11 +28,10 @@ namespace Google.Protobuf.WellKnownTypes {
"bS5nb29nbGUucHJvdG9idWZCCEFueVByb3RvUAGgAQGiAgNHUEKqAh5Hb29n",
"bGUuUHJvdG9idWYuV2VsbEtub3duVHlwZXNiBnByb3RvMw=="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
});
internal__static_google_protobuf_Any__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Any), descriptor.MessageTypes[0],
new string[] { "TypeUrl", "Value", }, new string[] { });
new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Any), new[]{ "TypeUrl", "Value" }, null, null, null)
}));
}
#endregion
......@@ -47,14 +43,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Any> _parser = new pb::MessageParser<Any>(() => new Any());
public static pb::MessageParser<Any> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "type_url", "value" };
private static readonly uint[] _fieldTags = new uint[] { 10, 18 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Any.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Any.internal__static_google_protobuf_Any__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -14,10 +14,6 @@ namespace Google.Protobuf.WellKnownTypes {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class Api {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Api__FieldAccessorTable;
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Method__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -41,16 +37,11 @@ namespace Google.Protobuf.WellKnownTypes {
"dG9QAaICA0dQQqoCHkdvb2dsZS5Qcm90b2J1Zi5XZWxsS25vd25UeXBlc2IG",
"cHJvdG8z"));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
global::Google.Protobuf.WellKnownTypes.Proto.SourceContext.Descriptor,
global::Google.Protobuf.WellKnownTypes.Proto.Type.Descriptor,
});
internal__static_google_protobuf_Api__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Api), descriptor.MessageTypes[0],
new string[] { "Name", "Methods", "Options", "Version", "SourceContext", }, new string[] { });
internal__static_google_protobuf_Method__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Method), descriptor.MessageTypes[1],
new string[] { "Name", "RequestTypeUrl", "RequestStreaming", "ResponseTypeUrl", "ResponseStreaming", "Options", }, new string[] { });
new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.Proto.SourceContext.Descriptor, global::Google.Protobuf.WellKnownTypes.Proto.Type.Descriptor, },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Api), new[]{ "Name", "Methods", "Options", "Version", "SourceContext" }, null, null, null),
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Method), new[]{ "Name", "RequestTypeUrl", "RequestStreaming", "ResponseTypeUrl", "ResponseStreaming", "Options" }, null, null, null)
}));
}
#endregion
......@@ -62,14 +53,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Api> _parser = new pb::MessageParser<Api>(() => new Api());
public static pb::MessageParser<Api> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "methods", "name", "options", "source_context", "version" };
private static readonly uint[] _fieldTags = new uint[] { 18, 10, 26, 42, 34 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Api.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Api.internal__static_google_protobuf_Api__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......@@ -280,14 +269,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Method> _parser = new pb::MessageParser<Method>(() => new Method());
public static pb::MessageParser<Method> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "name", "options", "request_streaming", "request_type_url", "response_streaming", "response_type_url" };
private static readonly uint[] _fieldTags = new uint[] { 10, 50, 24, 18, 40, 34 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Api.Descriptor.MessageTypes[1]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Api.internal__static_google_protobuf_Method__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -14,9 +14,6 @@ namespace Google.Protobuf.WellKnownTypes {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class Duration {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Duration__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -32,11 +29,10 @@ namespace Google.Protobuf.WellKnownTypes {
"AaICA0dQQqoCHkdvb2dsZS5Qcm90b2J1Zi5XZWxsS25vd25UeXBlc2IGcHJv",
"dG8z"));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
});
internal__static_google_protobuf_Duration__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Duration), descriptor.MessageTypes[0],
new string[] { "Seconds", "Nanos", }, new string[] { });
new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Duration), new[]{ "Seconds", "Nanos" }, null, null, null)
}));
}
#endregion
......@@ -48,14 +44,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Duration> _parser = new pb::MessageParser<Duration>(() => new Duration());
public static pb::MessageParser<Duration> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "nanos", "seconds" };
private static readonly uint[] _fieldTags = new uint[] { 16, 8 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Duration.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Duration.internal__static_google_protobuf_Duration__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -14,9 +14,6 @@ namespace Google.Protobuf.WellKnownTypes {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class Empty {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Empty__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -31,11 +28,10 @@ namespace Google.Protobuf.WellKnownTypes {
"UAGiAgNHUEKqAh5Hb29nbGUuUHJvdG9idWYuV2VsbEtub3duVHlwZXNiBnBy",
"b3RvMw=="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
});
internal__static_google_protobuf_Empty__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Empty), descriptor.MessageTypes[0],
new string[] { }, new string[] { });
new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Empty), null, null, null, null)
}));
}
#endregion
......@@ -47,14 +43,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Empty> _parser = new pb::MessageParser<Empty>(() => new Empty());
public static pb::MessageParser<Empty> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { };
private static readonly uint[] _fieldTags = new uint[] { };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Empty.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Empty.internal__static_google_protobuf_Empty__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -14,9 +14,6 @@ namespace Google.Protobuf.WellKnownTypes {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class FieldMask {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_google_protobuf_FieldMask__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -31,11 +28,10 @@ namespace Google.Protobuf.WellKnownTypes {
"Z2xlLnByb3RvYnVmQg5GaWVsZE1hc2tQcm90b1ABogIDR1BCqgIeR29vZ2xl",
"LlByb3RvYnVmLldlbGxLbm93blR5cGVzYgZwcm90bzM="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
});
internal__static_google_protobuf_FieldMask__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.FieldMask), descriptor.MessageTypes[0],
new string[] { "Paths", }, new string[] { });
new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.FieldMask), new[]{ "Paths" }, null, null, null)
}));
}
#endregion
......@@ -47,14 +43,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<FieldMask> _parser = new pb::MessageParser<FieldMask>(() => new FieldMask());
public static pb::MessageParser<FieldMask> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "paths" };
private static readonly uint[] _fieldTags = new uint[] { 10 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.FieldMask.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.FieldMask.internal__static_google_protobuf_FieldMask__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -14,9 +14,6 @@ namespace Google.Protobuf.WellKnownTypes {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class SourceContext {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_google_protobuf_SourceContext__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -32,11 +29,10 @@ namespace Google.Protobuf.WellKnownTypes {
"AaICA0dQQqoCHkdvb2dsZS5Qcm90b2J1Zi5XZWxsS25vd25UeXBlc2IGcHJv",
"dG8z"));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
});
internal__static_google_protobuf_SourceContext__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.SourceContext), descriptor.MessageTypes[0],
new string[] { "FileName", }, new string[] { });
new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.SourceContext), new[]{ "FileName" }, null, null, null)
}));
}
#endregion
......@@ -48,14 +44,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<SourceContext> _parser = new pb::MessageParser<SourceContext>(() => new SourceContext());
public static pb::MessageParser<SourceContext> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "file_name" };
private static readonly uint[] _fieldTags = new uint[] { 10 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.SourceContext.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.SourceContext.internal__static_google_protobuf_SourceContext__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -14,11 +14,6 @@ namespace Google.Protobuf.WellKnownTypes {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class Struct {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Struct__FieldAccessorTable;
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Value__FieldAccessorTable;
internal static pbr::FieldAccessorTable internal__static_google_protobuf_ListValue__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -43,17 +38,12 @@ namespace Google.Protobuf.WellKnownTypes {
"AQGiAgNHUEKqAh5Hb29nbGUuUHJvdG9idWYuV2VsbEtub3duVHlwZXNiBnBy",
"b3RvMw=="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
});
internal__static_google_protobuf_Struct__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Struct), descriptor.MessageTypes[0],
new string[] { "Fields", }, new string[] { });
internal__static_google_protobuf_Value__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Value), descriptor.MessageTypes[1],
new string[] { "NullValue", "NumberValue", "StringValue", "BoolValue", "StructValue", "ListValue", }, new string[] { "Kind", });
internal__static_google_protobuf_ListValue__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.ListValue), descriptor.MessageTypes[2],
new string[] { "Values", }, new string[] { });
new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(new[] {typeof(global::Google.Protobuf.WellKnownTypes.NullValue), }, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Struct), new[]{ "Fields" }, null, null, new pbr::GeneratedCodeInfo[] { null, }),
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Value), new[]{ "NullValue", "NumberValue", "StringValue", "BoolValue", "StructValue", "ListValue" }, new[]{ "Kind" }, null, null),
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.ListValue), new[]{ "Values" }, null, null, null)
}));
}
#endregion
......@@ -72,14 +62,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Struct> _parser = new pb::MessageParser<Struct>(() => new Struct());
public static pb::MessageParser<Struct> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "fields" };
private static readonly uint[] _fieldTags = new uint[] { 10 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Struct.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Struct.internal__static_google_protobuf_Struct__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......@@ -183,14 +171,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Value> _parser = new pb::MessageParser<Value>(() => new Value());
public static pb::MessageParser<Value> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "bool_value", "list_value", "null_value", "number_value", "string_value", "struct_value" };
private static readonly uint[] _fieldTags = new uint[] { 32, 50, 8, 17, 26, 42 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Struct.Descriptor.MessageTypes[1]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Struct.internal__static_google_protobuf_Value__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......@@ -488,14 +474,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<ListValue> _parser = new pb::MessageParser<ListValue>(() => new ListValue());
public static pb::MessageParser<ListValue> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "values" };
private static readonly uint[] _fieldTags = new uint[] { 10 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Struct.Descriptor.MessageTypes[2]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Struct.internal__static_google_protobuf_ListValue__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -14,9 +14,6 @@ namespace Google.Protobuf.WellKnownTypes {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class Timestamp {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Timestamp__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -32,11 +29,10 @@ namespace Google.Protobuf.WellKnownTypes {
"AaABAaICA0dQQqoCHkdvb2dsZS5Qcm90b2J1Zi5XZWxsS25vd25UeXBlc2IG",
"cHJvdG8z"));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
});
internal__static_google_protobuf_Timestamp__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Timestamp), descriptor.MessageTypes[0],
new string[] { "Seconds", "Nanos", }, new string[] { });
new pbr::FileDescriptor[] { },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Timestamp), new[]{ "Seconds", "Nanos" }, null, null, null)
}));
}
#endregion
......@@ -48,14 +44,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Timestamp> _parser = new pb::MessageParser<Timestamp>(() => new Timestamp());
public static pb::MessageParser<Timestamp> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "nanos", "seconds" };
private static readonly uint[] _fieldTags = new uint[] { 16, 8 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Timestamp.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Timestamp.internal__static_google_protobuf_Timestamp__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -14,13 +14,6 @@ namespace Google.Protobuf.WellKnownTypes {
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static partial class Type {
#region Static variables
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Type__FieldAccessorTable;
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Field__FieldAccessorTable;
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Enum__FieldAccessorTable;
internal static pbr::FieldAccessorTable internal__static_google_protobuf_EnumValue__FieldAccessorTable;
internal static pbr::FieldAccessorTable internal__static_google_protobuf_Option__FieldAccessorTable;
#endregion
#region Descriptor
public static pbr::FileDescriptor Descriptor {
get { return descriptor; }
......@@ -62,25 +55,14 @@ namespace Google.Protobuf.WellKnownTypes {
"UAGiAgNHUEKqAh5Hb29nbGUuUHJvdG9idWYuV2VsbEtub3duVHlwZXNiBnBy",
"b3RvMw=="));
descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,
new pbr::FileDescriptor[] {
global::Google.Protobuf.WellKnownTypes.Proto.Any.Descriptor,
global::Google.Protobuf.WellKnownTypes.Proto.SourceContext.Descriptor,
});
internal__static_google_protobuf_Type__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Type), descriptor.MessageTypes[0],
new string[] { "Name", "Fields", "Oneofs", "Options", "SourceContext", }, new string[] { });
internal__static_google_protobuf_Field__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Field), descriptor.MessageTypes[1],
new string[] { "Kind", "Cardinality", "Number", "Name", "TypeUrl", "OneofIndex", "Packed", "Options", }, new string[] { });
internal__static_google_protobuf_Enum__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Enum), descriptor.MessageTypes[2],
new string[] { "Name", "Enumvalue", "Options", "SourceContext", }, new string[] { });
internal__static_google_protobuf_EnumValue__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.EnumValue), descriptor.MessageTypes[3],
new string[] { "Name", "Number", "Options", }, new string[] { });
internal__static_google_protobuf_Option__FieldAccessorTable =
new pbr::FieldAccessorTable(typeof(global::Google.Protobuf.WellKnownTypes.Option), descriptor.MessageTypes[4],
new string[] { "Name", "Value", }, new string[] { });
new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.Proto.Any.Descriptor, global::Google.Protobuf.WellKnownTypes.Proto.SourceContext.Descriptor, },
new pbr::GeneratedCodeInfo(null, new pbr::GeneratedCodeInfo[] {
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Type), new[]{ "Name", "Fields", "Oneofs", "Options", "SourceContext" }, null, null, null),
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Field), new[]{ "Kind", "Cardinality", "Number", "Name", "TypeUrl", "OneofIndex", "Packed", "Options" }, null, new[]{ typeof(global::Google.Protobuf.WellKnownTypes.Field.Types.Kind), typeof(global::Google.Protobuf.WellKnownTypes.Field.Types.Cardinality) }, null),
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Enum), new[]{ "Name", "Enumvalue", "Options", "SourceContext" }, null, null, null),
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.EnumValue), new[]{ "Name", "Number", "Options" }, null, null, null),
new pbr::GeneratedCodeInfo(typeof(global::Google.Protobuf.WellKnownTypes.Option), new[]{ "Name", "Value" }, null, null, null)
}));
}
#endregion
......@@ -92,14 +74,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Type> _parser = new pb::MessageParser<Type>(() => new Type());
public static pb::MessageParser<Type> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "fields", "name", "oneofs", "options", "source_context" };
private static readonly uint[] _fieldTags = new uint[] { 18, 10, 26, 34, 42 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Type.Descriptor.MessageTypes[0]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Type.internal__static_google_protobuf_Type__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......@@ -302,14 +282,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Field> _parser = new pb::MessageParser<Field>(() => new Field());
public static pb::MessageParser<Field> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "cardinality", "kind", "name", "number", "oneof_index", "options", "packed", "type_url" };
private static readonly uint[] _fieldTags = new uint[] { 16, 8, 34, 24, 56, 74, 64, 50 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Type.Descriptor.MessageTypes[1]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Type.internal__static_google_protobuf_Field__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......@@ -636,14 +614,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Enum> _parser = new pb::MessageParser<Enum>(() => new Enum());
public static pb::MessageParser<Enum> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "enumvalue", "name", "options", "source_context" };
private static readonly uint[] _fieldTags = new uint[] { 18, 10, 26, 34 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Type.Descriptor.MessageTypes[2]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Type.internal__static_google_protobuf_Enum__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......@@ -827,14 +803,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<EnumValue> _parser = new pb::MessageParser<EnumValue>(() => new EnumValue());
public static pb::MessageParser<EnumValue> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "name", "number", "options" };
private static readonly uint[] _fieldTags = new uint[] { 10, 16, 26 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Type.Descriptor.MessageTypes[3]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Type.internal__static_google_protobuf_EnumValue__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......@@ -992,14 +966,12 @@ namespace Google.Protobuf.WellKnownTypes {
private static readonly pb::MessageParser<Option> _parser = new pb::MessageParser<Option>(() => new Option());
public static pb::MessageParser<Option> Parser { get { return _parser; } }
private static readonly string[] _fieldNames = new string[] { "name", "value" };
private static readonly uint[] _fieldTags = new uint[] { 10, 18 };
public static pbr::MessageDescriptor Descriptor {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Type.Descriptor.MessageTypes[4]; }
}
pbr::FieldAccessorTable pb::IReflectedMessage.Fields {
get { return global::Google.Protobuf.WellKnownTypes.Proto.Type.internal__static_google_protobuf_Option__FieldAccessorTable; }
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}
private bool _frozen = false;
......
......@@ -85,7 +85,6 @@ void FieldGeneratorBase::SetCommonFieldVariables(
}
(*variables)["capitalized_type_name"] = capitalized_type_name();
(*variables)["number"] = number();
(*variables)["field_ordinal"] = field_ordinal();
(*variables)["has_property_check"] =
(*variables)["property_name"] + " != " + (*variables)["default_value"];
(*variables)["other_has_property_check"] = "other." +
......@@ -431,10 +430,6 @@ std::string FieldGeneratorBase::capitalized_type_name() {
}
}
std::string FieldGeneratorBase::field_ordinal() {
return SimpleItoa(fieldOrdinal_);
}
} // namespace csharp
} // namespace compiler
} // namespace protobuf
......
......@@ -85,7 +85,6 @@ class FieldGeneratorBase : public SourceGeneratorBase {
std::string default_value(const FieldDescriptor* descriptor);
std::string number();
std::string capitalized_type_name();
std::string field_ordinal();
private:
void SetCommonFieldVariables(map<string, string>* variables);
......
......@@ -77,6 +77,8 @@ std::string GetFullUmbrellaClassName(const FileDescriptor* descriptor);
std::string GetQualifiedUmbrellaClassName(const FileDescriptor* descriptor);
std::string GetClassName(const Descriptor* descriptor);
std::string GetClassName(const EnumDescriptor* descriptor);
std::string GetFieldName(const FieldDescriptor* descriptor);
......@@ -119,10 +121,6 @@ inline bool IsDescriptorProto(const FileDescriptor* descriptor) {
return descriptor->name() == "google/protobuf/descriptor_proto_file.proto";
}
inline bool IsMapEntry(const Descriptor* descriptor) {
return descriptor->options().map_entry();
}
inline bool IsWrapperType(const FieldDescriptor* descriptor) {
return descriptor->type() == FieldDescriptor::TYPE_MESSAGE &&
descriptor->message_type()->file()->name() == "google/protobuf/wrappers.proto";
......
......@@ -96,88 +96,11 @@ const std::vector<const FieldDescriptor*>& MessageGenerator::fields_by_number()
return fields_by_number_;
}
/// Get an identifier that uniquely identifies this type within the file.
/// This is used to declare static variables related to this type at the
/// outermost file scope.
std::string GetUniqueFileScopeIdentifier(const Descriptor* descriptor) {
std::string result = descriptor->full_name();
std::replace(result.begin(), result.end(), '.', '_');
return "static_" + result;
}
void MessageGenerator::GenerateStaticVariables(io::Printer* printer) {
// Because descriptor.proto (Google.Protobuf.DescriptorProtos) is
// used in the construction of descriptors, we have a tricky bootstrapping
// problem. To help control static initialization order, we make sure all
// descriptors and other static data that depends on them are members of
// the proto-descriptor class. This way, they will be initialized in
// a deterministic order.
std::string identifier = GetUniqueFileScopeIdentifier(descriptor_);
// The descriptor for this type.
printer->Print(
"internal static pbr::FieldAccessorTable internal__$identifier$__FieldAccessorTable;\n",
"identifier", GetUniqueFileScopeIdentifier(descriptor_),
"full_class_name", full_class_name());
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
// Don't generate accessor table fields for maps...
if (!IsMapEntryMessage(descriptor_->nested_type(i))) {
MessageGenerator messageGenerator(descriptor_->nested_type(i));
messageGenerator.GenerateStaticVariables(printer);
}
}
}
void MessageGenerator::GenerateStaticVariableInitializers(io::Printer* printer) {
map<string, string> vars;
vars["identifier"] = GetUniqueFileScopeIdentifier(descriptor_);
vars["full_class_name"] = full_class_name();
// Work out how to get to the message descriptor (which may be multiply nested) from the file
// descriptor.
string descriptor_chain;
const Descriptor* current_descriptor = descriptor_;
while (current_descriptor->containing_type()) {
descriptor_chain = ".NestedTypes[" + SimpleItoa(current_descriptor->index()) + "]" + descriptor_chain;
current_descriptor = current_descriptor->containing_type();
}
descriptor_chain = "descriptor.MessageTypes[" + SimpleItoa(current_descriptor->index()) + "]" + descriptor_chain;
vars["descriptor_chain"] = descriptor_chain;
printer->Print(
vars,
"internal__$identifier$__FieldAccessorTable = \n"
" new pbr::FieldAccessorTable(typeof($full_class_name$), $descriptor_chain$,\n");
printer->Print(" new string[] { ");
for (int i = 0; i < descriptor_->field_count(); i++) {
printer->Print("\"$property_name$\", ",
"property_name", GetPropertyName(descriptor_->field(i)));
}
printer->Print("}, new string[] { ");
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
printer->Print("\"$oneof_name$\", ",
"oneof_name",
UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true));
}
printer->Print("});\n");
// Generate static member initializers for all non-map-entry nested types.
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
if (!IsMapEntryMessage(descriptor_->nested_type(i))) {
MessageGenerator messageGenerator(descriptor_->nested_type(i));
messageGenerator.GenerateStaticVariableInitializers(printer);
}
}
}
void MessageGenerator::Generate(io::Printer* printer) {
map<string, string> vars;
vars["class_name"] = class_name();
vars["access_level"] = class_access_level();
vars["umbrella_class_name"] = GetFullUmbrellaClassName(descriptor_->file());
vars["identifier"] = GetUniqueFileScopeIdentifier(descriptor_);
printer->Print(
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n");
......@@ -192,19 +115,6 @@ void MessageGenerator::Generate(io::Printer* printer) {
vars,
"private static readonly pb::MessageParser<$class_name$> _parser = new pb::MessageParser<$class_name$>(() => new $class_name$());\n"
"public static pb::MessageParser<$class_name$> Parser { get { return _parser; } }\n\n");
printer->Print(
"private static readonly string[] _fieldNames = "
"new string[] { $slash$$field_names$$slash$ };\n",
"field_names", JoinStrings(field_names(), "\", \""),
"slash", field_names().size() > 0 ? "\"" : "");
std::vector<std::string> tags;
for (int i = 0; i < field_names().size(); i++) {
uint32 tag = FixedMakeTag(descriptor_->FindFieldByName(field_names()[i]));
tags.push_back(SimpleItoa(tag));
}
printer->Print(
"private static readonly uint[] _fieldTags = new uint[] { $tags$ };\n",
"tags", JoinStrings(tags, ", "));
// Access the message descriptor via the relevant file descriptor or containing message descriptor.
if (!descriptor_->containing_type()) {
......@@ -221,8 +131,8 @@ void MessageGenerator::Generate(io::Printer* printer) {
" get { return $descriptor_accessor$; }\n"
"}\n"
"\n"
"pbr::FieldAccessorTable pb::IReflectedMessage.Fields {\n"
" get { return $umbrella_class_name$.internal__$identifier$__FieldAccessorTable; }\n"
"pbr::MessageDescriptor pb::IMessage.Descriptor {\n"
" get { return Descriptor; }\n"
"}\n"
"\n"
"private bool _frozen = false;\n"
......@@ -258,6 +168,7 @@ void MessageGenerator::Generate(io::Printer* printer) {
for (int i = 0; i < descriptor_->oneof_decl_count(); i++) {
vars["name"] = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), false);
vars["property_name"] = UnderscoresToCamelCase(descriptor_->oneof_decl(i)->name(), true);
vars["original_name"] = descriptor_->oneof_decl(i)->name();
printer->Print(
vars,
"private object $name$_;\n"
......
......@@ -53,8 +53,6 @@ class MessageGenerator : public SourceGeneratorBase {
void GenerateCloningCode(io::Printer* printer);
void GenerateFreezingCode(io::Printer* printer);
void GenerateFrameworkMethods(io::Printer* printer);
void GenerateStaticVariables(io::Printer* printer);
void GenerateStaticVariableInitializers(io::Printer* printer);
void Generate(io::Printer* printer);
private:
......
......@@ -59,7 +59,7 @@ void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer) {
printer->Print(
variables_,
"private static readonly pb::FieldCodec<$type_name$> _repeated_$name$_codec\n"
" = pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x);");
" = pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x);\n");
printer->Print(variables_,
"private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
AddDeprecatedFlag(printer);
......
......@@ -36,6 +36,7 @@
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/compiler/csharp/csharp_enum.h>
......@@ -63,12 +64,6 @@ UmbrellaClassGenerator::~UmbrellaClassGenerator() {
void UmbrellaClassGenerator::Generate(io::Printer* printer) {
WriteIntroduction(printer);
printer->Print("#region Static variables\n");
for (int i = 0; i < file_->message_type_count(); i++) {
MessageGenerator messageGenerator(file_->message_type(i));
messageGenerator.GenerateStaticVariables(printer);
}
printer->Print("#endregion\n");
WriteDescriptor(printer);
// Close the class declaration.
printer->Outdent();
......@@ -174,33 +169,126 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) {
printer->Print("\"$base64$\", \n", "base64", base64.substr(0, 60));
base64 = base64.substr(60);
}
printer->Outdent();
printer->Print("\"$base64$\"));\n", "base64", base64);
printer->Outdent();
printer->Outdent();
printer->Outdent();
// -----------------------------------------------------------------
// Invoke InternalBuildGeneratedFileFrom() to build the file.
printer->Print(
"descriptor = pbr::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,\n");
printer->Print(" new pbr::FileDescriptor[] {\n");
printer->Print(" new pbr::FileDescriptor[] { ");
for (int i = 0; i < file_->dependency_count(); i++) {
printer->Print(
" $full_umbrella_class_name$.Descriptor, \n",
"$full_umbrella_class_name$.Descriptor, ",
"full_umbrella_class_name",
GetFullUmbrellaClassName(file_->dependency(i)));
}
printer->Print(" });\n");
// Then invoke any other static variable initializers, e.g. field accessors.
printer->Print("},\n"
" new pbr::GeneratedCodeInfo(");
// Specify all the generated code information, recursively.
if (file_->enum_type_count() > 0) {
printer->Print("new[] {");
for (int i = 0; i < file_->enum_type_count(); i++) {
printer->Print("typeof($type_name$), ", "type_name", GetClassName(file_->enum_type(i)));
}
printer->Print("}, ");
}
else {
printer->Print("null, ");
}
if (file_->message_type_count() > 0) {
printer->Print("new pbr::GeneratedCodeInfo[] {\n");
printer->Indent();
printer->Indent();
printer->Indent();
for (int i = 0; i < file_->message_type_count(); i++) {
MessageGenerator messageGenerator(file_->message_type(i));
messageGenerator.GenerateStaticVariableInitializers(printer);
WriteGeneratedCodeInfo(file_->message_type(i), printer, i == file_->message_type_count() - 1);
}
printer->Outdent();
printer->Print("\n}));\n");
printer->Outdent();
printer->Outdent();
}
else {
printer->Print("null));\n");
}
printer->Outdent();
printer->Print("}\n");
printer->Print("#endregion\n\n");
}
// Write out the generated code for a particular message. This consists of the CLR type, property names
// corresponding to fields, names corresponding to oneofs, nested enums, and nested types. Each array part
// can be specified as null if it would be empty, to make the generated code somewhat simpler to read.
// We write a line break at the end of each generated code info, so that in the final file we'll see all
// the types, pre-ordered depth first, one per line. The indentation will be slightly unusual,
// in that it will look like a single array when it's actually constructing a tree, but it'll be easy to
// read even with multiple levels of nesting.
// The "last" parameter indicates whether this message descriptor is the last one being printed in this immediate
// context. It governs whether or not a trailing comma and newline is written after the constructor, effectively
// just controlling the formatting in the generated code.
void UmbrellaClassGenerator::WriteGeneratedCodeInfo(const Descriptor* descriptor, io::Printer* printer, bool last) {
if (IsMapEntryMessage(descriptor)) {
printer->Print("null, ");
return;
}
// Generated message type
printer->Print("new pbr::GeneratedCodeInfo(typeof($type_name$), ", "type_name", GetClassName(descriptor));
// Fields
if (descriptor->field_count() > 0) {
std::vector<std::string> fields;
for (int i = 0; i < descriptor->field_count(); i++) {
fields.push_back(GetPropertyName(descriptor->field(i)));
}
printer->Print("new[]{ \"$fields$\" }, ", "fields", JoinStrings(fields, "\", \""));
}
else {
printer->Print("null, ");
}
// Oneofs
if (descriptor->oneof_decl_count() > 0) {
std::vector<std::string> oneofs;
for (int i = 0; i < descriptor->oneof_decl_count(); i++) {
oneofs.push_back(UnderscoresToCamelCase(descriptor->oneof_decl(i)->name(), true));
}
printer->Print("new[]{ \"$oneofs$\" }, ", "oneofs", JoinStrings(oneofs, "\", \""));
}
else {
printer->Print("null, ");
}
// Nested enums
if (descriptor->enum_type_count() > 0) {
std::vector<std::string> enums;
for (int i = 0; i < descriptor->enum_type_count(); i++) {
enums.push_back(GetClassName(descriptor->enum_type(i)));
}
printer->Print("new[]{ typeof($enums$) }, ", "enums", JoinStrings(enums, "), typeof("));
}
else {
printer->Print("null, ");
}
// Nested types
if (descriptor->nested_type_count() > 0) {
// Need to specify array type explicitly here, as all elements may be null.
printer->Print("new pbr::GeneratedCodeInfo[] { ");
for (int i = 0; i < descriptor->nested_type_count(); i++) {
WriteGeneratedCodeInfo(descriptor->nested_type(i), printer, i == descriptor->nested_type_count() - 1);
}
printer->Print("}");
}
else {
printer->Print("null");
}
printer->Print(last ? ")" : "),\n");
}
} // namespace csharp
} // namespace compiler
} // namespace protobuf
......
......@@ -57,6 +57,7 @@ class UmbrellaClassGenerator : public SourceGeneratorBase {
void WriteIntroduction(io::Printer* printer);
void WriteDescriptor(io::Printer* printer);
void WriteGeneratedCodeInfo(const Descriptor* descriptor, io::Printer* printer, bool last);
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UmbrellaClassGenerator);
};
......
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