Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame^] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. |
| 3 | // http://code.google.com/p/protobuf/ |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | using System; |
| 17 | using System.Collections; |
| 18 | using System.Collections.Generic; |
| 19 | using Google.ProtocolBuffers.Collections; |
| 20 | using Google.ProtocolBuffers.Descriptors; |
| 21 | |
| 22 | namespace Google.ProtocolBuffers { |
| 23 | /// <summary> |
| 24 | /// A class which represents an arbitrary set of fields of some message type. |
| 25 | /// This is used to implement DynamicMessage, and also to represent extensions |
| 26 | /// in GeneratedMessage. This class is internal, since outside users should probably |
| 27 | /// be using DynamicMessage. |
| 28 | /// |
| 29 | /// As in the Java implementation, this class goes against the rest of the framework |
| 30 | /// in terms of mutability. Instead of having a mutable Builder class and an immutable |
| 31 | /// FieldSet class, FieldSet just has a MakeImmutable() method. This is safe so long as |
| 32 | /// all callers are careful not to let a mutable FieldSet escape into the open. This would |
| 33 | /// be impossible to guarantee if this were a public class, of course. |
| 34 | /// |
| 35 | /// All repeated fields are stored as IList[object] even |
| 36 | /// TODO(jonskeet): Finish this comment! |
| 37 | /// </summary> |
| 38 | internal sealed class FieldSet { |
| 39 | |
| 40 | private static readonly FieldSet defaultInstance = new FieldSet(new Dictionary<FieldDescriptor, object>()).MakeImmutable(); |
| 41 | |
| 42 | private IDictionary<FieldDescriptor, object> fields; |
| 43 | |
| 44 | private FieldSet(IDictionary<FieldDescriptor, object> fields) { |
| 45 | this.fields = fields; |
| 46 | } |
| 47 | |
| 48 | public static FieldSet CreateInstance() { |
| 49 | // Use SortedList to keep fields in the canonical order |
| 50 | return new FieldSet(new SortedList<FieldDescriptor, object>()); |
| 51 | } |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Makes this FieldSet immutable, and returns it for convenience. Any |
| 55 | /// mutable repeated fields are made immutable, as well as the map itself. |
| 56 | /// </summary> |
| 57 | internal FieldSet MakeImmutable() { |
| 58 | // First check if we have any repeated values |
| 59 | bool hasRepeats = false; |
| 60 | foreach (object value in fields.Values) { |
| 61 | IList<object> list = value as IList<object>; |
| 62 | if (list != null && !list.IsReadOnly) { |
| 63 | hasRepeats = true; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (hasRepeats) { |
| 69 | var tmp = new SortedList<FieldDescriptor, object>(); |
| 70 | foreach (KeyValuePair<FieldDescriptor, object> entry in fields) { |
| 71 | IList<object> list = entry.Value as IList<object>; |
| 72 | tmp[entry.Key] = list == null ? entry.Value : Lists.AsReadOnly(list); |
| 73 | } |
| 74 | fields = tmp; |
| 75 | } |
| 76 | |
| 77 | fields = Dictionaries.AsReadOnly(fields); |
| 78 | |
| 79 | return this; |
| 80 | } |
| 81 | |
| 82 | /// <summary> |
| 83 | /// Returns the default, immutable instance with no fields defined. |
| 84 | /// </summary> |
| 85 | internal static FieldSet DefaultInstance { |
| 86 | get { return defaultInstance; } |
| 87 | } |
| 88 | |
| 89 | /// <summary> |
| 90 | /// Returns an immutable mapping of fields. Note that although the mapping itself |
| 91 | /// is immutable, the entries may not be (i.e. any repeated values are represented by |
| 92 | /// mutable lists). The behaviour is not specified if the contents are mutated. |
| 93 | /// </summary> |
| 94 | internal IDictionary<FieldDescriptor, object> AllFields { |
| 95 | get { return Dictionaries.AsReadOnly(fields); } |
| 96 | } |
| 97 | |
| 98 | /// <summary> |
| 99 | /// See <see cref="IMessage.HasField"/>. |
| 100 | /// </summary> |
| 101 | public bool HasField(FieldDescriptor field) { |
| 102 | if (field.IsRepeated) { |
| 103 | throw new ArgumentException("HasField() can only be called on non-repeated fields."); |
| 104 | } |
| 105 | |
| 106 | return fields.ContainsKey(field); |
| 107 | } |
| 108 | |
| 109 | /// <summary> |
| 110 | /// Clears all fields. |
| 111 | /// </summary> |
| 112 | internal void Clear() { |
| 113 | fields.Clear(); |
| 114 | } |
| 115 | |
| 116 | /// <summary> |
| 117 | /// See <see cref="IMessage.Item(FieldDescriptor)"/> |
| 118 | /// </summary> |
| 119 | /// <remarks> |
| 120 | /// If the field is not set, the behaviour when fetching this property varies by field type: |
| 121 | /// <list> |
| 122 | /// <item>For singular message values, null is returned.</item> |
| 123 | /// <item>For singular non-message values, the default value of the field is returned.</item> |
| 124 | /// <item>For repeated values, an empty immutable list is returned. This will be compatible |
| 125 | /// with IList[object], regardless of the type of the repeated item.</item> |
| 126 | /// </list> |
| 127 | /// This method returns null if the field is a singular message type |
| 128 | /// and is not set; in this case it is up to the caller to fetch the |
| 129 | /// message's default instance. For repeated fields of message types, |
| 130 | /// an empty collection is returned. For repeated fields of non-message |
| 131 | /// types, null is returned. |
| 132 | /// <para /> |
| 133 | /// When setting this property, any list values are copied, and each element is checked |
| 134 | /// to ensure it is of an appropriate type. |
| 135 | /// </remarks> |
| 136 | /// |
| 137 | internal object this[FieldDescriptor field] { |
| 138 | get { |
| 139 | object result; |
| 140 | if (fields.TryGetValue(field, out result)) { |
| 141 | return result; |
| 142 | } |
| 143 | if (field.MappedType == MappedType.Message) { |
| 144 | if (field.IsRepeated) { |
| 145 | return new List<object>(); |
| 146 | } else { |
| 147 | return null; |
| 148 | } |
| 149 | } |
| 150 | return field.DefaultValue; |
| 151 | } |
| 152 | set { |
| 153 | if (field.IsRepeated) { |
| 154 | List<object> list = value as List<object>; |
| 155 | if (list == null) { |
| 156 | throw new ArgumentException("Wrong object type used with protocol message reflection."); |
| 157 | } |
| 158 | |
| 159 | // Wrap the contents in a new list so that the caller cannot change |
| 160 | // the list's contents after setting it. |
| 161 | List<object> newList = new List<object>(list); |
| 162 | foreach (object element in newList) { |
| 163 | VerifyType(field, element); |
| 164 | } |
| 165 | value = newList; |
| 166 | } |
| 167 | else { |
| 168 | VerifyType(field, value); |
| 169 | } |
| 170 | fields[field] = value; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /// <summary> |
| 175 | /// See <see cref="IMessage.Item(FieldDescriptor,int)" /> |
| 176 | /// </summary> |
| 177 | internal object this[FieldDescriptor field, int index] { |
| 178 | get { |
| 179 | if (!field.IsRepeated) { |
| 180 | throw new ArgumentException("Indexer specifying field and index can only be called on repeated fields."); |
| 181 | } |
| 182 | |
| 183 | return ((IList<object>) this[field])[index]; |
| 184 | } |
| 185 | set { |
| 186 | if (!field.IsRepeated) { |
| 187 | throw new ArgumentException("Indexer specifying field and index can only be called on repeated fields."); |
| 188 | } |
| 189 | VerifyType(field, value); |
| 190 | object list; |
| 191 | if (!fields.TryGetValue(field, out list)) { |
| 192 | throw new ArgumentOutOfRangeException(); |
| 193 | } |
| 194 | ((IList<object>) list)[index] = value; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /// <summary> |
| 199 | /// See <see cref="IBuilder{TMessage, TBuilder}.AddRepeatedField" /> |
| 200 | /// </summary> |
| 201 | internal void AddRepeatedField(FieldDescriptor field, object value) { |
| 202 | if (!field.IsRepeated) { |
| 203 | throw new ArgumentException("AddRepeatedField can only be called on repeated fields."); |
| 204 | } |
| 205 | VerifyType(field, value); |
| 206 | object list; |
| 207 | if (!fields.TryGetValue(field, out list)) { |
| 208 | list = new List<object>(); |
| 209 | fields[field] = list; |
| 210 | } |
| 211 | ((IList<object>) list).Add(value); |
| 212 | } |
| 213 | |
| 214 | /// <summary> |
| 215 | /// Returns an enumerator for the field map. Used to write the fields out. |
| 216 | /// </summary> |
| 217 | internal IEnumerator<KeyValuePair<FieldDescriptor, object>> GetEnumerator() { |
| 218 | return fields.GetEnumerator(); |
| 219 | } |
| 220 | |
| 221 | /// <summary> |
| 222 | /// See <see cref="IMessage.IsInitialized" /> |
| 223 | /// </summary> |
| 224 | /// <remarks> |
| 225 | /// Since FieldSet itself does not have any way of knowing about |
| 226 | /// required fields that aren't actually present in the set, it is up |
| 227 | /// to the caller to check for genuinely required fields. This property |
| 228 | /// merely checks that any messages present are themselves initialized. |
| 229 | /// </remarks> |
| 230 | internal bool IsInitialized { |
| 231 | get { |
| 232 | foreach (KeyValuePair<FieldDescriptor, object> entry in fields) { |
| 233 | FieldDescriptor field = entry.Key; |
| 234 | if (field.MappedType == MappedType.Message) { |
| 235 | if (field.IsRepeated) { |
| 236 | foreach(IMessage message in (IEnumerable) entry.Value) { |
| 237 | if (!message.IsInitialized) { |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | } else { |
| 242 | if (!((IMessage) entry.Value).IsInitialized) { |
| 243 | return false; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /// <summary> |
| 253 | /// Verifies whether all the required fields in the specified message |
| 254 | /// descriptor are present in this field set, as well as whether |
| 255 | /// all the embedded messages are themselves initialized. |
| 256 | /// </summary> |
| 257 | internal bool IsInitializedWithRespectTo(MessageDescriptor type) { |
| 258 | foreach (FieldDescriptor field in type.Fields) { |
| 259 | if (field.IsRequired && !HasField(field)) { |
| 260 | return false; |
| 261 | } |
| 262 | } |
| 263 | return IsInitialized; |
| 264 | } |
| 265 | |
| 266 | /// <summary> |
| 267 | /// See <see cref="IBuilder{TMessage, TBuilder}.ClearField" /> |
| 268 | /// </summary> |
| 269 | public void ClearField(FieldDescriptor field) { |
| 270 | fields.Remove(field); |
| 271 | } |
| 272 | |
| 273 | /// <summary> |
| 274 | /// See <see cref="IMessage.GetRepeatedFieldCount" /> |
| 275 | /// </summary> |
| 276 | public int GetRepeatedFieldCount(FieldDescriptor field) { |
| 277 | if (!field.IsRepeated) { |
| 278 | throw new ArgumentException("GetRepeatedFieldCount() can only be called on repeated fields."); |
| 279 | } |
| 280 | |
| 281 | return ((IList<object>) this[field]).Count; |
| 282 | } |
| 283 | |
| 284 | /// <summary> |
| 285 | /// Implementation of both <c>MergeFrom</c> methods. |
| 286 | /// </summary> |
| 287 | /// <param name="otherFields"></param> |
| 288 | private void MergeFields(IEnumerable<KeyValuePair<FieldDescriptor, object>> otherFields) { |
| 289 | // Note: We don't attempt to verify that other's fields have valid |
| 290 | // types. Doing so would be a losing battle. We'd have to verify |
| 291 | // all sub-messages as well, and we'd have to make copies of all of |
| 292 | // them to insure that they don't change after verification (since |
| 293 | // the IMessage interface itself cannot enforce immutability of |
| 294 | // implementations). |
| 295 | // TODO(jonskeet): Provide a function somewhere called MakeDeepCopy() |
| 296 | // which allows people to make secure deep copies of messages. |
| 297 | |
| 298 | foreach (KeyValuePair<FieldDescriptor, object> entry in otherFields) { |
| 299 | FieldDescriptor field = entry.Key; |
| 300 | object existingValue; |
| 301 | fields.TryGetValue(field, out existingValue); |
| 302 | if (field.IsRepeated) { |
| 303 | if (existingValue == null) { |
| 304 | existingValue = new List<object>(); |
| 305 | fields[field] = existingValue; |
| 306 | } |
| 307 | IList<object> list = (IList<object>) existingValue; |
| 308 | foreach (object otherValue in (IEnumerable) entry.Value) { |
| 309 | list.Add(otherValue); |
| 310 | } |
| 311 | } else if (field.MappedType == MappedType.Message && existingValue != null) { |
| 312 | IMessage existingMessage = (IMessage)existingValue; |
| 313 | IMessage merged = existingMessage.WeakCreateBuilderForType() |
| 314 | .WeakMergeFrom(existingMessage) |
| 315 | .WeakMergeFrom((IMessage) entry.Value) |
| 316 | .WeakBuild(); |
| 317 | this[field] = merged; |
| 318 | } else { |
| 319 | this[field] = entry.Value; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /// <summary> |
| 325 | /// See <see cref="IBuilder{TMessage, TBuilder}.MergeFrom(IMessage)" /> |
| 326 | /// </summary> |
| 327 | public void MergeFrom(IMessage other) { |
| 328 | MergeFields(other.AllFields); |
| 329 | } |
| 330 | |
| 331 | /// <summary> |
| 332 | /// Like <see cref="MergeFrom(IMessage)"/>, but merges from another <c>FieldSet</c>. |
| 333 | /// </summary> |
| 334 | public void MergeFrom(FieldSet other) { |
| 335 | MergeFields(other.fields); |
| 336 | } |
| 337 | |
| 338 | /// <summary> |
| 339 | /// See <see cref="IMessage.WriteTo(CodedOutputStream)" />. |
| 340 | /// </summary> |
| 341 | public void WriteTo(CodedOutputStream output) { |
| 342 | foreach (KeyValuePair<FieldDescriptor, object> entry in fields) { |
| 343 | WriteField(entry.Key, entry.Value, output); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /// <summary> |
| 348 | /// Writes a single field to a CodedOutputStream. |
| 349 | /// </summary> |
| 350 | public void WriteField(FieldDescriptor field, Object value, CodedOutputStream output) { |
| 351 | if (field.IsExtension && field.ContainingType.Options.MessageSetWireFormat) { |
| 352 | output.WriteMessageSetExtension(field.FieldNumber, (IMessage) value); |
| 353 | } else { |
| 354 | if (field.IsRepeated) { |
| 355 | foreach (object element in (IEnumerable) value) { |
| 356 | output.WriteField(field.FieldType, field.FieldNumber, element); |
| 357 | } |
| 358 | } else { |
| 359 | output.WriteField(field.FieldType, field.FieldNumber, value); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /// <summary> |
| 365 | /// See <see cref="IMessage.SerializedSize" />. It's up to the caller to |
| 366 | /// cache the resulting size if desired. |
| 367 | /// </summary> |
| 368 | public int SerializedSize { |
| 369 | get { |
| 370 | int size = 0; |
| 371 | foreach (KeyValuePair<FieldDescriptor, object> entry in fields) { |
| 372 | FieldDescriptor field = entry.Key; |
| 373 | object value = entry.Value; |
| 374 | |
| 375 | if (field.IsExtension && field.ContainingType.Options.MessageSetWireFormat) { |
| 376 | size += CodedOutputStream.ComputeMessageSetExtensionSize(field.FieldNumber, (IMessage) value); |
| 377 | } else { |
| 378 | if (field.IsRepeated) { |
| 379 | foreach (object element in (IEnumerable) value) { |
| 380 | size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element); |
| 381 | } |
| 382 | } else { |
| 383 | size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, value); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | return size; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /// <summary> |
| 392 | /// Verifies that the given object is of the correct type to be a valid |
| 393 | /// value for the given field. |
| 394 | /// </summary> |
| 395 | /// <remarks> |
| 396 | /// For repeated fields, this checks if the object is of the right |
| 397 | /// element type, not whether it's a list. |
| 398 | /// </remarks> |
| 399 | /// <exception cref="ArgumentException">The value is not of the right type.</exception> |
| 400 | private static void VerifyType(FieldDescriptor field, object value) { |
| 401 | bool isValid = false; |
| 402 | switch (field.MappedType) { |
| 403 | case MappedType.Int32: isValid = value is int; break; |
| 404 | case MappedType.Int64: isValid = value is long; break; |
| 405 | case MappedType.UInt32: isValid = value is uint; break; |
| 406 | case MappedType.UInt64: isValid = value is ulong; break; |
| 407 | case MappedType.Single: isValid = value is float; break; |
| 408 | case MappedType.Double: isValid = value is double; break; |
| 409 | case MappedType.Boolean: isValid = value is bool; break; |
| 410 | case MappedType.String: isValid = value is string; break; |
| 411 | case MappedType.ByteString: isValid = value is ByteString; break; |
| 412 | case MappedType.Enum: |
| 413 | EnumValueDescriptor enumValue = value as EnumValueDescriptor; |
| 414 | isValid = enumValue != null && enumValue.EnumDescriptor == field.EnumType; |
| 415 | break; |
| 416 | case MappedType.Message: |
| 417 | IMessage messageValue = value as IMessage; |
| 418 | isValid = messageValue != null && messageValue.DescriptorForType == field.MessageType; |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | if (!isValid) { |
| 423 | // When chaining calls to SetField(), it can be hard to tell from |
| 424 | // the stack trace which exact call failed, since the whole chain is |
| 425 | // considered one line of code. So, let's make sure to include the |
| 426 | // field name and other useful info in the exception. |
| 427 | throw new ArgumentException("Wrong object type used with protocol message reflection. " |
| 428 | + "Message type \"" + field.ContainingType.FullName |
| 429 | + "\", field \"" + (field.IsExtension ? field.FullName : field.Name) |
| 430 | + "\", value was type \"" + value.GetType().Name + "\"."); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | } |