FieldGeneratorBase.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using Google.ProtocolBuffers.Descriptors;
  3. using System.Globalization;
  4. namespace Google.ProtocolBuffers.ProtoGen {
  5. internal abstract class FieldGeneratorBase : SourceGeneratorBase<FieldDescriptor> {
  6. protected FieldGeneratorBase(FieldDescriptor descriptor)
  7. : base(descriptor) {
  8. }
  9. private static bool AllPrintableAscii(string text) {
  10. foreach (char c in text) {
  11. if (c < 0x20 || c > 0x7e) {
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. protected string DefaultValue {
  18. get {
  19. string suffix = "";
  20. switch (Descriptor.FieldType) {
  21. case FieldType.Float: suffix = "F"; break;
  22. case FieldType.Double: suffix = "D"; break;
  23. case FieldType.Int64: suffix = "L"; break;
  24. case FieldType.UInt64: suffix = "UL"; break;
  25. }
  26. switch (Descriptor.FieldType) {
  27. case FieldType.Float:
  28. case FieldType.Double:
  29. case FieldType.Int32:
  30. case FieldType.Int64:
  31. case FieldType.SInt32:
  32. case FieldType.SInt64:
  33. case FieldType.SFixed32:
  34. case FieldType.SFixed64:
  35. case FieldType.UInt32:
  36. case FieldType.UInt64:
  37. case FieldType.Fixed32:
  38. case FieldType.Fixed64:
  39. // The simple Object.ToString converts using the current culture.
  40. // We want to always use the invariant culture so it's predictable.
  41. IConvertible value = (IConvertible) Descriptor.DefaultValue;
  42. return value.ToString(CultureInfo.InvariantCulture) + suffix;
  43. case FieldType.Bool:
  44. return (bool) Descriptor.DefaultValue ? "true" : "false";
  45. case FieldType.Bytes:
  46. if (!Descriptor.HasDefaultValue) {
  47. return "pb::ByteString.Empty";
  48. }
  49. return string.Format("(pb::ByteString) {0}.Descriptor.Fields[{1}].DefaultValue", GetClassName(Descriptor.ContainingType), Descriptor.Index);
  50. case FieldType.String:
  51. if (AllPrintableAscii(Descriptor.Proto.DefaultValue)) {
  52. // All chars are ASCII and printable. In this case we only
  53. // need to escape quotes and backslashes.
  54. return "\"" + Descriptor.Proto.DefaultValue
  55. .Replace("\\", "\\\\")
  56. .Replace("'", "\\'")
  57. .Replace("\"", "\\\"")
  58. + "\"";
  59. }
  60. return string.Format("(string) {0}.Descriptor.Fields[{1}].DefaultValue", GetClassName(Descriptor.ContainingType), Descriptor.Index);
  61. case FieldType.Enum:
  62. return TypeName + "." + ((EnumValueDescriptor) Descriptor.DefaultValue).Name;
  63. case FieldType.Message:
  64. case FieldType.Group:
  65. return TypeName + ".DefaultInstance";
  66. default:
  67. throw new InvalidOperationException("Invalid field descriptor type");
  68. }
  69. }
  70. }
  71. protected string PropertyName {
  72. get {
  73. return Descriptor.CSharpOptions.PropertyName;
  74. }
  75. }
  76. protected string CapitalizedName {
  77. get { return NameHelpers.UnderscoresToPascalCase(GetFieldName(Descriptor)); }
  78. }
  79. protected string Name {
  80. get { return NameHelpers.UnderscoresToCamelCase(GetFieldName(Descriptor)); }
  81. }
  82. protected int Number {
  83. get { return Descriptor.FieldNumber; }
  84. }
  85. protected string TypeName {
  86. get {
  87. switch (Descriptor.FieldType) {
  88. case FieldType.Enum:
  89. return GetClassName(Descriptor.EnumType);
  90. case FieldType.Message:
  91. case FieldType.Group:
  92. return GetClassName(Descriptor.MessageType);
  93. default:
  94. return DescriptorUtil.GetMappedTypeName(Descriptor.MappedType);
  95. }
  96. }
  97. }
  98. protected string MessageOrGroup {
  99. get { return Descriptor.FieldType == FieldType.Group ? "Group" : "Message"; }
  100. }
  101. /// <summary>
  102. /// Returns the type name as used in CodedInputStream method names: SFixed32, UInt32 etc.
  103. /// </summary>
  104. protected string CapitalizedTypeName {
  105. get {
  106. // Our enum names match perfectly. How serendipitous.
  107. return Descriptor.FieldType.ToString();
  108. }
  109. }
  110. }
  111. }