简体   繁体   中英

What is the simplest way to compare two google::protobuf::Message objects with each other?

I want to compare two arbitrary google::protobuf::Message objects.

I assume that objects are equal when

  • their descriptions contain fields with equal field numbers and corresponding field types.
  • their reflections have same set of fields (and the same number of repeated fields) and equal corresponding field values.

I know, that I can write such comparison function by myself, but may be there is already some simple or predefined solution?

Also, may be I'm missing some principal issue about incomparability of such objects - I would like to know if it's so.

From https://groups.google.com/d/msg/protobuf/5sOExQkB2eQ/ZSBNZI0K54YJ :

In C++, you could serialize the two and compare the bytes. Alternatively, you could write some code that iterates over the fields via reflection and compares them.

You can use google::protobuf::util::MessageDifferencer for this. Take a look at my answer here .

My experience has shown me that the comparison depends on the message structure. If you have a deep tree like message structure then do not compare if you have to be finished quickly.

For example I have a 7 nodes deep structure (don't ask why) but it would be a bed idea for me to compare field by field.

One tick you can use is to get sizes of the repeated fields and compare them. But you are sill dependent on the message structure and size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM