blob: d20745a4ca483d2719de74d3a70dd1d216f86f79 [file] [log] [blame]
Jon Skeet60c059b2008-10-23 21:17:56 +01001using System;
Jon Skeet68036862008-10-22 13:30:34 +01002using Google.ProtocolBuffers.Descriptors;
3using System.Globalization;
4
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
82 protected string CapitalizedName {
Jon Skeetd6343be2008-11-12 23:39:44 +000083 get { return NameHelpers.UnderscoresToPascalCase(GetFieldName(Descriptor)); }
Jon Skeet68036862008-10-22 13:30:34 +010084 }
85
86 protected string Name {
Jon Skeetd6343be2008-11-12 23:39:44 +000087 get { return NameHelpers.UnderscoresToCamelCase(GetFieldName(Descriptor)); }
Jon Skeet68036862008-10-22 13:30:34 +010088 }
89
90 protected int Number {
91 get { return Descriptor.FieldNumber; }
92 }
93
94 protected string TypeName {
95 get {
96 switch (Descriptor.FieldType) {
97 case FieldType.Enum:
Jon Skeetd6343be2008-11-12 23:39:44 +000098 return GetClassName(Descriptor.EnumType);
Jon Skeet68036862008-10-22 13:30:34 +010099 case FieldType.Message:
100 case FieldType.Group:
Jon Skeetd6343be2008-11-12 23:39:44 +0000101 return GetClassName(Descriptor.MessageType);
Jon Skeet68036862008-10-22 13:30:34 +0100102 default:
103 return DescriptorUtil.GetMappedTypeName(Descriptor.MappedType);
104 }
105 }
106 }
107
108 protected string MessageOrGroup {
109 get { return Descriptor.FieldType == FieldType.Group ? "Group" : "Message"; }
110 }
111
112 /// <summary>
113 /// Returns the type name as used in CodedInputStream method names: SFixed32, UInt32 etc.
114 /// </summary>
115 protected string CapitalizedTypeName {
116 get {
117 // Our enum names match perfectly. How serendipitous.
118 return Descriptor.FieldType.ToString();
119 }
120 }
121 }
122}