Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame^] | 1 | using System; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 2 | using Google.ProtocolBuffers.Descriptors; |
| 3 | using System.Globalization; |
| 4 | |
| 5 | namespace 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 | } |
| 53 | return string.Format("(pb::ByteString) {0}.Descriptor.Fields[{1}].DefaultValue", DescriptorUtil.GetClassName(Descriptor.ContainingType), Descriptor.Index); |
| 54 | 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 | } |
| 64 | return string.Format("(string) {0}.Descriptor.Fields[{1}].DefaultValue", DescriptorUtil.GetClassName(Descriptor.ContainingType), Descriptor.Index); |
| 65 | 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 | |
| 76 | /// <summary> |
| 77 | /// Usually the same as CapitalizedName, except when the enclosing type has the same name, |
| 78 | /// in which case an underscore is appended. |
| 79 | /// </summary> |
| 80 | protected string PropertyName { |
| 81 | get { |
| 82 | string ret = CapitalizedName; |
| 83 | if (ret == Descriptor.ContainingType.Name) { |
| 84 | ret += "_"; |
| 85 | } |
| 86 | return ret; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | protected string CapitalizedName { |
| 91 | get { return Helpers.UnderscoresToPascalCase(DescriptorUtil.GetFieldName(Descriptor)); } |
| 92 | } |
| 93 | |
| 94 | protected string Name { |
| 95 | get { return Helpers.UnderscoresToCamelCase(DescriptorUtil.GetFieldName(Descriptor)); } |
| 96 | } |
| 97 | |
| 98 | protected int Number { |
| 99 | get { return Descriptor.FieldNumber; } |
| 100 | } |
| 101 | |
| 102 | protected string TypeName { |
| 103 | get { |
| 104 | switch (Descriptor.FieldType) { |
| 105 | case FieldType.Enum: |
| 106 | return DescriptorUtil.GetClassName(Descriptor.EnumType); |
| 107 | case FieldType.Message: |
| 108 | case FieldType.Group: |
| 109 | return DescriptorUtil.GetClassName(Descriptor.MessageType); |
| 110 | default: |
| 111 | return DescriptorUtil.GetMappedTypeName(Descriptor.MappedType); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | protected string MessageOrGroup { |
| 117 | get { return Descriptor.FieldType == FieldType.Group ? "Group" : "Message"; } |
| 118 | } |
| 119 | |
| 120 | /// <summary> |
| 121 | /// Returns the type name as used in CodedInputStream method names: SFixed32, UInt32 etc. |
| 122 | /// </summary> |
| 123 | protected string CapitalizedTypeName { |
| 124 | get { |
| 125 | // Our enum names match perfectly. How serendipitous. |
| 126 | return Descriptor.FieldType.ToString(); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |