UninitializedMessageException.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. {
  44. /// <summary>
  45. /// TODO(jonskeet): Write summary text.
  46. /// </summary>
  47. public sealed class UninitializedMessageException : Exception
  48. {
  49. private readonly IList<string> missingFields;
  50. private UninitializedMessageException(IList<string> missingFields)
  51. : base(BuildDescription(missingFields))
  52. {
  53. this.missingFields = new List<string>(missingFields);
  54. }
  55. /// <summary>
  56. /// Returns a read-only list of human-readable names of
  57. /// required fields missing from this message. Each name
  58. /// is a full path to a field, e.g. "foo.bar[5].baz"
  59. /// </summary>
  60. public IList<string> MissingFields
  61. {
  62. get { return missingFields; }
  63. }
  64. /// <summary>
  65. /// Converts this exception into an InvalidProtocolBufferException.
  66. /// When a parsed message is missing required fields, this should be thrown
  67. /// instead of UninitializedMessageException.
  68. /// </summary>
  69. public InvalidProtocolBufferException AsInvalidProtocolBufferException()
  70. {
  71. return new InvalidProtocolBufferException(Message);
  72. }
  73. /// <summary>
  74. /// Constructs the description string for a given list of missing fields.
  75. /// </summary>
  76. private static string BuildDescription(IEnumerable<string> missingFields)
  77. {
  78. StringBuilder description = new StringBuilder("Message missing required fields: ");
  79. bool first = true;
  80. foreach (string field in missingFields)
  81. {
  82. if (first)
  83. {
  84. first = false;
  85. }
  86. else
  87. {
  88. description.Append(", ");
  89. }
  90. description.Append(field);
  91. }
  92. return description.ToString();
  93. }
  94. /// <summary>
  95. /// For Lite exceptions that do not known how to enumerate missing fields
  96. /// </summary>
  97. public UninitializedMessageException(IMessageLite message)
  98. : base(String.Format("Message {0} is missing required fields", message.GetType()))
  99. {
  100. missingFields = new List<string>();
  101. }
  102. #if !LITE
  103. public UninitializedMessageException(IMessage message)
  104. : this(FindMissingFields(message))
  105. {
  106. }
  107. /// <summary>
  108. /// Returns a list of the full "paths" of missing required
  109. /// fields in the specified message.
  110. /// </summary>
  111. private static IList<String> FindMissingFields(IMessage message)
  112. {
  113. List<String> results = new List<String>();
  114. FindMissingFields(message, "", results);
  115. return results;
  116. }
  117. /// <summary>
  118. /// Recursive helper implementing FindMissingFields.
  119. /// </summary>
  120. private static void FindMissingFields(IMessage message, String prefix, List<String> results)
  121. {
  122. foreach (FieldDescriptor field in message.DescriptorForType.Fields)
  123. {
  124. if (field.IsRequired && !message.HasField(field))
  125. {
  126. results.Add(prefix + field.Name);
  127. }
  128. }
  129. foreach (KeyValuePair<FieldDescriptor, object> entry in message.AllFields)
  130. {
  131. FieldDescriptor field = entry.Key;
  132. object value = entry.Value;
  133. if (field.MappedType == MappedType.Message)
  134. {
  135. if (field.IsRepeated)
  136. {
  137. int i = 0;
  138. foreach (object element in (IEnumerable) value)
  139. {
  140. if (element is IMessage)
  141. {
  142. FindMissingFields((IMessage) element, SubMessagePrefix(prefix, field, i++), results);
  143. }
  144. else
  145. {
  146. results.Add(prefix + field.Name);
  147. }
  148. }
  149. }
  150. else
  151. {
  152. if (message.HasField(field))
  153. {
  154. if (value is IMessage)
  155. {
  156. FindMissingFields((IMessage) value, SubMessagePrefix(prefix, field, -1), results);
  157. }
  158. else
  159. {
  160. results.Add(prefix + field.Name);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. private static String SubMessagePrefix(String prefix, FieldDescriptor field, int index)
  168. {
  169. StringBuilder result = new StringBuilder(prefix);
  170. if (field.IsExtension)
  171. {
  172. result.Append('(')
  173. .Append(field.FullName)
  174. .Append(')');
  175. }
  176. else
  177. {
  178. result.Append(field.Name);
  179. }
  180. if (index != -1)
  181. {
  182. result.Append('[')
  183. .Append(index)
  184. .Append(']');
  185. }
  186. result.Append('.');
  187. return result.ToString();
  188. }
  189. #endif
  190. }
  191. }