EnumDescriptor.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. namespace Google.Protobuf.Reflection
  36. {
  37. /// <summary>
  38. /// Descriptor for an enum type in a .proto file.
  39. /// </summary>
  40. public sealed class EnumDescriptor : DescriptorBase
  41. {
  42. private readonly EnumDescriptorProto proto;
  43. private readonly MessageDescriptor containingType;
  44. private readonly IList<EnumValueDescriptor> values;
  45. private readonly Type clrType;
  46. internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, Type clrType)
  47. : base(file, file.ComputeFullName(parent, proto.Name), index)
  48. {
  49. this.proto = proto;
  50. this.clrType = clrType;
  51. containingType = parent;
  52. if (proto.Value.Count == 0)
  53. {
  54. // We cannot allow enums with no values because this would mean there
  55. // would be no valid default value for fields of this type.
  56. throw new DescriptorValidationException(this, "Enums must contain at least one value.");
  57. }
  58. values = DescriptorUtil.ConvertAndMakeReadOnly(proto.Value,
  59. (value, i) => new EnumValueDescriptor(value, file, this, i));
  60. File.DescriptorPool.AddSymbol(this);
  61. }
  62. internal EnumDescriptorProto Proto { get { return proto; } }
  63. /// <summary>
  64. /// The brief name of the descriptor's target.
  65. /// </summary>
  66. public override string Name { get { return proto.Name; } }
  67. internal override IReadOnlyList<DescriptorBase> GetNestedDescriptorListForField(int fieldNumber)
  68. {
  69. switch (fieldNumber)
  70. {
  71. case EnumDescriptorProto.ValueFieldNumber:
  72. return (IReadOnlyList<DescriptorBase>) Values;
  73. default:
  74. return null;
  75. }
  76. }
  77. /// <summary>
  78. /// The CLR type for this enum. For generated code, this will be a CLR enum type.
  79. /// </summary>
  80. public Type ClrType { get { return clrType; } }
  81. /// <value>
  82. /// If this is a nested type, get the outer descriptor, otherwise null.
  83. /// </value>
  84. public MessageDescriptor ContainingType
  85. {
  86. get { return containingType; }
  87. }
  88. /// <value>
  89. /// An unmodifiable list of defined value descriptors for this enum.
  90. /// </value>
  91. public IList<EnumValueDescriptor> Values
  92. {
  93. get { return values; }
  94. }
  95. /// <summary>
  96. /// Finds an enum value by number. If multiple enum values have the
  97. /// same number, this returns the first defined value with that number.
  98. /// If there is no value for the given number, this returns <c>null</c>.
  99. /// </summary>
  100. public EnumValueDescriptor FindValueByNumber(int number)
  101. {
  102. return File.DescriptorPool.FindEnumValueByNumber(this, number);
  103. }
  104. /// <summary>
  105. /// Finds an enum value by name.
  106. /// </summary>
  107. /// <param name="name">The unqualified name of the value (e.g. "FOO").</param>
  108. /// <returns>The value's descriptor, or null if not found.</returns>
  109. public EnumValueDescriptor FindValueByName(string name)
  110. {
  111. return File.DescriptorPool.FindSymbol<EnumValueDescriptor>(FullName + "." + name);
  112. }
  113. /// <summary>
  114. /// The (possibly empty) set of custom options for this enum.
  115. /// </summary>
  116. [Obsolete("CustomOptions are obsolete. Use the Options property.")]
  117. public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
  118. /// <summary>
  119. /// The <c>EnumOptions</c>, defined in <c>descriptor.proto</c>.
  120. /// If the options message is not present (=there are no options), <c>null</c> is returned.
  121. /// Custom options can be retrieved as extensions of the returned message.
  122. /// NOTE: A defensive copy is created each time this property is retrieved.
  123. /// </summary>
  124. public EnumOptions Options => Proto.Options?.Clone();
  125. /// <summary>
  126. /// Gets a single value enum option for this descriptor
  127. /// </summary>
  128. [Obsolete("GetOption is obsolete. Use the Options property.")]
  129. public T GetOption<T>(Extension<EnumOptions, T> extension)
  130. {
  131. var value = Proto.Options.GetExtension(extension);
  132. return value is IDeepCloneable<T> ? (value as IDeepCloneable<T>).Clone() : value;
  133. }
  134. /// <summary>
  135. /// Gets a repeated value enum option for this descriptor
  136. /// </summary>
  137. [Obsolete("GetOption is obsolete. Use the Options property.")]
  138. public RepeatedField<T> GetOption<T>(RepeatedExtension<EnumOptions, T> extension)
  139. {
  140. return Proto.Options.GetExtension(extension).Clone();
  141. }
  142. }
  143. }