blob: 207f3fdc354c1407ae921a8c819989803a0b0975 [file] [log] [blame]
Jon Skeet60c059b2008-10-23 21:17:56 +01001using System;
Jon Skeet68036862008-10-22 13:30:34 +01002using System.Globalization;
Jon Skeet642a8142009-01-27 12:25:21 +00003using Google.ProtocolBuffers.Descriptors;
Jon Skeet68036862008-10-22 13:30:34 +01004
5namespace Google.ProtocolBuffers.ProtoGen {
6 internal abstract class FieldGeneratorBase : SourceGeneratorBase<FieldDescriptor> {
7 protected FieldGeneratorBase(FieldDescriptor descriptor)
8 : base(descriptor) {
9 }
10
11 private static bool AllPrintableAscii(string text) {
12 foreach (char c in text) {
13 if (c < 0x20 || c > 0x7e) {
14 return false;
15 }
16 }
17 return true;
18 }
19
20 protected string DefaultValue {
21 get {
22 string suffix = "";
23 switch (Descriptor.FieldType) {
24 case FieldType.Float: suffix = "F"; break;
25 case FieldType.Double: suffix = "D"; break;
26 case FieldType.Int64: suffix = "L"; break;
27 case FieldType.UInt64: suffix = "UL"; break;
28 }
29 switch (Descriptor.FieldType) {
30 case FieldType.Float:
31 case FieldType.Double:
32 case FieldType.Int32:
33 case FieldType.Int64:
34 case FieldType.SInt32:
35 case FieldType.SInt64:
36 case FieldType.SFixed32:
37 case FieldType.SFixed64:
38 case FieldType.UInt32:
39 case FieldType.UInt64:
40 case FieldType.Fixed32:
41 case FieldType.Fixed64:
42 // The simple Object.ToString converts using the current culture.
43 // We want to always use the invariant culture so it's predictable.
44 IConvertible value = (IConvertible) Descriptor.DefaultValue;
45 return value.ToString(CultureInfo.InvariantCulture) + suffix;
46 case FieldType.Bool:
47 return (bool) Descriptor.DefaultValue ? "true" : "false";
48
49 case FieldType.Bytes:
50 if (!Descriptor.HasDefaultValue) {
51 return "pb::ByteString.Empty";
52 }
Jon Skeetd6343be2008-11-12 23:39:44 +000053 return string.Format("(pb::ByteString) {0}.Descriptor.Fields[{1}].DefaultValue", GetClassName(Descriptor.ContainingType), Descriptor.Index);
Jon Skeet68036862008-10-22 13:30:34 +010054 case FieldType.String:
55 if (AllPrintableAscii(Descriptor.Proto.DefaultValue)) {
56 // All chars are ASCII and printable. In this case we only
57 // need to escape quotes and backslashes.
58 return "\"" + Descriptor.Proto.DefaultValue
59 .Replace("\\", "\\\\")
60 .Replace("'", "\\'")
61 .Replace("\"", "\\\"")
62 + "\"";
63 }
Jon Skeetd6343be2008-11-12 23:39:44 +000064 return string.Format("(string) {0}.Descriptor.Fields[{1}].DefaultValue", GetClassName(Descriptor.ContainingType), Descriptor.Index);
Jon Skeet68036862008-10-22 13:30:34 +010065 case FieldType.Enum:
66 return TypeName + "." + ((EnumValueDescriptor) Descriptor.DefaultValue).Name;
67 case FieldType.Message:
68 case FieldType.Group:
69 return TypeName + ".DefaultInstance";
70 default:
71 throw new InvalidOperationException("Invalid field descriptor type");
72 }
73 }
74 }
75
Jon Skeet68036862008-10-22 13:30:34 +010076 protected string PropertyName {
77 get {
Jon Skeet1d131c92008-11-13 22:29:48 +000078 return Descriptor.CSharpOptions.PropertyName;
Jon Skeet68036862008-10-22 13:30:34 +010079 }
80 }
81
Jon Skeet68036862008-10-22 13:30:34 +010082 protected string Name {
Jon Skeetd6343be2008-11-12 23:39:44 +000083 get { return NameHelpers.UnderscoresToCamelCase(GetFieldName(Descriptor)); }
Jon Skeet68036862008-10-22 13:30:34 +010084 }
85
86 protected int Number {
87 get { return Descriptor.FieldNumber; }
88 }
89
Jon Skeet642a8142009-01-27 12:25:21 +000090 protected void AddNullCheck(TextGenerator writer) {
91 AddNullCheck(writer, "value");
92 }
93
94 protected void AddNullCheck(TextGenerator writer, string name) {
95 if (IsNullableType) {
96 writer.WriteLine(" pb::ThrowHelper.ThrowIfNull({0}, \"{0}\");", name);
97 }
98 }
99
100 protected bool IsNullableType {
101 get {
102 switch (Descriptor.FieldType) {
103 case FieldType.Float:
104 case FieldType.Double:
105 case FieldType.Int32:
106 case FieldType.Int64:
107 case FieldType.SInt32:
108 case FieldType.SInt64:
109 case FieldType.SFixed32:
110 case FieldType.SFixed64:
111 case FieldType.UInt32:
112 case FieldType.UInt64:
113 case FieldType.Fixed32:
114 case FieldType.Fixed64:
115 case FieldType.Bool:
116 case FieldType.Enum:
117 return false;
118 case FieldType.Bytes:
119 case FieldType.String:
120 case FieldType.Message:
121 case FieldType.Group:
122 return true;
123 default:
124 throw new InvalidOperationException("Invalid field descriptor type");
125 }
126 }
127 }
128
Jon Skeet68036862008-10-22 13:30:34 +0100129 protected string TypeName {
130 get {
131 switch (Descriptor.FieldType) {
132 case FieldType.Enum:
Jon Skeetd6343be2008-11-12 23:39:44 +0000133 return GetClassName(Descriptor.EnumType);
Jon Skeet68036862008-10-22 13:30:34 +0100134 case FieldType.Message:
135 case FieldType.Group:
Jon Skeetd6343be2008-11-12 23:39:44 +0000136 return GetClassName(Descriptor.MessageType);
Jon Skeet68036862008-10-22 13:30:34 +0100137 default:
138 return DescriptorUtil.GetMappedTypeName(Descriptor.MappedType);
139 }
140 }
141 }
142
143 protected string MessageOrGroup {
144 get { return Descriptor.FieldType == FieldType.Group ? "Group" : "Message"; }
145 }
146
147 /// <summary>
148 /// Returns the type name as used in CodedInputStream method names: SFixed32, UInt32 etc.
149 /// </summary>
150 protected string CapitalizedTypeName {
151 get {
152 // Our enum names match perfectly. How serendipitous.
153 return Descriptor.FieldType.ToString();
154 }
155 }
156 }
157}