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