MessageDescriptor.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using Google.Protobuf.Collections;
  33. using System;
  34. using System.Collections.Generic;
  35. using System.Linq;
  36. namespace Google.Protobuf.Reflection
  37. {
  38. /// <summary>
  39. /// Describes a message type.
  40. /// </summary>
  41. public sealed class MessageDescriptor : DescriptorBase
  42. {
  43. private static readonly HashSet<string> WellKnownTypeNames = new HashSet<string>
  44. {
  45. "google/protobuf/any.proto",
  46. "google/protobuf/api.proto",
  47. "google/protobuf/duration.proto",
  48. "google/protobuf/empty.proto",
  49. "google/protobuf/wrappers.proto",
  50. "google/protobuf/timestamp.proto",
  51. "google/protobuf/field_mask.proto",
  52. "google/protobuf/source_context.proto",
  53. "google/protobuf/struct.proto",
  54. "google/protobuf/type.proto",
  55. };
  56. private readonly DescriptorProto proto;
  57. private readonly MessageDescriptor containingType;
  58. private readonly IList<MessageDescriptor> nestedTypes;
  59. private readonly IList<EnumDescriptor> enumTypes;
  60. private readonly IList<FieldDescriptor> fields;
  61. private readonly IList<OneofDescriptor> oneofs;
  62. // CLR representation of the type described by this descriptor, if any.
  63. private readonly Type generatedType;
  64. private IDictionary<int, IFieldAccessor> fieldAccessorsByFieldNumber;
  65. internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex, GeneratedCodeInfo generatedCodeInfo)
  66. : base(file, file.ComputeFullName(parent, proto.Name), typeIndex)
  67. {
  68. this.proto = proto;
  69. generatedType = generatedCodeInfo == null ? null : generatedCodeInfo.ClrType;
  70. containingType = parent;
  71. oneofs = DescriptorUtil.ConvertAndMakeReadOnly(
  72. proto.OneofDecl,
  73. (oneof, index) =>
  74. new OneofDescriptor(oneof, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.OneofNames[index]));
  75. nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(
  76. proto.NestedType,
  77. (type, index) =>
  78. new MessageDescriptor(type, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedTypes[index]));
  79. enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(
  80. proto.EnumType,
  81. (type, index) =>
  82. new EnumDescriptor(type, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.NestedEnums[index]));
  83. fields = DescriptorUtil.ConvertAndMakeReadOnly(
  84. proto.Field,
  85. (field, index) =>
  86. new FieldDescriptor(field, file, this, index, generatedCodeInfo == null ? null : generatedCodeInfo.PropertyNames[index]));
  87. file.DescriptorPool.AddSymbol(this);
  88. }
  89. /// <summary>
  90. /// Returns the total number of nested types and enums, recursively.
  91. /// </summary>
  92. private int CountTotalGeneratedTypes()
  93. {
  94. return nestedTypes.Sum(nested => nested.CountTotalGeneratedTypes()) + enumTypes.Count;
  95. }
  96. /// <summary>
  97. /// The brief name of the descriptor's target.
  98. /// </summary>
  99. public override string Name { get { return proto.Name; } }
  100. internal DescriptorProto Proto { get { return proto; } }
  101. /// <summary>
  102. /// The generated type for this message, or <c>null</c> if the descriptor does not represent a generated type.
  103. /// </summary>
  104. public Type GeneratedType { get { return generatedType; } }
  105. /// <summary>
  106. /// Returns whether this message is one of the "well known types" which may have runtime/protoc support.
  107. /// </summary>
  108. internal bool IsWellKnownType
  109. {
  110. get
  111. {
  112. return File.Package == "google.protobuf" && WellKnownTypeNames.Contains(File.Name);
  113. }
  114. }
  115. /// <value>
  116. /// If this is a nested type, get the outer descriptor, otherwise null.
  117. /// </value>
  118. public MessageDescriptor ContainingType
  119. {
  120. get { return containingType; }
  121. }
  122. /// <value>
  123. /// An unmodifiable list of this message type's fields.
  124. /// </value>
  125. public IList<FieldDescriptor> Fields
  126. {
  127. get { return fields; }
  128. }
  129. /// <value>
  130. /// An unmodifiable list of this message type's nested types.
  131. /// </value>
  132. public IList<MessageDescriptor> NestedTypes
  133. {
  134. get { return nestedTypes; }
  135. }
  136. /// <value>
  137. /// An unmodifiable list of this message type's enum types.
  138. /// </value>
  139. public IList<EnumDescriptor> EnumTypes
  140. {
  141. get { return enumTypes; }
  142. }
  143. public IList<OneofDescriptor> Oneofs
  144. {
  145. get { return oneofs; }
  146. }
  147. /// <summary>
  148. /// Returns a map from field number to accessor.
  149. /// TODO: Revisit this. It's mostly in place to make the transition from FieldAccessorTable
  150. /// to descriptor-based reflection simple in terms of tests. Work out what we really want.
  151. /// </summary>
  152. public IDictionary<int, IFieldAccessor> FieldAccessorsByFieldNumber { get { return fieldAccessorsByFieldNumber; } }
  153. /// <summary>
  154. /// Finds a field by field name.
  155. /// </summary>
  156. /// <param name="name">The unqualified name of the field (e.g. "foo").</param>
  157. /// <returns>The field's descriptor, or null if not found.</returns>
  158. public FieldDescriptor FindFieldByName(String name)
  159. {
  160. return File.DescriptorPool.FindSymbol<FieldDescriptor>(FullName + "." + name);
  161. }
  162. /// <summary>
  163. /// Finds a field by field number.
  164. /// </summary>
  165. /// <param name="number">The field number within this message type.</param>
  166. /// <returns>The field's descriptor, or null if not found.</returns>
  167. public FieldDescriptor FindFieldByNumber(int number)
  168. {
  169. return File.DescriptorPool.FindFieldByNumber(this, number);
  170. }
  171. /// <summary>
  172. /// Finds a nested descriptor by name. The is valid for fields, nested
  173. /// message types, oneofs and enums.
  174. /// </summary>
  175. /// <param name="name">The unqualified name of the descriptor, e.g. "Foo"</param>
  176. /// <returns>The descriptor, or null if not found.</returns>
  177. public T FindDescriptor<T>(string name)
  178. where T : class, IDescriptor
  179. {
  180. return File.DescriptorPool.FindSymbol<T>(FullName + "." + name);
  181. }
  182. /// <summary>
  183. /// Looks up and cross-links all fields and nested types.
  184. /// </summary>
  185. internal void CrossLink()
  186. {
  187. foreach (MessageDescriptor message in nestedTypes)
  188. {
  189. message.CrossLink();
  190. }
  191. foreach (FieldDescriptor field in fields)
  192. {
  193. field.CrossLink();
  194. }
  195. foreach (OneofDescriptor oneof in oneofs)
  196. {
  197. oneof.CrossLink();
  198. }
  199. fieldAccessorsByFieldNumber = new ReadOnlyDictionary<int, IFieldAccessor>(fields.ToDictionary(field => field.FieldNumber, field => field.Accessor));
  200. }
  201. }
  202. }