DescriptorUtil.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Google.ProtocolBuffers.Descriptors;
  5. using Google.ProtocolBuffers.DescriptorProtos;
  6. namespace Google.ProtocolBuffers.ProtoGen {
  7. /// <summary>
  8. /// Utility class for determining namespaces etc.
  9. /// </summary>
  10. internal static class DescriptorUtil {
  11. internal static bool NestClasses(IDescriptor descriptor) {
  12. // Defaults to false
  13. return descriptor.File.Options.GetExtension(CSharpOptions.CSharpNestClasses);
  14. }
  15. internal static string GetNamespace(FileDescriptor descriptor) {
  16. if (descriptor.Name == "google/protobuf/descriptor.proto") {
  17. return typeof(DescriptorProtoFile).Namespace;
  18. }
  19. return descriptor.Options.HasExtension(CSharpOptions.CSharpNamespace) ?
  20. descriptor.Options.GetExtension(CSharpOptions.CSharpNamespace) : descriptor.Package;
  21. }
  22. // Groups are hacky: The name of the field is just the lower-cased name
  23. // of the group type. In C#, though, we would like to retain the original
  24. // capitalization of the type name.
  25. internal static string GetFieldName(FieldDescriptor descriptor) {
  26. if (descriptor.FieldType == FieldType.Group) {
  27. return descriptor.MessageType.Name;
  28. } else {
  29. return descriptor.Name;
  30. }
  31. }
  32. internal static string GetClassName(IDescriptor descriptor) {
  33. return ToCSharpName(descriptor.FullName, descriptor.File);
  34. }
  35. internal static string GetFullUmbrellaClassName(FileDescriptor descriptor) {
  36. string result = GetNamespace(descriptor);
  37. if (result != "") result += '.';
  38. result += GetUmbrellaClassName(descriptor);
  39. return "global::" + result;
  40. }
  41. internal static string GetUmbrellaClassName(FileDescriptor descriptor) {
  42. if (descriptor.Name == "google/protobuf/descriptor.proto") {
  43. return typeof(DescriptorProtoFile).Name;
  44. }
  45. FileOptions options = descriptor.Options;
  46. if (options.HasExtension(CSharpOptions.CSharpUmbrellaClassname)) {
  47. return descriptor.Options.GetExtension(CSharpOptions.CSharpUmbrellaClassname);
  48. }
  49. int lastSlash = descriptor.Name.LastIndexOf('/');
  50. string baseName = descriptor.Name.Substring(lastSlash + 1);
  51. return Helpers.UnderscoresToPascalCase(StripProto(baseName));
  52. }
  53. private static string StripProto(string text) {
  54. if (!Helpers.StripSuffix(ref text, ".protodevel")) {
  55. Helpers.StripSuffix(ref text, ".proto");
  56. }
  57. return text;
  58. }
  59. private static string ToCSharpName(string name, FileDescriptor file) {
  60. string result;
  61. if (!NestClasses(file)) {
  62. result = GetNamespace(file);
  63. } else {
  64. result = GetUmbrellaClassName(file);
  65. }
  66. if (result != "") {
  67. result += '.';
  68. }
  69. string classname;
  70. if (file.Package == "") {
  71. classname = name;
  72. } else {
  73. // Strip the proto package from full_name since we've replaced it with
  74. // the C# namespace.
  75. classname = name.Substring(file.Package.Length + 1);
  76. }
  77. result += classname.Replace(".", ".Types.");
  78. return "global::" + result;
  79. }
  80. internal static string GetMappedTypeName(MappedType type) {
  81. switch(type) {
  82. case MappedType.Int32: return "int";
  83. case MappedType.Int64: return "long";
  84. case MappedType.UInt32: return "uint";
  85. case MappedType.UInt64: return "ulong";
  86. case MappedType.Single: return "float";
  87. case MappedType.Double: return "double";
  88. case MappedType.Boolean: return "bool";
  89. case MappedType.String: return "string";
  90. case MappedType.ByteString: return "pb::ByteString";
  91. case MappedType.Enum: return null;
  92. case MappedType.Message: return null;
  93. default:
  94. throw new ArgumentOutOfRangeException("Unknown mapped type " + type);
  95. }
  96. }
  97. }
  98. }