将两个google :: protobuf :: Message对象相互比较的最简单方法是什么?

时间:2022-08-25 14:18:05

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

我想比较两个任意的google :: protobuf :: Message对象。

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.

此外,可能是我遗漏了一些关于这些对象无法比较的主要问题 - 我想知道是否是这样。

3 个解决方案

#1


8  

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.

在C ++中,您可以序列化两者并比较字节。或者,您可以编写一些代码,通过反射迭代字段并比较它们。

#2


4  

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

你可以使用google :: protobuf :: util :: MessageDifferencer。看看我的答案。

#3


0  

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.

例如,我有一个7节点的深度结构(不要问为什么),但对于我来说,逐字逐句比较将是一个想法。

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.

您可以使用的一个标记是获取重复字段的大小并进行比较。但是你依赖于消息结构和大小。

#1


8  

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.

在C ++中,您可以序列化两者并比较字节。或者,您可以编写一些代码,通过反射迭代字段并比较它们。

#2


4  

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

你可以使用google :: protobuf :: util :: MessageDifferencer。看看我的答案。

#3


0  

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.

例如,我有一个7节点的深度结构(不要问为什么),但对于我来说,逐字逐句比较将是一个想法。

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.

您可以使用的一个标记是获取重复字段的大小并进行比较。但是你依赖于消息结构和大小。