OneofDescriptor.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 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 System.Collections.Generic;
  34. using System.Collections.ObjectModel;
  35. using Google.Protobuf.Collections;
  36. using Google.Protobuf.Compatibility;
  37. namespace Google.Protobuf.Reflection
  38. {
  39. /// <summary>
  40. /// Describes a "oneof" field collection in a message type: a set of
  41. /// fields of which at most one can be set in any particular message.
  42. /// </summary>
  43. public sealed class OneofDescriptor : DescriptorBase
  44. {
  45. private readonly OneofDescriptorProto proto;
  46. private MessageDescriptor containingType;
  47. private IList<FieldDescriptor> fields;
  48. private readonly OneofAccessor accessor;
  49. internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, string clrName)
  50. : base(file, file.ComputeFullName(parent, proto.Name), index)
  51. {
  52. this.proto = proto;
  53. containingType = parent;
  54. file.DescriptorPool.AddSymbol(this);
  55. accessor = CreateAccessor(clrName);
  56. }
  57. /// <summary>
  58. /// The brief name of the descriptor's target.
  59. /// </summary>
  60. public override string Name { get { return proto.Name; } }
  61. /// <summary>
  62. /// Gets the message type containing this oneof.
  63. /// </summary>
  64. /// <value>
  65. /// The message type containing this oneof.
  66. /// </value>
  67. public MessageDescriptor ContainingType
  68. {
  69. get { return containingType; }
  70. }
  71. /// <summary>
  72. /// Gets the fields within this oneof, in declaration order.
  73. /// </summary>
  74. /// <value>
  75. /// The fields within this oneof, in declaration order.
  76. /// </value>
  77. public IList<FieldDescriptor> Fields { get { return fields; } }
  78. /// <summary>
  79. /// Gets an accessor for reflective access to the values associated with the oneof
  80. /// in a particular message.
  81. /// </summary>
  82. /// <remarks>
  83. /// <para>
  84. /// In descriptors for generated code, the value returned by this property will always be non-null.
  85. /// </para>
  86. /// <para>
  87. /// In dynamically loaded descriptors, the value returned by this property will current be null;
  88. /// if and when dynamic messages are supported, it will return a suitable accessor to work with
  89. /// them.
  90. /// </para>
  91. /// </remarks>
  92. /// <value>
  93. /// The accessor used for reflective access.
  94. /// </value>
  95. public OneofAccessor Accessor { get { return accessor; } }
  96. /// <summary>
  97. /// The (possibly empty) set of custom options for this oneof.
  98. /// </summary>
  99. [Obsolete("CustomOptions are obsolete. Use GetOption")]
  100. public CustomOptions CustomOptions => new CustomOptions(proto.Options._extensions?.ValuesByNumber);
  101. /// <summary>
  102. /// Gets a single value oneof option for this descriptor
  103. /// </summary>
  104. public T GetOption<T>(Extension<OneofOptions, T> extension)
  105. {
  106. var value = proto.Options.GetExtension(extension);
  107. return value is IDeepCloneable<T> ? (value as IDeepCloneable<T>).Clone() : value;
  108. }
  109. /// <summary>
  110. /// Gets a repeated value oneof option for this descriptor
  111. /// </summary>
  112. public RepeatedField<T> GetOption<T>(RepeatedExtension<OneofOptions, T> extension)
  113. {
  114. return proto.Options.GetExtension(extension).Clone();
  115. }
  116. internal void CrossLink()
  117. {
  118. List<FieldDescriptor> fieldCollection = new List<FieldDescriptor>();
  119. foreach (var field in ContainingType.Fields.InDeclarationOrder())
  120. {
  121. if (field.ContainingOneof == this)
  122. {
  123. fieldCollection.Add(field);
  124. }
  125. }
  126. fields = new ReadOnlyCollection<FieldDescriptor>(fieldCollection);
  127. }
  128. private OneofAccessor CreateAccessor(string clrName)
  129. {
  130. // We won't have a CLR name if this is from a dynamically-loaded FileDescriptor.
  131. // TODO: Support dynamic messages.
  132. if (clrName == null)
  133. {
  134. return null;
  135. }
  136. var caseProperty = containingType.ClrType.GetProperty(clrName + "Case");
  137. if (caseProperty == null)
  138. {
  139. throw new DescriptorValidationException(this, $"Property {clrName}Case not found in {containingType.ClrType}");
  140. }
  141. var clearMethod = containingType.ClrType.GetMethod("Clear" + clrName);
  142. if (clearMethod == null)
  143. {
  144. throw new DescriptorValidationException(this, $"Method Clear{clrName} not found in {containingType.ClrType}");
  145. }
  146. return new OneofAccessor(caseProperty, clearMethod, this);
  147. }
  148. }
  149. }