append "ByDef" to names of message accessors that use reflection

PiperOrigin-RevId: 493018484
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent a3e49f90ac
commit b747edb830
  1. 4
      lua/msg.c
  2. 4
      python/convert.c
  3. 4
      python/extension_dict.c
  4. 32
      python/message.c
  5. 24
      upb/json/decode.c
  6. 24
      upb/json/encode.c
  7. 4
      upb/message/test.cc
  8. 1
      upb/reflection/field_def.h
  9. 28
      upb/reflection/message.c
  10. 105
      upb/reflection/message.h
  11. 2
      upb/util/required_fields.c

@ -825,7 +825,7 @@ static int lupb_msg_index(lua_State* L) {
}
} else {
/* Value type, just push value and return .*/
upb_MessageValue val = upb_Message_Get(msg, f);
upb_MessageValue val = upb_Message_GetFieldByDef(msg, f);
lupb_pushmsgval(L, 0, upb_FieldDef_CType(f), val);
}
@ -882,7 +882,7 @@ static int lupb_Message_Newindex(lua_State* L) {
lupb_Arena_Fuseobjs(L, 1, 3);
}
upb_Message_Set(msg, f, msgval, lupb_Arenaget(L, 1));
upb_Message_SetFieldByDef(msg, f, msgval, lupb_Arenaget(L, 1));
/* Return the new value for chained assignments. */
lua_pushvalue(L, 3);

@ -397,7 +397,7 @@ bool upb_Message_IsEqual(const upb_Message* msg1, const upb_Message* msg2,
// using upb_Message_Next(msg2). If the two messages have the same set
// of fields, this will yield the same field.
// 3. For extension fields, we have to actually search for the corresponding
// field, which we do with upb_Message_Get(msg2, ext_f1).
// field, which we do with upb_Message_GetFieldByDef(msg2, ext_f1).
// 4. Once iteration over msg1 is complete, we call upb_Message_Next(msg2)
// one
// final time to verify that we have visited all of msg2's regular fields
@ -413,7 +413,7 @@ bool upb_Message_IsEqual(const upb_Message* msg1, const upb_Message* msg2,
size_t iter2 = kUpb_Message_Begin;
while (upb_Message_Next(msg1, m, symtab, &f1, &val1, &iter1)) {
if (upb_FieldDef_IsExtension(f1)) {
val2 = upb_Message_Get(msg2, f1);
val2 = upb_Message_GetFieldByDef(msg2, f1);
} else {
if (!upb_Message_Next(msg2, m, NULL, &f2, &val2, &iter2) || f1 != f2) {
return false;

@ -113,10 +113,10 @@ static int PyUpb_ExtensionDict_Contains(PyObject* _self, PyObject* key) {
upb_Message* msg = PyUpb_Message_GetIfReified(self->msg);
if (!msg) return 0;
if (upb_FieldDef_IsRepeated(f)) {
upb_MessageValue val = upb_Message_Get(msg, f);
upb_MessageValue val = upb_Message_GetFieldByDef(msg, f);
return upb_Array_Size(val.array_val) > 0;
} else {
return upb_Message_Has(msg, f);
return upb_Message_HasFieldByDef(msg, f);
}
}

@ -477,7 +477,7 @@ static bool PyUpb_Message_InitScalarAttribute(upb_Message* msg,
upb_MessageValue msgval;
assert(!PyErr_Occurred());
if (!PyUpb_PyToUpb(value, f, &msgval, arena)) return false;
upb_Message_Set(msg, f, msgval, arena);
upb_Message_SetFieldByDef(msg, f, msgval, arena);
return true;
}
@ -607,7 +607,7 @@ static const upb_FieldDef* PyUpb_Message_InitAsMsg(PyUpb_Message* m,
static void PyUpb_Message_SetField(PyUpb_Message* parent, const upb_FieldDef* f,
PyUpb_Message* child, upb_Arena* arena) {
upb_MessageValue msgval = {.msg_val = PyUpb_Message_GetMsg(child)};
upb_Message_Set(PyUpb_Message_GetMsg(parent), f, msgval, arena);
upb_Message_SetFieldByDef(PyUpb_Message_GetMsg(parent), f, msgval, arena);
PyUpb_WeakMap_Delete(parent->unset_subobj_map, f);
// Releases a ref previously owned by child->ptr.parent of our child.
Py_DECREF(child);
@ -719,8 +719,9 @@ static void PyUpb_Message_SyncSubobjs(PyUpb_Message* self) {
while (PyUpb_WeakMap_Next(subobj_map, &key, &obj, &iter)) {
const upb_FieldDef* f = key;
if (upb_FieldDef_HasPresence(f) && !upb_Message_Has(msg, f)) continue;
upb_MessageValue msgval = upb_Message_Get(msg, f);
if (upb_FieldDef_HasPresence(f) && !upb_Message_HasFieldByDef(msg, f))
continue;
upb_MessageValue msgval = upb_Message_GetFieldByDef(msg, f);
PyUpb_WeakMap_DeleteIter(subobj_map, &iter);
if (upb_FieldDef_IsMap(f)) {
if (!msgval.map_val) continue;
@ -785,7 +786,8 @@ void PyUpb_Message_SetConcreteSubobj(PyObject* _self, const upb_FieldDef* f,
PyUpb_Message* self = (void*)_self;
PyUpb_Message_EnsureReified(self);
PyUpb_Message_CacheDelete(_self, f);
upb_Message_Set(self->ptr.msg, f, subobj, PyUpb_Arena_Get(self->arena));
upb_Message_SetFieldByDef(self->ptr.msg, f, subobj,
PyUpb_Arena_Get(self->arena));
}
static void PyUpb_Message_Dealloc(PyObject* _self) {
@ -905,7 +907,7 @@ PyObject* PyUpb_Message_GetScalarValue(PyUpb_Message* self,
// Unset message always returns default values.
val = upb_FieldDef_Default(field);
} else {
val = upb_Message_Get(self->ptr.msg, field);
val = upb_Message_GetFieldByDef(self->ptr.msg, field);
}
return PyUpb_UpbToPy(val, field, self->arena);
}
@ -929,7 +931,7 @@ PyObject* PyUpb_Message_GetFieldValue(PyObject* _self,
bool seq = upb_FieldDef_IsRepeated(field);
if ((PyUpb_Message_IsStub(self) && (submsg || seq)) ||
(submsg && !seq && !upb_Message_Has(self->ptr.msg, field))) {
(submsg && !seq && !upb_Message_HasFieldByDef(self->ptr.msg, field))) {
return PyUpb_Message_GetStub(self, field);
} else if (seq) {
return PyUpb_Message_GetPresentWrapper(self, field);
@ -959,7 +961,7 @@ int PyUpb_Message_SetFieldValue(PyObject* _self, const upb_FieldDef* field,
return -1;
}
upb_Message_Set(self->ptr.msg, field, val, arena);
upb_Message_SetFieldByDef(self->ptr.msg, field, val, arena);
return 0;
}
@ -1041,7 +1043,7 @@ static PyObject* PyUpb_Message_HasField(PyObject* _self, PyObject* arg) {
if (PyUpb_Message_IsStub(self)) Py_RETURN_FALSE;
return PyBool_FromLong(field ? upb_Message_Has(self->ptr.msg, field)
return PyBool_FromLong(field ? upb_Message_HasFieldByDef(self->ptr.msg, field)
: upb_Message_WhichOneof(self->ptr.msg, oneof) !=
NULL);
}
@ -1288,13 +1290,13 @@ static PyObject* PyUpb_Message_Clear(PyUpb_Message* self, PyObject* args) {
const upb_FieldDef* f = key;
PyUpb_WeakMap_DeleteIter(subobj_map, &iter);
if (upb_FieldDef_IsMap(f)) {
assert(upb_Message_Get(msg, f).map_val == NULL);
assert(upb_Message_GetFieldByDef(msg, f).map_val == NULL);
PyUpb_MapContainer_Reify(obj, NULL);
} else if (upb_FieldDef_IsRepeated(f)) {
assert(upb_Message_Get(msg, f).array_val == NULL);
assert(upb_Message_GetFieldByDef(msg, f).array_val == NULL);
PyUpb_RepeatedContainer_Reify(obj, NULL);
} else {
assert(!upb_Message_Has(msg, f));
assert(!upb_Message_HasFieldByDef(msg, f));
PyUpb_Message* sub = (void*)obj;
assert(self == sub->ptr.parent);
PyUpb_Message_Reify(sub, f, NULL);
@ -1302,7 +1304,7 @@ static PyObject* PyUpb_Message_Clear(PyUpb_Message* self, PyObject* args) {
}
}
upb_Message_Clear(self->ptr.msg, msgdef);
upb_Message_ClearByDef(self->ptr.msg, msgdef);
Py_RETURN_NONE;
}
@ -1335,7 +1337,7 @@ void PyUpb_Message_DoClearField(PyObject* _self, const upb_FieldDef* f) {
}
Py_XDECREF(sub);
upb_Message_ClearField(self->ptr.msg, f);
upb_Message_ClearFieldByDef(self->ptr.msg, f);
}
static PyObject* PyUpb_Message_ClearExtension(PyObject* _self, PyObject* arg) {
@ -1460,7 +1462,7 @@ static PyObject* PyUpb_Message_HasExtension(PyObject* _self,
return NULL;
}
if (!msg) Py_RETURN_FALSE;
return PyBool_FromLong(upb_Message_Has(msg, f));
return PyBool_FromLong(upb_Message_HasFieldByDef(msg, f));
}
void PyUpb_Message_ReportInitializationErrors(const upb_MessageDef* msgdef,

@ -938,7 +938,7 @@ static void jsondec_field(jsondec* d, upb_Message* msg,
jsondec_tomsg(d, submsg, subm);
} else {
upb_MessageValue val = jsondec_value(d, f);
upb_Message_Set(msg, f, val, d->arena);
upb_Message_SetFieldByDef(msg, f, val, d->arena);
}
d->debug_field = preserved;
@ -1091,9 +1091,10 @@ static void jsondec_timestamp(jsondec* d, upb_Message* msg,
jsondec_err(d, "Timestamp out of range");
}
upb_Message_Set(msg, upb_MessageDef_FindFieldByNumber(m, 1), seconds,
d->arena);
upb_Message_Set(msg, upb_MessageDef_FindFieldByNumber(m, 2), nanos, d->arena);
upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 1),
seconds, d->arena);
upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 2), nanos,
d->arena);
return;
malformed:
@ -1126,9 +1127,10 @@ static void jsondec_duration(jsondec* d, upb_Message* msg,
nanos.int32_val = -nanos.int32_val;
}
upb_Message_Set(msg, upb_MessageDef_FindFieldByNumber(m, 1), seconds,
d->arena);
upb_Message_Set(msg, upb_MessageDef_FindFieldByNumber(m, 2), nanos, d->arena);
upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 1),
seconds, d->arena);
upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 2), nanos,
d->arena);
}
static void jsondec_listvalue(jsondec* d, upb_Message* msg,
@ -1223,7 +1225,7 @@ static void jsondec_wellknownvalue(jsondec* d, upb_Message* msg,
UPB_UNREACHABLE();
}
upb_Message_Set(msg, f, val, d->arena);
upb_Message_SetFieldByDef(msg, f, val, d->arena);
}
static upb_StringView jsondec_mask(jsondec* d, const char* buf,
@ -1310,7 +1312,7 @@ static const upb_MessageDef* jsondec_typeurl(jsondec* d, upb_Message* msg,
upb_MessageValue val;
val.str_val = type_url;
upb_Message_Set(msg, type_url_f, val, d->arena);
upb_Message_SetFieldByDef(msg, type_url_f, val, d->arena);
/* Find message name after the last '/' */
while (ptr > type_url.data && *--ptr != '/') {
@ -1394,14 +1396,14 @@ static void jsondec_any(jsondec* d, upb_Message* msg, const upb_MessageDef* m) {
(char**)&encoded.str_val.data, &encoded.str_val.size);
// TODO(b/235839510): We should fail gracefully here on a bad return status.
UPB_ASSERT(status == kUpb_EncodeStatus_Ok);
upb_Message_Set(msg, value_f, encoded, d->arena);
upb_Message_SetFieldByDef(msg, value_f, encoded, d->arena);
}
static void jsondec_wrapper(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(m, 1);
upb_MessageValue val = jsondec_value(d, value_f);
upb_Message_Set(msg, value_f, val, d->arena);
upb_Message_SetFieldByDef(msg, value_f, val, d->arena);
}
static void jsondec_wellknown(jsondec* d, upb_Message* msg,

@ -143,8 +143,8 @@ static void jsonenc_timestamp(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* seconds_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_FieldDef* nanos_f = upb_MessageDef_FindFieldByNumber(m, 2);
int64_t seconds = upb_Message_Get(msg, seconds_f).int64_val;
int32_t nanos = upb_Message_Get(msg, nanos_f).int32_val;
int64_t seconds = upb_Message_GetFieldByDef(msg, seconds_f).int64_val;
int32_t nanos = upb_Message_GetFieldByDef(msg, nanos_f).int32_val;
int L, N, I, J, K, hour, min, sec;
if (seconds < -62135596800) {
@ -186,8 +186,8 @@ static void jsonenc_duration(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* seconds_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_FieldDef* nanos_f = upb_MessageDef_FindFieldByNumber(m, 2);
int64_t seconds = upb_Message_Get(msg, seconds_f).int64_val;
int32_t nanos = upb_Message_Get(msg, nanos_f).int32_val;
int64_t seconds = upb_Message_GetFieldByDef(msg, seconds_f).int64_val;
int32_t nanos = upb_Message_GetFieldByDef(msg, nanos_f).int32_val;
bool negative = false;
if (seconds > 315576000000 || seconds < -315576000000 ||
@ -348,7 +348,7 @@ static void upb_JsonEncode_Float(jsonenc* e, float val) {
static void jsonenc_wrapper(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(m, 1);
upb_MessageValue val = upb_Message_Get(msg, val_f);
upb_MessageValue val = upb_Message_GetFieldByDef(msg, val_f);
jsonenc_scalar(e, val, val_f);
}
@ -393,8 +393,8 @@ static void jsonenc_any(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* type_url_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(m, 2);
upb_StringView type_url = upb_Message_Get(msg, type_url_f).str_val;
upb_StringView value = upb_Message_Get(msg, value_f).str_val;
upb_StringView type_url = upb_Message_GetFieldByDef(msg, type_url_f).str_val;
upb_StringView value = upb_Message_GetFieldByDef(msg, value_f).str_val;
const upb_MessageDef* any_m = jsonenc_getanymsg(e, type_url);
const upb_MiniTable* any_layout = upb_MessageDef_MiniTable(any_m);
upb_Arena* arena = jsonenc_arena(e);
@ -452,7 +452,7 @@ static void jsonenc_fieldpath(jsonenc* e, upb_StringView path) {
static void jsonenc_fieldmask(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* paths_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_Array* paths = upb_Message_Get(msg, paths_f).array_val;
const upb_Array* paths = upb_Message_GetFieldByDef(msg, paths_f).array_val;
bool first = true;
size_t i, n = 0;
@ -473,7 +473,7 @@ static void jsonenc_struct(jsonenc* e, const upb_Message* msg,
jsonenc_putstr(e, "{");
const upb_FieldDef* fields_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_Map* fields = upb_Message_Get(msg, fields_f).map_val;
const upb_Map* fields = upb_Message_GetFieldByDef(msg, fields_f).map_val;
if (fields) {
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(fields_f);
@ -498,7 +498,7 @@ static void jsonenc_listvalue(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* values_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_MessageDef* values_m = upb_FieldDef_MessageSubDef(values_f);
const upb_Array* values = upb_Message_Get(msg, values_f).array_val;
const upb_Array* values = upb_Message_GetFieldByDef(msg, values_f).array_val;
size_t i;
bool first = true;
@ -738,8 +738,8 @@ static void jsonenc_msgfields(jsonenc* e, const upb_Message* msg,
int n = upb_MessageDef_FieldCount(m);
for (i = 0; i < n; i++) {
f = upb_MessageDef_Field(m, i);
if (!upb_FieldDef_HasPresence(f) || upb_Message_Has(msg, f)) {
jsonenc_fieldval(e, f, upb_Message_Get(msg, f), &first);
if (!upb_FieldDef_HasPresence(f) || upb_Message_HasFieldByDef(msg, f)) {
jsonenc_fieldval(e, f, upb_Message_GetFieldByDef(msg, f), &first);
}
}
} else {

@ -449,7 +449,7 @@ TEST(MessageTest, MaxRequiredFields) {
for (int i = 1; i <= 61; i++) {
upb::FieldDefPtr f = m.FindFieldByNumber(i);
ASSERT_TRUE(f);
upb_Message_Set(test_msg, f.ptr(), val, arena.ptr());
upb_Message_SetFieldByDef(test_msg, f.ptr(), val, arena.ptr());
}
// Fails, field 63 still isn't set.
@ -460,7 +460,7 @@ TEST(MessageTest, MaxRequiredFields) {
// Succeeds, all required fields are set.
upb::FieldDefPtr f = m.FindFieldByNumber(62);
ASSERT_TRUE(f);
upb_Message_Set(test_msg, f.ptr(), val, arena.ptr());
upb_Message_SetFieldByDef(test_msg, f.ptr(), val, arena.ptr());
serialized = upb_test_TestMaxRequiredFields_serialize_ex(
test_msg, kUpb_EncodeOption_CheckRequired, arena.ptr(), &size);
ASSERT_TRUE(serialized != nullptr);

@ -47,6 +47,7 @@ extern "C" {
const upb_OneofDef* upb_FieldDef_ContainingOneof(const upb_FieldDef* f);
const upb_MessageDef* upb_FieldDef_ContainingType(const upb_FieldDef* f);
upb_CType upb_FieldDef_CType(const upb_FieldDef* f);
upb_MessageValue upb_FieldDef_Default(const upb_FieldDef* f);
const upb_EnumDef* upb_FieldDef_EnumSubDef(const upb_FieldDef* f);
const upb_MessageDef* upb_FieldDef_ExtensionScope(const upb_FieldDef* f);
const upb_FileDef* upb_FieldDef_File(const upb_FieldDef* f);

@ -44,7 +44,7 @@
// Must be last.
#include "upb/port/def.inc"
bool upb_Message_Has(const upb_Message* msg, const upb_FieldDef* f) {
bool upb_Message_HasFieldByDef(const upb_Message* msg, const upb_FieldDef* f) {
UPB_ASSERT(upb_FieldDef_HasPresence(f));
return _upb_MiniTable_HasField(msg, upb_FieldDef_MiniTable(f));
}
@ -54,7 +54,7 @@ const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
const upb_FieldDef* f = upb_OneofDef_Field(o, 0);
if (upb_OneofDef_IsSynthetic(o)) {
UPB_ASSERT(upb_OneofDef_FieldCount(o) == 1);
return upb_Message_Has(msg, f) ? f : NULL;
return upb_Message_HasFieldByDef(msg, f) ? f : NULL;
} else {
const upb_MiniTableField* field = upb_FieldDef_MiniTable(f);
uint32_t oneof_case = _upb_getoneofcase_field(msg, field);
@ -64,8 +64,8 @@ const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
}
}
upb_MessageValue upb_Message_Get(const upb_Message* msg,
const upb_FieldDef* f) {
upb_MessageValue upb_Message_GetFieldByDef(const upb_Message* msg,
const upb_FieldDef* f) {
upb_MessageValue default_val = upb_FieldDef_Default(f);
upb_MessageValue ret;
_upb_MiniTable_GetField(msg, upb_FieldDef_MiniTable(f), &default_val, &ret);
@ -76,12 +76,12 @@ upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
const upb_FieldDef* f,
upb_Arena* a) {
UPB_ASSERT(upb_FieldDef_IsSubMessage(f) || upb_FieldDef_IsRepeated(f));
if (upb_FieldDef_HasPresence(f) && !upb_Message_Has(msg, f)) {
// We need to skip the upb_Message_Get() call in this case.
if (upb_FieldDef_HasPresence(f) && !upb_Message_HasFieldByDef(msg, f)) {
// We need to skip the upb_Message_GetFieldByDef() call in this case.
goto make;
}
upb_MessageValue val = upb_Message_Get(msg, f);
upb_MessageValue val = upb_Message_GetFieldByDef(msg, f);
if (val.array_val) {
return (upb_MutableMessageValue){.array = (upb_Array*)val.array_val};
}
@ -106,21 +106,21 @@ make:
}
val.array_val = ret.array;
upb_Message_Set(msg, f, val, a);
upb_Message_SetFieldByDef(msg, f, val, a);
return ret;
}
bool upb_Message_Set(upb_Message* msg, const upb_FieldDef* f,
upb_MessageValue val, upb_Arena* a) {
bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f,
upb_MessageValue val, upb_Arena* a) {
return _upb_MiniTable_SetField(msg, upb_FieldDef_MiniTable(f), &val, a);
}
void upb_Message_ClearField(upb_Message* msg, const upb_FieldDef* f) {
void upb_Message_ClearFieldByDef(upb_Message* msg, const upb_FieldDef* f) {
_upb_MiniTable_ClearField(msg, upb_FieldDef_MiniTable(f));
}
void upb_Message_Clear(upb_Message* msg, const upb_MessageDef* m) {
void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m) {
_upb_Message_Clear(msg, upb_MessageDef_MiniTable(m));
}
@ -135,11 +135,11 @@ bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
while (++i < n) {
const upb_FieldDef* f = upb_MessageDef_Field(m, i);
const upb_MiniTableField* field = upb_FieldDef_MiniTable(f);
upb_MessageValue val = upb_Message_Get(msg, f);
upb_MessageValue val = upb_Message_GetFieldByDef(msg, f);
// Skip field if unset or empty.
if (upb_MiniTableField_HasPresence(field)) {
if (!upb_Message_Has(msg, f)) continue;
if (!upb_Message_HasFieldByDef(msg, f)) continue;
} else {
switch (upb_FieldMode_Get(field)) {
case kUpb_FieldMode_Map:

@ -38,62 +38,87 @@
extern "C" {
#endif
upb_MessageValue upb_FieldDef_Default(const upb_FieldDef* f);
/* Returns the value associated with this field. */
upb_MessageValue upb_Message_Get(const upb_Message* msg, const upb_FieldDef* f);
/* Returns a mutable pointer to a map, array, or submessage value. If the given
* arena is non-NULL this will construct a new object if it was not previously
* present. May not be called for primitive fields. */
// Returns a mutable pointer to a map, array, or submessage value. If the given
// arena is non-NULL this will construct a new object if it was not previously
// present. May not be called for primitive fields.
upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
const upb_FieldDef* f,
upb_Arena* a);
/* May only be called for fields where upb_FieldDef_HasPresence(f) == true. */
bool upb_Message_Has(const upb_Message* msg, const upb_FieldDef* f);
/* Returns the field that is set in the oneof, or NULL if none are set. */
// Returns the field that is set in the oneof, or NULL if none are set.
const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
const upb_OneofDef* o);
/* Sets the given field to the given value. For a msg/array/map/string, the
* caller must ensure that the target data outlives |msg| (by living either in
* the same arena or a different arena that outlives it).
*
* Returns false if allocation fails. */
bool upb_Message_Set(upb_Message* msg, const upb_FieldDef* f,
upb_MessageValue val, upb_Arena* a);
/* Clears any field presence and sets the value back to its default. */
void upb_Message_ClearField(upb_Message* msg, const upb_FieldDef* f);
/* Clear all data and unknown fields. */
void upb_Message_Clear(upb_Message* msg, const upb_MessageDef* m);
/* Iterate over present fields.
*
* size_t iter = kUpb_Message_Begin;
* const upb_FieldDef *f;
* upb_MessageValue val;
* while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) {
* process_field(f, val);
* }
*
* If ext_pool is NULL, no extensions will be returned. If the given symtab
* returns extensions that don't match what is in this message, those extensions
* will be skipped.
*/
// Clear all data and unknown fields.
void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m);
// Clears any field presence and sets the value back to its default.
void upb_Message_ClearFieldByDef(upb_Message* msg, const upb_FieldDef* f);
// May only be called for fields where upb_FieldDef_HasPresence(f) == true.
bool upb_Message_HasFieldByDef(const upb_Message* msg, const upb_FieldDef* f);
// Returns the value in the message associated with this field def.
upb_MessageValue upb_Message_GetFieldByDef(const upb_Message* msg,
const upb_FieldDef* f);
// Sets the given field to the given value. For a msg/array/map/string, the
// caller must ensure that the target data outlives |msg| (by living either in
// the same arena or a different arena that outlives it).
//
// Returns false if allocation fails.
bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f,
upb_MessageValue val, upb_Arena* a);
// Iterate over present fields.
//
// size_t iter = kUpb_Message_Begin;
// const upb_FieldDef *f;
// upb_MessageValue val;
// while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) {
// process_field(f, val);
// }
//
// If ext_pool is NULL, no extensions will be returned. If the given symtab
// returns extensions that don't match what is in this message, those extensions
// will be skipped.
#define kUpb_Message_Begin -1
bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool, const upb_FieldDef** f,
upb_MessageValue* val, size_t* iter);
/* Clears all unknown field data from this message and all submessages. */
// Clears all unknown field data from this message and all submessages.
bool upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
int maxdepth);
// DEPRECATED FUNCTIONS
// PHP and Ruby need these until we can version-bump them to the current upb.
UPB_INLINE void upb_Message_Clear(upb_Message* msg, const upb_MessageDef* m) {
return upb_Message_ClearByDef(msg, m);
}
UPB_INLINE void upb_Message_ClearField(upb_Message* msg,
const upb_FieldDef* f) {
return upb_Message_ClearFieldByDef(msg, f);
}
UPB_INLINE bool upb_Message_Has(const upb_Message* msg, const upb_FieldDef* f) {
return upb_Message_HasFieldByDef(msg, f);
}
UPB_INLINE upb_MessageValue upb_Message_Get(const upb_Message* msg,
const upb_FieldDef* f) {
return upb_Message_GetFieldByDef(msg, f);
}
UPB_INLINE bool upb_Message_Set(upb_Message* msg, const upb_FieldDef* f,
upb_MessageValue val, upb_Arena* a) {
return upb_Message_SetFieldByDef(msg, f, val, a);
}
#ifdef __cplusplus
} /* extern "C" */
#endif

@ -208,7 +208,7 @@ static void upb_util_FindUnsetInMessage(upb_FindContext* ctx,
const upb_FieldDef* f = upb_MessageDef_Field(m, i);
if (upb_FieldDef_Label(f) != kUpb_Label_Required) continue;
if (!msg || !upb_Message_Has(msg, f)) {
if (!msg || !upb_Message_HasFieldByDef(msg, f)) {
// A required field is missing.
ctx->has_unset_required = true;

Loading…
Cancel
Save