UninitializedMessageException.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using System.Text;
  38. #if !LITE
  39. using Google.ProtocolBuffers.Collections;
  40. using Google.ProtocolBuffers.Descriptors;
  41. #endif
  42. namespace Google.ProtocolBuffers {
  43. /// <summary>
  44. /// TODO(jonskeet): Write summary text.
  45. /// </summary>
  46. public sealed class UninitializedMessageException : Exception {
  47. private readonly IList<string> missingFields;
  48. private UninitializedMessageException(IList<string> missingFields)
  49. : base(BuildDescription(missingFields)) {
  50. this.missingFields = new List<string>(missingFields);
  51. }
  52. /// <summary>
  53. /// Returns a read-only list of human-readable names of
  54. /// required fields missing from this message. Each name
  55. /// is a full path to a field, e.g. "foo.bar[5].baz"
  56. /// </summary>
  57. public IList<string> MissingFields {
  58. get { return missingFields; }
  59. }
  60. /// <summary>
  61. /// Converts this exception into an InvalidProtocolBufferException.
  62. /// When a parsed message is missing required fields, this should be thrown
  63. /// instead of UninitializedMessageException.
  64. /// </summary>
  65. public InvalidProtocolBufferException AsInvalidProtocolBufferException() {
  66. return new InvalidProtocolBufferException(Message);
  67. }
  68. /// <summary>
  69. /// Constructs the description string for a given list of missing fields.
  70. /// </summary>
  71. private static string BuildDescription(IEnumerable<string> missingFields) {
  72. StringBuilder description = new StringBuilder("Message missing required fields: ");
  73. bool first = true;
  74. foreach(string field in missingFields) {
  75. if (first) {
  76. first = false;
  77. } else {
  78. description.Append(", ");
  79. }
  80. description.Append(field);
  81. }
  82. return description.ToString();
  83. }
  84. #if !LITE
  85. public UninitializedMessageException(IMessage message)
  86. : this(FindMissingFields(message)) {
  87. }
  88. /// <summary>
  89. /// Returns a list of the full "paths" of missing required
  90. /// fields in the specified message.
  91. /// </summary>
  92. private static IList<String> FindMissingFields(IMessage message) {
  93. List<String> results = new List<String>();
  94. FindMissingFields(message, "", results);
  95. return results;
  96. }
  97. /// <summary>
  98. /// Recursive helper implementing FindMissingFields.
  99. /// </summary>
  100. private static void FindMissingFields(IMessage message, String prefix, List<String> results) {
  101. foreach (FieldDescriptor field in message.DescriptorForType.Fields) {
  102. if (field.IsRequired && !message.HasField(field)) {
  103. results.Add(prefix + field.Name);
  104. }
  105. }
  106. foreach (KeyValuePair<FieldDescriptor, object> entry in message.AllFields) {
  107. FieldDescriptor field = entry.Key;
  108. object value = entry.Value;
  109. if (field.MappedType == MappedType.Message) {
  110. if (field.IsRepeated) {
  111. int i = 0;
  112. foreach (object element in (IEnumerable) value) {
  113. FindMissingFields((IMessage) element, SubMessagePrefix(prefix, field, i++), results);
  114. }
  115. } else {
  116. if (message.HasField(field)) {
  117. FindMissingFields((IMessage) value, SubMessagePrefix(prefix, field, -1), results);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. private static String SubMessagePrefix(String prefix, FieldDescriptor field, int index) {
  124. StringBuilder result = new StringBuilder(prefix);
  125. if (field.IsExtension) {
  126. result.Append('(')
  127. .Append(field.FullName)
  128. .Append(')');
  129. } else {
  130. result.Append(field.Name);
  131. }
  132. if (index != -1) {
  133. result.Append('[')
  134. .Append(index)
  135. .Append(']');
  136. }
  137. result.Append('.');
  138. return result.ToString();
  139. }
  140. #endif
  141. }
  142. }