OLD | NEW |
1 #region Copyright notice and license | 1 #region Copyright notice and license |
2 // Protocol Buffers - Google's data interchange format | 2 // Protocol Buffers - Google's data interchange format |
3 // Copyright 2008 Google Inc. All rights reserved. | 3 // Copyright 2008 Google Inc. All rights reserved. |
4 // https://developers.google.com/protocol-buffers/ | 4 // https://developers.google.com/protocol-buffers/ |
5 // | 5 // |
6 // Redistribution and use in source and binary forms, with or without | 6 // Redistribution and use in source and binary forms, with or without |
7 // modification, are permitted provided that the following conditions are | 7 // modification, are permitted provided that the following conditions are |
8 // met: | 8 // met: |
9 // | 9 // |
10 // * Redistributions of source code must retain the above copyright | 10 // * Redistributions of source code must retain the above copyright |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 "google/protobuf/field_mask.proto", | 53 "google/protobuf/field_mask.proto", |
54 "google/protobuf/source_context.proto", | 54 "google/protobuf/source_context.proto", |
55 "google/protobuf/struct.proto", | 55 "google/protobuf/struct.proto", |
56 "google/protobuf/type.proto", | 56 "google/protobuf/type.proto", |
57 }; | 57 }; |
58 | 58 |
59 private readonly IList<FieldDescriptor> fieldsInDeclarationOrder; | 59 private readonly IList<FieldDescriptor> fieldsInDeclarationOrder; |
60 private readonly IList<FieldDescriptor> fieldsInNumberOrder; | 60 private readonly IList<FieldDescriptor> fieldsInNumberOrder; |
61 private readonly IDictionary<string, FieldDescriptor> jsonFieldMap; | 61 private readonly IDictionary<string, FieldDescriptor> jsonFieldMap; |
62 | 62 |
63 internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, M
essageDescriptor parent, int typeIndex, GeneratedCodeInfo generatedCodeInfo) | 63 internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, M
essageDescriptor parent, int typeIndex, GeneratedClrTypeInfo generatedCodeInfo) |
64 : base(file, file.ComputeFullName(parent, proto.Name), typeIndex) | 64 : base(file, file.ComputeFullName(parent, proto.Name), typeIndex) |
65 { | 65 { |
66 Proto = proto; | 66 Proto = proto; |
67 Parser = generatedCodeInfo?.Parser; | 67 Parser = generatedCodeInfo?.Parser; |
68 ClrType = generatedCodeInfo?.ClrType; | 68 ClrType = generatedCodeInfo?.ClrType; |
69 ContainingType = parent; | 69 ContainingType = parent; |
70 | 70 |
71 // Note use of generatedCodeInfo. rather than generatedCodeInfo?. he
re... we don't expect | 71 // Note use of generatedCodeInfo. rather than generatedCodeInfo?. he
re... we don't expect |
72 // to see any nested oneofs, types or enums in "not actually generat
ed" code... we do | 72 // to see any nested oneofs, types or enums in "not actually generat
ed" code... we do |
73 // expect fields though (for map entry messages). | 73 // expect fields though (for map entry messages). |
(...skipping 11 matching lines...) Expand all Loading... |
85 proto.EnumType, | 85 proto.EnumType, |
86 (type, index) => | 86 (type, index) => |
87 new EnumDescriptor(type, file, this, index, generatedCodeInfo.Ne
stedEnums[index])); | 87 new EnumDescriptor(type, file, this, index, generatedCodeInfo.Ne
stedEnums[index])); |
88 | 88 |
89 fieldsInDeclarationOrder = DescriptorUtil.ConvertAndMakeReadOnly( | 89 fieldsInDeclarationOrder = DescriptorUtil.ConvertAndMakeReadOnly( |
90 proto.Field, | 90 proto.Field, |
91 (field, index) => | 91 (field, index) => |
92 new FieldDescriptor(field, file, this, index, generatedCodeInfo?
.PropertyNames[index])); | 92 new FieldDescriptor(field, file, this, index, generatedCodeInfo?
.PropertyNames[index])); |
93 fieldsInNumberOrder = new ReadOnlyCollection<FieldDescriptor>(fields
InDeclarationOrder.OrderBy(field => field.FieldNumber).ToArray()); | 93 fieldsInNumberOrder = new ReadOnlyCollection<FieldDescriptor>(fields
InDeclarationOrder.OrderBy(field => field.FieldNumber).ToArray()); |
94 // TODO: Use field => field.Proto.JsonName when we're confident it's
appropriate. (And then use it in the formatter, too.) | 94 // TODO: Use field => field.Proto.JsonName when we're confident it's
appropriate. (And then use it in the formatter, too.) |
95 jsonFieldMap = new ReadOnlyDictionary<string, FieldDescriptor>(field
sInNumberOrder.ToDictionary(field => JsonFormatter.ToCamelCase(field.Name))); | 95 jsonFieldMap = CreateJsonFieldMap(fieldsInNumberOrder); |
96 file.DescriptorPool.AddSymbol(this); | 96 file.DescriptorPool.AddSymbol(this); |
97 Fields = new FieldCollection(this); | 97 Fields = new FieldCollection(this); |
98 } | 98 } |
99 | 99 |
| 100 private static ReadOnlyDictionary<string, FieldDescriptor> CreateJsonFie
ldMap(IList<FieldDescriptor> fields) |
| 101 { |
| 102 var map = new Dictionary<string, FieldDescriptor>(); |
| 103 foreach (var field in fields) |
| 104 { |
| 105 map[field.Name] = field; |
| 106 map[field.JsonName] = field; |
| 107 } |
| 108 return new ReadOnlyDictionary<string, FieldDescriptor>(map); |
| 109 } |
| 110 |
100 /// <summary> | 111 /// <summary> |
101 /// The brief name of the descriptor's target. | 112 /// The brief name of the descriptor's target. |
102 /// </summary> | 113 /// </summary> |
103 public override string Name => Proto.Name; | 114 public override string Name => Proto.Name; |
104 | 115 |
105 internal DescriptorProto Proto { get; } | 116 internal DescriptorProto Proto { get; } |
106 | 117 |
107 /// <summary> | 118 /// <summary> |
108 /// The CLR type used to represent message instances from this descripto
r. | 119 /// The CLR type used to represent message instances from this descripto
r. |
109 /// </summary> | 120 /// </summary> |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 /// Returns the fields in the message as an immutable list, in ascen
ding field number | 259 /// 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 | 260 /// 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 | 261 /// 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. | 262 /// to use the <see cref="FieldCollection"/> indexer. |
252 /// </value> | 263 /// </value> |
253 public IList<FieldDescriptor> InFieldNumberOrder() => messageDescrip
tor.fieldsInNumberOrder; | 264 public IList<FieldDescriptor> InFieldNumberOrder() => messageDescrip
tor.fieldsInNumberOrder; |
254 | 265 |
255 // TODO: consider making this public in the future. (Being conservat
ive for now...) | 266 // TODO: consider making this public in the future. (Being conservat
ive for now...) |
256 | 267 |
257 /// <value> | 268 /// <value> |
258 /// Returns a read-only dictionary mapping the field names in this m
essage as they're used | 269 /// Returns a read-only dictionary mapping the field names in this m
essage as they're available |
259 /// in the JSON representation to the field descriptors. For example
, a field <c>foo_bar</c> | 270 /// in the JSON representation to the field descriptors. For example
, a field <c>foo_bar</c> |
260 /// in the message would result in an entry with a key <c>fooBar</c>
. | 271 /// in the message would result two entries, one with a key <c>fooBa
r</c> and one with a key |
| 272 /// <c>foo_bar</c>, both referring to the same field. |
261 /// </value> | 273 /// </value> |
262 internal IDictionary<string, FieldDescriptor> ByJsonName() => messag
eDescriptor.jsonFieldMap; | 274 internal IDictionary<string, FieldDescriptor> ByJsonName() => messag
eDescriptor.jsonFieldMap; |
263 | 275 |
264 /// <summary> | 276 /// <summary> |
265 /// Retrieves the descriptor for the field with the given number. | 277 /// Retrieves the descriptor for the field with the given number. |
266 /// </summary> | 278 /// </summary> |
267 /// <param name="number">Number of the field to retrieve the descrip
tor for</param> | 279 /// <param name="number">Number of the field to retrieve the descrip
tor for</param> |
268 /// <returns>The accessor for the given field</returns> | 280 /// <returns>The accessor for the given field</returns> |
269 /// <exception cref="KeyNotFoundException">The message descriptor do
es not contain a field | 281 /// <exception cref="KeyNotFoundException">The message descriptor do
es not contain a field |
270 /// with the given number</exception> | 282 /// with the given number</exception> |
(...skipping 25 matching lines...) Expand all Loading... |
296 if (fieldDescriptor == null) | 308 if (fieldDescriptor == null) |
297 { | 309 { |
298 throw new KeyNotFoundException("No such field name"); | 310 throw new KeyNotFoundException("No such field name"); |
299 } | 311 } |
300 return fieldDescriptor; | 312 return fieldDescriptor; |
301 } | 313 } |
302 } | 314 } |
303 } | 315 } |
304 } | 316 } |
305 } | 317 } |
OLD | NEW |