FieldGeneratorBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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", TypeName, Number);
  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", TypeName, Number);
  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. /// <summary>
  72. /// Usually the same as CapitalizedName, except when the enclosing type has the same name,
  73. /// in which case an underscore is appended.
  74. /// </summary>
  75. protected string PropertyName {
  76. get {
  77. string ret = CapitalizedName;
  78. if (ret == Descriptor.ContainingType.Name) {
  79. ret += "_";
  80. }
  81. return ret;
  82. }
  83. }
  84. protected string CapitalizedName {
  85. get { return Helpers.UnderscoresToPascalCase(DescriptorUtil.GetFieldName(Descriptor)); }
  86. }
  87. protected string Name {
  88. get { return Helpers.UnderscoresToCamelCase(DescriptorUtil.GetFieldName(Descriptor)); }
  89. }
  90. protected int Number {
  91. get { return Descriptor.FieldNumber; }
  92. }
  93. protected string TypeName {
  94. get {
  95. switch (Descriptor.FieldType) {
  96. case FieldType.Enum:
  97. return DescriptorUtil.GetClassName(Descriptor.EnumType);
  98. case FieldType.Message:
  99. case FieldType.Group:
  100. return DescriptorUtil.GetClassName(Descriptor.MessageType);
  101. default:
  102. return DescriptorUtil.GetMappedTypeName(Descriptor.MappedType);
  103. }
  104. }
  105. }
  106. protected string MessageOrGroup {
  107. get { return Descriptor.FieldType == FieldType.Group ? "Group" : "Message"; }
  108. }
  109. /// <summary>
  110. /// Returns the type name as used in CodedInputStream method names: SFixed32, UInt32 etc.
  111. /// </summary>
  112. protected string CapitalizedTypeName {
  113. get {
  114. // Our enum names match perfectly. How serendipitous.
  115. return Descriptor.FieldType.ToString();
  116. }
  117. }
  118. }
  119. }