| OLD | NEW |
| (Empty) |
| 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 | |
| 33 using System; | |
| 34 using System.Collections.Generic; | |
| 35 using System.Collections.ObjectModel; | |
| 36 using System.Linq; | |
| 37 | |
| 38 namespace Google.Protobuf.Reflection | |
| 39 { | |
| 40 /// <summary> | |
| 41 /// Describes a message type. | |
| 42 /// </summary> | |
| 43 public sealed class MessageDescriptor : DescriptorBase | |
| 44 { | |
| 45 private static readonly HashSet<string> WellKnownTypeNames = new HashSet
<string> | |
| 46 { | |
| 47 "google/protobuf/any.proto", | |
| 48 "google/protobuf/api.proto", | |
| 49 "google/protobuf/duration.proto", | |
| 50 "google/protobuf/empty.proto", | |
| 51 "google/protobuf/wrappers.proto", | |
| 52 "google/protobuf/timestamp.proto", | |
| 53 "google/protobuf/field_mask.proto", | |
| 54 "google/protobuf/source_context.proto", | |
| 55 "google/protobuf/struct.proto", | |
| 56 "google/protobuf/type.proto", | |
| 57 }; | |
| 58 | |
| 59 private readonly DescriptorProto proto; | |
| 60 private readonly MessageDescriptor containingType; | |
| 61 private readonly IList<MessageDescriptor> nestedTypes; | |
| 62 private readonly IList<EnumDescriptor> enumTypes; | |
| 63 private readonly IList<FieldDescriptor> fieldsInDeclarationOrder; | |
| 64 private readonly IList<FieldDescriptor> fieldsInNumberOrder; | |
| 65 private readonly FieldCollection fields; | |
| 66 private readonly IList<OneofDescriptor> oneofs; | |
| 67 // CLR representation of the type described by this descriptor, if any. | |
| 68 private readonly Type generatedType; | |
| 69 | |
| 70 internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, M
essageDescriptor parent, int typeIndex, GeneratedCodeInfo generatedCodeInfo) | |
| 71 : base(file, file.ComputeFullName(parent, proto.Name), typeIndex) | |
| 72 { | |
| 73 this.proto = proto; | |
| 74 generatedType = generatedCodeInfo == null ? null : generatedCodeInfo
.ClrType; | |
| 75 | |
| 76 containingType = parent; | |
| 77 | |
| 78 oneofs = DescriptorUtil.ConvertAndMakeReadOnly( | |
| 79 proto.OneofDecl, | |
| 80 (oneof, index) => | |
| 81 new OneofDescriptor(oneof, file, this, index, generatedCodeInfo
== null ? null : generatedCodeInfo.OneofNames[index])); | |
| 82 | |
| 83 nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly( | |
| 84 proto.NestedType, | |
| 85 (type, index) => | |
| 86 new MessageDescriptor(type, file, this, index, generatedCodeInfo
== null ? null : generatedCodeInfo.NestedTypes[index])); | |
| 87 | |
| 88 enumTypes = DescriptorUtil.ConvertAndMakeReadOnly( | |
| 89 proto.EnumType, | |
| 90 (type, index) => | |
| 91 new EnumDescriptor(type, file, this, index, generatedCodeInfo ==
null ? null : generatedCodeInfo.NestedEnums[index])); | |
| 92 | |
| 93 fieldsInDeclarationOrder = DescriptorUtil.ConvertAndMakeReadOnly( | |
| 94 proto.Field, | |
| 95 (field, index) => | |
| 96 new FieldDescriptor(field, file, this, index, generatedCodeInfo
== null ? null : generatedCodeInfo.PropertyNames[index])); | |
| 97 fieldsInNumberOrder = new ReadOnlyCollection<FieldDescriptor>(fields
InDeclarationOrder.OrderBy(field => field.FieldNumber).ToArray()); | |
| 98 file.DescriptorPool.AddSymbol(this); | |
| 99 fields = new FieldCollection(this); | |
| 100 } | |
| 101 | |
| 102 /// <summary> | |
| 103 /// Returns the total number of nested types and enums, recursively. | |
| 104 /// </summary> | |
| 105 private int CountTotalGeneratedTypes() | |
| 106 { | |
| 107 return nestedTypes.Sum(nested => nested.CountTotalGeneratedTypes())
+ enumTypes.Count; | |
| 108 } | |
| 109 | |
| 110 /// <summary> | |
| 111 /// The brief name of the descriptor's target. | |
| 112 /// </summary> | |
| 113 public override string Name { get { return proto.Name; } } | |
| 114 | |
| 115 internal DescriptorProto Proto { get { return proto; } } | |
| 116 | |
| 117 /// <summary> | |
| 118 /// The generated type for this message, or <c>null</c> if the descripto
r does not represent a generated type. | |
| 119 /// </summary> | |
| 120 public Type GeneratedType { get { return generatedType; } } | |
| 121 | |
| 122 /// <summary> | |
| 123 /// Returns whether this message is one of the "well known types" which
may have runtime/protoc support. | |
| 124 /// </summary> | |
| 125 internal bool IsWellKnownType | |
| 126 { | |
| 127 get | |
| 128 { | |
| 129 return File.Package == "google.protobuf" && WellKnownTypeNames.C
ontains(File.Name); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 /// <value> | |
| 134 /// If this is a nested type, get the outer descriptor, otherwise null. | |
| 135 /// </value> | |
| 136 public MessageDescriptor ContainingType | |
| 137 { | |
| 138 get { return containingType; } | |
| 139 } | |
| 140 | |
| 141 /// <value> | |
| 142 /// A collection of fields, which can be retrieved by name or field numb
er. | |
| 143 /// </value> | |
| 144 public FieldCollection Fields | |
| 145 { | |
| 146 get { return fields; } | |
| 147 } | |
| 148 | |
| 149 /// <value> | |
| 150 /// An unmodifiable list of this message type's nested types. | |
| 151 /// </value> | |
| 152 public IList<MessageDescriptor> NestedTypes | |
| 153 { | |
| 154 get { return nestedTypes; } | |
| 155 } | |
| 156 | |
| 157 /// <value> | |
| 158 /// An unmodifiable list of this message type's enum types. | |
| 159 /// </value> | |
| 160 public IList<EnumDescriptor> EnumTypes | |
| 161 { | |
| 162 get { return enumTypes; } | |
| 163 } | |
| 164 | |
| 165 /// <value> | |
| 166 /// An unmodifiable list of the "oneof" field collections in this messag
e type. | |
| 167 /// </value> | |
| 168 public IList<OneofDescriptor> Oneofs | |
| 169 { | |
| 170 get { return oneofs; } | |
| 171 } | |
| 172 | |
| 173 /// <summary> | |
| 174 /// Finds a field by field name. | |
| 175 /// </summary> | |
| 176 /// <param name="name">The unqualified name of the field (e.g. "foo").</
param> | |
| 177 /// <returns>The field's descriptor, or null if not found.</returns> | |
| 178 public FieldDescriptor FindFieldByName(String name) | |
| 179 { | |
| 180 return File.DescriptorPool.FindSymbol<FieldDescriptor>(FullName + ".
" + name); | |
| 181 } | |
| 182 | |
| 183 /// <summary> | |
| 184 /// Finds a field by field number. | |
| 185 /// </summary> | |
| 186 /// <param name="number">The field number within this message type.</par
am> | |
| 187 /// <returns>The field's descriptor, or null if not found.</returns> | |
| 188 public FieldDescriptor FindFieldByNumber(int number) | |
| 189 { | |
| 190 return File.DescriptorPool.FindFieldByNumber(this, number); | |
| 191 } | |
| 192 | |
| 193 /// <summary> | |
| 194 /// Finds a nested descriptor by name. The is valid for fields, nested | |
| 195 /// message types, oneofs and enums. | |
| 196 /// </summary> | |
| 197 /// <param name="name">The unqualified name of the descriptor, e.g. "Foo
"</param> | |
| 198 /// <returns>The descriptor, or null if not found.</returns> | |
| 199 public T FindDescriptor<T>(string name) | |
| 200 where T : class, IDescriptor | |
| 201 { | |
| 202 return File.DescriptorPool.FindSymbol<T>(FullName + "." + name); | |
| 203 } | |
| 204 | |
| 205 /// <summary> | |
| 206 /// Looks up and cross-links all fields and nested types. | |
| 207 /// </summary> | |
| 208 internal void CrossLink() | |
| 209 { | |
| 210 foreach (MessageDescriptor message in nestedTypes) | |
| 211 { | |
| 212 message.CrossLink(); | |
| 213 } | |
| 214 | |
| 215 foreach (FieldDescriptor field in fieldsInDeclarationOrder) | |
| 216 { | |
| 217 field.CrossLink(); | |
| 218 } | |
| 219 | |
| 220 foreach (OneofDescriptor oneof in oneofs) | |
| 221 { | |
| 222 oneof.CrossLink(); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 /// <summary> | |
| 227 /// A collection to simplify retrieving the field accessor for a particu
lar field. | |
| 228 /// </summary> | |
| 229 public sealed class FieldCollection | |
| 230 { | |
| 231 private readonly MessageDescriptor messageDescriptor; | |
| 232 | |
| 233 internal FieldCollection(MessageDescriptor messageDescriptor) | |
| 234 { | |
| 235 this.messageDescriptor = messageDescriptor; | |
| 236 } | |
| 237 | |
| 238 /// <value> | |
| 239 /// Returns the fields in the message as an immutable list, in the o
rder in which they | |
| 240 /// are declared in the source .proto file. | |
| 241 /// </value> | |
| 242 public IList<FieldDescriptor> InDeclarationOrder() | |
| 243 { | |
| 244 return messageDescriptor.fieldsInDeclarationOrder; | |
| 245 } | |
| 246 | |
| 247 /// <value> | |
| 248 /// Returns the fields in the message as an immutable list, in ascen
ding field number | |
| 249 /// order. Field numbers need not be contiguous, so there is no dire
ct mapping from the | |
| 250 /// index in the list to the field number; to retrieve a field by fi
eld number, it is better | |
| 251 /// to use the <see cref="FieldCollection"/> indexer. | |
| 252 /// </value> | |
| 253 public IList<FieldDescriptor> InFieldNumberOrder() | |
| 254 { | |
| 255 return messageDescriptor.fieldsInNumberOrder; | |
| 256 } | |
| 257 | |
| 258 /// <summary> | |
| 259 /// Retrieves the descriptor for the field with the given number. | |
| 260 /// </summary> | |
| 261 /// <param name="number">Number of the field to retrieve the descrip
tor for</param> | |
| 262 /// <returns>The accessor for the given field</returns> | |
| 263 /// <exception cref="KeyNotFoundException">The message descriptor do
es not contain a field | |
| 264 /// with the given number</exception> | |
| 265 public FieldDescriptor this[int number] | |
| 266 { | |
| 267 get | |
| 268 { | |
| 269 var fieldDescriptor = messageDescriptor.FindFieldByNumber(nu
mber); | |
| 270 if (fieldDescriptor == null) | |
| 271 { | |
| 272 throw new KeyNotFoundException("No such field number"); | |
| 273 } | |
| 274 return fieldDescriptor; | |
| 275 } | |
| 276 } | |
| 277 | |
| 278 /// <summary> | |
| 279 /// Retrieves the descriptor for the field with the given name. | |
| 280 /// </summary> | |
| 281 /// <param name="name">Name of the field to retrieve the descriptor
for</param> | |
| 282 /// <returns>The descriptor for the given field</returns> | |
| 283 /// <exception cref="KeyNotFoundException">The message descriptor do
es not contain a field | |
| 284 /// with the given name</exception> | |
| 285 public FieldDescriptor this[string name] | |
| 286 { | |
| 287 get | |
| 288 { | |
| 289 var fieldDescriptor = messageDescriptor.FindFieldByName(name
); | |
| 290 if (fieldDescriptor == null) | |
| 291 { | |
| 292 throw new KeyNotFoundException("No such field name"); | |
| 293 } | |
| 294 return fieldDescriptor; | |
| 295 } | |
| 296 } | |
| 297 } | |
| 298 } | |
| 299 } | |
| OLD | NEW |