Skip to content

Commit

Permalink
append "ByDef" to names of message accessors that use reflection
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 493018484
  • Loading branch information
ericsalo authored and copybara-github committed Dec 5, 2022
1 parent a3e49f9 commit b747edb
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 101 deletions.
4 changes: 2 additions & 2 deletions lua/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions python/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions python/extension_dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
32 changes: 17 additions & 15 deletions python/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -1288,21 +1290,21 @@ 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);
}
}
}

upb_Message_Clear(self->ptr.msg, msgdef);
upb_Message_ClearByDef(self->ptr.msg, msgdef);
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 13 additions & 11 deletions upb/json/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 != '/') {
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions upb/json/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions upb/message/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions upb/reflection/field_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit b747edb

Please sign in to comment.