Exemplo n.º 1
0
 private CSharpFileOptions BuildOrFakeCSharpOptions()
 {
     // TODO(jonskeet): Check if we could use FileDescriptorProto.Descriptor.Name - interesting bootstrap issues
     if (proto.Name == "google/protobuf/descriptor.proto")
     {
         return(new CSharpFileOptions.Builder {
             Namespace = "Google.ProtocolBuffers.DescriptorProtos",
             UmbrellaClassname = "DescriptorProtoFile", NestClasses = false, MultipleFiles = false, PublicClasses = true
         }.Build());
     }
     if (proto.Name == "google/protobuf/csharp_options.proto")
     {
         return(new CSharpFileOptions.Builder {
             Namespace = "Google.ProtocolBuffers.DescriptorProtos",
             UmbrellaClassname = "CSharpOptions", NestClasses = false, MultipleFiles = false, PublicClasses = true
         }.Build());
     }
     CSharpFileOptions.Builder builder = CSharpFileOptions.CreateBuilder();
     if (proto.Options.HasExtension(DescriptorProtos.CSharpOptions.CSharpFileOptions))
     {
         builder.MergeFrom(proto.Options.GetExtension(DescriptorProtos.CSharpOptions.CSharpFileOptions));
     }
     if (!builder.HasNamespace)
     {
         builder.Namespace = Package;
     }
     if (!builder.HasMultipleFiles)
     {
         builder.MultipleFiles = false;
     }
     if (!builder.HasNestClasses)
     {
         builder.NestClasses = false;
     }
     if (!builder.HasPublicClasses)
     {
         builder.PublicClasses = true;
     }
     if (!builder.HasUmbrellaClassname)
     {
         int    lastSlash = Name.LastIndexOf('/');
         string baseName  = Name.Substring(lastSlash + 1);
         builder.UmbrellaClassname = NameHelpers.UnderscoresToPascalCase(NameHelpers.StripProto(baseName));
     }
     return(builder.Build());
 }
        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));
            }
            if (!builder.HasNamespace)
            {
                builder.Namespace = Package;
            }
            if (!builder.HasUmbrellaClassname)
            {
                int    lastSlash = Name.LastIndexOf('/');
                string baseName  = Name.Substring(lastSlash + 1);
                builder.UmbrellaClassname = NameHelpers.UnderscoresToPascalCase(NameHelpers.StripProto(baseName));
            }

            // 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());
        }