upb: define kUpb_CompareOption_IncludeUnknownFields flag for upb_Message_IsEqual()

PiperOrigin-RevId: 622335300
pull/16408/head
Eric Salo 2024-04-05 18:38:21 -07:00 committed by Copybara-Service
parent 2880938514
commit 3d21bc26c9
4 changed files with 16 additions and 4 deletions

View File

@ -13,6 +13,7 @@
#include "python/message.h"
#include "python/protobuf.h"
#include "upb/base/upcast.h"
#include "upb/message/compare.h"
#include "upb/reflection/def.h"
#include "upb/util/def_to_proto.h"
@ -197,8 +198,9 @@ static PyObject* PyUpb_DescriptorPool_DoAddSerializedFile(
goto done;
}
const upb_MessageDef* m = PyUpb_DescriptorPool_GetFileProtoDef();
const int options = kUpb_CompareOption_IncludeUnknownFields;
if (upb_Message_IsEqualByDef(UPB_UPCAST(proto), UPB_UPCAST(existing), m,
/*options=*/0)) {
options)) {
result = PyUpb_FileDescriptor_Get(file);
goto done;
}

View File

@ -12,6 +12,7 @@
#include "python/extension_dict.h"
#include "python/map.h"
#include "python/repeated.h"
#include "upb/message/compare.h"
#include "upb/message/copy.h"
#include "upb/reflection/def.h"
#include "upb/reflection/message.h"
@ -575,7 +576,8 @@ static bool PyUpb_Message_IsEqual(PyUpb_Message* m1, PyObject* _m2) {
const bool e2 = PyUpb_Message_IsEmpty(m2_msg, m1_msgdef, symtab);
if (e1 || e2) return e1 && e2;
return upb_Message_IsEqualByDef(m1_msg, m2_msg, m1_msgdef, 0);
const int options = kUpb_CompareOption_IncludeUnknownFields;
return upb_Message_IsEqualByDef(m1_msg, m2_msg, m1_msgdef, options);
}
static const upb_FieldDef* PyUpb_Message_InitAsMsg(PyUpb_Message* m,

View File

@ -25,8 +25,8 @@
// Must be last.
#include "upb/port/def.inc"
#define kUpb_BaseField_Begin ((size_t) - 1)
#define kUpb_Extension_Begin ((size_t) - 1)
#define kUpb_BaseField_Begin ((size_t)-1)
#define kUpb_Extension_Begin ((size_t)-1)
#ifdef __cplusplus
extern "C" {
@ -232,6 +232,8 @@ bool upb_Message_IsEqual(const upb_Message* msg1, const upb_Message* msg2,
if (!_upb_Message_BaseFieldsAreEqual(msg1, msg2, m, options)) return false;
if (!_upb_Message_ExtensionsAreEqual(msg1, msg2, m, options)) return false;
if (!(options & kUpb_CompareOption_IncludeUnknownFields)) return true;
// Check the unknown fields.
size_t usize1, usize2;
const char* uf1 = upb_Message_GetUnknown(msg1, &usize1);

View File

@ -20,6 +20,12 @@
// Must be last.
#include "upb/port/def.inc"
enum {
// If set, upb_Message_IsEqual() will attempt to compare unknown fields.
// By its very nature this comparison is inexact.
kUpb_CompareOption_IncludeUnknownFields = (1 << 0)
};
#ifdef __cplusplus
extern "C" {
#endif