From 57599ef16daeda48381711dae1600cf0c467689d Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Wed, 3 Nov 2010 11:21:52 +0000 Subject: A few stylistic issues --- src/ProtocolBuffers/Descriptors/FileDescriptor.cs | 153 ++++++++++++---------- 1 file changed, 85 insertions(+), 68 deletions(-) (limited to 'src/ProtocolBuffers/Descriptors') 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 { - /// /// 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. /// public sealed class FileDescriptor : IDescriptor { - private FileDescriptorProto proto; private readonly IList messageTypes; private readonly IList 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)); } - /// - /// ROK - Added to allow the GeneratorOptions to sepcify the default for any or all of these values - /// - 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(); + /// + /// Allows a file descriptor to be configured with a set of external options, e.g. from the + /// command-line arguments to protogen. + /// + 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 { } /// - /// The defined in descriptor.proto. + /// The defined in descriptor.proto. /// public FileOptions Options { get { return proto.Options; } @@ -238,15 +253,15 @@ namespace Google.ProtocolBuffers.Descriptors { internal DescriptorPool DescriptorPool { get { return pool; } } - + /// /// Finds a type (message, enum, service or extension) in the file by name. Does not find nested types. /// /// The unqualified type name to look for. /// The type of descriptor to look for (or ITypeDescriptor for any) /// The type's descriptor, or null if not found. - public T FindTypeByName(String name) - where T : class, IDescriptor { + public T FindTypeByName(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(); } } - + /// /// 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; } - /// /// 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 -- cgit v1.2.3