aboutsummaryrefslogtreecommitdiff
path: root/csharp/ProtoGen/DescriptorUtil.cs
blob: e70ad46817d2a329da02150115aec6d2c3a9daf4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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);
      }
    }
  }
}