Commit bea87743 authored by Jon Skeet's avatar Jon Skeet

Merge pull request #634 from jskeet/reflection2

Reflection part 2 - for discussion
parents 7b5c3967 c1c6b2d0
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
...@@ -82,6 +82,7 @@ ...@@ -82,6 +82,7 @@
<Compile Include="Collections\RepeatedFieldTest.cs" /> <Compile Include="Collections\RepeatedFieldTest.cs" />
<Compile Include="JsonFormatterTest.cs" /> <Compile Include="JsonFormatterTest.cs" />
<Compile Include="Reflection\DescriptorsTest.cs" /> <Compile Include="Reflection\DescriptorsTest.cs" />
<Compile Include="Reflection\FieldAccessTest.cs" />
<Compile Include="SampleEnum.cs" /> <Compile Include="SampleEnum.cs" />
<Compile Include="SampleMessages.cs" /> <Compile Include="SampleMessages.cs" />
<Compile Include="TestProtos\MapUnittestProto3.cs" /> <Compile Include="TestProtos\MapUnittestProto3.cs" />
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
using System.Linq; using System.Linq;
using Google.Protobuf.TestProtos; using Google.Protobuf.TestProtos;
using NUnit.Framework; using NUnit.Framework;
using UnitTest.Issues.TestProtos;
namespace Google.Protobuf.Reflection namespace Google.Protobuf.Reflection
{ {
...@@ -102,15 +103,16 @@ namespace Google.Protobuf.Reflection ...@@ -102,15 +103,16 @@ namespace Google.Protobuf.Reflection
Assert.AreEqual(UnittestProto3.Descriptor, nestedType.File); Assert.AreEqual(UnittestProto3.Descriptor, nestedType.File);
Assert.AreEqual(messageType, nestedType.ContainingType); Assert.AreEqual(messageType, nestedType.ContainingType);
FieldDescriptor field = messageType.Fields[0]; FieldDescriptor field = messageType.Fields.InDeclarationOrder()[0];
Assert.AreEqual("single_int32", field.Name); Assert.AreEqual("single_int32", field.Name);
Assert.AreEqual(field, messageType.FindDescriptor<FieldDescriptor>("single_int32")); Assert.AreEqual(field, messageType.FindDescriptor<FieldDescriptor>("single_int32"));
Assert.Null(messageType.FindDescriptor<FieldDescriptor>("no_such_field")); Assert.Null(messageType.FindDescriptor<FieldDescriptor>("no_such_field"));
Assert.AreEqual(field, messageType.FindFieldByNumber(1)); Assert.AreEqual(field, messageType.FindFieldByNumber(1));
Assert.Null(messageType.FindFieldByNumber(571283)); Assert.Null(messageType.FindFieldByNumber(571283));
for (int i = 0; i < messageType.Fields.Count; i++) var fieldsInDeclarationOrder = messageType.Fields.InDeclarationOrder();
for (int i = 0; i < fieldsInDeclarationOrder.Count; i++)
{ {
Assert.AreEqual(i, messageType.Fields[i].Index); Assert.AreEqual(i, fieldsInDeclarationOrder[i].Index);
} }
Assert.AreEqual(nestedType, messageType.NestedTypes[0]); Assert.AreEqual(nestedType, messageType.NestedTypes[0]);
...@@ -220,5 +222,19 @@ namespace Google.Protobuf.Reflection ...@@ -220,5 +222,19 @@ namespace Google.Protobuf.Reflection
CollectionAssert.AreEquivalent(expectedFields, descriptor.Fields); CollectionAssert.AreEquivalent(expectedFields, descriptor.Fields);
} }
[Test]
public void ConstructionWithoutGeneratedCodeInfo()
{
var data = UnittestIssues.Descriptor.Proto.ToByteArray();
var newDescriptor = Google.Protobuf.Reflection.FileDescriptor.InternalBuildGeneratedFileFrom(data, new Reflection.FileDescriptor[] { }, null);
// We should still be able to get at a field...
var messageDescriptor = newDescriptor.FindTypeByName<MessageDescriptor>("ItemField");
var fieldDescriptor = messageDescriptor.FindFieldByName("item");
// But there shouldn't be an accessor (or a generated type for the message)
Assert.IsNull(fieldDescriptor.Accessor);
Assert.IsNull(messageDescriptor.GeneratedType);
}
} }
} }
\ No newline at end of file
This diff is collapsed.
...@@ -192,23 +192,23 @@ namespace Google.Protobuf.WellKnownTypes ...@@ -192,23 +192,23 @@ namespace Google.Protobuf.WellKnownTypes
Uint32Field = 3, Uint32Field = 3,
Uint64Field = 4 Uint64Field = 4
}; };
var fields = TestWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber; var fields = TestWellKnownTypes.Descriptor.Fields;
Assert.AreEqual("x", fields[TestWellKnownTypes.StringFieldFieldNumber].GetValue(message)); Assert.AreEqual("x", fields[TestWellKnownTypes.StringFieldFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(ByteString.CopyFrom(1, 2, 3), fields[TestWellKnownTypes.BytesFieldFieldNumber].GetValue(message)); Assert.AreEqual(ByteString.CopyFrom(1, 2, 3), fields[TestWellKnownTypes.BytesFieldFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(true, fields[TestWellKnownTypes.BoolFieldFieldNumber].GetValue(message)); Assert.AreEqual(true, fields[TestWellKnownTypes.BoolFieldFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(12.5f, fields[TestWellKnownTypes.FloatFieldFieldNumber].GetValue(message)); Assert.AreEqual(12.5f, fields[TestWellKnownTypes.FloatFieldFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(12.25d, fields[TestWellKnownTypes.DoubleFieldFieldNumber].GetValue(message)); Assert.AreEqual(12.25d, fields[TestWellKnownTypes.DoubleFieldFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(1, fields[TestWellKnownTypes.Int32FieldFieldNumber].GetValue(message)); Assert.AreEqual(1, fields[TestWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(2L, fields[TestWellKnownTypes.Int64FieldFieldNumber].GetValue(message)); Assert.AreEqual(2L, fields[TestWellKnownTypes.Int64FieldFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(3U, fields[TestWellKnownTypes.Uint32FieldFieldNumber].GetValue(message)); Assert.AreEqual(3U, fields[TestWellKnownTypes.Uint32FieldFieldNumber].Accessor.GetValue(message));
Assert.AreEqual(4UL, fields[TestWellKnownTypes.Uint64FieldFieldNumber].GetValue(message)); Assert.AreEqual(4UL, fields[TestWellKnownTypes.Uint64FieldFieldNumber].Accessor.GetValue(message));
// And a couple of null fields... // And a couple of null fields...
message.StringField = null; message.StringField = null;
message.FloatField = null; message.FloatField = null;
Assert.IsNull(fields[TestWellKnownTypes.StringFieldFieldNumber].GetValue(message)); Assert.IsNull(fields[TestWellKnownTypes.StringFieldFieldNumber].Accessor.GetValue(message));
Assert.IsNull(fields[TestWellKnownTypes.FloatFieldFieldNumber].GetValue(message)); Assert.IsNull(fields[TestWellKnownTypes.FloatFieldFieldNumber].Accessor.GetValue(message));
} }
[Test] [Test]
...@@ -216,8 +216,8 @@ namespace Google.Protobuf.WellKnownTypes ...@@ -216,8 +216,8 @@ namespace Google.Protobuf.WellKnownTypes
{ {
// Just a single example... note that we can't have a null value here // Just a single example... note that we can't have a null value here
var message = new RepeatedWellKnownTypes { Int32Field = { 1, 2 } }; var message = new RepeatedWellKnownTypes { Int32Field = { 1, 2 } };
var fields = RepeatedWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber; var fields = RepeatedWellKnownTypes.Descriptor.Fields;
var list = (IList) fields[RepeatedWellKnownTypes.Int32FieldFieldNumber].GetValue(message); var list = (IList) fields[RepeatedWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message);
CollectionAssert.AreEqual(new[] { 1, 2 }, list); CollectionAssert.AreEqual(new[] { 1, 2 }, list);
} }
...@@ -226,8 +226,8 @@ namespace Google.Protobuf.WellKnownTypes ...@@ -226,8 +226,8 @@ namespace Google.Protobuf.WellKnownTypes
{ {
// Just a single example... note that we can't have a null value here // Just a single example... note that we can't have a null value here
var message = new MapWellKnownTypes { Int32Field = { { 1, 2 }, { 3, null } } }; var message = new MapWellKnownTypes { Int32Field = { { 1, 2 }, { 3, null } } };
var fields = MapWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber; var fields = MapWellKnownTypes.Descriptor.Fields;
var dictionary = (IDictionary) fields[MapWellKnownTypes.Int32FieldFieldNumber].GetValue(message); var dictionary = (IDictionary) fields[MapWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message);
Assert.AreEqual(2, dictionary[1]); Assert.AreEqual(2, dictionary[1]);
Assert.IsNull(dictionary[3]); Assert.IsNull(dictionary[3]);
Assert.IsTrue(dictionary.Contains(3)); Assert.IsTrue(dictionary.Contains(3));
......
...@@ -140,7 +140,7 @@ namespace Google.Protobuf ...@@ -140,7 +140,7 @@ namespace Google.Protobuf
var fields = message.Descriptor.Fields; var fields = message.Descriptor.Fields;
bool first = true; bool first = true;
// First non-oneof fields // First non-oneof fields
foreach (var field in fields) foreach (var field in fields.InFieldNumberOrder())
{ {
var accessor = field.Accessor; var accessor = field.Accessor;
// Oneofs are written later // Oneofs are written later
......
...@@ -231,7 +231,7 @@ namespace Google.Protobuf.Reflection ...@@ -231,7 +231,7 @@ namespace Google.Protobuf.Reflection
/// Finds a type (message, enum, service or extension) in the file by name. Does not find nested types. /// Finds a type (message, enum, service or extension) in the file by name. Does not find nested types.
/// </summary> /// </summary>
/// <param name="name">The unqualified type name to look for.</param> /// <param name="name">The unqualified type name to look for.</param>
/// <typeparam name="T">The type of descriptor to look for (or ITypeDescriptor for any)</typeparam> /// <typeparam name="T">The type of descriptor to look for</typeparam>
/// <returns>The type's descriptor, or null if not found.</returns> /// <returns>The type's descriptor, or null if not found.</returns>
public T FindTypeByName<T>(String name) public T FindTypeByName<T>(String name)
where T : class, IDescriptor where T : class, IDescriptor
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
using Google.Protobuf.Collections; using Google.Protobuf.Collections;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
namespace Google.Protobuf.Reflection namespace Google.Protobuf.Reflection
...@@ -60,11 +61,12 @@ namespace Google.Protobuf.Reflection ...@@ -60,11 +61,12 @@ namespace Google.Protobuf.Reflection
private readonly MessageDescriptor containingType; private readonly MessageDescriptor containingType;
private readonly IList<MessageDescriptor> nestedTypes; private readonly IList<MessageDescriptor> nestedTypes;
private readonly IList<EnumDescriptor> enumTypes; private readonly IList<EnumDescriptor> enumTypes;
private readonly IList<FieldDescriptor> fields; private readonly IList<FieldDescriptor> fieldsInDeclarationOrder;
private readonly IList<FieldDescriptor> fieldsInNumberOrder;
private readonly FieldCollection fields;
private readonly IList<OneofDescriptor> oneofs; private readonly IList<OneofDescriptor> oneofs;
// CLR representation of the type described by this descriptor, if any. // CLR representation of the type described by this descriptor, if any.
private readonly Type generatedType; private readonly Type generatedType;
private IDictionary<int, IFieldAccessor> fieldAccessorsByFieldNumber;
internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex, GeneratedCodeInfo generatedCodeInfo) internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex, GeneratedCodeInfo generatedCodeInfo)
: base(file, file.ComputeFullName(parent, proto.Name), typeIndex) : base(file, file.ComputeFullName(parent, proto.Name), typeIndex)
...@@ -89,11 +91,13 @@ namespace Google.Protobuf.Reflection ...@@ -89,11 +91,13 @@ namespace Google.Protobuf.Reflection
(type, index) => (type, index) =>
new EnumDescriptor(type, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedEnums[index])); new EnumDescriptor(type, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedEnums[index]));
fields = DescriptorUtil.ConvertAndMakeReadOnly( fieldsInDeclarationOrder = DescriptorUtil.ConvertAndMakeReadOnly(
proto.Field, proto.Field,
(field, index) => (field, index) =>
new FieldDescriptor(field, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.PropertyNames[index])); new FieldDescriptor(field, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.PropertyNames[index]));
fieldsInNumberOrder = new ReadOnlyCollection<FieldDescriptor>(fieldsInDeclarationOrder.OrderBy(field => field.FieldNumber).ToArray());
file.DescriptorPool.AddSymbol(this); file.DescriptorPool.AddSymbol(this);
fields = new FieldCollection(this);
} }
/// <summary> /// <summary>
...@@ -136,9 +140,9 @@ namespace Google.Protobuf.Reflection ...@@ -136,9 +140,9 @@ namespace Google.Protobuf.Reflection
} }
/// <value> /// <value>
/// An unmodifiable list of this message type's fields. /// A collection of fields, which can be retrieved by name or field number.
/// </value> /// </value>
public IList<FieldDescriptor> Fields public FieldCollection Fields
{ {
get { return fields; } get { return fields; }
} }
...@@ -164,13 +168,6 @@ namespace Google.Protobuf.Reflection ...@@ -164,13 +168,6 @@ namespace Google.Protobuf.Reflection
get { return oneofs; } 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> /// <summary>
/// Finds a field by field name. /// Finds a field by field name.
/// </summary> /// </summary>
...@@ -213,7 +210,7 @@ namespace Google.Protobuf.Reflection ...@@ -213,7 +210,7 @@ namespace Google.Protobuf.Reflection
message.CrossLink(); message.CrossLink();
} }
foreach (FieldDescriptor field in fields) foreach (FieldDescriptor field in fieldsInDeclarationOrder)
{ {
field.CrossLink(); field.CrossLink();
} }
...@@ -222,8 +219,79 @@ namespace Google.Protobuf.Reflection ...@@ -222,8 +219,79 @@ namespace Google.Protobuf.Reflection
{ {
oneof.CrossLink(); oneof.CrossLink();
} }
}
/// <summary>
/// A collection to simplify retrieving the field accessor for a particular field.
/// </summary>
public sealed class FieldCollection
{
private readonly MessageDescriptor messageDescriptor;
internal FieldCollection(MessageDescriptor messageDescriptor)
{
this.messageDescriptor = messageDescriptor;
}
fieldAccessorsByFieldNumber = new ReadOnlyDictionary<int, IFieldAccessor>(fields.ToDictionary(field => field.FieldNumber, field => field.Accessor)); /// <value>
/// Returns the fields in the message as an immutable list, in the order in which they
/// are declared in the source .proto file.
/// </value>
public IList<FieldDescriptor> InDeclarationOrder()
{
return messageDescriptor.fieldsInDeclarationOrder;
}
/// <value>
/// Returns the fields in the message as an immutable list, in ascending field number
/// order. Field numbers need not be contiguous, so there is no direct mapping from the
/// index in the list to the field number; to retrieve a field by field number, it is better
/// to use the <see cref="FieldCollection"/> indexer.
/// </value>
public IList<FieldDescriptor> InFieldNumberOrder()
{
return messageDescriptor.fieldsInDeclarationOrder;
}
/// <summary>
/// Retrieves the descriptor for the field with the given number.
/// </summary>
/// <param name="number">Number of the field to retrieve the descriptor for</param>
/// <returns>The accessor for the given field</returns>
/// <exception cref="KeyNotFoundException">The message descriptor does not contain a field
/// with the given number</exception>
public FieldDescriptor this[int number]
{
get
{
var fieldDescriptor = messageDescriptor.FindFieldByNumber(number);
if (fieldDescriptor == null)
{
throw new KeyNotFoundException("No such field number");
}
return fieldDescriptor;
}
}
/// <summary>
/// Retrieves the descriptor for the field with the given name.
/// </summary>
/// <param name="number">Number of the field to retrieve the descriptor for</param>
/// <returns>The descriptor for the given field</returns>
/// <exception cref="KeyNotFoundException">The message descriptor does not contain a field
/// with the given name</exception>
public FieldDescriptor this[string name]
{
get
{
var fieldDescriptor = messageDescriptor.FindFieldByName(name);
if (fieldDescriptor == null)
{
throw new KeyNotFoundException("No such field name");
}
return fieldDescriptor;
}
}
} }
} }
} }
\ No newline at end of file
...@@ -70,7 +70,7 @@ namespace Google.Protobuf.Reflection ...@@ -70,7 +70,7 @@ namespace Google.Protobuf.Reflection
internal void CrossLink() internal void CrossLink()
{ {
List<FieldDescriptor> fieldCollection = new List<FieldDescriptor>(); List<FieldDescriptor> fieldCollection = new List<FieldDescriptor>();
foreach (var field in ContainingType.Fields) foreach (var field in ContainingType.Fields.InDeclarationOrder())
{ {
if (field.ContainingOneof == this) if (field.ContainingOneof == this)
{ {
......
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