Commit edff8888 authored by Jon Skeet's avatar Jon Skeet

Merge pull request #641 from jtattermusch/csharp_descriptor_database

Expose original binary data for file descriptor
parents bea87743 3b8c83ef
...@@ -82,6 +82,8 @@ namespace Google.Protobuf.Reflection ...@@ -82,6 +82,8 @@ namespace Google.Protobuf.Reflection
{ {
Assert.AreEqual(i, file.EnumTypes[i].Index); Assert.AreEqual(i, file.EnumTypes[i].Index);
} }
Assert.AreEqual(10, file.SerializedData[0]);
} }
[Test] [Test]
......
...@@ -43,6 +43,7 @@ namespace Google.Protobuf.Reflection ...@@ -43,6 +43,7 @@ namespace Google.Protobuf.Reflection
/// </summary> /// </summary>
public sealed class FileDescriptor : IDescriptor public sealed class FileDescriptor : IDescriptor
{ {
private readonly ByteString descriptorData;
private readonly FileDescriptorProto proto; private readonly FileDescriptorProto proto;
private readonly IList<MessageDescriptor> messageTypes; private readonly IList<MessageDescriptor> messageTypes;
private readonly IList<EnumDescriptor> enumTypes; private readonly IList<EnumDescriptor> enumTypes;
...@@ -62,8 +63,9 @@ namespace Google.Protobuf.Reflection ...@@ -62,8 +63,9 @@ namespace Google.Protobuf.Reflection
get { return proto.Syntax == "proto3" ? ProtoSyntax.Proto3 : ProtoSyntax.Proto2; } get { return proto.Syntax == "proto3" ? ProtoSyntax.Proto3 : ProtoSyntax.Proto2; }
} }
private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo) private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo)
{ {
this.descriptorData = descriptorData;
this.pool = pool; this.pool = pool;
this.proto = proto; this.proto = proto;
this.dependencies = new ReadOnlyCollection<FileDescriptor>((FileDescriptor[]) dependencies.Clone()); this.dependencies = new ReadOnlyCollection<FileDescriptor>((FileDescriptor[]) dependencies.Clone());
...@@ -203,6 +205,14 @@ namespace Google.Protobuf.Reflection ...@@ -203,6 +205,14 @@ namespace Google.Protobuf.Reflection
get { return publicDependencies; } get { return publicDependencies; }
} }
/// <value>
/// The original serialized binary form of this descriptor.
/// </value>
public ByteString SerializedData
{
get { return descriptorData; }
}
/// <value> /// <value>
/// Implementation of IDescriptor.FullName - just returns the same as Name. /// Implementation of IDescriptor.FullName - just returns the same as Name.
/// </value> /// </value>
...@@ -257,6 +267,9 @@ namespace Google.Protobuf.Reflection ...@@ -257,6 +267,9 @@ namespace Google.Protobuf.Reflection
/// <summary> /// <summary>
/// Builds a FileDescriptor from its protocol buffer representation. /// Builds a FileDescriptor from its protocol buffer representation.
/// </summary> /// </summary>
/// <param name="descriptorData">The original serialized descriptor data.
/// We have only limited proto2 support, so serializing FileDescriptorProto
/// would not necessarily give us this.</param>
/// <param name="proto">The protocol message form of the FileDescriptor.</param> /// <param name="proto">The protocol message form of the FileDescriptor.</param>
/// <param name="dependencies">FileDescriptors corresponding to all of the /// <param name="dependencies">FileDescriptors corresponding to all of the
/// file's dependencies, in the exact order listed in the .proto file. May be null, /// file's dependencies, in the exact order listed in the .proto file. May be null,
...@@ -266,7 +279,7 @@ namespace Google.Protobuf.Reflection ...@@ -266,7 +279,7 @@ namespace Google.Protobuf.Reflection
/// <exception cref="DescriptorValidationException">If <paramref name="proto"/> is not /// <exception cref="DescriptorValidationException">If <paramref name="proto"/> is not
/// a valid descriptor. This can occur for a number of reasons, such as a field /// 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> /// 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, GeneratedCodeInfo generatedCodeInfo) private static FileDescriptor BuildFrom(ByteString descriptorData, FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies, GeneratedCodeInfo generatedCodeInfo)
{ {
// Building descriptors involves two steps: translating and linking. // Building descriptors involves two steps: translating and linking.
// In the translation step (implemented by FileDescriptor's // In the translation step (implemented by FileDescriptor's
...@@ -283,7 +296,7 @@ namespace Google.Protobuf.Reflection ...@@ -283,7 +296,7 @@ namespace Google.Protobuf.Reflection
} }
DescriptorPool pool = new DescriptorPool(dependencies); DescriptorPool pool = new DescriptorPool(dependencies);
FileDescriptor result = new FileDescriptor(proto, dependencies, pool, allowUnknownDependencies, generatedCodeInfo); FileDescriptor result = new FileDescriptor(descriptorData, proto, dependencies, pool, allowUnknownDependencies, generatedCodeInfo);
// TODO(jonskeet): Reinstate these checks, or get rid of them entirely. They aren't in the Java code, // 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".) // and fail for the CustomOptions test right now. (We get "descriptor.proto" vs "google/protobuf/descriptor.proto".)
...@@ -342,11 +355,13 @@ namespace Google.Protobuf.Reflection ...@@ -342,11 +355,13 @@ namespace Google.Protobuf.Reflection
throw new ArgumentException("Failed to parse protocol buffer descriptor for generated code.", e); throw new ArgumentException("Failed to parse protocol buffer descriptor for generated code.", e);
} }
try try
{ {
// When building descriptors for generated code, we allow unknown // When building descriptors for generated code, we allow unknown
// dependencies by default. // dependencies by default.
return BuildFrom(proto, dependencies, true, generatedCodeInfo); return BuildFrom(ByteString.CopyFrom(descriptorData), proto, dependencies, true, generatedCodeInfo);
} }
catch (DescriptorValidationException e) catch (DescriptorValidationException e)
{ {
......
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