FieldDescriptor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 System;
  33. using Google.Protobuf.DescriptorProtos;
  34. namespace Google.Protobuf.Reflection
  35. {
  36. /// <summary>
  37. /// Descriptor for a field or extension within a message in a .proto file.
  38. /// </summary>
  39. public sealed class FieldDescriptor : DescriptorBase, IComparable<FieldDescriptor>
  40. {
  41. private readonly FieldDescriptorProto proto;
  42. private EnumDescriptor enumType;
  43. private MessageDescriptor messageType;
  44. private readonly MessageDescriptor containingType;
  45. private readonly OneofDescriptor containingOneof;
  46. private FieldType fieldType;
  47. internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file,
  48. MessageDescriptor parent, int index)
  49. : base(file, file.ComputeFullName(parent, proto.Name), index)
  50. {
  51. this.proto = proto;
  52. if (proto.Type != 0)
  53. {
  54. fieldType = GetFieldTypeFromProtoType(proto.Type);
  55. }
  56. if (FieldNumber <= 0)
  57. {
  58. throw new DescriptorValidationException(this,
  59. "Field numbers must be positive integers.");
  60. }
  61. containingType = parent;
  62. // OneofIndex "defaults" to -1 due to a hack in FieldDescriptor.OnConstruction.
  63. if (proto.OneofIndex != -1)
  64. {
  65. if (proto.OneofIndex < 0 || proto.OneofIndex >= parent.Proto.OneofDecl.Count)
  66. {
  67. throw new DescriptorValidationException(this,
  68. "FieldDescriptorProto.oneof_index is out of range for type " + parent.Name);
  69. }
  70. containingOneof = parent.Oneofs[proto.OneofIndex];
  71. }
  72. file.DescriptorPool.AddSymbol(this);
  73. }
  74. /// <summary>
  75. /// The brief name of the descriptor's target.
  76. /// </summary>
  77. public override string Name { get { return proto.Name; } }
  78. internal FieldDescriptorProto Proto { get { return proto; } }
  79. /// <summary>
  80. /// Maps a field type as included in the .proto file to a FieldType.
  81. /// </summary>
  82. private static FieldType GetFieldTypeFromProtoType(FieldDescriptorProto.Types.Type type)
  83. {
  84. switch (type)
  85. {
  86. case FieldDescriptorProto.Types.Type.TYPE_DOUBLE:
  87. return FieldType.Double;
  88. case FieldDescriptorProto.Types.Type.TYPE_FLOAT:
  89. return FieldType.Float;
  90. case FieldDescriptorProto.Types.Type.TYPE_INT64:
  91. return FieldType.Int64;
  92. case FieldDescriptorProto.Types.Type.TYPE_UINT64:
  93. return FieldType.UInt64;
  94. case FieldDescriptorProto.Types.Type.TYPE_INT32:
  95. return FieldType.Int32;
  96. case FieldDescriptorProto.Types.Type.TYPE_FIXED64:
  97. return FieldType.Fixed64;
  98. case FieldDescriptorProto.Types.Type.TYPE_FIXED32:
  99. return FieldType.Fixed32;
  100. case FieldDescriptorProto.Types.Type.TYPE_BOOL:
  101. return FieldType.Bool;
  102. case FieldDescriptorProto.Types.Type.TYPE_STRING:
  103. return FieldType.String;
  104. case FieldDescriptorProto.Types.Type.TYPE_GROUP:
  105. return FieldType.Group;
  106. case FieldDescriptorProto.Types.Type.TYPE_MESSAGE:
  107. return FieldType.Message;
  108. case FieldDescriptorProto.Types.Type.TYPE_BYTES:
  109. return FieldType.Bytes;
  110. case FieldDescriptorProto.Types.Type.TYPE_UINT32:
  111. return FieldType.UInt32;
  112. case FieldDescriptorProto.Types.Type.TYPE_ENUM:
  113. return FieldType.Enum;
  114. case FieldDescriptorProto.Types.Type.TYPE_SFIXED32:
  115. return FieldType.SFixed32;
  116. case FieldDescriptorProto.Types.Type.TYPE_SFIXED64:
  117. return FieldType.SFixed64;
  118. case FieldDescriptorProto.Types.Type.TYPE_SINT32:
  119. return FieldType.SInt32;
  120. case FieldDescriptorProto.Types.Type.TYPE_SINT64:
  121. return FieldType.SInt64;
  122. default:
  123. throw new ArgumentException("Invalid type specified");
  124. }
  125. }
  126. public bool IsRepeated
  127. {
  128. get { return Proto.Label == FieldDescriptorProto.Types.Label.LABEL_REPEATED; }
  129. }
  130. public bool IsMap
  131. {
  132. get { return fieldType == FieldType.Message && messageType.Proto.Options != null && messageType.Proto.Options.MapEntry; }
  133. }
  134. public bool IsPacked
  135. {
  136. get { return Proto.Options != null && Proto.Options.Packed; }
  137. }
  138. /// <summary>
  139. /// Get the field's containing type. For extensions, this is the type being
  140. /// extended, not the location where the extension was defined. See
  141. /// <see cref="ExtensionScope" />.
  142. /// </summary>
  143. public MessageDescriptor ContainingType
  144. {
  145. get { return containingType; }
  146. }
  147. public OneofDescriptor ContainingOneof
  148. {
  149. get { return containingOneof; }
  150. }
  151. public FieldType FieldType
  152. {
  153. get { return fieldType; }
  154. }
  155. public int FieldNumber
  156. {
  157. get { return Proto.Number; }
  158. }
  159. /// <summary>
  160. /// Compares this descriptor with another one, ordering in "canonical" order
  161. /// which simply means ascending order by field number. <paramref name="other"/>
  162. /// must be a field of the same type, i.e. the <see cref="ContainingType"/> of
  163. /// both fields must be the same.
  164. /// </summary>
  165. public int CompareTo(FieldDescriptor other)
  166. {
  167. if (other.containingType != containingType)
  168. {
  169. throw new ArgumentException("FieldDescriptors can only be compared to other FieldDescriptors " +
  170. "for fields of the same message type.");
  171. }
  172. return FieldNumber - other.FieldNumber;
  173. }
  174. /// <summary>
  175. /// For enum fields, returns the field's type.
  176. /// </summary>
  177. public EnumDescriptor EnumType
  178. {
  179. get
  180. {
  181. if (fieldType != FieldType.Enum)
  182. {
  183. throw new InvalidOperationException("EnumType is only valid for enum fields.");
  184. }
  185. return enumType;
  186. }
  187. }
  188. /// <summary>
  189. /// For embedded message and group fields, returns the field's type.
  190. /// </summary>
  191. public MessageDescriptor MessageType
  192. {
  193. get
  194. {
  195. if (fieldType != FieldType.Message)
  196. {
  197. throw new InvalidOperationException("MessageType is only valid for enum fields.");
  198. }
  199. return messageType;
  200. }
  201. }
  202. /// <summary>
  203. /// Look up and cross-link all field types etc.
  204. /// </summary>
  205. internal void CrossLink()
  206. {
  207. if (Proto.TypeName != "")
  208. {
  209. IDescriptor typeDescriptor =
  210. File.DescriptorPool.LookupSymbol(Proto.TypeName, this);
  211. if (Proto.Type != 0)
  212. {
  213. // Choose field type based on symbol.
  214. if (typeDescriptor is MessageDescriptor)
  215. {
  216. fieldType = FieldType.Message;
  217. }
  218. else if (typeDescriptor is EnumDescriptor)
  219. {
  220. fieldType = FieldType.Enum;
  221. }
  222. else
  223. {
  224. throw new DescriptorValidationException(this, "\"" + Proto.TypeName + "\" is not a type.");
  225. }
  226. }
  227. if (fieldType == FieldType.Message)
  228. {
  229. if (!(typeDescriptor is MessageDescriptor))
  230. {
  231. throw new DescriptorValidationException(this,
  232. "\"" + Proto.TypeName + "\" is not a message type.");
  233. }
  234. messageType = (MessageDescriptor) typeDescriptor;
  235. if (Proto.DefaultValue != "")
  236. {
  237. throw new DescriptorValidationException(this, "Messages can't have default values.");
  238. }
  239. }
  240. else if (fieldType == FieldType.Enum)
  241. {
  242. if (!(typeDescriptor is EnumDescriptor))
  243. {
  244. throw new DescriptorValidationException(this, "\"" + Proto.TypeName + "\" is not an enum type.");
  245. }
  246. enumType = (EnumDescriptor) typeDescriptor;
  247. }
  248. else
  249. {
  250. throw new DescriptorValidationException(this, "Field with primitive type has type_name.");
  251. }
  252. }
  253. else
  254. {
  255. if (fieldType == FieldType.Message || fieldType == FieldType.Enum)
  256. {
  257. throw new DescriptorValidationException(this, "Field with message or enum type missing type_name.");
  258. }
  259. }
  260. // Note: no attempt to perform any default value parsing
  261. File.DescriptorPool.AddFieldByNumber(this);
  262. if (containingType != null && containingType.Proto.Options != null && containingType.Proto.Options.MessageSetWireFormat)
  263. {
  264. throw new DescriptorValidationException(this, "MessageSet format is not supported.");
  265. }
  266. }
  267. }
  268. }