blob: d98987a16bd43b47756d1754b56f348bf0242843 [file] [log] [blame]
Jon Skeet0aac0e42009-09-09 18:48:02 +01001#region Copyright notice and license
Jon Skeet60c059b2008-10-23 21:17:56 +01002// 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 Skeet68036862008-10-22 13:30:34 +01006// http://code.google.com/p/protobuf/
7//
Jon Skeet60c059b2008-10-23 21:17:56 +01008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
Jon Skeet68036862008-10-22 13:30:34 +010011//
Jon Skeet60c059b2008-10-23 21:17:56 +010012// * 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 Skeet68036862008-10-22 13:30:34 +010021//
Jon Skeet60c059b2008-10-23 21:17:56 +010022// 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 Skeet0aac0e42009-09-09 18:48:02 +010033#endregion
34
Jon Skeet68036862008-10-22 13:30:34 +010035using System;
36using System.Collections;
37using System.Collections.Generic;
38using System.Text;
csharptestd9c59e62010-11-04 19:36:28 -050039#if !LITE
Jon Skeet68036862008-10-22 13:30:34 +010040using Google.ProtocolBuffers.Collections;
41using Google.ProtocolBuffers.Descriptors;
csharptestd9c59e62010-11-04 19:36:28 -050042#endif
Jon Skeet68036862008-10-22 13:30:34 +010043
44namespace 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 Skeet68036862008-10-22 13:30:34 +010052 private UninitializedMessageException(IList<string> missingFields)
53 : base(BuildDescription(missingFields)) {
csharptestd9c59e62010-11-04 19:36:28 -050054 this.missingFields = new List<string>(missingFields);
Jon Skeet68036862008-10-22 13:30:34 +010055 }
56
Jon Skeetcb8644d2009-06-17 16:09:22 +010057
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 Skeet68036862008-10-22 13:30:34 +010066
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
csharptestd9c59e62010-11-04 19:36:28 -050093#if !LITE
94 public UninitializedMessageException(IMessage message)
95 : this(FindMissingFields(message)) {
96 }
97
Jon Skeet68036862008-10-22 13:30:34 +010098 /// <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 }
csharptestd9c59e62010-11-04 19:36:28 -0500154#endif
Jon Skeet68036862008-10-22 13:30:34 +0100155 }
156}