Jon Skeet | 0aac0e4 | 2009-09-09 18:48:02 +0100 | [diff] [blame] | 1 | #region Copyright notice and license |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 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: |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 6 | // http://code.google.com/p/protobuf/ |
| 7 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions are |
| 10 | // met: |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 11 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 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. |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 21 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 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. |
Jon Skeet | 0aac0e4 | 2009-09-09 18:48:02 +0100 | [diff] [blame] | 33 | #endregion |
| 34 | |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 35 | using System; |
| 36 | using System.Collections; |
| 37 | using System.Collections.Generic; |
| 38 | using System.Text; |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 39 | #if !LITE |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 40 | using Google.ProtocolBuffers.Collections; |
| 41 | using Google.ProtocolBuffers.Descriptors; |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 42 | #endif |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 43 | |
| 44 | namespace Google.ProtocolBuffers { |
| 45 | /// <summary> |
| 46 | /// TODO(jonskeet): Write summary text. |
| 47 | /// </summary> |
| 48 | public sealed class UninitializedMessageException : Exception { |
| 49 | |
| 50 | private readonly IList<string> missingFields; |
| 51 | |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 52 | private UninitializedMessageException(IList<string> missingFields) |
| 53 | : base(BuildDescription(missingFields)) { |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 54 | this.missingFields = new List<string>(missingFields); |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Jon Skeet | cb8644d | 2009-06-17 16:09:22 +0100 | [diff] [blame] | 57 | |
| 58 | /// <summary> |
| 59 | /// Returns a read-only list of human-readable names of |
| 60 | /// required fields missing from this message. Each name |
| 61 | /// is a full path to a field, e.g. "foo.bar[5].baz" |
| 62 | /// </summary> |
| 63 | public IList<string> MissingFields { |
| 64 | get { return missingFields; } |
| 65 | } |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 66 | |
| 67 | /// <summary> |
| 68 | /// Converts this exception into an InvalidProtocolBufferException. |
| 69 | /// When a parsed message is missing required fields, this should be thrown |
| 70 | /// instead of UninitializedMessageException. |
| 71 | /// </summary> |
| 72 | public InvalidProtocolBufferException AsInvalidProtocolBufferException() { |
| 73 | return new InvalidProtocolBufferException(Message); |
| 74 | } |
| 75 | |
| 76 | /// <summary> |
| 77 | /// Constructs the description string for a given list of missing fields. |
| 78 | /// </summary> |
| 79 | private static string BuildDescription(IEnumerable<string> missingFields) { |
| 80 | StringBuilder description = new StringBuilder("Message missing required fields: "); |
| 81 | bool first = true; |
| 82 | foreach(string field in missingFields) { |
| 83 | if (first) { |
| 84 | first = false; |
| 85 | } else { |
| 86 | description.Append(", "); |
| 87 | } |
| 88 | description.Append(field); |
| 89 | } |
| 90 | return description.ToString(); |
| 91 | } |
| 92 | |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 93 | #if !LITE |
| 94 | public UninitializedMessageException(IMessage message) |
| 95 | : this(FindMissingFields(message)) { |
| 96 | } |
| 97 | |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 98 | /// <summary> |
| 99 | /// Returns a list of the full "paths" of missing required |
| 100 | /// fields in the specified message. |
| 101 | /// </summary> |
| 102 | private static IList<String> FindMissingFields(IMessage message) { |
| 103 | List<String> results = new List<String>(); |
| 104 | FindMissingFields(message, "", results); |
| 105 | return results; |
| 106 | } |
| 107 | |
| 108 | /// <summary> |
| 109 | /// Recursive helper implementing FindMissingFields. |
| 110 | /// </summary> |
| 111 | private static void FindMissingFields(IMessage message, String prefix, List<String> results) { |
| 112 | foreach (FieldDescriptor field in message.DescriptorForType.Fields) { |
| 113 | if (field.IsRequired && !message.HasField(field)) { |
| 114 | results.Add(prefix + field.Name); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | foreach (KeyValuePair<FieldDescriptor, object> entry in message.AllFields) { |
| 119 | FieldDescriptor field = entry.Key; |
| 120 | object value = entry.Value; |
| 121 | |
| 122 | if (field.MappedType == MappedType.Message) { |
| 123 | if (field.IsRepeated) { |
| 124 | int i = 0; |
| 125 | foreach (object element in (IEnumerable) value) { |
| 126 | FindMissingFields((IMessage) element, SubMessagePrefix(prefix, field, i++), results); |
| 127 | } |
| 128 | } else { |
| 129 | if (message.HasField(field)) { |
| 130 | FindMissingFields((IMessage) value, SubMessagePrefix(prefix, field, -1), results); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private static String SubMessagePrefix(String prefix, FieldDescriptor field, int index) { |
| 138 | StringBuilder result = new StringBuilder(prefix); |
| 139 | if (field.IsExtension) { |
| 140 | result.Append('(') |
| 141 | .Append(field.FullName) |
| 142 | .Append(')'); |
| 143 | } else { |
| 144 | result.Append(field.Name); |
| 145 | } |
| 146 | if (index != -1) { |
| 147 | result.Append('[') |
| 148 | .Append(index) |
| 149 | .Append(']'); |
| 150 | } |
| 151 | result.Append('.'); |
| 152 | return result.ToString(); |
| 153 | } |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame^] | 154 | #endif |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 155 | } |
| 156 | } |