FieldDescriptor.cs 12 KB

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