| OLD | NEW |
| (Empty) | |
| 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ |
| 4 // |
| 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are |
| 7 // met: |
| 8 // |
| 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the |
| 14 // distribution. |
| 15 // * Neither the name of Google Inc. nor the names of its |
| 16 // contributors may be used to endorse or promote products derived from |
| 17 // this software without specific prior written permission. |
| 18 // |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
| 31 // Author: jschorr@google.com (Joseph Schorr) |
| 32 // Based on original Protocol Buffers design by |
| 33 // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 // |
| 35 // This file defines static methods and classes for comparing Protocol |
| 36 // Messages (see //google/protobuf/util/message_differencer.h for more |
| 37 // information). |
| 38 |
| 39 #include <google/protobuf/util/message_differencer.h> |
| 40 |
| 41 #include <algorithm> |
| 42 #include <memory> |
| 43 #ifndef _SHARED_PTR_H |
| 44 #include <google/protobuf/stubs/shared_ptr.h> |
| 45 #endif |
| 46 #include <utility> |
| 47 |
| 48 #include <google/protobuf/stubs/common.h> |
| 49 #include <google/protobuf/stubs/stringprintf.h> |
| 50 #include <google/protobuf/any.h> |
| 51 #include <google/protobuf/io/printer.h> |
| 52 #include <google/protobuf/io/zero_copy_stream.h> |
| 53 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 54 #include <google/protobuf/dynamic_message.h> |
| 55 #include <google/protobuf/text_format.h> |
| 56 #include <google/protobuf/util/field_comparator.h> |
| 57 #include <google/protobuf/stubs/strutil.h> |
| 58 |
| 59 namespace google { |
| 60 namespace protobuf { |
| 61 |
| 62 namespace util { |
| 63 |
| 64 // When comparing a repeated field as map, MultipleFieldMapKeyComparator can |
| 65 // be used to specify multiple fields as key for key comparison. |
| 66 // Two elements of a repeated field will be regarded as having the same key |
| 67 // iff they have the same value for every specified key field. |
| 68 // Note that you can also specify only one field as key. |
| 69 class MessageDifferencer::MultipleFieldsMapKeyComparator |
| 70 : public MessageDifferencer::MapKeyComparator { |
| 71 public: |
| 72 MultipleFieldsMapKeyComparator( |
| 73 MessageDifferencer* message_differencer, |
| 74 const vector<vector<const FieldDescriptor*> >& key_field_paths) |
| 75 : message_differencer_(message_differencer), |
| 76 key_field_paths_(key_field_paths) { |
| 77 GOOGLE_CHECK(!key_field_paths_.empty()); |
| 78 for (int i = 0; i < key_field_paths_.size(); ++i) { |
| 79 GOOGLE_CHECK(!key_field_paths_[i].empty()); |
| 80 } |
| 81 } |
| 82 MultipleFieldsMapKeyComparator( |
| 83 MessageDifferencer* message_differencer, |
| 84 const FieldDescriptor* key) |
| 85 : message_differencer_(message_differencer) { |
| 86 vector<const FieldDescriptor*> key_field_path; |
| 87 key_field_path.push_back(key); |
| 88 key_field_paths_.push_back(key_field_path); |
| 89 } |
| 90 virtual bool IsMatch( |
| 91 const Message& message1, |
| 92 const Message& message2, |
| 93 const vector<SpecificField>& parent_fields) const { |
| 94 for (int i = 0; i < key_field_paths_.size(); ++i) { |
| 95 if (!IsMatchInternal(message1, message2, parent_fields, |
| 96 key_field_paths_[i], 0)) { |
| 97 return false; |
| 98 } |
| 99 } |
| 100 return true; |
| 101 } |
| 102 private: |
| 103 bool IsMatchInternal( |
| 104 const Message& message1, |
| 105 const Message& message2, |
| 106 const vector<SpecificField>& parent_fields, |
| 107 const vector<const FieldDescriptor*>& key_field_path, |
| 108 int path_index) const { |
| 109 const FieldDescriptor* field = key_field_path[path_index]; |
| 110 vector<SpecificField> current_parent_fields(parent_fields); |
| 111 if (path_index == key_field_path.size() - 1) { |
| 112 if (field->is_repeated()) { |
| 113 if (!message_differencer_->CompareRepeatedField( |
| 114 message1, message2, field, ¤t_parent_fields)) { |
| 115 return false; |
| 116 } |
| 117 } else { |
| 118 if (!message_differencer_->CompareFieldValueUsingParentFields( |
| 119 message1, message2, field, -1, -1, ¤t_parent_fields)) { |
| 120 return false; |
| 121 } |
| 122 } |
| 123 return true; |
| 124 } else { |
| 125 const Reflection* reflection1 = message1.GetReflection(); |
| 126 const Reflection* reflection2 = message2.GetReflection(); |
| 127 bool has_field1 = reflection1->HasField(message1, field); |
| 128 bool has_field2 = reflection2->HasField(message2, field); |
| 129 if (!has_field1 && !has_field2) { |
| 130 return true; |
| 131 } |
| 132 if (has_field1 != has_field2) { |
| 133 return false; |
| 134 } |
| 135 SpecificField specific_field; |
| 136 specific_field.field = field; |
| 137 current_parent_fields.push_back(specific_field); |
| 138 return IsMatchInternal( |
| 139 reflection1->GetMessage(message1, field), |
| 140 reflection2->GetMessage(message2, field), |
| 141 current_parent_fields, |
| 142 key_field_path, |
| 143 path_index + 1); |
| 144 } |
| 145 } |
| 146 MessageDifferencer* message_differencer_; |
| 147 vector<vector<const FieldDescriptor*> > key_field_paths_; |
| 148 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MultipleFieldsMapKeyComparator); |
| 149 }; |
| 150 |
| 151 bool MessageDifferencer::Equals(const Message& message1, |
| 152 const Message& message2) { |
| 153 MessageDifferencer differencer; |
| 154 |
| 155 return differencer.Compare(message1, message2); |
| 156 } |
| 157 |
| 158 bool MessageDifferencer::Equivalent(const Message& message1, |
| 159 const Message& message2) { |
| 160 MessageDifferencer differencer; |
| 161 differencer.set_message_field_comparison(MessageDifferencer::EQUIVALENT); |
| 162 |
| 163 return differencer.Compare(message1, message2); |
| 164 } |
| 165 |
| 166 bool MessageDifferencer::ApproximatelyEquals(const Message& message1, |
| 167 const Message& message2) { |
| 168 MessageDifferencer differencer; |
| 169 differencer.set_float_comparison( |
| 170 MessageDifferencer::APPROXIMATE); |
| 171 |
| 172 return differencer.Compare(message1, message2); |
| 173 } |
| 174 |
| 175 bool MessageDifferencer::ApproximatelyEquivalent(const Message& message1, |
| 176 const Message& message2) { |
| 177 MessageDifferencer differencer; |
| 178 differencer.set_message_field_comparison(MessageDifferencer::EQUIVALENT); |
| 179 differencer.set_float_comparison(MessageDifferencer::APPROXIMATE); |
| 180 |
| 181 return differencer.Compare(message1, message2); |
| 182 } |
| 183 |
| 184 // =========================================================================== |
| 185 |
| 186 MessageDifferencer::MessageDifferencer() |
| 187 : reporter_(NULL), |
| 188 field_comparator_(NULL), |
| 189 message_field_comparison_(EQUAL), |
| 190 scope_(FULL), |
| 191 repeated_field_comparison_(AS_LIST), |
| 192 report_matches_(false), |
| 193 output_string_(NULL) { } |
| 194 |
| 195 MessageDifferencer::~MessageDifferencer() { |
| 196 for (int i = 0; i < owned_key_comparators_.size(); ++i) { |
| 197 delete owned_key_comparators_[i]; |
| 198 } |
| 199 for (int i = 0; i < ignore_criteria_.size(); ++i) { |
| 200 delete ignore_criteria_[i]; |
| 201 } |
| 202 } |
| 203 |
| 204 void MessageDifferencer::set_field_comparator(FieldComparator* comparator) { |
| 205 GOOGLE_CHECK(comparator) << "Field comparator can't be NULL."; |
| 206 field_comparator_ = comparator; |
| 207 } |
| 208 |
| 209 void MessageDifferencer::set_message_field_comparison( |
| 210 MessageFieldComparison comparison) { |
| 211 message_field_comparison_ = comparison; |
| 212 } |
| 213 |
| 214 void MessageDifferencer::set_scope(Scope scope) { |
| 215 scope_ = scope; |
| 216 } |
| 217 |
| 218 MessageDifferencer::Scope MessageDifferencer::scope() { |
| 219 return scope_; |
| 220 } |
| 221 |
| 222 void MessageDifferencer::set_float_comparison(FloatComparison comparison) { |
| 223 default_field_comparator_.set_float_comparison( |
| 224 comparison == EXACT ? |
| 225 DefaultFieldComparator::EXACT : DefaultFieldComparator::APPROXIMATE); |
| 226 } |
| 227 |
| 228 void MessageDifferencer::set_repeated_field_comparison( |
| 229 RepeatedFieldComparison comparison) { |
| 230 repeated_field_comparison_ = comparison; |
| 231 } |
| 232 |
| 233 void MessageDifferencer::TreatAsSet(const FieldDescriptor* field) { |
| 234 GOOGLE_CHECK(field->is_repeated()) << "Field must be repeated: " |
| 235 << field->full_name(); |
| 236 const MapKeyComparator* key_comparator = GetMapKeyComparator(field); |
| 237 GOOGLE_CHECK(key_comparator == NULL) |
| 238 << "Cannot treat this repeated field as both Map and Set for" |
| 239 << " comparison. Field name is: " << field->full_name(); |
| 240 set_fields_.insert(field); |
| 241 } |
| 242 |
| 243 void MessageDifferencer::TreatAsMap(const FieldDescriptor* field, |
| 244 const FieldDescriptor* key) { |
| 245 GOOGLE_CHECK(field->is_repeated()) << "Field must be repeated: " |
| 246 << field->full_name(); |
| 247 GOOGLE_CHECK_EQ(FieldDescriptor::CPPTYPE_MESSAGE, field->cpp_type()) |
| 248 << "Field has to be message type. Field name is: " |
| 249 << field->full_name(); |
| 250 GOOGLE_CHECK(key->containing_type() == field->message_type()) |
| 251 << key->full_name() |
| 252 << " must be a direct subfield within the repeated field " |
| 253 << field->full_name() << ", not " << key->containing_type()->full_name(); |
| 254 GOOGLE_CHECK(set_fields_.find(field) == set_fields_.end()) |
| 255 << "Cannot treat this repeated field as both Map and Set for " |
| 256 << "comparison."; |
| 257 MapKeyComparator* key_comparator = |
| 258 new MultipleFieldsMapKeyComparator(this, key); |
| 259 owned_key_comparators_.push_back(key_comparator); |
| 260 map_field_key_comparator_[field] = key_comparator; |
| 261 } |
| 262 |
| 263 void MessageDifferencer::TreatAsMapWithMultipleFieldsAsKey( |
| 264 const FieldDescriptor* field, |
| 265 const vector<const FieldDescriptor*>& key_fields) { |
| 266 vector<vector<const FieldDescriptor*> > key_field_paths; |
| 267 for (int i = 0; i < key_fields.size(); ++i) { |
| 268 vector<const FieldDescriptor*> key_field_path; |
| 269 key_field_path.push_back(key_fields[i]); |
| 270 key_field_paths.push_back(key_field_path); |
| 271 } |
| 272 TreatAsMapWithMultipleFieldPathsAsKey(field, key_field_paths); |
| 273 } |
| 274 |
| 275 void MessageDifferencer::TreatAsMapWithMultipleFieldPathsAsKey( |
| 276 const FieldDescriptor* field, |
| 277 const vector<vector<const FieldDescriptor*> >& key_field_paths) { |
| 278 GOOGLE_CHECK(field->is_repeated()) << "Field must be repeated: " |
| 279 << field->full_name(); |
| 280 GOOGLE_CHECK_EQ(FieldDescriptor::CPPTYPE_MESSAGE, field->cpp_type()) |
| 281 << "Field has to be message type. Field name is: " |
| 282 << field->full_name(); |
| 283 for (int i = 0; i < key_field_paths.size(); ++i) { |
| 284 const vector<const FieldDescriptor*>& key_field_path = key_field_paths[i]; |
| 285 for (int j = 0; j < key_field_path.size(); ++j) { |
| 286 const FieldDescriptor* parent_field = |
| 287 j == 0 ? field : key_field_path[j - 1]; |
| 288 const FieldDescriptor* child_field = key_field_path[j]; |
| 289 GOOGLE_CHECK(child_field->containing_type() == parent_field->message_type(
)) |
| 290 << child_field->full_name() |
| 291 << " must be a direct subfield within the field: " |
| 292 << parent_field->full_name(); |
| 293 if (j != 0) { |
| 294 GOOGLE_CHECK_EQ(FieldDescriptor::CPPTYPE_MESSAGE, parent_field->cpp_type
()) |
| 295 << parent_field->full_name() << " has to be of type message."; |
| 296 GOOGLE_CHECK(!parent_field->is_repeated()) |
| 297 << parent_field->full_name() << " cannot be a repeated field."; |
| 298 } |
| 299 } |
| 300 } |
| 301 GOOGLE_CHECK(set_fields_.find(field) == set_fields_.end()) |
| 302 << "Cannot treat this repeated field as both Map and Set for " |
| 303 << "comparison."; |
| 304 MapKeyComparator* key_comparator = |
| 305 new MultipleFieldsMapKeyComparator(this, key_field_paths); |
| 306 owned_key_comparators_.push_back(key_comparator); |
| 307 map_field_key_comparator_[field] = key_comparator; |
| 308 } |
| 309 |
| 310 void MessageDifferencer::TreatAsMapUsingKeyComparator( |
| 311 const FieldDescriptor* field, |
| 312 const MapKeyComparator* key_comparator) { |
| 313 GOOGLE_CHECK(field->is_repeated()) << "Field must be repeated: " |
| 314 << field->full_name(); |
| 315 GOOGLE_CHECK_EQ(FieldDescriptor::CPPTYPE_MESSAGE, field->cpp_type()) |
| 316 << "Field has to be message type. Field name is: " |
| 317 << field->full_name(); |
| 318 GOOGLE_CHECK(set_fields_.find(field) == set_fields_.end()) |
| 319 << "Cannot treat this repeated field as both Map and Set for " |
| 320 << "comparison."; |
| 321 map_field_key_comparator_[field] = key_comparator; |
| 322 } |
| 323 |
| 324 void MessageDifferencer::AddIgnoreCriteria(IgnoreCriteria* ignore_criteria) { |
| 325 ignore_criteria_.push_back(ignore_criteria); |
| 326 } |
| 327 |
| 328 void MessageDifferencer::IgnoreField(const FieldDescriptor* field) { |
| 329 ignored_fields_.insert(field); |
| 330 } |
| 331 |
| 332 void MessageDifferencer::SetFractionAndMargin(const FieldDescriptor* field, |
| 333 double fraction, double margin) { |
| 334 default_field_comparator_.SetFractionAndMargin(field, fraction, margin); |
| 335 } |
| 336 |
| 337 void MessageDifferencer::ReportDifferencesToString(string* output) { |
| 338 GOOGLE_DCHECK(output) << "Specified output string was NULL"; |
| 339 |
| 340 output_string_ = output; |
| 341 output_string_->clear(); |
| 342 } |
| 343 |
| 344 void MessageDifferencer::ReportDifferencesTo(Reporter* reporter) { |
| 345 // If an output string is set, clear it to prevent |
| 346 // it superceding the specified reporter. |
| 347 if (output_string_) { |
| 348 output_string_ = NULL; |
| 349 } |
| 350 |
| 351 reporter_ = reporter; |
| 352 } |
| 353 |
| 354 bool MessageDifferencer::FieldBefore(const FieldDescriptor* field1, |
| 355 const FieldDescriptor* field2) { |
| 356 // Handle sentinel values (i.e. make sure NULLs are always ordered |
| 357 // at the end of the list). |
| 358 if (field1 == NULL) { |
| 359 return false; |
| 360 } |
| 361 |
| 362 if (field2 == NULL) { |
| 363 return true; |
| 364 } |
| 365 |
| 366 // Always order fields by their tag number |
| 367 return (field1->number() < field2->number()); |
| 368 } |
| 369 |
| 370 bool MessageDifferencer::Compare(const Message& message1, |
| 371 const Message& message2) { |
| 372 vector<SpecificField> parent_fields; |
| 373 |
| 374 bool result = false; |
| 375 |
| 376 // Setup the internal reporter if need be. |
| 377 if (output_string_) { |
| 378 io::StringOutputStream output_stream(output_string_); |
| 379 StreamReporter reporter(&output_stream); |
| 380 reporter_ = &reporter; |
| 381 result = Compare(message1, message2, &parent_fields); |
| 382 reporter_ = NULL; |
| 383 } else { |
| 384 result = Compare(message1, message2, &parent_fields); |
| 385 } |
| 386 |
| 387 return result; |
| 388 } |
| 389 |
| 390 bool MessageDifferencer::CompareWithFields( |
| 391 const Message& message1, |
| 392 const Message& message2, |
| 393 const vector<const FieldDescriptor*>& message1_fields_arg, |
| 394 const vector<const FieldDescriptor*>& message2_fields_arg) { |
| 395 if (message1.GetDescriptor() != message2.GetDescriptor()) { |
| 396 GOOGLE_LOG(DFATAL) << "Comparison between two messages with different " |
| 397 << "descriptors."; |
| 398 return false; |
| 399 } |
| 400 |
| 401 vector<SpecificField> parent_fields; |
| 402 |
| 403 bool result = false; |
| 404 |
| 405 vector<const FieldDescriptor*> message1_fields(message1_fields_arg); |
| 406 vector<const FieldDescriptor*> message2_fields(message2_fields_arg); |
| 407 |
| 408 std::sort(message1_fields.begin(), message1_fields.end(), FieldBefore); |
| 409 std::sort(message2_fields.begin(), message2_fields.end(), FieldBefore); |
| 410 // Append NULL sentinel values. |
| 411 message1_fields.push_back(NULL); |
| 412 message2_fields.push_back(NULL); |
| 413 |
| 414 // Setup the internal reporter if need be. |
| 415 if (output_string_) { |
| 416 io::StringOutputStream output_stream(output_string_); |
| 417 StreamReporter reporter(&output_stream); |
| 418 reporter_ = &reporter; |
| 419 result = CompareRequestedFieldsUsingSettings( |
| 420 message1, message2, message1_fields, message2_fields, &parent_fields); |
| 421 reporter_ = NULL; |
| 422 } else { |
| 423 result = CompareRequestedFieldsUsingSettings( |
| 424 message1, message2, message1_fields, message2_fields, &parent_fields); |
| 425 } |
| 426 |
| 427 return result; |
| 428 } |
| 429 |
| 430 bool MessageDifferencer::Compare( |
| 431 const Message& message1, |
| 432 const Message& message2, |
| 433 vector<SpecificField>* parent_fields) { |
| 434 const Descriptor* descriptor1 = message1.GetDescriptor(); |
| 435 const Descriptor* descriptor2 = message2.GetDescriptor(); |
| 436 if (descriptor1 != descriptor2) { |
| 437 GOOGLE_LOG(DFATAL) << "Comparison between two messages with different " |
| 438 << "descriptors."; |
| 439 return false; |
| 440 } |
| 441 // Expand google.protobuf.Any payload if possible. |
| 442 if (descriptor1->full_name() == internal::kAnyFullTypeName) { |
| 443 google::protobuf::scoped_ptr<Message> data1; |
| 444 google::protobuf::scoped_ptr<Message> data2; |
| 445 if (UnpackAny(message1, &data1) && UnpackAny(message2, &data2)) { |
| 446 return Compare(*data1, *data2, parent_fields); |
| 447 } |
| 448 } |
| 449 const Reflection* reflection1 = message1.GetReflection(); |
| 450 const Reflection* reflection2 = message2.GetReflection(); |
| 451 |
| 452 // Retrieve all the set fields, including extensions. |
| 453 vector<const FieldDescriptor*> message1_fields; |
| 454 vector<const FieldDescriptor*> message2_fields; |
| 455 |
| 456 reflection1->ListFields(message1, &message1_fields); |
| 457 reflection2->ListFields(message2, &message2_fields); |
| 458 |
| 459 // Add sentinel values to deal with the |
| 460 // case where the number of the fields in |
| 461 // each list are different. |
| 462 message1_fields.push_back(NULL); |
| 463 message2_fields.push_back(NULL); |
| 464 |
| 465 bool unknown_compare_result = true; |
| 466 // Ignore unknown fields in EQUIVALENT mode |
| 467 if (message_field_comparison_ != EQUIVALENT) { |
| 468 const google::protobuf::UnknownFieldSet* unknown_field_set1 = |
| 469 &reflection1->GetUnknownFields(message1); |
| 470 const google::protobuf::UnknownFieldSet* unknown_field_set2 = |
| 471 &reflection2->GetUnknownFields(message2); |
| 472 if (!CompareUnknownFields(message1, message2, |
| 473 *unknown_field_set1, *unknown_field_set2, |
| 474 parent_fields)) { |
| 475 if (reporter_ == NULL) { |
| 476 return false; |
| 477 }; |
| 478 unknown_compare_result = false; |
| 479 } |
| 480 } |
| 481 |
| 482 return CompareRequestedFieldsUsingSettings( |
| 483 message1, message2, |
| 484 message1_fields, message2_fields, |
| 485 parent_fields) && unknown_compare_result; |
| 486 } |
| 487 |
| 488 bool MessageDifferencer::CompareRequestedFieldsUsingSettings( |
| 489 const Message& message1, |
| 490 const Message& message2, |
| 491 const vector<const FieldDescriptor*>& message1_fields, |
| 492 const vector<const FieldDescriptor*>& message2_fields, |
| 493 vector<SpecificField>* parent_fields) { |
| 494 if (scope_ == FULL) { |
| 495 if (message_field_comparison_ == EQUIVALENT) { |
| 496 // We need to merge the field lists of both messages (i.e. |
| 497 // we are merely checking for a difference in field values, |
| 498 // rather than the addition or deletion of fields). |
| 499 vector<const FieldDescriptor*> fields_union; |
| 500 CombineFields(message1_fields, FULL, message2_fields, FULL, |
| 501 &fields_union); |
| 502 return CompareWithFieldsInternal(message1, message2, fields_union, |
| 503 fields_union, parent_fields); |
| 504 } else { |
| 505 // Simple equality comparison, use the unaltered field lists. |
| 506 return CompareWithFieldsInternal(message1, message2, message1_fields, |
| 507 message2_fields, parent_fields); |
| 508 } |
| 509 } else { |
| 510 if (message_field_comparison_ == EQUIVALENT) { |
| 511 // We use the list of fields for message1 for both messages when |
| 512 // comparing. This way, extra fields in message2 are ignored, |
| 513 // and missing fields in message2 use their default value. |
| 514 return CompareWithFieldsInternal(message1, message2, message1_fields, |
| 515 message1_fields, parent_fields); |
| 516 } else { |
| 517 // We need to consider the full list of fields for message1 |
| 518 // but only the intersection for message2. This way, any fields |
| 519 // only present in message2 will be ignored, but any fields only |
| 520 // present in message1 will be marked as a difference. |
| 521 vector<const FieldDescriptor*> fields_intersection; |
| 522 CombineFields(message1_fields, PARTIAL, message2_fields, PARTIAL, |
| 523 &fields_intersection); |
| 524 return CompareWithFieldsInternal(message1, message2, message1_fields, |
| 525 fields_intersection, parent_fields); |
| 526 } |
| 527 } |
| 528 } |
| 529 |
| 530 void MessageDifferencer::CombineFields( |
| 531 const vector<const FieldDescriptor*>& fields1, |
| 532 Scope fields1_scope, |
| 533 const vector<const FieldDescriptor*>& fields2, |
| 534 Scope fields2_scope, |
| 535 vector<const FieldDescriptor*>* combined_fields) { |
| 536 |
| 537 int index1 = 0; |
| 538 int index2 = 0; |
| 539 |
| 540 while (index1 < fields1.size() && index2 < fields2.size()) { |
| 541 const FieldDescriptor* field1 = fields1[index1]; |
| 542 const FieldDescriptor* field2 = fields2[index2]; |
| 543 |
| 544 if (FieldBefore(field1, field2)) { |
| 545 if (fields1_scope == FULL) { |
| 546 combined_fields->push_back(fields1[index1]); |
| 547 } |
| 548 ++index1; |
| 549 } else if (FieldBefore(field2, field1)) { |
| 550 if (fields2_scope == FULL) { |
| 551 combined_fields->push_back(fields2[index2]); |
| 552 } |
| 553 ++index2; |
| 554 } else { |
| 555 combined_fields->push_back(fields1[index1]); |
| 556 ++index1; |
| 557 ++index2; |
| 558 } |
| 559 } |
| 560 } |
| 561 |
| 562 bool MessageDifferencer::CompareWithFieldsInternal( |
| 563 const Message& message1, |
| 564 const Message& message2, |
| 565 const vector<const FieldDescriptor*>& message1_fields, |
| 566 const vector<const FieldDescriptor*>& message2_fields, |
| 567 vector<SpecificField>* parent_fields) { |
| 568 bool isDifferent = false; |
| 569 int field_index1 = 0; |
| 570 int field_index2 = 0; |
| 571 |
| 572 const Reflection* reflection1 = message1.GetReflection(); |
| 573 const Reflection* reflection2 = message2.GetReflection(); |
| 574 |
| 575 while (true) { |
| 576 const FieldDescriptor* field1 = message1_fields[field_index1]; |
| 577 const FieldDescriptor* field2 = message2_fields[field_index2]; |
| 578 |
| 579 // Once we have reached sentinel values, we are done the comparison. |
| 580 if (field1 == NULL && field2 == NULL) { |
| 581 break; |
| 582 } |
| 583 |
| 584 // Check for differences in the field itself. |
| 585 if (FieldBefore(field1, field2)) { |
| 586 // Field 1 is not in the field list for message 2. |
| 587 if (IsIgnored(message1, message2, field1, *parent_fields)) { |
| 588 // We are ignoring field1. Report the ignore and move on to |
| 589 // the next field in message1_fields. |
| 590 if (reporter_ != NULL) { |
| 591 SpecificField specific_field; |
| 592 specific_field.field = field1; |
| 593 |
| 594 parent_fields->push_back(specific_field); |
| 595 reporter_->ReportIgnored(message1, message2, *parent_fields); |
| 596 parent_fields->pop_back(); |
| 597 } |
| 598 ++field_index1; |
| 599 continue; |
| 600 } |
| 601 |
| 602 if (reporter_ != NULL) { |
| 603 int count = field1->is_repeated() ? |
| 604 reflection1->FieldSize(message1, field1) : 1; |
| 605 |
| 606 for (int i = 0; i < count; ++i) { |
| 607 SpecificField specific_field; |
| 608 specific_field.field = field1; |
| 609 specific_field.index = field1->is_repeated() ? i : -1; |
| 610 |
| 611 parent_fields->push_back(specific_field); |
| 612 reporter_->ReportDeleted(message1, message2, *parent_fields); |
| 613 parent_fields->pop_back(); |
| 614 } |
| 615 |
| 616 isDifferent = true; |
| 617 } else { |
| 618 return false; |
| 619 } |
| 620 |
| 621 ++field_index1; |
| 622 continue; |
| 623 } else if (FieldBefore(field2, field1)) { |
| 624 // Field 2 is not in the field list for message 1. |
| 625 if (IsIgnored(message1, message2, field2, *parent_fields)) { |
| 626 // We are ignoring field2. Report the ignore and move on to |
| 627 // the next field in message2_fields. |
| 628 if (reporter_ != NULL) { |
| 629 SpecificField specific_field; |
| 630 specific_field.field = field2; |
| 631 |
| 632 parent_fields->push_back(specific_field); |
| 633 reporter_->ReportIgnored(message1, message2, *parent_fields); |
| 634 parent_fields->pop_back(); |
| 635 } |
| 636 ++field_index2; |
| 637 continue; |
| 638 } |
| 639 |
| 640 if (reporter_ != NULL) { |
| 641 int count = field2->is_repeated() ? |
| 642 reflection2->FieldSize(message2, field2) : 1; |
| 643 |
| 644 for (int i = 0; i < count; ++i) { |
| 645 SpecificField specific_field; |
| 646 specific_field.field = field2; |
| 647 specific_field.index = field2->is_repeated() ? i : -1; |
| 648 specific_field.new_index = specific_field.index; |
| 649 |
| 650 parent_fields->push_back(specific_field); |
| 651 reporter_->ReportAdded(message1, message2, *parent_fields); |
| 652 parent_fields->pop_back(); |
| 653 } |
| 654 |
| 655 isDifferent = true; |
| 656 } else { |
| 657 return false; |
| 658 } |
| 659 |
| 660 ++field_index2; |
| 661 continue; |
| 662 } |
| 663 |
| 664 // By this point, field1 and field2 are guarenteed to point to the same |
| 665 // field, so we can now compare the values. |
| 666 if (IsIgnored(message1, message2, field1, *parent_fields)) { |
| 667 // Ignore this field. Report and move on. |
| 668 if (reporter_ != NULL) { |
| 669 SpecificField specific_field; |
| 670 specific_field.field = field1; |
| 671 |
| 672 parent_fields->push_back(specific_field); |
| 673 reporter_->ReportIgnored(message1, message2, *parent_fields); |
| 674 parent_fields->pop_back(); |
| 675 } |
| 676 |
| 677 ++field_index1; |
| 678 ++field_index2; |
| 679 continue; |
| 680 } |
| 681 |
| 682 bool fieldDifferent = false; |
| 683 if (field1->is_repeated()) { |
| 684 fieldDifferent = !CompareRepeatedField(message1, message2, field1, |
| 685 parent_fields); |
| 686 if (fieldDifferent) { |
| 687 if (reporter_ == NULL) return false; |
| 688 isDifferent = true; |
| 689 } |
| 690 } else { |
| 691 fieldDifferent = !CompareFieldValueUsingParentFields( |
| 692 message1, message2, field1, -1, -1, parent_fields); |
| 693 |
| 694 // If we have found differences, either report them or terminate if |
| 695 // no reporter is present. |
| 696 if (fieldDifferent && reporter_ == NULL) { |
| 697 return false; |
| 698 } |
| 699 |
| 700 if (reporter_ != NULL) { |
| 701 SpecificField specific_field; |
| 702 specific_field.field = field1; |
| 703 parent_fields->push_back(specific_field); |
| 704 if (fieldDifferent) { |
| 705 reporter_->ReportModified(message1, message2, *parent_fields); |
| 706 isDifferent = true; |
| 707 } else if (report_matches_) { |
| 708 reporter_->ReportMatched(message1, message2, *parent_fields); |
| 709 } |
| 710 parent_fields->pop_back(); |
| 711 } |
| 712 } |
| 713 // Increment the field indicies. |
| 714 ++field_index1; |
| 715 ++field_index2; |
| 716 } |
| 717 |
| 718 return !isDifferent; |
| 719 } |
| 720 |
| 721 bool MessageDifferencer::IsMatch(const FieldDescriptor* repeated_field, |
| 722 const MapKeyComparator* key_comparator, |
| 723 const Message* message1, |
| 724 const Message* message2, |
| 725 const vector<SpecificField>& parent_fields, |
| 726 int index1, int index2) { |
| 727 vector<SpecificField> current_parent_fields(parent_fields); |
| 728 if (repeated_field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { |
| 729 return CompareFieldValueUsingParentFields( |
| 730 *message1, *message2, repeated_field, index1, index2, |
| 731 ¤t_parent_fields); |
| 732 } |
| 733 // Back up the Reporter and output_string_. They will be reset in the |
| 734 // following code. |
| 735 Reporter* backup_reporter = reporter_; |
| 736 string* output_string = output_string_; |
| 737 reporter_ = NULL; |
| 738 output_string_ = NULL; |
| 739 bool match; |
| 740 |
| 741 if (key_comparator == NULL) { |
| 742 match = CompareFieldValueUsingParentFields( |
| 743 *message1, *message2, repeated_field, index1, index2, |
| 744 ¤t_parent_fields); |
| 745 } else { |
| 746 const Reflection* reflection1 = message1->GetReflection(); |
| 747 const Reflection* reflection2 = message2->GetReflection(); |
| 748 const Message& m1 = |
| 749 reflection1->GetRepeatedMessage(*message1, repeated_field, index1); |
| 750 const Message& m2 = |
| 751 reflection2->GetRepeatedMessage(*message2, repeated_field, index2); |
| 752 SpecificField specific_field; |
| 753 specific_field.field = repeated_field; |
| 754 current_parent_fields.push_back(specific_field); |
| 755 match = key_comparator->IsMatch(m1, m2, current_parent_fields); |
| 756 } |
| 757 |
| 758 reporter_ = backup_reporter; |
| 759 output_string_ = output_string; |
| 760 return match; |
| 761 } |
| 762 |
| 763 bool MessageDifferencer::CompareRepeatedField( |
| 764 const Message& message1, |
| 765 const Message& message2, |
| 766 const FieldDescriptor* repeated_field, |
| 767 vector<SpecificField>* parent_fields) { |
| 768 // the input FieldDescriptor is guaranteed to be repeated field. |
| 769 const Reflection* reflection1 = message1.GetReflection(); |
| 770 const Reflection* reflection2 = message2.GetReflection(); |
| 771 const int count1 = reflection1->FieldSize(message1, repeated_field); |
| 772 const int count2 = reflection2->FieldSize(message2, repeated_field); |
| 773 const bool treated_as_subset = IsTreatedAsSubset(repeated_field); |
| 774 |
| 775 // If the field is not treated as subset and no detailed reports is needed, |
| 776 // we do a quick check on the number of the elements to avoid unnecessary |
| 777 // comparison. |
| 778 if (count1 != count2 && reporter_ == NULL && !treated_as_subset) { |
| 779 return false; |
| 780 } |
| 781 // A match can never be found if message1 has more items than message2. |
| 782 if (count1 > count2 && reporter_ == NULL) { |
| 783 return false; |
| 784 } |
| 785 |
| 786 // These two list are used for store the index of the correspondent |
| 787 // element in peer repeated field. |
| 788 vector<int> match_list1; |
| 789 vector<int> match_list2; |
| 790 |
| 791 // Try to match indices of the repeated fields. Return false if match fails |
| 792 // and there's no detailed report needed. |
| 793 if (!MatchRepeatedFieldIndices(message1, message2, repeated_field, |
| 794 *parent_fields, &match_list1, &match_list2) && |
| 795 reporter_ == NULL) { |
| 796 return false; |
| 797 } |
| 798 |
| 799 bool fieldDifferent = false; |
| 800 SpecificField specific_field; |
| 801 specific_field.field = repeated_field; |
| 802 |
| 803 // At this point, we have already matched pairs of fields (with the reporting |
| 804 // to be done later). Now to check if the paired elements are different. |
| 805 for (int i = 0; i < count1; i++) { |
| 806 if (match_list1[i] == -1) continue; |
| 807 specific_field.index = i; |
| 808 specific_field.new_index = match_list1[i]; |
| 809 |
| 810 const bool result = CompareFieldValueUsingParentFields( |
| 811 message1, message2, repeated_field, i, specific_field.new_index, |
| 812 parent_fields); |
| 813 |
| 814 // If we have found differences, either report them or terminate if |
| 815 // no reporter is present. Note that ReportModified, ReportMoved, and |
| 816 // ReportMatched are all mutually exclusive. |
| 817 if (!result) { |
| 818 if (reporter_ == NULL) return false; |
| 819 parent_fields->push_back(specific_field); |
| 820 reporter_->ReportModified(message1, message2, *parent_fields); |
| 821 parent_fields->pop_back(); |
| 822 fieldDifferent = true; |
| 823 } else if (reporter_ != NULL && |
| 824 specific_field.index != specific_field.new_index) { |
| 825 parent_fields->push_back(specific_field); |
| 826 reporter_->ReportMoved(message1, message2, *parent_fields); |
| 827 parent_fields->pop_back(); |
| 828 } else if (report_matches_ && reporter_ != NULL) { |
| 829 parent_fields->push_back(specific_field); |
| 830 reporter_->ReportMatched(message1, message2, *parent_fields); |
| 831 parent_fields->pop_back(); |
| 832 } |
| 833 } |
| 834 |
| 835 // Report any remaining additions or deletions. |
| 836 for (int i = 0; i < count2; ++i) { |
| 837 if (match_list2[i] != -1) continue; |
| 838 if (!treated_as_subset) { |
| 839 fieldDifferent = true; |
| 840 } |
| 841 |
| 842 if (reporter_ == NULL) continue; |
| 843 specific_field.index = i; |
| 844 specific_field.new_index = i; |
| 845 parent_fields->push_back(specific_field); |
| 846 reporter_->ReportAdded(message1, message2, *parent_fields); |
| 847 parent_fields->pop_back(); |
| 848 } |
| 849 |
| 850 for (int i = 0; i < count1; ++i) { |
| 851 if (match_list1[i] != -1) continue; |
| 852 specific_field.index = i; |
| 853 parent_fields->push_back(specific_field); |
| 854 reporter_->ReportDeleted(message1, message2, *parent_fields); |
| 855 parent_fields->pop_back(); |
| 856 fieldDifferent = true; |
| 857 } |
| 858 return !fieldDifferent; |
| 859 } |
| 860 |
| 861 bool MessageDifferencer::CompareFieldValue(const Message& message1, |
| 862 const Message& message2, |
| 863 const FieldDescriptor* field, |
| 864 int index1, |
| 865 int index2) { |
| 866 return CompareFieldValueUsingParentFields(message1, message2, field, index1, |
| 867 index2, NULL); |
| 868 } |
| 869 |
| 870 bool MessageDifferencer::CompareFieldValueUsingParentFields( |
| 871 const Message& message1, const Message& message2, |
| 872 const FieldDescriptor* field, int index1, int index2, |
| 873 vector<SpecificField>* parent_fields) { |
| 874 FieldContext field_context(parent_fields); |
| 875 FieldComparator::ComparisonResult result = GetFieldComparisonResult( |
| 876 message1, message2, field, index1, index2, &field_context); |
| 877 |
| 878 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && |
| 879 result == FieldComparator::RECURSE) { |
| 880 // Get the nested messages and compare them using one of the Compare |
| 881 // methods. |
| 882 const Reflection* reflection1 = message1.GetReflection(); |
| 883 const Reflection* reflection2 = message2.GetReflection(); |
| 884 const Message& m1 = field->is_repeated() ? |
| 885 reflection1->GetRepeatedMessage(message1, field, index1) : |
| 886 reflection1->GetMessage(message1, field); |
| 887 const Message& m2 = field->is_repeated() ? |
| 888 reflection2->GetRepeatedMessage(message2, field, index2) : |
| 889 reflection2->GetMessage(message2, field); |
| 890 |
| 891 // parent_fields is used in calls to Reporter methods. |
| 892 if (parent_fields != NULL) { |
| 893 // Append currently compared field to the end of parent_fields. |
| 894 SpecificField specific_field; |
| 895 specific_field.field = field; |
| 896 specific_field.index = index1; |
| 897 specific_field.new_index = index2; |
| 898 parent_fields->push_back(specific_field); |
| 899 const bool compare_result = Compare(m1, m2, parent_fields); |
| 900 parent_fields->pop_back(); |
| 901 return compare_result; |
| 902 } else { |
| 903 // Recreates parent_fields as if m1 and m2 had no parents. |
| 904 return Compare(m1, m2); |
| 905 } |
| 906 } else { |
| 907 return (result == FieldComparator::SAME); |
| 908 } |
| 909 } |
| 910 |
| 911 bool MessageDifferencer::CheckPathChanged( |
| 912 const vector<SpecificField>& field_path) { |
| 913 for (int i = 0; i < field_path.size(); ++i) { |
| 914 if (field_path[i].index != field_path[i].new_index) return true; |
| 915 } |
| 916 return false; |
| 917 } |
| 918 |
| 919 bool MessageDifferencer::IsTreatedAsSet(const FieldDescriptor* field) { |
| 920 if (!field->is_repeated()) return false; |
| 921 if (field->is_map()) return true; |
| 922 if (repeated_field_comparison_ == AS_SET) return true; |
| 923 return (set_fields_.find(field) != set_fields_.end()); |
| 924 } |
| 925 |
| 926 bool MessageDifferencer::IsTreatedAsSubset(const FieldDescriptor* field) { |
| 927 return scope_ == PARTIAL && |
| 928 (IsTreatedAsSet(field) || GetMapKeyComparator(field) != NULL); |
| 929 } |
| 930 |
| 931 bool MessageDifferencer::IsIgnored( |
| 932 const Message& message1, |
| 933 const Message& message2, |
| 934 const FieldDescriptor* field, |
| 935 const vector<SpecificField>& parent_fields) { |
| 936 if (ignored_fields_.find(field) != ignored_fields_.end()) { |
| 937 return true; |
| 938 } |
| 939 for (int i = 0; i < ignore_criteria_.size(); ++i) { |
| 940 if (ignore_criteria_[i]->IsIgnored(message1, message2, field, |
| 941 parent_fields)) { |
| 942 return true; |
| 943 } |
| 944 } |
| 945 return false; |
| 946 } |
| 947 |
| 948 const MessageDifferencer::MapKeyComparator* MessageDifferencer |
| 949 ::GetMapKeyComparator(const FieldDescriptor* field) { |
| 950 if (!field->is_repeated()) return NULL; |
| 951 if (map_field_key_comparator_.find(field) != |
| 952 map_field_key_comparator_.end()) { |
| 953 return map_field_key_comparator_[field]; |
| 954 } |
| 955 return NULL; |
| 956 } |
| 957 |
| 958 namespace { |
| 959 |
| 960 typedef pair<int, const UnknownField*> IndexUnknownFieldPair; |
| 961 |
| 962 struct UnknownFieldOrdering { |
| 963 inline bool operator()(const IndexUnknownFieldPair& a, |
| 964 const IndexUnknownFieldPair& b) const { |
| 965 if (a.second->number() < b.second->number()) return true; |
| 966 if (a.second->number() > b.second->number()) return false; |
| 967 return a.second->type() < b.second->type(); |
| 968 } |
| 969 }; |
| 970 |
| 971 } // namespace |
| 972 |
| 973 bool MessageDifferencer::UnpackAny(const Message& any, |
| 974 google::protobuf::scoped_ptr<Message>* data)
{ |
| 975 const Reflection* reflection = any.GetReflection(); |
| 976 const FieldDescriptor* type_url_field; |
| 977 const FieldDescriptor* value_field; |
| 978 if (!internal::GetAnyFieldDescriptors(any, &type_url_field, &value_field)) { |
| 979 return false; |
| 980 } |
| 981 const string& type_url = reflection->GetString(any, type_url_field); |
| 982 string full_type_name; |
| 983 if (!internal::ParseAnyTypeUrl(type_url, &full_type_name)) { |
| 984 return false; |
| 985 } |
| 986 |
| 987 const google::protobuf::Descriptor* desc = |
| 988 any.GetDescriptor()->file()->pool()->FindMessageTypeByName( |
| 989 full_type_name); |
| 990 if (desc == NULL) { |
| 991 GOOGLE_LOG(ERROR) << "Proto type '" << full_type_name << "' not found"; |
| 992 return false; |
| 993 } |
| 994 |
| 995 if (dynamic_message_factory_ == NULL) { |
| 996 dynamic_message_factory_.reset(new DynamicMessageFactory()); |
| 997 } |
| 998 data->reset(dynamic_message_factory_->GetPrototype(desc)->New()); |
| 999 string serialized_value = reflection->GetString(any, value_field); |
| 1000 if (!(*data)->ParseFromString(serialized_value)) { |
| 1001 GOOGLE_LOG(ERROR) << "Failed to parse value for " << full_type_name; |
| 1002 return false; |
| 1003 } |
| 1004 return true; |
| 1005 } |
| 1006 |
| 1007 bool MessageDifferencer::CompareUnknownFields( |
| 1008 const Message& message1, const Message& message2, |
| 1009 const google::protobuf::UnknownFieldSet& unknown_field_set1, |
| 1010 const google::protobuf::UnknownFieldSet& unknown_field_set2, |
| 1011 vector<SpecificField>* parent_field) { |
| 1012 // Ignore unknown fields in EQUIVALENT mode. |
| 1013 if (message_field_comparison_ == EQUIVALENT) return true; |
| 1014 |
| 1015 if (unknown_field_set1.empty() && unknown_field_set2.empty()) { |
| 1016 return true; |
| 1017 } |
| 1018 |
| 1019 bool is_different = false; |
| 1020 |
| 1021 // We first sort the unknown fields by field number and type (in other words, |
| 1022 // in tag order), making sure to preserve ordering of values with the same |
| 1023 // tag. This allows us to report only meaningful differences between the |
| 1024 // two sets -- that is, differing values for the same tag. We use |
| 1025 // IndexUnknownFieldPairs to keep track of the field's original index for |
| 1026 // reporting purposes. |
| 1027 vector<IndexUnknownFieldPair> fields1; // unknown_field_set1, sorted |
| 1028 vector<IndexUnknownFieldPair> fields2; // unknown_field_set2, sorted |
| 1029 fields1.reserve(unknown_field_set1.field_count()); |
| 1030 fields2.reserve(unknown_field_set2.field_count()); |
| 1031 |
| 1032 for (int i = 0; i < unknown_field_set1.field_count(); i++) { |
| 1033 fields1.push_back(std::make_pair(i, &unknown_field_set1.field(i))); |
| 1034 } |
| 1035 for (int i = 0; i < unknown_field_set2.field_count(); i++) { |
| 1036 fields2.push_back(std::make_pair(i, &unknown_field_set2.field(i))); |
| 1037 } |
| 1038 |
| 1039 UnknownFieldOrdering is_before; |
| 1040 std::stable_sort(fields1.begin(), fields1.end(), is_before); |
| 1041 std::stable_sort(fields2.begin(), fields2.end(), is_before); |
| 1042 |
| 1043 // In order to fill in SpecificField::index, we have to keep track of how |
| 1044 // many values we've seen with the same field number and type. |
| 1045 // current_repeated points at the first field in this range, and |
| 1046 // current_repeated_start{1,2} are the indexes of the first field in the |
| 1047 // range within fields1 and fields2. |
| 1048 const UnknownField* current_repeated = NULL; |
| 1049 int current_repeated_start1 = 0; |
| 1050 int current_repeated_start2 = 0; |
| 1051 |
| 1052 // Now that we have two sorted lists, we can detect fields which appear only |
| 1053 // in one list or the other by traversing them simultaneously. |
| 1054 int index1 = 0; |
| 1055 int index2 = 0; |
| 1056 while (index1 < fields1.size() || index2 < fields2.size()) { |
| 1057 enum { ADDITION, DELETION, MODIFICATION, COMPARE_GROUPS, |
| 1058 NO_CHANGE } change_type; |
| 1059 |
| 1060 // focus_field is the field we're currently reporting on. (In the case |
| 1061 // of a modification, it's the field on the left side.) |
| 1062 const UnknownField* focus_field; |
| 1063 bool match = false; |
| 1064 |
| 1065 if (index2 == fields2.size() || |
| 1066 (index1 < fields1.size() && |
| 1067 is_before(fields1[index1], fields2[index2]))) { |
| 1068 // fields1[index1] is not present in fields2. |
| 1069 change_type = DELETION; |
| 1070 focus_field = fields1[index1].second; |
| 1071 } else if (index1 == fields1.size() || |
| 1072 is_before(fields2[index2], fields1[index1])) { |
| 1073 // fields2[index2] is not present in fields1. |
| 1074 if (scope_ == PARTIAL) { |
| 1075 // Ignore. |
| 1076 ++index2; |
| 1077 continue; |
| 1078 } |
| 1079 change_type = ADDITION; |
| 1080 focus_field = fields2[index2].second; |
| 1081 } else { |
| 1082 // Field type and number are the same. See if the values differ. |
| 1083 change_type = MODIFICATION; |
| 1084 focus_field = fields1[index1].second; |
| 1085 |
| 1086 switch (focus_field->type()) { |
| 1087 case UnknownField::TYPE_VARINT: |
| 1088 match = fields1[index1].second->varint() == |
| 1089 fields2[index2].second->varint(); |
| 1090 break; |
| 1091 case UnknownField::TYPE_FIXED32: |
| 1092 match = fields1[index1].second->fixed32() == |
| 1093 fields2[index2].second->fixed32(); |
| 1094 break; |
| 1095 case UnknownField::TYPE_FIXED64: |
| 1096 match = fields1[index1].second->fixed64() == |
| 1097 fields2[index2].second->fixed64(); |
| 1098 break; |
| 1099 case UnknownField::TYPE_LENGTH_DELIMITED: |
| 1100 match = fields1[index1].second->length_delimited() == |
| 1101 fields2[index2].second->length_delimited(); |
| 1102 break; |
| 1103 case UnknownField::TYPE_GROUP: |
| 1104 // We must deal with this later, after building the SpecificField. |
| 1105 change_type = COMPARE_GROUPS; |
| 1106 break; |
| 1107 } |
| 1108 if (match && change_type != COMPARE_GROUPS) { |
| 1109 change_type = NO_CHANGE; |
| 1110 } |
| 1111 } |
| 1112 |
| 1113 if (current_repeated == NULL || |
| 1114 focus_field->number() != current_repeated->number() || |
| 1115 focus_field->type() != current_repeated->type()) { |
| 1116 // We've started a new repeated field. |
| 1117 current_repeated = focus_field; |
| 1118 current_repeated_start1 = index1; |
| 1119 current_repeated_start2 = index2; |
| 1120 } |
| 1121 |
| 1122 if (change_type == NO_CHANGE && reporter_ == NULL) { |
| 1123 // Fields were already compared and matched and we have no reporter. |
| 1124 ++index1; |
| 1125 ++index2; |
| 1126 continue; |
| 1127 } |
| 1128 |
| 1129 if (change_type == ADDITION || change_type == DELETION || |
| 1130 change_type == MODIFICATION) { |
| 1131 if (reporter_ == NULL) { |
| 1132 // We found a difference and we have no reproter. |
| 1133 return false; |
| 1134 } |
| 1135 is_different = true; |
| 1136 } |
| 1137 |
| 1138 // Build the SpecificField. This is slightly complicated. |
| 1139 SpecificField specific_field; |
| 1140 specific_field.unknown_field_number = focus_field->number(); |
| 1141 specific_field.unknown_field_type = focus_field->type(); |
| 1142 |
| 1143 specific_field.unknown_field_set1 = &unknown_field_set1; |
| 1144 specific_field.unknown_field_set2 = &unknown_field_set2; |
| 1145 |
| 1146 if (change_type != ADDITION) { |
| 1147 specific_field.unknown_field_index1 = fields1[index1].first; |
| 1148 } |
| 1149 if (change_type != DELETION) { |
| 1150 specific_field.unknown_field_index2 = fields2[index2].first; |
| 1151 } |
| 1152 |
| 1153 // Calculate the field index. |
| 1154 if (change_type == ADDITION) { |
| 1155 specific_field.index = index2 - current_repeated_start2; |
| 1156 specific_field.new_index = index2 - current_repeated_start2; |
| 1157 } else { |
| 1158 specific_field.index = index1 - current_repeated_start1; |
| 1159 specific_field.new_index = index2 - current_repeated_start2; |
| 1160 } |
| 1161 |
| 1162 parent_field->push_back(specific_field); |
| 1163 |
| 1164 switch (change_type) { |
| 1165 case ADDITION: |
| 1166 reporter_->ReportAdded(message1, message2, *parent_field); |
| 1167 ++index2; |
| 1168 break; |
| 1169 case DELETION: |
| 1170 reporter_->ReportDeleted(message1, message2, *parent_field); |
| 1171 ++index1; |
| 1172 break; |
| 1173 case MODIFICATION: |
| 1174 reporter_->ReportModified(message1, message2, *parent_field); |
| 1175 ++index1; |
| 1176 ++index2; |
| 1177 break; |
| 1178 case COMPARE_GROUPS: |
| 1179 if (!CompareUnknownFields(message1, message2, |
| 1180 fields1[index1].second->group(), |
| 1181 fields2[index2].second->group(), |
| 1182 parent_field)) { |
| 1183 if (reporter_ == NULL) return false; |
| 1184 is_different = true; |
| 1185 reporter_->ReportModified(message1, message2, *parent_field); |
| 1186 } |
| 1187 ++index1; |
| 1188 ++index2; |
| 1189 break; |
| 1190 case NO_CHANGE: |
| 1191 ++index1; |
| 1192 ++index2; |
| 1193 if (report_matches_) { |
| 1194 reporter_->ReportMatched(message1, message2, *parent_field); |
| 1195 } |
| 1196 } |
| 1197 |
| 1198 parent_field->pop_back(); |
| 1199 } |
| 1200 |
| 1201 return !is_different; |
| 1202 } |
| 1203 |
| 1204 namespace { |
| 1205 |
| 1206 // Find maximum bipartite matching using the argumenting path algorithm. |
| 1207 class MaximumMatcher { |
| 1208 public: |
| 1209 typedef ResultCallback2<bool, int, int> NodeMatchCallback; |
| 1210 // MaximumMatcher takes ownership of the passed in callback and uses it to |
| 1211 // determine whether a node on the left side of the bipartial graph matches |
| 1212 // a node on the right side. count1 is the number of nodes on the left side |
| 1213 // of the graph and count2 to is the number of nodes on the right side. |
| 1214 // Every node is referred to using 0-based indices. |
| 1215 // If a maximum match is found, the result will be stored in match_list1 and |
| 1216 // match_list2. match_list1[i] == j means the i-th node on the left side is |
| 1217 // matched to the j-th node on the right side and match_list2[x] == y means |
| 1218 // the x-th node on the right side is matched to y-th node on the left side. |
| 1219 // match_list1[i] == -1 means the node is not matched. Same with match_list2. |
| 1220 MaximumMatcher(int count1, int count2, NodeMatchCallback* callback, |
| 1221 vector<int>* match_list1, vector<int>* match_list2); |
| 1222 // Find a maximum match and return the number of matched node pairs. |
| 1223 // If early_return is true, this method will return 0 immediately when it |
| 1224 // finds that not all nodes on the left side can be matched. |
| 1225 int FindMaximumMatch(bool early_return); |
| 1226 private: |
| 1227 // Determines whether the node on the left side of the bipartial graph |
| 1228 // matches the one on the right side. |
| 1229 bool Match(int left, int right); |
| 1230 // Find an argumenting path starting from the node v on the left side. If a |
| 1231 // path can be found, update match_list2_ to reflect the path and return |
| 1232 // true. |
| 1233 bool FindArgumentPathDFS(int v, vector<bool>* visited); |
| 1234 |
| 1235 int count1_; |
| 1236 int count2_; |
| 1237 google::protobuf::scoped_ptr<NodeMatchCallback> match_callback_; |
| 1238 map<pair<int, int>, bool> cached_match_results_; |
| 1239 vector<int>* match_list1_; |
| 1240 vector<int>* match_list2_; |
| 1241 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MaximumMatcher); |
| 1242 }; |
| 1243 |
| 1244 MaximumMatcher::MaximumMatcher(int count1, int count2, |
| 1245 NodeMatchCallback* callback, |
| 1246 vector<int>* match_list1, |
| 1247 vector<int>* match_list2) |
| 1248 : count1_(count1), count2_(count2), match_callback_(callback), |
| 1249 match_list1_(match_list1), match_list2_(match_list2) { |
| 1250 match_list1_->assign(count1, -1); |
| 1251 match_list2_->assign(count2, -1); |
| 1252 } |
| 1253 |
| 1254 int MaximumMatcher::FindMaximumMatch(bool early_return) { |
| 1255 int result = 0; |
| 1256 for (int i = 0; i < count1_; ++i) { |
| 1257 vector<bool> visited(count1_); |
| 1258 if (FindArgumentPathDFS(i, &visited)) { |
| 1259 ++result; |
| 1260 } else if (early_return) { |
| 1261 return 0; |
| 1262 } |
| 1263 } |
| 1264 // Backfill match_list1_ as we only filled match_list2_ when finding |
| 1265 // argumenting pathes. |
| 1266 for (int i = 0; i < count2_; ++i) { |
| 1267 if ((*match_list2_)[i] != -1) { |
| 1268 (*match_list1_)[(*match_list2_)[i]] = i; |
| 1269 } |
| 1270 } |
| 1271 return result; |
| 1272 } |
| 1273 |
| 1274 bool MaximumMatcher::Match(int left, int right) { |
| 1275 pair<int, int> p(left, right); |
| 1276 map<pair<int, int>, bool>::iterator it = cached_match_results_.find(p); |
| 1277 if (it != cached_match_results_.end()) { |
| 1278 return it->second; |
| 1279 } |
| 1280 cached_match_results_[p] = match_callback_->Run(left, right); |
| 1281 return cached_match_results_[p]; |
| 1282 } |
| 1283 |
| 1284 bool MaximumMatcher::FindArgumentPathDFS(int v, vector<bool>* visited) { |
| 1285 (*visited)[v] = true; |
| 1286 // We try to match those un-matched nodes on the right side first. This is |
| 1287 // the step that the navie greedy matching algorithm uses. In the best cases |
| 1288 // where the greedy algorithm can find a maximum matching, we will always |
| 1289 // find a match in this step and the performance will be identical to the |
| 1290 // greedy algorithm. |
| 1291 for (int i = 0; i < count2_; ++i) { |
| 1292 int matched = (*match_list2_)[i]; |
| 1293 if (matched == -1 && Match(v, i)) { |
| 1294 (*match_list2_)[i] = v; |
| 1295 return true; |
| 1296 } |
| 1297 } |
| 1298 // Then we try those already matched nodes and see if we can find an |
| 1299 // alternaive match for the node matched to them. |
| 1300 // The greedy algorithm will stop before this and fail to produce the |
| 1301 // correct result. |
| 1302 for (int i = 0; i < count2_; ++i) { |
| 1303 int matched = (*match_list2_)[i]; |
| 1304 if (matched != -1 && Match(v, i)) { |
| 1305 if (!(*visited)[matched] && FindArgumentPathDFS(matched, visited)) { |
| 1306 (*match_list2_)[i] = v; |
| 1307 return true; |
| 1308 } |
| 1309 } |
| 1310 } |
| 1311 return false; |
| 1312 } |
| 1313 |
| 1314 } // namespace |
| 1315 |
| 1316 bool MessageDifferencer::MatchRepeatedFieldIndices( |
| 1317 const Message& message1, |
| 1318 const Message& message2, |
| 1319 const FieldDescriptor* repeated_field, |
| 1320 const vector<SpecificField>& parent_fields, |
| 1321 vector<int>* match_list1, |
| 1322 vector<int>* match_list2) { |
| 1323 const int count1 = |
| 1324 message1.GetReflection()->FieldSize(message1, repeated_field); |
| 1325 const int count2 = |
| 1326 message2.GetReflection()->FieldSize(message2, repeated_field); |
| 1327 const MapKeyComparator* key_comparator = GetMapKeyComparator(repeated_field); |
| 1328 |
| 1329 match_list1->assign(count1, -1); |
| 1330 match_list2->assign(count2, -1); |
| 1331 |
| 1332 SpecificField specific_field; |
| 1333 specific_field.field = repeated_field; |
| 1334 |
| 1335 bool success = true; |
| 1336 // Find potential match if this is a special repeated field. |
| 1337 if (key_comparator != NULL || IsTreatedAsSet(repeated_field)) { |
| 1338 if (scope_ == PARTIAL) { |
| 1339 // When partial matching is enabled, Compare(a, b) && Compare(a, c) |
| 1340 // doesn't neccessarily imply Compare(b, c). Therefore a naive greedy |
| 1341 // algorithm will fail to find a maximum matching. |
| 1342 // Here we use the argumenting path algorithm. |
| 1343 MaximumMatcher::NodeMatchCallback* callback = NewPermanentCallback( |
| 1344 this, &MessageDifferencer::IsMatch, repeated_field, key_comparator, |
| 1345 &message1, &message2, parent_fields); |
| 1346 MaximumMatcher matcher(count1, count2, callback, match_list1, |
| 1347 match_list2); |
| 1348 // If diff info is not needed, we should end the matching process as |
| 1349 // soon as possible if not all items can be matched. |
| 1350 bool early_return = (reporter_ == NULL); |
| 1351 int match_count = matcher.FindMaximumMatch(early_return); |
| 1352 if (match_count != count1 && reporter_ == NULL) return false; |
| 1353 success = success && (match_count == count1); |
| 1354 } else { |
| 1355 for (int i = 0; i < count1; ++i) { |
| 1356 // Indicates any matched elements for this repeated field. |
| 1357 bool match = false; |
| 1358 |
| 1359 specific_field.index = i; |
| 1360 specific_field.new_index = i; |
| 1361 |
| 1362 for (int j = 0; j < count2; j++) { |
| 1363 if (match_list2->at(j) != -1) continue; |
| 1364 specific_field.index = i; |
| 1365 specific_field.new_index = j; |
| 1366 |
| 1367 match = IsMatch(repeated_field, key_comparator, |
| 1368 &message1, &message2, parent_fields, i, j); |
| 1369 |
| 1370 if (match) { |
| 1371 match_list1->at(specific_field.index) = specific_field.new_index; |
| 1372 match_list2->at(specific_field.new_index) = specific_field.index; |
| 1373 break; |
| 1374 } |
| 1375 } |
| 1376 if (!match && reporter_ == NULL) return false; |
| 1377 success = success && match; |
| 1378 } |
| 1379 } |
| 1380 } else { |
| 1381 // If this field should be treated as list, just label the match_list. |
| 1382 for (int i = 0; i < count1 && i < count2; i++) { |
| 1383 match_list1->at(i) = i; |
| 1384 match_list2->at(i) = i; |
| 1385 } |
| 1386 } |
| 1387 |
| 1388 return success; |
| 1389 } |
| 1390 |
| 1391 FieldComparator::ComparisonResult MessageDifferencer::GetFieldComparisonResult( |
| 1392 const Message& message1, const Message& message2, |
| 1393 const FieldDescriptor* field, int index1, int index2, |
| 1394 const FieldContext* field_context) { |
| 1395 FieldComparator* comparator = field_comparator_ != NULL ? |
| 1396 field_comparator_ : &default_field_comparator_; |
| 1397 return comparator->Compare(message1, message2, field, |
| 1398 index1, index2, field_context); |
| 1399 } |
| 1400 |
| 1401 // =========================================================================== |
| 1402 |
| 1403 MessageDifferencer::Reporter::Reporter() { } |
| 1404 MessageDifferencer::Reporter::~Reporter() {} |
| 1405 |
| 1406 // =========================================================================== |
| 1407 |
| 1408 MessageDifferencer::MapKeyComparator::MapKeyComparator() {} |
| 1409 MessageDifferencer::MapKeyComparator::~MapKeyComparator() {} |
| 1410 |
| 1411 // =========================================================================== |
| 1412 |
| 1413 MessageDifferencer::IgnoreCriteria::IgnoreCriteria() {} |
| 1414 MessageDifferencer::IgnoreCriteria::~IgnoreCriteria() {} |
| 1415 |
| 1416 // =========================================================================== |
| 1417 |
| 1418 // Note that the printer's delimiter is not used, because if we are given a |
| 1419 // printer, we don't know its delimiter. |
| 1420 MessageDifferencer::StreamReporter::StreamReporter( |
| 1421 io::ZeroCopyOutputStream* output) : printer_(new io::Printer(output, '$')), |
| 1422 delete_printer_(true), |
| 1423 report_modified_aggregates_(false) { } |
| 1424 |
| 1425 MessageDifferencer::StreamReporter::StreamReporter( |
| 1426 io::Printer* printer) : printer_(printer), |
| 1427 delete_printer_(false), |
| 1428 report_modified_aggregates_(false) { } |
| 1429 |
| 1430 MessageDifferencer::StreamReporter::~StreamReporter() { |
| 1431 if (delete_printer_) delete printer_; |
| 1432 } |
| 1433 |
| 1434 void MessageDifferencer::StreamReporter::PrintPath( |
| 1435 const vector<SpecificField>& field_path, bool left_side) { |
| 1436 for (int i = 0; i < field_path.size(); ++i) { |
| 1437 if (i > 0) { |
| 1438 printer_->Print("."); |
| 1439 } |
| 1440 |
| 1441 SpecificField specific_field = field_path[i]; |
| 1442 |
| 1443 if (specific_field.field != NULL) { |
| 1444 if (specific_field.field->is_extension()) { |
| 1445 printer_->Print("($name$)", "name", |
| 1446 specific_field.field->full_name()); |
| 1447 } else { |
| 1448 printer_->PrintRaw(specific_field.field->name()); |
| 1449 } |
| 1450 } else { |
| 1451 printer_->PrintRaw(SimpleItoa(specific_field.unknown_field_number)); |
| 1452 } |
| 1453 if (left_side && specific_field.index >= 0) { |
| 1454 printer_->Print("[$name$]", "name", SimpleItoa(specific_field.index)); |
| 1455 } |
| 1456 if (!left_side && specific_field.new_index >= 0) { |
| 1457 printer_->Print("[$name$]", "name", SimpleItoa(specific_field.new_index)); |
| 1458 } |
| 1459 } |
| 1460 } |
| 1461 |
| 1462 void MessageDifferencer:: |
| 1463 StreamReporter::PrintValue(const Message& message, |
| 1464 const vector<SpecificField>& field_path, |
| 1465 bool left_side) { |
| 1466 const SpecificField& specific_field = field_path.back(); |
| 1467 const FieldDescriptor* field = specific_field.field; |
| 1468 if (field != NULL) { |
| 1469 string output; |
| 1470 int index = left_side ? specific_field.index : specific_field.new_index; |
| 1471 if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 1472 const Reflection* reflection = message.GetReflection(); |
| 1473 const Message& field_message = field->is_repeated() ? |
| 1474 reflection->GetRepeatedMessage(message, field, index) : |
| 1475 reflection->GetMessage(message, field); |
| 1476 output = field_message.ShortDebugString(); |
| 1477 if (output.empty()) { |
| 1478 printer_->Print("{ }"); |
| 1479 } else { |
| 1480 printer_->Print("{ $name$ }", "name", output); |
| 1481 } |
| 1482 } else { |
| 1483 TextFormat::PrintFieldValueToString(message, field, index, &output); |
| 1484 printer_->PrintRaw(output); |
| 1485 } |
| 1486 } else { |
| 1487 const UnknownFieldSet* unknown_fields = |
| 1488 (left_side ? |
| 1489 specific_field.unknown_field_set1 : |
| 1490 specific_field.unknown_field_set2); |
| 1491 const UnknownField* unknown_field = &unknown_fields->field( |
| 1492 left_side ? |
| 1493 specific_field.unknown_field_index1 : |
| 1494 specific_field.unknown_field_index2); |
| 1495 PrintUnknownFieldValue(unknown_field); |
| 1496 } |
| 1497 } |
| 1498 |
| 1499 void MessageDifferencer:: |
| 1500 StreamReporter::PrintUnknownFieldValue(const UnknownField* unknown_field) { |
| 1501 GOOGLE_CHECK(unknown_field != NULL) << " Cannot print NULL unknown_field."; |
| 1502 |
| 1503 string output; |
| 1504 switch (unknown_field->type()) { |
| 1505 case UnknownField::TYPE_VARINT: |
| 1506 output = SimpleItoa(unknown_field->varint()); |
| 1507 break; |
| 1508 case UnknownField::TYPE_FIXED32: |
| 1509 output = StrCat("0x", strings::Hex(unknown_field->fixed32(), |
| 1510 strings::ZERO_PAD_8)); |
| 1511 break; |
| 1512 case UnknownField::TYPE_FIXED64: |
| 1513 output = StrCat("0x", strings::Hex(unknown_field->fixed64(), |
| 1514 strings::ZERO_PAD_16)); |
| 1515 break; |
| 1516 case UnknownField::TYPE_LENGTH_DELIMITED: |
| 1517 output = StringPrintf("\"%s\"", |
| 1518 CEscape(unknown_field->length_delimited()).c_str()); |
| 1519 break; |
| 1520 case UnknownField::TYPE_GROUP: |
| 1521 // TODO(kenton): Print the contents of the group like we do for |
| 1522 // messages. Requires an equivalent of ShortDebugString() for |
| 1523 // UnknownFieldSet. |
| 1524 output = "{ ... }"; |
| 1525 break; |
| 1526 } |
| 1527 printer_->PrintRaw(output); |
| 1528 } |
| 1529 |
| 1530 void MessageDifferencer::StreamReporter::Print(const string& str) { |
| 1531 printer_->Print(str.c_str()); |
| 1532 } |
| 1533 |
| 1534 void MessageDifferencer::StreamReporter::ReportAdded( |
| 1535 const Message& message1, |
| 1536 const Message& message2, |
| 1537 const vector<SpecificField>& field_path) { |
| 1538 printer_->Print("added: "); |
| 1539 PrintPath(field_path, false); |
| 1540 printer_->Print(": "); |
| 1541 PrintValue(message2, field_path, false); |
| 1542 printer_->Print("\n"); // Print for newlines. |
| 1543 } |
| 1544 |
| 1545 void MessageDifferencer::StreamReporter::ReportDeleted( |
| 1546 const Message& message1, |
| 1547 const Message& message2, |
| 1548 const vector<SpecificField>& field_path) { |
| 1549 printer_->Print("deleted: "); |
| 1550 PrintPath(field_path, true); |
| 1551 printer_->Print(": "); |
| 1552 PrintValue(message1, field_path, true); |
| 1553 printer_->Print("\n"); // Print for newlines |
| 1554 } |
| 1555 |
| 1556 void MessageDifferencer::StreamReporter::ReportModified( |
| 1557 const Message& message1, |
| 1558 const Message& message2, |
| 1559 const vector<SpecificField>& field_path) { |
| 1560 if (!report_modified_aggregates_ && field_path.back().field == NULL) { |
| 1561 if (field_path.back().unknown_field_type == UnknownField::TYPE_GROUP) { |
| 1562 // Any changes to the subfields have already been printed. |
| 1563 return; |
| 1564 } |
| 1565 } else if (!report_modified_aggregates_) { |
| 1566 if (field_path.back().field->cpp_type() == |
| 1567 FieldDescriptor::CPPTYPE_MESSAGE) { |
| 1568 // Any changes to the subfields have already been printed. |
| 1569 return; |
| 1570 } |
| 1571 } |
| 1572 |
| 1573 printer_->Print("modified: "); |
| 1574 PrintPath(field_path, true); |
| 1575 if (CheckPathChanged(field_path)) { |
| 1576 printer_->Print(" -> "); |
| 1577 PrintPath(field_path, false); |
| 1578 } |
| 1579 printer_->Print(": "); |
| 1580 PrintValue(message1, field_path, true); |
| 1581 printer_->Print(" -> "); |
| 1582 PrintValue(message2, field_path, false); |
| 1583 printer_->Print("\n"); // Print for newlines. |
| 1584 } |
| 1585 |
| 1586 void MessageDifferencer::StreamReporter::ReportMoved( |
| 1587 const Message& message1, |
| 1588 const Message& message2, |
| 1589 const vector<SpecificField>& field_path) { |
| 1590 printer_->Print("moved: "); |
| 1591 PrintPath(field_path, true); |
| 1592 printer_->Print(" -> "); |
| 1593 PrintPath(field_path, false); |
| 1594 printer_->Print(" : "); |
| 1595 PrintValue(message1, field_path, true); |
| 1596 printer_->Print("\n"); // Print for newlines. |
| 1597 } |
| 1598 |
| 1599 void MessageDifferencer::StreamReporter::ReportMatched( |
| 1600 const Message& message1, |
| 1601 const Message& message2, |
| 1602 const vector<SpecificField>& field_path) { |
| 1603 printer_->Print("matched: "); |
| 1604 PrintPath(field_path, true); |
| 1605 if (CheckPathChanged(field_path)) { |
| 1606 printer_->Print(" -> "); |
| 1607 PrintPath(field_path, false); |
| 1608 } |
| 1609 printer_->Print(" : "); |
| 1610 PrintValue(message1, field_path, true); |
| 1611 printer_->Print("\n"); // Print for newlines. |
| 1612 } |
| 1613 |
| 1614 void MessageDifferencer::StreamReporter::ReportIgnored( |
| 1615 const Message& message1, |
| 1616 const Message& message2, |
| 1617 const vector<SpecificField>& field_path) { |
| 1618 printer_->Print("ignored: "); |
| 1619 PrintPath(field_path, true); |
| 1620 if (CheckPathChanged(field_path)) { |
| 1621 printer_->Print(" -> "); |
| 1622 PrintPath(field_path, false); |
| 1623 } |
| 1624 printer_->Print("\n"); // Print for newlines. |
| 1625 } |
| 1626 |
| 1627 } // namespace util |
| 1628 } // namespace protobuf |
| 1629 } // namespace google |
| OLD | NEW |