aboutsummaryrefslogtreecommitdiff
path: root/csharp/ProtoGen/DescriptorUtil.cs
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2008-10-22 07:11:17 +0100
committerJon Skeet <skeet@pobox.com>2008-10-22 07:11:17 +0100
commite60ce8bfafca616ed4fd430ae4f82360de165e80 (patch)
treecb1a82655772f4995c02debb31cfeb208960acf1 /csharp/ProtoGen/DescriptorUtil.cs
parent7f90d8ee57d4af4215bc31dbb07726c023c5e047 (diff)
downloadprotobuf-e60ce8bfafca616ed4fd430ae4f82360de165e80.tar.gz
protobuf-e60ce8bfafca616ed4fd430ae4f82360de165e80.tar.bz2
protobuf-e60ce8bfafca616ed4fd430ae4f82360de165e80.zip
Final commit before changing layout
Diffstat (limited to 'csharp/ProtoGen/DescriptorUtil.cs')
-rw-r--r--csharp/ProtoGen/DescriptorUtil.cs108
1 files changed, 108 insertions, 0 deletions
diff --git a/csharp/ProtoGen/DescriptorUtil.cs b/csharp/ProtoGen/DescriptorUtil.cs
new file mode 100644
index 00000000..e70ad468
--- /dev/null
+++ b/csharp/ProtoGen/DescriptorUtil.cs
@@ -0,0 +1,108 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Google.ProtocolBuffers.Descriptors;
+using Google.ProtocolBuffers.DescriptorProtos;
+
+namespace Google.ProtocolBuffers.ProtoGen {
+ /// <summary>
+ /// Utility class for determining namespaces etc.
+ /// </summary>
+ internal static class DescriptorUtil {
+
+ internal static bool NestClasses(IDescriptor descriptor) {
+ // Defaults to false
+ return descriptor.File.Options.GetExtension(CSharpOptions.CSharpNestClasses);
+ }
+
+ internal static string GetNamespace(FileDescriptor descriptor) {
+ if (descriptor.Name == "google/protobuf/descriptor.proto") {
+ return typeof(DescriptorProtoFile).Namespace;
+ }
+ return descriptor.Options.HasExtension(CSharpOptions.CSharpNamespace) ?
+ descriptor.Options.GetExtension(CSharpOptions.CSharpNamespace) : descriptor.Package;
+ }
+
+ // Groups are hacky: The name of the field is just the lower-cased name
+ // of the group type. In C#, though, we would like to retain the original
+ // capitalization of the type name.
+ internal static string GetFieldName(FieldDescriptor descriptor) {
+ if (descriptor.FieldType == FieldType.Group) {
+ return descriptor.MessageType.Name;
+ } else {
+ return descriptor.Name;
+ }
+ }
+
+ internal static string GetClassName(IDescriptor descriptor) {
+ return ToCSharpName(descriptor.FullName, descriptor.File);
+ }
+
+ internal static string GetFullUmbrellaClassName(FileDescriptor descriptor) {
+ string result = GetNamespace(descriptor);
+ if (result != "") result += '.';
+ result += GetUmbrellaClassName(descriptor);
+ return "global::" + result;
+ }
+
+ internal static string GetUmbrellaClassName(FileDescriptor descriptor) {
+ if (descriptor.Name == "google/protobuf/descriptor.proto") {
+ return typeof(DescriptorProtoFile).Name;
+ }
+ FileOptions options = descriptor.Options;
+ if (options.HasExtension(CSharpOptions.CSharpUmbrellaClassname)) {
+ return descriptor.Options.GetExtension(CSharpOptions.CSharpUmbrellaClassname);
+ }
+ int lastSlash = descriptor.Name.LastIndexOf('/');
+ string baseName = descriptor.Name.Substring(lastSlash + 1);
+ return Helpers.UnderscoresToPascalCase(StripProto(baseName));
+ }
+
+ private static string StripProto(string text) {
+ if (!Helpers.StripSuffix(ref text, ".protodevel")) {
+ Helpers.StripSuffix(ref text, ".proto");
+ }
+ return text;
+ }
+
+ private static string ToCSharpName(string name, FileDescriptor file) {
+ string result;
+ if (!NestClasses(file)) {
+ result = GetNamespace(file);
+ } else {
+ result = GetUmbrellaClassName(file);
+ }
+ if (result != "") {
+ result += '.';
+ }
+ string classname;
+ if (file.Package == "") {
+ classname = name;
+ } else {
+ // Strip the proto package from full_name since we've replaced it with
+ // the C# namespace.
+ classname = name.Substring(file.Package.Length + 1);
+ }
+ result += classname.Replace(".", ".Types.");
+ return "global::" + result;
+ }
+
+ internal static string GetMappedTypeName(MappedType type) {
+ switch(type) {
+ case MappedType.Int32: return "int";
+ case MappedType.Int64: return "long";
+ case MappedType.UInt32: return "uint";
+ case MappedType.UInt64: return "ulong";
+ case MappedType.Single: return "float";
+ case MappedType.Double: return "double";
+ case MappedType.Boolean: return "bool";
+ case MappedType.String: return "string";
+ case MappedType.ByteString: return "pb::ByteString";
+ case MappedType.Enum: return null;
+ case MappedType.Message: return null;
+ default:
+ throw new ArgumentOutOfRangeException("Unknown mapped type " + type);
+ }
+ }
+ }
+}