message_differencer.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. // Author: jschorr@google.com (Joseph Schorr)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This file defines static methods and classes for comparing Protocol
  35. // Messages.
  36. //
  37. // Aug. 2008: Added Unknown Fields Comparison for messages.
  38. // Aug. 2009: Added different options to compare repeated fields.
  39. // Apr. 2010: Moved field comparison to FieldComparator.
  40. #ifndef GOOGLE_PROTOBUF_UTIL_MESSAGE_DIFFERENCER_H__
  41. #define GOOGLE_PROTOBUF_UTIL_MESSAGE_DIFFERENCER_H__
  42. #include <map>
  43. #include <set>
  44. #include <string>
  45. #include <vector>
  46. #include <google/protobuf/descriptor.h> // FieldDescriptor
  47. #include <google/protobuf/message.h> // Message
  48. #include <google/protobuf/unknown_field_set.h>
  49. #include <google/protobuf/util/field_comparator.h>
  50. namespace google {
  51. namespace protobuf {
  52. class DynamicMessageFactory;
  53. class FieldDescriptor;
  54. namespace io {
  55. class ZeroCopyOutputStream;
  56. class Printer;
  57. }
  58. namespace util {
  59. class DefaultFieldComparator;
  60. class FieldContext; // declared below MessageDifferencer
  61. // A basic differencer that can be used to determine
  62. // the differences between two specified Protocol Messages. If any differences
  63. // are found, the Compare method will return false, and any differencer reporter
  64. // specified via ReportDifferencesTo will have its reporting methods called (see
  65. // below for implementation of the report). Based off of the original
  66. // ProtocolDifferencer implementation in //net/proto/protocol-differencer.h
  67. // (Thanks Todd!).
  68. //
  69. // MessageDifferencer REQUIRES that compared messages be the same type, defined
  70. // as messages that share the same descriptor. If not, the behavior of this
  71. // class is undefined.
  72. //
  73. // People disagree on what MessageDifferencer should do when asked to compare
  74. // messages with different descriptors. Some people think it should always
  75. // return false. Others expect it to try to look for similar fields and
  76. // compare them anyway -- especially if the descriptors happen to be identical.
  77. // If we chose either of these behaviors, some set of people would find it
  78. // surprising, and could end up writing code expecting the other behavior
  79. // without realizing their error. Therefore, we forbid that usage.
  80. //
  81. // This class is implemented based on the proto2 reflection. The performance
  82. // should be good enough for normal usages. However, for places where the
  83. // performance is extremely sensitive, there are several alternatives:
  84. // - Comparing serialized string
  85. // Downside: false negatives (there are messages that are the same but their
  86. // serialized strings are different).
  87. // - Equals code generator by compiler plugin (net/proto2/contrib/equals_plugin)
  88. // Downside: more generated code; maintenance overhead for the additional rule
  89. // (must be in sync with the original proto_library).
  90. //
  91. // Note on handling of google.protobuf.Any: MessageDifferencer automatically
  92. // unpacks Any::value into a Message and compares its individual fields.
  93. // Messages encoded in a repeated Any cannot be compared using TreatAsMap.
  94. //
  95. //
  96. // Note on thread-safety: MessageDifferencer is *not* thread-safe. You need to
  97. // guard it with a lock to use the same MessageDifferencer instance from
  98. // multiple threads. Note that it's fine to call static comparison methods
  99. // (like MessageDifferencer::Equals) concurrently.
  100. class LIBPROTOBUF_EXPORT MessageDifferencer {
  101. public:
  102. // Determines whether the supplied messages are equal. Equality is defined as
  103. // all fields within the two messages being set to the same value. Primitive
  104. // fields and strings are compared by value while embedded messages/groups
  105. // are compared as if via a recursive call. Use IgnoreField() and Compare()
  106. // if some fields should be ignored in the comparison.
  107. //
  108. // This method REQUIRES that the two messages have the same
  109. // Descriptor (message1.GetDescriptor() == message2.GetDescriptor()).
  110. static bool Equals(const Message& message1, const Message& message2);
  111. // Determines whether the supplied messages are equivalent. Equivalency is
  112. // defined as all fields within the two messages having the same value. This
  113. // differs from the Equals method above in that fields with default values
  114. // are considered set to said value automatically. For details on how default
  115. // values are defined for each field type, see http://shortn/_x2Gv6XFrWt.
  116. // Also, Equivalent() ignores unknown fields. Use IgnoreField() and Compare()
  117. // if some fields should be ignored in the comparison.
  118. //
  119. // This method REQUIRES that the two messages have the same
  120. // Descriptor (message1.GetDescriptor() == message2.GetDescriptor()).
  121. static bool Equivalent(const Message& message1, const Message& message2);
  122. // Determines whether the supplied messages are approximately equal.
  123. // Approximate equality is defined as all fields within the two messages
  124. // being approximately equal. Primitive (non-float) fields and strings are
  125. // compared by value, floats are compared using MathUtil::AlmostEquals() and
  126. // embedded messages/groups are compared as if via a recursive call. Use
  127. // IgnoreField() and Compare() if some fields should be ignored in the
  128. // comparison.
  129. //
  130. // This method REQUIRES that the two messages have the same
  131. // Descriptor (message1.GetDescriptor() == message2.GetDescriptor()).
  132. static bool ApproximatelyEquals(const Message& message1,
  133. const Message& message2);
  134. // Determines whether the supplied messages are approximately equivalent.
  135. // Approximate equivalency is defined as all fields within the two messages
  136. // being approximately equivalent. As in
  137. // MessageDifferencer::ApproximatelyEquals, primitive (non-float) fields and
  138. // strings are compared by value, floats are compared using
  139. // MathUtil::AlmostEquals() and embedded messages/groups are compared as if
  140. // via a recursive call. However, fields with default values are considered
  141. // set to said value, as per MessageDiffencer::Equivalent. Use IgnoreField()
  142. // and Compare() if some fields should be ignored in the comparison.
  143. //
  144. // This method REQUIRES that the two messages have the same
  145. // Descriptor (message1.GetDescriptor() == message2.GetDescriptor()).
  146. static bool ApproximatelyEquivalent(const Message& message1,
  147. const Message& message2);
  148. // Identifies an individual field in a message instance. Used for field_path,
  149. // below.
  150. struct SpecificField {
  151. // For known fields, "field" is filled in and "unknown_field_number" is -1.
  152. // For unknown fields, "field" is NULL, "unknown_field_number" is the field
  153. // number, and "unknown_field_type" is its type.
  154. const FieldDescriptor* field;
  155. int unknown_field_number;
  156. UnknownField::Type unknown_field_type;
  157. // If this a repeated field, "index" is the index within it. For unknown
  158. // fields, this is the index of the field among all unknown fields of the
  159. // same field number and type.
  160. int index;
  161. // If "field" is a repeated field which is being treated as a map or
  162. // a set (see TreatAsMap() and TreatAsSet(), below), new_index indicates
  163. // the index the position to which the element has moved. If the element
  164. // has not moved, "new_index" will have the same value as "index".
  165. int new_index;
  166. // For unknown fields, these are the pointers to the UnknownFieldSet
  167. // containing the unknown fields. In certain cases (e.g. proto1's
  168. // MessageSet, or nested groups of unknown fields), these may differ from
  169. // the messages' internal UnknownFieldSets.
  170. const UnknownFieldSet* unknown_field_set1;
  171. const UnknownFieldSet* unknown_field_set2;
  172. // For unknown fields, these are the index of the field within the
  173. // UnknownFieldSets. One or the other will be -1 when
  174. // reporting an addition or deletion.
  175. int unknown_field_index1;
  176. int unknown_field_index2;
  177. SpecificField()
  178. : field(NULL),
  179. unknown_field_number(-1),
  180. index(-1),
  181. new_index(-1),
  182. unknown_field_set1(NULL),
  183. unknown_field_set2(NULL),
  184. unknown_field_index1(-1),
  185. unknown_field_index2(-1) {}
  186. };
  187. // Abstract base class from which all MessageDifferencer
  188. // reporters derive. The five Report* methods below will be called when
  189. // a field has been added, deleted, modified, moved, or matched. The third
  190. // argument is a vector of FieldDescriptor pointers which describes the chain
  191. // of fields that was taken to find the current field. For example, for a
  192. // field found in an embedded message, the vector will contain two
  193. // FieldDescriptors. The first will be the field of the embedded message
  194. // itself and the second will be the actual field in the embedded message
  195. // that was added/deleted/modified.
  196. class LIBPROTOBUF_EXPORT Reporter {
  197. public:
  198. Reporter();
  199. virtual ~Reporter();
  200. // Reports that a field has been added into Message2.
  201. virtual void ReportAdded(
  202. const Message& message1, const Message& message2,
  203. const std::vector<SpecificField>& field_path) = 0;
  204. // Reports that a field has been deleted from Message1.
  205. virtual void ReportDeleted(
  206. const Message& message1,
  207. const Message& message2,
  208. const std::vector<SpecificField>& field_path) = 0;
  209. // Reports that the value of a field has been modified.
  210. virtual void ReportModified(
  211. const Message& message1,
  212. const Message& message2,
  213. const std::vector<SpecificField>& field_path) = 0;
  214. // Reports that a repeated field has been moved to another location. This
  215. // only applies when using TreatAsSet or TreatAsMap() -- see below. Also
  216. // note that for any given field, ReportModified and ReportMoved are
  217. // mutually exclusive. If a field has been both moved and modified, then
  218. // only ReportModified will be called.
  219. virtual void ReportMoved(
  220. const Message& /* message1 */,
  221. const Message& /* message2 */,
  222. const std::vector<SpecificField>& /* field_path */) { }
  223. // Reports that two fields match. Useful for doing side-by-side diffs.
  224. // This function is mutually exclusive with ReportModified and ReportMoved.
  225. // Note that you must call set_report_matches(true) before calling Compare
  226. // to make use of this function.
  227. virtual void ReportMatched(
  228. const Message& /* message1 */,
  229. const Message& /* message2 */,
  230. const std::vector<SpecificField>& /* field_path */) { }
  231. // Reports that two fields would have been compared, but the
  232. // comparison has been skipped because the field was marked as
  233. // 'ignored' using IgnoreField(). This function is mutually
  234. // exclusive with all the other Report() functions.
  235. //
  236. // The contract of ReportIgnored is slightly different than the
  237. // other Report() functions, in that |field_path.back().index| is
  238. // always equal to -1, even if the last field is repeated. This is
  239. // because while the other Report() functions indicate where in a
  240. // repeated field the action (Addition, Deletion, etc...)
  241. // happened, when a repeated field is 'ignored', the differencer
  242. // simply calls ReportIgnored on the repeated field as a whole and
  243. // moves on without looking at its individual elements.
  244. //
  245. // Furthermore, ReportIgnored() does not indicate whether the
  246. // fields were in fact equal or not, as Compare() does not inspect
  247. // these fields at all. It is up to the Reporter to decide whether
  248. // the fields are equal or not (perhaps with a second call to
  249. // Compare()), if it cares.
  250. virtual void ReportIgnored(
  251. const Message& /* message1 */,
  252. const Message& /* message2 */,
  253. const std::vector<SpecificField>& /* field_path */) { }
  254. // Report that an unknown field is ignored. (see comment above).
  255. // Note this is a different function since the last SpecificField in field
  256. // path has a null field. This could break existing Reporter.
  257. virtual void ReportUnknownFieldIgnored(
  258. const Message& /* message1 */, const Message& /* message2 */,
  259. const std::vector<SpecificField>& /* field_path */) {}
  260. private:
  261. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reporter);
  262. };
  263. // MapKeyComparator is used to determine if two elements have the same key
  264. // when comparing elements of a repeated field as a map.
  265. class LIBPROTOBUF_EXPORT MapKeyComparator {
  266. public:
  267. MapKeyComparator();
  268. virtual ~MapKeyComparator();
  269. virtual bool IsMatch(
  270. const Message& /* message1 */,
  271. const Message& /* message2 */,
  272. const std::vector<SpecificField>& /* parent_fields */) const {
  273. GOOGLE_CHECK(false) << "IsMatch() is not implemented.";
  274. return false;
  275. }
  276. private:
  277. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MapKeyComparator);
  278. };
  279. // Abstract base class from which all IgnoreCriteria derive.
  280. // By adding IgnoreCriteria more complex ignore logic can be implemented.
  281. // IgnoreCriteria are registed with AddIgnoreCriteria. For each compared
  282. // field IsIgnored is called on each added IgnoreCriteria until one returns
  283. // true or all return false.
  284. // IsIgnored is called for fields where at least one side has a value.
  285. class LIBPROTOBUF_EXPORT IgnoreCriteria {
  286. public:
  287. IgnoreCriteria();
  288. virtual ~IgnoreCriteria();
  289. // Returns true if the field should be ignored.
  290. virtual bool IsIgnored(
  291. const Message& /* message1 */,
  292. const Message& /* message2 */,
  293. const FieldDescriptor* /* field */,
  294. const std::vector<SpecificField>& /* parent_fields */) = 0;
  295. // Returns true if the unknown field should be ignored.
  296. // Note: This will be called for unknown fields as well in which case
  297. // field.field will be null.
  298. virtual bool IsUnknownFieldIgnored(
  299. const Message& /* message1 */, const Message& /* message2 */,
  300. const SpecificField& /* field */,
  301. const std::vector<SpecificField>& /* parent_fields */) {
  302. return false;
  303. }
  304. };
  305. // To add a Reporter, construct default here, then use ReportDifferencesTo or
  306. // ReportDifferencesToString.
  307. explicit MessageDifferencer();
  308. ~MessageDifferencer();
  309. enum MessageFieldComparison {
  310. EQUAL, // Fields must be present in both messages
  311. // for the messages to be considered the same.
  312. EQUIVALENT, // Fields with default values are considered set
  313. // for comparison purposes even if not explicitly
  314. // set in the messages themselves. Unknown fields
  315. // are ignored.
  316. };
  317. enum Scope {
  318. FULL, // All fields of both messages are considered in the comparison.
  319. PARTIAL // Only fields present in the first message are considered; fields
  320. // set only in the second message will be skipped during
  321. // comparison.
  322. };
  323. // DEPRECATED. Use FieldComparator::FloatComparison instead.
  324. enum FloatComparison {
  325. EXACT, // Floats and doubles are compared exactly.
  326. APPROXIMATE // Floats and doubles are compared using the
  327. // MathUtil::AlmostEquals method.
  328. };
  329. enum RepeatedFieldComparison {
  330. AS_LIST, // Repeated fields are compared in order. Differing values at
  331. // the same index are reported using ReportModified(). If the
  332. // repeated fields have different numbers of elements, the
  333. // unpaired elements are reported using ReportAdded() or
  334. // ReportDeleted().
  335. AS_SET, // Treat all the repeated fields as sets.
  336. // See TreatAsSet(), as below.
  337. };
  338. // The elements of the given repeated field will be treated as a set for
  339. // diffing purposes, so different orderings of the same elements will be
  340. // considered equal. Elements which are present on both sides of the
  341. // comparison but which have changed position will be reported with
  342. // ReportMoved(). Elements which only exist on one side or the other are
  343. // reported with ReportAdded() and ReportDeleted() regardless of their
  344. // positions. ReportModified() is never used for this repeated field. If
  345. // the only differences between the compared messages is that some fields
  346. // have been moved, then the comparison returns true.
  347. //
  348. // Note that despite the name of this method, this is really
  349. // comparison as multisets: if one side of the comparison has a duplicate
  350. // in the repeated field but the other side doesn't, this will count as
  351. // a mismatch.
  352. //
  353. // If the scope of comparison is set to PARTIAL, then in addition to what's
  354. // above, extra values added to repeated fields of the second message will
  355. // not cause the comparison to fail.
  356. //
  357. // Note that set comparison is currently O(k * n^2) (where n is the total
  358. // number of elements, and k is the average size of each element). In theory
  359. // it could be made O(n * k) with a more complex hashing implementation. Feel
  360. // free to contribute one if the current implementation is too slow for you.
  361. // If partial matching is also enabled, the time complexity will be O(k * n^2
  362. // + n^3) in which n^3 is the time complexity of the maximum matching
  363. // algorithm.
  364. //
  365. // REQUIRES: field->is_repeated() and field not registered with TreatAsList
  366. void TreatAsSet(const FieldDescriptor* field);
  367. // The elements of the given repeated field will be treated as a list for
  368. // diffing purposes, so different orderings of the same elements will NOT be
  369. // considered equal.
  370. //
  371. // REQUIRED: field->is_repeated() and field not registered with TreatAsSet
  372. void TreatAsList(const FieldDescriptor* field);
  373. // The elements of the given repeated field will be treated as a map for
  374. // diffing purposes, with |key| being the map key. Thus, elements with the
  375. // same key will be compared even if they do not appear at the same index.
  376. // Differences are reported similarly to TreatAsSet(), except that
  377. // ReportModified() is used to report elements with the same key but
  378. // different values. Note that if an element is both moved and modified,
  379. // only ReportModified() will be called. As with TreatAsSet, if the only
  380. // differences between the compared messages is that some fields have been
  381. // moved, then the comparison returns true. See TreatAsSet for notes on
  382. // performance.
  383. //
  384. // REQUIRES: field->is_repeated()
  385. // REQUIRES: field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE
  386. // REQUIRES: key->containing_type() == field->message_type()
  387. void TreatAsMap(const FieldDescriptor* field, const FieldDescriptor* key);
  388. // Same as TreatAsMap except that this method will use multiple fields as
  389. // the key in comparison. All specified fields in 'key_fields' should be
  390. // present in the compared elements. Two elements will be treated as having
  391. // the same key iff they have the same value for every specified field. There
  392. // are two steps in the comparison process. The first one is key matching.
  393. // Every element from one message will be compared to every element from
  394. // the other message. Only fields in 'key_fields' are compared in this step
  395. // to decide if two elements have the same key. The second step is value
  396. // comparison. Those pairs of elements with the same key (with equal value
  397. // for every field in 'key_fields') will be compared in this step.
  398. // Time complexity of the first step is O(s * m * n ^ 2) where s is the
  399. // average size of the fields specified in 'key_fields', m is the number of
  400. // fields in 'key_fields' and n is the number of elements. If partial
  401. // matching is enabled, an extra O(n^3) will be incured by the maximum
  402. // matching algorithm. The second step is O(k * n) where k is the average
  403. // size of each element.
  404. void TreatAsMapWithMultipleFieldsAsKey(
  405. const FieldDescriptor* field,
  406. const std::vector<const FieldDescriptor*>& key_fields);
  407. // Same as TreatAsMapWithMultipleFieldsAsKey, except that each of the field
  408. // do not necessarily need to be a direct subfield. Each element in
  409. // key_field_paths indicate a path from the message being compared, listing
  410. // successive subfield to reach the key field.
  411. //
  412. // REQUIRES:
  413. // for key_field_path in key_field_paths:
  414. // key_field_path[0]->containing_type() == field->message_type()
  415. // for i in [0, key_field_path.size() - 1):
  416. // key_field_path[i+1]->containing_type() ==
  417. // key_field_path[i]->message_type()
  418. // key_field_path[i]->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE
  419. // !key_field_path[i]->is_repeated()
  420. void TreatAsMapWithMultipleFieldPathsAsKey(
  421. const FieldDescriptor* field,
  422. const std::vector<std::vector<const FieldDescriptor*> >& key_field_paths);
  423. // Uses a custom MapKeyComparator to determine if two elements have the same
  424. // key when comparing a repeated field as a map.
  425. // The caller is responsible to delete the key_comparator.
  426. // This method varies from TreatAsMapWithMultipleFieldsAsKey only in the
  427. // first key matching step. Rather than comparing some specified fields, it
  428. // will invoke the IsMatch method of the given 'key_comparator' to decide if
  429. // two elements have the same key.
  430. void TreatAsMapUsingKeyComparator(
  431. const FieldDescriptor* field,
  432. const MapKeyComparator* key_comparator);
  433. // Initiates and returns a new instance of MultipleFieldsMapKeyComparator.
  434. MapKeyComparator* CreateMultipleFieldsMapKeyComparator(
  435. const std::vector<std::vector<const FieldDescriptor*> >& key_field_paths);
  436. // Add a custom ignore criteria that is evaluated in addition to the
  437. // ignored fields added with IgnoreField.
  438. // Takes ownership of ignore_criteria.
  439. void AddIgnoreCriteria(IgnoreCriteria* ignore_criteria);
  440. // Indicates that any field with the given descriptor should be
  441. // ignored for the purposes of comparing two messages. This applies
  442. // to fields nested in the message structure as well as top level
  443. // ones. When the MessageDifferencer encounters an ignored field,
  444. // ReportIgnored is called on the reporter, if one is specified.
  445. //
  446. // The only place where the field's 'ignored' status is not applied is when
  447. // it is being used as a key in a field passed to TreatAsMap or is one of
  448. // the fields passed to TreatAsMapWithMultipleFieldsAsKey.
  449. // In this case it is compared in key matching but after that it's ignored
  450. // in value comparison.
  451. void IgnoreField(const FieldDescriptor* field);
  452. // Sets the field comparator used to determine differences between protocol
  453. // buffer fields. By default it's set to a DefaultFieldComparator instance.
  454. // MessageDifferencer doesn't take ownership over the passed object.
  455. // Note that this method must be called before Compare for the comparator to
  456. // be used.
  457. void set_field_comparator(FieldComparator* comparator);
  458. // DEPRECATED. Pass a DefaultFieldComparator instance instead.
  459. // Sets the fraction and margin for the float comparison of a given field.
  460. // Uses MathUtil::WithinFractionOrMargin to compare the values.
  461. // NOTE: this method does nothing if differencer's field comparator has been
  462. // set to a custom object.
  463. //
  464. // REQUIRES: field->cpp_type == FieldDescriptor::CPPTYPE_DOUBLE or
  465. // field->cpp_type == FieldDescriptor::CPPTYPE_FLOAT
  466. // REQUIRES: float_comparison_ == APPROXIMATE
  467. void SetFractionAndMargin(const FieldDescriptor* field, double fraction,
  468. double margin);
  469. // Sets the type of comparison (as defined in the MessageFieldComparison
  470. // enumeration above) that is used by this differencer when determining how
  471. // to compare fields in messages.
  472. void set_message_field_comparison(MessageFieldComparison comparison);
  473. // Tells the differencer whether or not to report matches. This method must
  474. // be called before Compare. The default for a new differencer is false.
  475. void set_report_matches(bool report_matches) {
  476. report_matches_ = report_matches;
  477. }
  478. // Tells the differencer whether or not to report moves (in a set or map
  479. // repeated field). This method must be called before Compare. The default for
  480. // a new differencer is true.
  481. void set_report_moves(bool report_moves) {
  482. report_moves_ = report_moves;
  483. }
  484. // Sets the scope of the comparison (as defined in the Scope enumeration
  485. // above) that is used by this differencer when determining which fields to
  486. // compare between the messages.
  487. void set_scope(Scope scope);
  488. // Returns the current scope used by this differencer.
  489. Scope scope();
  490. // DEPRECATED. Pass a DefaultFieldComparator instance instead.
  491. // Sets the type of comparison (as defined in the FloatComparison enumeration
  492. // above) that is used by this differencer when comparing float (and double)
  493. // fields in messages.
  494. // NOTE: this method does nothing if differencer's field comparator has been
  495. // set to a custom object.
  496. void set_float_comparison(FloatComparison comparison);
  497. // Sets the type of comparison for repeated field (as defined in the
  498. // RepeatedFieldComparison enumeration above) that is used by this
  499. // differencer when compare repeated fields in messages.
  500. void set_repeated_field_comparison(RepeatedFieldComparison comparison);
  501. // Compares the two specified messages, returning true if they are the same,
  502. // false otherwise. If this method returns false, any changes between the
  503. // two messages will be reported if a Reporter was specified via
  504. // ReportDifferencesTo (see also ReportDifferencesToString).
  505. //
  506. // This method REQUIRES that the two messages have the same
  507. // Descriptor (message1.GetDescriptor() == message2.GetDescriptor()).
  508. bool Compare(const Message& message1, const Message& message2);
  509. // Same as above, except comparing only the list of fields specified by the
  510. // two vectors of FieldDescriptors.
  511. bool CompareWithFields(
  512. const Message& message1, const Message& message2,
  513. const std::vector<const FieldDescriptor*>& message1_fields,
  514. const std::vector<const FieldDescriptor*>& message2_fields);
  515. // Automatically creates a reporter that will output the differences
  516. // found (if any) to the specified output string pointer. Note that this
  517. // method must be called before Compare.
  518. void ReportDifferencesToString(string* output);
  519. // Tells the MessageDifferencer to report differences via the specified
  520. // reporter. Note that this method must be called before Compare for
  521. // the reporter to be used. It is the responsibility of the caller to delete
  522. // this object.
  523. // If the provided pointer equals NULL, the MessageDifferencer stops reporting
  524. // differences to any previously set reporters or output strings.
  525. void ReportDifferencesTo(Reporter* reporter);
  526. // An implementation of the MessageDifferencer Reporter that outputs
  527. // any differences found in human-readable form to the supplied
  528. // ZeroCopyOutputStream or Printer. If a printer is used, the delimiter
  529. // *must* be '$'.
  530. //
  531. // WARNING: this reporter does not necessarily flush its output until it is
  532. // destroyed. As a result, it is not safe to assume the output is valid or
  533. // complete until after you destroy the reporter. For example, if you use a
  534. // StreamReporter to write to a StringOutputStream, the target string may
  535. // contain uninitialized data until the reporter is destroyed.
  536. class LIBPROTOBUF_EXPORT StreamReporter : public Reporter {
  537. public:
  538. explicit StreamReporter(io::ZeroCopyOutputStream* output);
  539. explicit StreamReporter(io::Printer* printer); // delimiter '$'
  540. virtual ~StreamReporter();
  541. // When set to true, the stream reporter will also output aggregates nodes
  542. // (i.e. messages and groups) whose subfields have been modified. When
  543. // false, will only report the individual subfields. Defaults to false.
  544. void set_report_modified_aggregates(bool report) {
  545. report_modified_aggregates_ = report;
  546. }
  547. // The following are implementations of the methods described above.
  548. virtual void ReportAdded(const Message& message1, const Message& message2,
  549. const std::vector<SpecificField>& field_path);
  550. virtual void ReportDeleted(const Message& message1,
  551. const Message& message2,
  552. const std::vector<SpecificField>& field_path);
  553. virtual void ReportModified(const Message& message1,
  554. const Message& message2,
  555. const std::vector<SpecificField>& field_path);
  556. virtual void ReportMoved(const Message& message1,
  557. const Message& message2,
  558. const std::vector<SpecificField>& field_path);
  559. virtual void ReportMatched(const Message& message1,
  560. const Message& message2,
  561. const std::vector<SpecificField>& field_path);
  562. virtual void ReportIgnored(const Message& message1,
  563. const Message& message2,
  564. const std::vector<SpecificField>& field_path);
  565. virtual void ReportUnknownFieldIgnored(
  566. const Message& message1, const Message& message2,
  567. const std::vector<SpecificField>& field_path);
  568. protected:
  569. // Prints the specified path of fields to the buffer. message is used to
  570. // print map keys.
  571. virtual void PrintPath(const std::vector<SpecificField>& field_path,
  572. bool left_side, const Message& message);
  573. // Prints the specified path of fields to the buffer.
  574. virtual void PrintPath(const std::vector<SpecificField>& field_path,
  575. bool left_side);
  576. // Prints the value of fields to the buffer. left_side is true if the
  577. // given message is from the left side of the comparison, false if it
  578. // was the right. This is relevant only to decide whether to follow
  579. // unknown_field_index1 or unknown_field_index2 when an unknown field
  580. // is encountered in field_path.
  581. virtual void PrintValue(const Message& message,
  582. const std::vector<SpecificField>& field_path,
  583. bool left_side);
  584. // Prints the specified path of unknown fields to the buffer.
  585. virtual void PrintUnknownFieldValue(const UnknownField* unknown_field);
  586. // Just print a string
  587. void Print(const string& str);
  588. private:
  589. io::Printer* printer_;
  590. bool delete_printer_;
  591. bool report_modified_aggregates_;
  592. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StreamReporter);
  593. };
  594. private:
  595. friend class DefaultFieldComparator;
  596. // A MapKeyComparator to be used in TreatAsMapUsingKeyComparator.
  597. // Implementation of this class needs to do field value comparison which
  598. // relies on some private methods of MessageDifferencer. That's why this
  599. // class is declared as a nested class of MessageDifferencer.
  600. class MultipleFieldsMapKeyComparator;
  601. // A MapKeyComparator for use with map_entries.
  602. class LIBPROTOBUF_EXPORT MapEntryKeyComparator : public MapKeyComparator {
  603. public:
  604. explicit MapEntryKeyComparator(MessageDifferencer* message_differencer);
  605. virtual bool IsMatch(const Message& message1, const Message& message2,
  606. const std::vector<SpecificField>& parent_fields) const;
  607. private:
  608. MessageDifferencer* message_differencer_;
  609. };
  610. // Returns true if field1's number() is less than field2's.
  611. static bool FieldBefore(const FieldDescriptor* field1,
  612. const FieldDescriptor* field2);
  613. // Combine the two lists of fields into the combined_fields output vector.
  614. // All fields present in both lists will always be included in the combined
  615. // list. Fields only present in one of the lists will only appear in the
  616. // combined list if the corresponding fields_scope option is set to FULL.
  617. void CombineFields(const std::vector<const FieldDescriptor*>& fields1,
  618. Scope fields1_scope,
  619. const std::vector<const FieldDescriptor*>& fields2,
  620. Scope fields2_scope,
  621. std::vector<const FieldDescriptor*>* combined_fields);
  622. // Internal version of the Compare method which performs the actual
  623. // comparison. The parent_fields vector is a vector containing field
  624. // descriptors of all fields accessed to get to this comparison operation
  625. // (i.e. if the current message is an embedded message, the parent_fields
  626. // vector will contain the field that has this embedded message).
  627. bool Compare(const Message& message1, const Message& message2,
  628. std::vector<SpecificField>* parent_fields);
  629. // Compares all the unknown fields in two messages.
  630. bool CompareUnknownFields(const Message& message1, const Message& message2,
  631. const google::protobuf::UnknownFieldSet&,
  632. const google::protobuf::UnknownFieldSet&,
  633. std::vector<SpecificField>* parent_fields);
  634. // Compares the specified messages for the requested field lists. The field
  635. // lists are modified depending on comparison settings, and then passed to
  636. // CompareWithFieldsInternal.
  637. bool CompareRequestedFieldsUsingSettings(
  638. const Message& message1, const Message& message2,
  639. const std::vector<const FieldDescriptor*>& message1_fields,
  640. const std::vector<const FieldDescriptor*>& message2_fields,
  641. std::vector<SpecificField>* parent_fields);
  642. // Compares the specified messages with the specified field lists.
  643. bool CompareWithFieldsInternal(
  644. const Message& message1, const Message& message2,
  645. const std::vector<const FieldDescriptor*>& message1_fields,
  646. const std::vector<const FieldDescriptor*>& message2_fields,
  647. std::vector<SpecificField>* parent_fields);
  648. // Compares the repeated fields, and report the error.
  649. bool CompareRepeatedField(const Message& message1, const Message& message2,
  650. const FieldDescriptor* field,
  651. std::vector<SpecificField>* parent_fields);
  652. // Shorthand for CompareFieldValueUsingParentFields with NULL parent_fields.
  653. bool CompareFieldValue(const Message& message1,
  654. const Message& message2,
  655. const FieldDescriptor* field,
  656. int index1,
  657. int index2);
  658. // Compares the specified field on the two messages, returning
  659. // true if they are the same, false otherwise. For repeated fields,
  660. // this method only compares the value in the specified index. This method
  661. // uses Compare functions to recurse into submessages.
  662. // The parent_fields vector is used in calls to a Reporter instance calls.
  663. // It can be NULL, in which case the MessageDifferencer will create new
  664. // list of parent messages if it needs to recursively compare the given field.
  665. // To avoid confusing users you should not set it to NULL unless you modified
  666. // Reporter to handle the change of parent_fields correctly.
  667. bool CompareFieldValueUsingParentFields(
  668. const Message& message1,
  669. const Message& message2,
  670. const FieldDescriptor* field,
  671. int index1,
  672. int index2,
  673. std::vector<SpecificField>* parent_fields);
  674. // Compares the specified field on the two messages, returning comparison
  675. // result, as returned by appropriate FieldComparator.
  676. FieldComparator::ComparisonResult GetFieldComparisonResult(
  677. const Message& message1, const Message& message2,
  678. const FieldDescriptor* field, int index1, int index2,
  679. const FieldContext* field_context);
  680. // Check if the two elements in the repeated field are match to each other.
  681. // if the key_comprator is NULL, this function returns true when the two
  682. // elements are equal.
  683. bool IsMatch(const FieldDescriptor* repeated_field,
  684. const MapKeyComparator* key_comparator,
  685. const Message* message1, const Message* message2,
  686. const std::vector<SpecificField>& parent_fields,
  687. int index1, int index2);
  688. // Returns true when this repeated field has been configured to be treated
  689. // as a set.
  690. bool IsTreatedAsSet(const FieldDescriptor* field);
  691. // Returns true when this repeated field is to be compared as a subset, ie.
  692. // has been configured to be treated as a set or map and scope is set to
  693. // PARTIAL.
  694. bool IsTreatedAsSubset(const FieldDescriptor* field);
  695. // Returns true if this field is to be ignored when this
  696. // MessageDifferencer compares messages.
  697. bool IsIgnored(
  698. const Message& message1,
  699. const Message& message2,
  700. const FieldDescriptor* field,
  701. const std::vector<SpecificField>& parent_fields);
  702. // Returns true if this unknown field is to be ignored when this
  703. // MessageDifferencer compares messages.
  704. bool IsUnknownFieldIgnored(const Message& message1, const Message& message2,
  705. const SpecificField& field,
  706. const std::vector<SpecificField>& parent_fields);
  707. // Returns MapKeyComparator* when this field has been configured to be treated
  708. // as a map or its is_map() return true. If not, returns NULL.
  709. const MapKeyComparator* GetMapKeyComparator(
  710. const FieldDescriptor* field) const;
  711. // Attempts to match indices of a repeated field, so that the contained values
  712. // match. Clears output vectors and sets their values to indices of paired
  713. // messages, ie. if message1[0] matches message2[1], then match_list1[0] == 1
  714. // and match_list2[1] == 0. The unmatched indices are indicated by -1.
  715. // This method returns false if the match failed. However, it doesn't mean
  716. // that the comparison succeeds when this method returns true (you need to
  717. // double-check in this case).
  718. bool MatchRepeatedFieldIndices(
  719. const Message& message1,
  720. const Message& message2,
  721. const FieldDescriptor* repeated_field,
  722. const std::vector<SpecificField>& parent_fields,
  723. std::vector<int>* match_list1,
  724. std::vector<int>* match_list2);
  725. // If "any" is of type google.protobuf.Any, extract its payload using
  726. // DynamicMessageFactory and store in "data".
  727. bool UnpackAny(const Message& any, std::unique_ptr<Message>* data);
  728. // Checks if index is equal to new_index in all the specific fields.
  729. static bool CheckPathChanged(const std::vector<SpecificField>& parent_fields);
  730. // Defines a map between field descriptors and their MapKeyComparators.
  731. // Used for repeated fields when they are configured as TreatAsMap.
  732. typedef std::map<const FieldDescriptor*,
  733. const MapKeyComparator*> FieldKeyComparatorMap;
  734. // Defines a set to store field descriptors. Used for repeated fields when
  735. // they are configured as TreatAsSet.
  736. typedef std::set<const FieldDescriptor*> FieldSet;
  737. Reporter* reporter_;
  738. DefaultFieldComparator default_field_comparator_;
  739. FieldComparator* field_comparator_;
  740. MessageFieldComparison message_field_comparison_;
  741. Scope scope_;
  742. RepeatedFieldComparison repeated_field_comparison_;
  743. FieldSet set_fields_;
  744. FieldSet list_fields_;
  745. // Keeps track of MapKeyComparators that are created within
  746. // MessageDifferencer. These MapKeyComparators should be deleted
  747. // before MessageDifferencer is destroyed.
  748. // When TreatAsMap or TreatAsMapWithMultipleFieldsAsKey is called, we don't
  749. // store the supplied FieldDescriptors directly. Instead, a new
  750. // MapKeyComparator is created for comparison purpose.
  751. std::vector<MapKeyComparator*> owned_key_comparators_;
  752. FieldKeyComparatorMap map_field_key_comparator_;
  753. MapEntryKeyComparator map_entry_key_comparator_;
  754. std::vector<IgnoreCriteria*> ignore_criteria_;
  755. FieldSet ignored_fields_;
  756. bool report_matches_;
  757. bool report_moves_;
  758. string* output_string_;
  759. std::unique_ptr<DynamicMessageFactory> dynamic_message_factory_;
  760. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageDifferencer);
  761. };
  762. // This class provides extra information to the FieldComparator::Compare
  763. // function.
  764. class LIBPROTOBUF_EXPORT FieldContext {
  765. public:
  766. explicit FieldContext(
  767. std::vector<MessageDifferencer::SpecificField>* parent_fields)
  768. : parent_fields_(parent_fields) {}
  769. std::vector<MessageDifferencer::SpecificField>* parent_fields() const {
  770. return parent_fields_;
  771. }
  772. private:
  773. std::vector<MessageDifferencer::SpecificField>* parent_fields_;
  774. };
  775. }
  776. }
  777. } // namespace google
  778. #endif // GOOGLE_PROTOBUF_UTIL_MESSAGE_DIFFERENCER_H__