diff options
author | Jon Skeet <skeet@pobox.com> | 2010-11-03 11:21:52 +0000 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2010-11-03 11:21:52 +0000 |
commit | 57599ef16daeda48381711dae1600cf0c467689d (patch) | |
tree | 3030080b7b81b64cc1b43f107ad255acad16cbca /src/ProtocolBuffers/Descriptors | |
parent | c58ce5dc0c6dbe792ad53c63926bb3e8facef47b (diff) | |
download | protobuf-57599ef16daeda48381711dae1600cf0c467689d.tar.gz protobuf-57599ef16daeda48381711dae1600cf0c467689d.tar.bz2 protobuf-57599ef16daeda48381711dae1600cf0c467689d.zip |
A few stylistic issues
Diffstat (limited to 'src/ProtocolBuffers/Descriptors')
-rw-r--r-- | src/ProtocolBuffers/Descriptors/FileDescriptor.cs | 153 |
1 files changed, 85 insertions, 68 deletions
diff --git a/src/ProtocolBuffers/Descriptors/FileDescriptor.cs b/src/ProtocolBuffers/Descriptors/FileDescriptor.cs index be4a054f..f1eba496 100644 --- a/src/ProtocolBuffers/Descriptors/FileDescriptor.cs +++ b/src/ProtocolBuffers/Descriptors/FileDescriptor.cs @@ -32,17 +32,17 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.IO; using Google.ProtocolBuffers.DescriptorProtos; +using FileOptions = Google.ProtocolBuffers.DescriptorProtos.FileOptions; namespace Google.ProtocolBuffers.Descriptors { - /// <summary> /// Describes a .proto file, including everything defined within. /// IDescriptor is implemented such that the File property returns this descriptor, /// and the FullName is the same as the Name. /// </summary> public sealed class FileDescriptor : IDescriptor<FileDescriptorProto> { - private FileDescriptorProto proto; private readonly IList<MessageDescriptor> messageTypes; private readonly IList<EnumDescriptor> enumTypes; @@ -52,7 +52,7 @@ namespace Google.ProtocolBuffers.Descriptors { private readonly DescriptorPool pool; private CSharpFileOptions csharpFileOptions; private readonly object optionsLock = new object(); - + private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool) { this.pool = pool; this.proto = proto; @@ -60,49 +60,60 @@ namespace Google.ProtocolBuffers.Descriptors { pool.AddPackage(Package, this); - messageTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.MessageTypeList, - (message, index) => new MessageDescriptor(message, this, null, index)); + messageTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.MessageTypeList, + (message, index) => + new MessageDescriptor(message, this, null, index)); enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumTypeList, - (enumType, index) => new EnumDescriptor(enumType, this, null, index)); + (enumType, index) => + new EnumDescriptor(enumType, this, null, index)); services = DescriptorUtil.ConvertAndMakeReadOnly(proto.ServiceList, - (service, index) => new ServiceDescriptor(service, this, index)); + (service, index) => new ServiceDescriptor(service, this, index)); extensions = DescriptorUtil.ConvertAndMakeReadOnly(proto.ExtensionList, - (field, index) => new FieldDescriptor(field, this, null, index, true)); + (field, index) => + new FieldDescriptor(field, this, null, index, true)); } - /// <summary> - /// ROK - Added to allow the GeneratorOptions to sepcify the default for any or all of these values - /// </summary> - internal void ConfigureWithDefaultOptions(CSharpFileOptions options) - { - csharpFileOptions = BuildOrFakeWithDefaultOptions(options); - } - private CSharpFileOptions BuildOrFakeWithDefaultOptions(CSharpFileOptions defaultOptions) - { - // ROK 2010-09-03 - fix for being able to relocate these files to any directory structure - if(proto.Package == "google.protobuf") { - string filename = System.IO.Path.GetFileName(proto.Name); - // TODO(jonskeet): Check if we could use FileDescriptorProto.Descriptor.Name - interesting bootstrap issues) - if (filename == "descriptor.proto") { - return new CSharpFileOptions.Builder { - Namespace = "Google.ProtocolBuffers.DescriptorProtos", - UmbrellaClassname = "DescriptorProtoFile", NestClasses = false, MultipleFiles = false, PublicClasses = true, - OutputDirectory = defaultOptions.OutputDirectory, IgnoreGoogleProtobuf = defaultOptions.IgnoreGoogleProtobuf - }.Build(); - } - if (filename == "csharp_options.proto") { - return new CSharpFileOptions.Builder { - Namespace = "Google.ProtocolBuffers.DescriptorProtos", - UmbrellaClassname = "CSharpOptions", NestClasses = false, MultipleFiles = false, PublicClasses = true, - OutputDirectory = defaultOptions.OutputDirectory, IgnoreGoogleProtobuf = defaultOptions.IgnoreGoogleProtobuf - }.Build(); - } - } - CSharpFileOptions.Builder builder = defaultOptions.ToBuilder(); + /// <summary> + /// Allows a file descriptor to be configured with a set of external options, e.g. from the + /// command-line arguments to protogen. + /// </summary> + internal void ConfigureWithDefaultOptions(CSharpFileOptions options) { + csharpFileOptions = BuildOrFakeWithDefaultOptions(options); + } + + private CSharpFileOptions BuildOrFakeWithDefaultOptions(CSharpFileOptions defaultOptions) { + // Fix for being able to relocate these files to any directory structure + if (proto.Package == "google.protobuf") { + string filename = Path.GetFileName(proto.Name); + // TODO(jonskeet): Check if we could use FileDescriptorProto.Descriptor.Name - interesting bootstrap issues) + if (filename == "descriptor.proto") { + return new CSharpFileOptions.Builder { + Namespace = "Google.ProtocolBuffers.DescriptorProtos", + UmbrellaClassname = "DescriptorProtoFile", + NestClasses = false, + MultipleFiles = false, + PublicClasses = true, + OutputDirectory = defaultOptions.OutputDirectory, + IgnoreGoogleProtobuf = defaultOptions.IgnoreGoogleProtobuf + }.Build(); + } + if (filename == "csharp_options.proto") { + return new CSharpFileOptions.Builder { + Namespace = "Google.ProtocolBuffers.DescriptorProtos", + UmbrellaClassname = "CSharpOptions", + NestClasses = false, + MultipleFiles = false, + PublicClasses = true, + OutputDirectory = defaultOptions.OutputDirectory, + IgnoreGoogleProtobuf = defaultOptions.IgnoreGoogleProtobuf + }.Build(); + } + } + CSharpFileOptions.Builder builder = defaultOptions.ToBuilder(); if (proto.Options.HasExtension(DescriptorProtos.CSharpOptions.CSharpFileOptions)) { builder.MergeFrom(proto.Options.GetExtension(DescriptorProtos.CSharpOptions.CSharpFileOptions)); } @@ -112,22 +123,26 @@ namespace Google.ProtocolBuffers.Descriptors { if (!builder.HasUmbrellaClassname) { int lastSlash = Name.LastIndexOf('/'); string baseName = Name.Substring(lastSlash + 1); - builder.UmbrellaClassname = NameHelpers.UnderscoresToPascalCase(NameHelpers.StripProto(baseName)); + builder.UmbrellaClassname = NameHelpers.UnderscoresToPascalCase(NameHelpers.StripProto(baseName)); } - // ROK 2010-09-03 - auto fix for name collision by placing umbrella class into a new namespace. This - // still won't fix the collisions with nesting enabled; however, you have to turn that on so whatever. - if(!builder.NestClasses && !builder.HasUmbrellaNamespace) { - bool collision = false; - foreach (IDescriptor d in MessageTypes) - collision |= d.Name == builder.UmbrellaClassname; - foreach (IDescriptor d in Services) - collision |= d.Name == builder.UmbrellaClassname; - foreach (IDescriptor d in EnumTypes) - collision |= d.Name == builder.UmbrellaClassname; - if (collision) - builder.UmbrellaNamespace = "Proto"; - } + // Auto-fix for name collision by placing umbrella class into a new namespace. This + // still won't fix the collisions with nesting enabled; however, you have to turn that on explicitly anyway. + if (!builder.NestClasses && !builder.HasUmbrellaNamespace) { + bool collision = false; + foreach (IDescriptor d in MessageTypes) { + collision |= d.Name == builder.UmbrellaClassname; + } + foreach (IDescriptor d in Services) { + collision |= d.Name == builder.UmbrellaClassname; + } + foreach (IDescriptor d in EnumTypes) { + collision |= d.Name == builder.UmbrellaClassname; + } + if (collision) { + builder.UmbrellaNamespace = "Proto"; + } + } return builder.Build(); } @@ -140,7 +155,7 @@ namespace Google.ProtocolBuffers.Descriptors { } /// <value> - /// The <see cref="FileOptions" /> defined in <c>descriptor.proto</c>. + /// The <see cref="DescriptorProtos.FileOptions" /> defined in <c>descriptor.proto</c>. /// </value> public FileOptions Options { get { return proto.Options; } @@ -238,15 +253,15 @@ namespace Google.ProtocolBuffers.Descriptors { internal DescriptorPool DescriptorPool { get { return pool; } } - + /// <summary> /// Finds a type (message, enum, service or extension) in the file by name. Does not find nested types. /// </summary> /// <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> /// <returns>The type's descriptor, or null if not found.</returns> - public T FindTypeByName<T>(String name) - where T : class, IDescriptor { + public T FindTypeByName<T>(String name) + where T : class, IDescriptor { // Don't allow looking up nested types. This will make optimization // easier later. if (name.IndexOf('.') != -1) { @@ -291,20 +306,20 @@ namespace Google.ProtocolBuffers.Descriptors { if (dependencies.Length != proto.DependencyCount) { throw new DescriptorValidationException(result, - "Dependencies passed to FileDescriptor.BuildFrom() don't match " + - "those listed in the FileDescriptorProto."); + "Dependencies passed to FileDescriptor.BuildFrom() don't match " + + "those listed in the FileDescriptorProto."); } for (int i = 0; i < proto.DependencyCount; i++) { if (dependencies[i].Name != proto.DependencyList[i]) { throw new DescriptorValidationException(result, - "Dependencies passed to FileDescriptor.BuildFrom() don't match " + - "those listed in the FileDescriptorProto."); + "Dependencies passed to FileDescriptor.BuildFrom() don't match " + + "those listed in the FileDescriptorProto."); } } result.CrossLink(); return result; - } + } private void CrossLink() { foreach (MessageDescriptor message in messageTypes) { @@ -323,7 +338,7 @@ namespace Google.ProtocolBuffers.Descriptors { message.CheckRequiredFields(); } } - + /// <summary> /// This method is to be called by generated code only. It is equivalent /// to BuildFrom except that the FileDescriptorProto is encoded in @@ -350,19 +365,21 @@ namespace Google.ProtocolBuffers.Descriptors { public delegate ExtensionRegistry InternalDescriptorAssigner(FileDescriptor descriptor); public static FileDescriptor InternalBuildGeneratedFileFrom(byte[] descriptorData, - FileDescriptor[] dependencies,InternalDescriptorAssigner descriptorAssigner) { - + FileDescriptor[] dependencies, + InternalDescriptorAssigner descriptorAssigner) { FileDescriptorProto proto; try { proto = FileDescriptorProto.ParseFrom(descriptorData); - } catch (InvalidProtocolBufferException e) { + } + catch (InvalidProtocolBufferException e) { throw new ArgumentException("Failed to parse protocol buffer descriptor for generated code.", e); } FileDescriptor result; try { result = BuildFrom(proto, dependencies); - } catch (DescriptorValidationException e) { + } + catch (DescriptorValidationException e) { throw new ArgumentException("Invalid embedded descriptor for \"" + proto.Name + "\".", e); } @@ -372,7 +389,8 @@ namespace Google.ProtocolBuffers.Descriptors { // We must re-parse the proto using the registry. try { proto = FileDescriptorProto.ParseFrom(descriptorData, registry); - } catch (InvalidProtocolBufferException e) { + } + catch (InvalidProtocolBufferException e) { throw new ArgumentException("Failed to parse protocol buffer descriptor for generated code.", e); } @@ -381,7 +399,6 @@ namespace Google.ProtocolBuffers.Descriptors { return result; } - /// <summary> /// Replace our FileDescriptorProto with the given one, which is /// identical except that it might contain extensions that weren't present @@ -412,4 +429,4 @@ namespace Google.ProtocolBuffers.Descriptors { } } } -} +}
\ No newline at end of file |