FieldDescriptor.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Reflection;
  19. using Google.ProtocolBuffers.Collections;
  20. using Google.ProtocolBuffers.DescriptorProtos;
  21. namespace Google.ProtocolBuffers.Descriptors {
  22. /// <summary>
  23. /// Descriptor for a field or extension within a message in a .proto file.
  24. /// </summary>
  25. public sealed class FieldDescriptor : IndexedDescriptorBase<FieldDescriptorProto, FieldOptions>, IComparable<FieldDescriptor> {
  26. private readonly MessageDescriptor extensionScope;
  27. private EnumDescriptor enumType;
  28. private MessageDescriptor messageType;
  29. private MessageDescriptor containingType;
  30. private object defaultValue;
  31. private FieldType fieldType;
  32. private MappedType mappedType;
  33. internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file,
  34. MessageDescriptor parent, int index, bool isExtension)
  35. : base(proto, file, ComputeFullName(file, parent, proto.Name), index) {
  36. if (proto.HasType) {
  37. fieldType = GetFieldTypeFromProtoType(proto.Type);
  38. mappedType = FieldTypeToMappedTypeMap[fieldType];
  39. }
  40. if (FieldNumber <= 0) {
  41. throw new DescriptorValidationException(this,
  42. "Field numbers must be positive integers.");
  43. }
  44. if (isExtension) {
  45. if (!proto.HasExtendee) {
  46. throw new DescriptorValidationException(this,
  47. "FieldDescriptorProto.Extendee not set for extension field.");
  48. }
  49. containingType = null; // Will be filled in when cross-linking
  50. if (parent != null) {
  51. extensionScope = parent;
  52. } else {
  53. extensionScope = null;
  54. }
  55. } else {
  56. if (proto.HasExtendee) {
  57. throw new DescriptorValidationException(this,
  58. "FieldDescriptorProto.Extendee set for non-extension field.");
  59. }
  60. containingType = parent;
  61. extensionScope = null;
  62. }
  63. file.DescriptorPool.AddSymbol(this);
  64. }
  65. /// <summary>
  66. /// Maps a field type as included in the .proto file to a FieldType.
  67. /// </summary>
  68. private static FieldType GetFieldTypeFromProtoType(FieldDescriptorProto.Types.Type type) {
  69. switch (type) {
  70. case FieldDescriptorProto.Types.Type.TYPE_DOUBLE: return FieldType.Double;
  71. case FieldDescriptorProto.Types.Type.TYPE_FLOAT: return FieldType.Float;
  72. case FieldDescriptorProto.Types.Type.TYPE_INT64: return FieldType.Int64;
  73. case FieldDescriptorProto.Types.Type.TYPE_UINT64: return FieldType.UInt64;
  74. case FieldDescriptorProto.Types.Type.TYPE_INT32: return FieldType.Int32;
  75. case FieldDescriptorProto.Types.Type.TYPE_FIXED64: return FieldType.Fixed64;
  76. case FieldDescriptorProto.Types.Type.TYPE_FIXED32: return FieldType.Fixed32;
  77. case FieldDescriptorProto.Types.Type.TYPE_BOOL: return FieldType.Bool;
  78. case FieldDescriptorProto.Types.Type.TYPE_STRING: return FieldType.String;
  79. case FieldDescriptorProto.Types.Type.TYPE_GROUP: return FieldType.Group;
  80. case FieldDescriptorProto.Types.Type.TYPE_MESSAGE: return FieldType.Message;
  81. case FieldDescriptorProto.Types.Type.TYPE_BYTES: return FieldType.Bytes;
  82. case FieldDescriptorProto.Types.Type.TYPE_UINT32: return FieldType.UInt32;
  83. case FieldDescriptorProto.Types.Type.TYPE_ENUM: return FieldType.Enum;
  84. case FieldDescriptorProto.Types.Type.TYPE_SFIXED32: return FieldType.SFixed32;
  85. case FieldDescriptorProto.Types.Type.TYPE_SFIXED64: return FieldType.SFixed64;
  86. case FieldDescriptorProto.Types.Type.TYPE_SINT32: return FieldType.SInt32;
  87. case FieldDescriptorProto.Types.Type.TYPE_SINT64: return FieldType.SInt64;
  88. default:
  89. throw new ArgumentException("Invalid type specified");
  90. }
  91. }
  92. /// <summary>
  93. /// Returns the default value for a mapped type.
  94. /// </summary>
  95. private static object GetDefaultValueForMappedType(MappedType type) {
  96. switch (type) {
  97. case MappedType.Int32: return 0;
  98. case MappedType.Int64: return (long) 0;
  99. case MappedType.UInt32: return (uint) 0;
  100. case MappedType.UInt64: return (ulong) 0;
  101. case MappedType.Single: return (float) 0;
  102. case MappedType.Double: return (double) 0;
  103. case MappedType.Boolean: return false;
  104. case MappedType.String: return "";
  105. case MappedType.ByteString: return ByteString.Empty;
  106. case MappedType.Message: return null;
  107. case MappedType.Enum: return null;
  108. default:
  109. throw new ArgumentException("Invalid type specified");
  110. }
  111. }
  112. public bool IsRequired {
  113. get { return Proto.Label == FieldDescriptorProto.Types.Label.LABEL_REQUIRED; }
  114. }
  115. public bool IsOptional {
  116. get { return Proto.Label == FieldDescriptorProto.Types.Label.LABEL_OPTIONAL; }
  117. }
  118. public bool IsRepeated {
  119. get { return Proto.Label == FieldDescriptorProto.Types.Label.LABEL_REPEATED; }
  120. }
  121. /// <valule>
  122. /// Indicates whether or not the field had an explicitly-defined default value.
  123. /// </value>
  124. public bool HasDefaultValue {
  125. get { return Proto.HasDefaultValue; }
  126. }
  127. /// <value>
  128. /// The field's default value. Valid for all types except messages
  129. /// and groups. For all other types, the object returned is of the
  130. /// same class that would be returned by IMessage[this].
  131. /// For repeated fields this will always be an empty immutable list compatible with IList[object].
  132. /// For message fields it will always be null. For singular values, it will depend on the descriptor.
  133. /// </value>
  134. public object DefaultValue {
  135. get {
  136. if (MappedType == MappedType.Message) {
  137. throw new InvalidOperationException("FieldDescriptor.DefaultValue called on an embedded message field.");
  138. }
  139. return defaultValue;
  140. }
  141. }
  142. /// <value>
  143. /// Indicates whether or not this field is an extension.
  144. /// </value>
  145. public bool IsExtension {
  146. get { return Proto.HasExtendee; }
  147. }
  148. /*
  149. * Get the field's containing type. For extensions, this is the type being
  150. * extended, not the location where the extension was defined. See
  151. * {@link #getExtensionScope()}.
  152. */
  153. /// <summary>
  154. /// Get the field's containing type. For extensions, this is the type being
  155. /// extended, not the location where the extension was defined. See
  156. /// <see cref="ExtensionScope" />.
  157. /// </summary>
  158. public MessageDescriptor ContainingType {
  159. get { return containingType; }
  160. }
  161. /// <summary>
  162. /// For extensions defined nested within message types, gets
  163. /// the outer type. Not valid for non-extension fields.
  164. /// </summary>
  165. /// <example>
  166. /// <code>
  167. /// message Foo {
  168. /// extensions 1000 to max;
  169. /// }
  170. /// extend Foo {
  171. /// optional int32 baz = 1234;
  172. /// }
  173. /// message Bar {
  174. /// extend Foo {
  175. /// optional int32 qux = 4321;
  176. /// }
  177. /// }
  178. /// </code>
  179. /// The containing type for both <c>baz</c> and <c>qux</c> is <c>Foo</c>.
  180. /// However, the extension scope for <c>baz</c> is <c>null</c> while
  181. /// the extension scope for <c>qux</c> is <c>Bar</c>.
  182. /// </example>
  183. public MessageDescriptor ExtensionScope {
  184. get {
  185. if (!IsExtension) {
  186. throw new InvalidOperationException("This field is not an extension.");
  187. }
  188. return extensionScope;
  189. }
  190. }
  191. public MappedType MappedType {
  192. get { return mappedType; }
  193. }
  194. public FieldType FieldType {
  195. get { return fieldType; }
  196. }
  197. public int FieldNumber {
  198. get { return Proto.Number; }
  199. }
  200. /// <summary>
  201. /// Compares this descriptor with another one, ordering in "canonical" order
  202. /// which simply means ascending order by field number. <paramref name="other"/>
  203. /// must be a field of the same type, i.e. the <see cref="ContainingType"/> of
  204. /// both fields must be the same.
  205. /// </summary>
  206. public int CompareTo(FieldDescriptor other) {
  207. if (other.containingType != containingType) {
  208. throw new ArgumentException("FieldDescriptors can only be compared to other FieldDescriptors " +
  209. "for fields of the same message type.");
  210. }
  211. return FieldNumber - other.FieldNumber;
  212. }
  213. /// <summary>
  214. /// For enum fields, returns the field's type.
  215. /// </summary>
  216. public EnumDescriptor EnumType {
  217. get {
  218. if (MappedType != MappedType.Enum) {
  219. throw new InvalidOperationException("EnumType is only valid for enum fields.");
  220. }
  221. return enumType;
  222. }
  223. }
  224. /// <summary>
  225. /// For embedded message and group fields, returns the field's type.
  226. /// </summary>
  227. public MessageDescriptor MessageType {
  228. get {
  229. if (MappedType != MappedType.Message) {
  230. throw new InvalidOperationException("MessageType is only valid for enum fields.");
  231. }
  232. return messageType;
  233. }
  234. }
  235. /// <summary>
  236. /// Immutable mapping from field type to mapped type. Built using the attributes on
  237. /// FieldType values.
  238. /// </summary>
  239. public static readonly IDictionary<FieldType, MappedType> FieldTypeToMappedTypeMap = MapFieldTypes();
  240. private static IDictionary<FieldType, MappedType> MapFieldTypes() {
  241. var map = new Dictionary<FieldType, MappedType>();
  242. foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public)) {
  243. FieldType fieldType = (FieldType)field.GetValue(null);
  244. FieldMappingAttribute mapping = (FieldMappingAttribute)field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0];
  245. map[fieldType] = mapping.MappedType;
  246. }
  247. return Dictionaries.AsReadOnly(map);
  248. }
  249. /// <summary>
  250. /// Look up and cross-link all field types etc.
  251. /// </summary>
  252. internal void CrossLink() {
  253. if (Proto.HasExtendee) {
  254. IDescriptor extendee = File.DescriptorPool.LookupSymbol(Proto.Extendee, this);
  255. if (!(extendee is MessageDescriptor)) {
  256. throw new DescriptorValidationException(this, "\"" + Proto.Extendee + "\" is not a message type.");
  257. }
  258. containingType = (MessageDescriptor) extendee;
  259. if (!containingType.IsExtensionNumber(FieldNumber)) {
  260. throw new DescriptorValidationException(this,
  261. "\"" + containingType.FullName + "\" does not declare " + FieldNumber + " as an extension number.");
  262. }
  263. }
  264. if (Proto.HasTypeName) {
  265. IDescriptor typeDescriptor =
  266. File.DescriptorPool.LookupSymbol(Proto.TypeName, this);
  267. if (!Proto.HasType) {
  268. // Choose field type based on symbol.
  269. if (typeDescriptor is MessageDescriptor) {
  270. fieldType = FieldType.Message;
  271. mappedType = MappedType.Message;
  272. } else if (typeDescriptor is EnumDescriptor) {
  273. fieldType = FieldType.Enum;
  274. mappedType = MappedType.Enum;
  275. } else {
  276. throw new DescriptorValidationException(this, "\"" + Proto.TypeName + "\" is not a type.");
  277. }
  278. }
  279. if (MappedType == MappedType.Message) {
  280. if (!(typeDescriptor is MessageDescriptor)) {
  281. throw new DescriptorValidationException(this, "\"" + Proto.TypeName + "\" is not a message type.");
  282. }
  283. messageType = (MessageDescriptor) typeDescriptor;
  284. if (Proto.HasDefaultValue) {
  285. throw new DescriptorValidationException(this, "Messages can't have default values.");
  286. }
  287. } else if (MappedType == Descriptors.MappedType.Enum) {
  288. if (!(typeDescriptor is EnumDescriptor)) {
  289. throw new DescriptorValidationException(this, "\"" + Proto.TypeName + "\" is not an enum type.");
  290. }
  291. enumType = (EnumDescriptor)typeDescriptor;
  292. } else {
  293. throw new DescriptorValidationException(this, "Field with primitive type has type_name.");
  294. }
  295. } else {
  296. if (MappedType == MappedType.Message || MappedType == MappedType.Enum) {
  297. throw new DescriptorValidationException(this, "Field with message or enum type missing type_name.");
  298. }
  299. }
  300. // We don't attempt to parse the default value until here because for
  301. // enums we need the enum type's descriptor.
  302. if (Proto.HasDefaultValue) {
  303. if (IsRepeated) {
  304. throw new DescriptorValidationException(this, "Repeated fields cannot have default values.");
  305. }
  306. try {
  307. switch (FieldType) {
  308. case FieldType.Int32:
  309. case FieldType.SInt32:
  310. case FieldType.SFixed32:
  311. defaultValue = TextFormat.ParseInt32(Proto.DefaultValue);
  312. break;
  313. case FieldType.UInt32:
  314. case FieldType.Fixed32:
  315. defaultValue = TextFormat.ParseUInt32(Proto.DefaultValue);
  316. break;
  317. case FieldType.Int64:
  318. case FieldType.SInt64:
  319. case FieldType.SFixed64:
  320. defaultValue = TextFormat.ParseInt64(Proto.DefaultValue);
  321. break;
  322. case FieldType.UInt64:
  323. case FieldType.Fixed64:
  324. defaultValue = TextFormat.ParseUInt64(Proto.DefaultValue);
  325. break;
  326. case FieldType.Float:
  327. defaultValue = float.Parse(Proto.DefaultValue);
  328. break;
  329. case FieldType.Double:
  330. defaultValue = double.Parse(Proto.DefaultValue);
  331. break;
  332. case FieldType.Bool:
  333. if (Proto.DefaultValue == "true") {
  334. defaultValue = true;
  335. } else if (Proto.DefaultValue == "false") {
  336. defaultValue = false;
  337. } else {
  338. throw new FormatException("Boolean values must be \"true\" or \"false\"");
  339. }
  340. break;
  341. case FieldType.String:
  342. defaultValue = Proto.DefaultValue;
  343. break;
  344. case FieldType.Bytes:
  345. try {
  346. defaultValue = TextFormat.UnescapeBytes(Proto.DefaultValue);
  347. } catch (FormatException e) {
  348. throw new DescriptorValidationException(this, "Couldn't parse default value: " + e.Message);
  349. }
  350. break;
  351. case FieldType.Enum:
  352. defaultValue = enumType.FindValueByName(Proto.DefaultValue);
  353. if (defaultValue == null) {
  354. throw new DescriptorValidationException(this, "Unknown enum default value: \"" + Proto.DefaultValue + "\"");
  355. }
  356. break;
  357. case FieldType.Message:
  358. case FieldType.Group:
  359. throw new DescriptorValidationException(this, "Message type had default value.");
  360. }
  361. } catch (FormatException e) {
  362. DescriptorValidationException validationException =
  363. new DescriptorValidationException(this, "Could not parse default value: \"" + Proto.DefaultValue + "\"", e);
  364. throw validationException;
  365. }
  366. } else {
  367. // Determine the default default for this field.
  368. if (IsRepeated) {
  369. defaultValue = Lists<object>.Empty;
  370. } else {
  371. switch (MappedType) {
  372. case MappedType.Enum:
  373. // We guarantee elsewhere that an enum type always has at least
  374. // one possible value.
  375. defaultValue = enumType.Values[0];
  376. break;
  377. case MappedType.Message:
  378. defaultValue = null;
  379. break;
  380. default:
  381. defaultValue = GetDefaultValueForMappedType(MappedType);
  382. break;
  383. }
  384. }
  385. }
  386. if (!IsExtension) {
  387. File.DescriptorPool.AddFieldByNumber(this);
  388. }
  389. if (containingType != null && containingType.Options.MessageSetWireFormat) {
  390. if (IsExtension) {
  391. if (!IsOptional || FieldType != FieldType.Message) {
  392. throw new DescriptorValidationException(this, "Extensions of MessageSets must be optional messages.");
  393. }
  394. } else {
  395. throw new DescriptorValidationException(this, "MessageSets cannot have fields, only extensions.");
  396. }
  397. }
  398. }
  399. }
  400. }