aboutsummaryrefslogtreecommitdiff
path: root/src/ProtoGen/FieldGeneratorBase.cs
blob: f34d01cd012f6a306ed03b507f4d2806f3f9dd06 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Globalization;
using Google.ProtocolBuffers.Descriptors;

namespace Google.ProtocolBuffers.ProtoGen {
  internal abstract class FieldGeneratorBase : SourceGeneratorBase<FieldDescriptor> {
    protected FieldGeneratorBase(FieldDescriptor descriptor)
        : base(descriptor) {
    }

    private static bool AllPrintableAscii(string text) {
      foreach (char c in text) {
        if (c < 0x20 || c > 0x7e) {
          return false;
        }
      }
      return true;
    }

    protected string DefaultValue {
      get {
        string suffix = "";
        switch (Descriptor.FieldType) {
          case FieldType.Float:  suffix = "F"; break;
          case FieldType.Double: suffix = "D"; break;
          case FieldType.Int64:  suffix = "L"; break;
          case FieldType.UInt64: suffix = "UL"; break;
        }
        switch (Descriptor.FieldType) {
          case FieldType.Float:
          case FieldType.Double:
          case FieldType.Int32:
          case FieldType.Int64:
          case FieldType.SInt32:
          case FieldType.SInt64:
          case FieldType.SFixed32:
          case FieldType.SFixed64:
          case FieldType.UInt32:
          case FieldType.UInt64:
          case FieldType.Fixed32:
          case FieldType.Fixed64:
            // The simple Object.ToString converts using the current culture.
            // We want to always use the invariant culture so it's predictable.
            IConvertible value = (IConvertible) Descriptor.DefaultValue;
            return value.ToString(CultureInfo.InvariantCulture) + suffix;
          case FieldType.Bool:
            return (bool) Descriptor.DefaultValue ? "true" : "false";

          case FieldType.Bytes:
            if (!Descriptor.HasDefaultValue) {
              return "pb::ByteString.Empty";
            }
            return string.Format("(pb::ByteString) {0}.Descriptor.Fields[{1}].DefaultValue", GetClassName(Descriptor.ContainingType), Descriptor.Index);
          case FieldType.String:
            if (AllPrintableAscii(Descriptor.Proto.DefaultValue)) {
              // All chars are ASCII and printable.  In this case we only
              // need to escape quotes and backslashes.
              return "\"" + Descriptor.Proto.DefaultValue
                  .Replace("\\", "\\\\")
                  .Replace("'", "\\'")
                  .Replace("\"", "\\\"")
                  + "\"";
            }
            return string.Format("(string) {0}.Descriptor.Fields[{1}].DefaultValue", GetClassName(Descriptor.ContainingType), Descriptor.Index);
          case FieldType.Enum:
            return TypeName + "." + ((EnumValueDescriptor) Descriptor.DefaultValue).Name;
          case FieldType.Message:
          case FieldType.Group:
            return TypeName + ".DefaultInstance";
          default:
            throw new InvalidOperationException("Invalid field descriptor type");
        }
      }
    }

    protected string PropertyName {
      get {
        return Descriptor.CSharpOptions.PropertyName;
      }
    }

    protected string Name {
      get { return NameHelpers.UnderscoresToCamelCase(GetFieldName(Descriptor)); }
    }

    protected int Number {
      get { return Descriptor.FieldNumber; }
    }

    protected void AddNullCheck(TextGenerator writer) {
      AddNullCheck(writer, "value");
    }

    protected void AddNullCheck(TextGenerator writer, string name) {
      if (IsNullableType) {
        writer.WriteLine("  pb::ThrowHelper.ThrowIfNull({0}, \"{0}\");", name);
      }
    }

    protected void AddClsComplianceCheck(TextGenerator writer) {
      if (!Descriptor.IsCLSCompliant) {
        writer.WriteLine("[global::System.CLSCompliant(false)]");
      }
    }

    /// <summary>
    /// For encodings with fixed sizes, returns that size in bytes.  Otherwise
    /// returns -1. TODO(jonskeet): Make this less ugly.
    /// </summary>
    protected int FixedSize {
      get {
        switch (Descriptor.FieldType) {
          case FieldType.UInt32:
          case FieldType.UInt64:
          case FieldType.Int32:
          case FieldType.Int64:
          case FieldType.SInt32:
          case FieldType.SInt64:
          case FieldType.Enum:
          case FieldType.Bytes:
          case FieldType.String:
          case FieldType.Message:
          case FieldType.Group:
            return -1;
          case FieldType.Float:
            return WireFormat.FloatSize;
          case FieldType.SFixed32:
            return WireFormat.SFixed32Size;
          case FieldType.Fixed32:
            return WireFormat.Fixed32Size;
          case FieldType.Double:
            return WireFormat.DoubleSize;
          case FieldType.SFixed64:
            return WireFormat.SFixed64Size;
          case FieldType.Fixed64:
            return WireFormat.Fixed64Size;
          case FieldType.Bool:
            return WireFormat.BoolSize;
          default:
            throw new InvalidOperationException("Invalid field descriptor type");
        }
      }
    }

    protected bool IsNullableType {
      get {
        switch (Descriptor.FieldType) {
          case FieldType.Float:
          case FieldType.Double:
          case FieldType.Int32:
          case FieldType.Int64:
          case FieldType.SInt32:
          case FieldType.SInt64:
          case FieldType.SFixed32:
          case FieldType.SFixed64:
          case FieldType.UInt32:
          case FieldType.UInt64:
          case FieldType.Fixed32:
          case FieldType.Fixed64:
          case FieldType.Bool:
          case FieldType.Enum:
            return false;
          case FieldType.Bytes:
          case FieldType.String:
          case FieldType.Message:
          case FieldType.Group:
            return true;
          default:
            throw new InvalidOperationException("Invalid field descriptor type");
        }
      }
    }

    protected string TypeName {
      get {
        switch (Descriptor.FieldType) {
          case FieldType.Enum:
            return GetClassName(Descriptor.EnumType);
          case FieldType.Message:
          case FieldType.Group:
            return GetClassName(Descriptor.MessageType);
          default:
            return DescriptorUtil.GetMappedTypeName(Descriptor.MappedType);
        }
      }
    }

    protected string MessageOrGroup {
      get { return Descriptor.FieldType == FieldType.Group ? "Group" : "Message"; }
    }

    /// <summary>
    /// Returns the type name as used in CodedInputStream method names: SFixed32, UInt32 etc.
    /// </summary>
    protected string CapitalizedTypeName {
      get {
        // Our enum names match perfectly. How serendipitous.
        return Descriptor.FieldType.ToString();
      }
    }
  }
}