diff --git a/php/ext/google/protobuf/php-upb.c b/php/ext/google/protobuf/php-upb.c index 12242a87ed..0ec6ae78a4 100644 --- a/php/ext/google/protobuf/php-upb.c +++ b/php/ext/google/protobuf/php-upb.c @@ -10749,6 +10749,9 @@ struct upb_MessageDef { upb_inttable itof; upb_strtable ntof; + // Looking up fields by json name. + upb_strtable jtof; + /* All nested defs. * MEM: We could save some space here by putting nested defs in a contiguous * region and calculating counts from offsets or vice-versa. */ @@ -10774,9 +10777,6 @@ struct upb_MessageDef { bool in_message_set; bool is_sorted; upb_WellKnown well_known_type; -#if UINTPTR_MAX == 0xffffffff - uint32_t padding; // Increase size to a multiple of 8. -#endif }; static void assign_msg_wellknowntype(upb_MessageDef* m) { @@ -10919,16 +10919,16 @@ bool upb_MessageDef_FindByNameWithSize(const upb_MessageDef* m, const upb_FieldDef* upb_MessageDef_FindByJsonNameWithSize( const upb_MessageDef* m, const char* name, size_t size) { upb_value val; - const upb_FieldDef* f; + + if (upb_strtable_lookup2(&m->jtof, name, size, &val)) { + return upb_value_getconstptr(val); + } if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) { return NULL; } - f = _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD); - if (!f) f = _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD_JSONNAME); - - return f; + return _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD); } int upb_MessageDef_ExtensionRangeCount(const upb_MessageDef* m) { @@ -11108,17 +11108,25 @@ void _upb_MessageDef_InsertField(upb_DefBuilder* ctx, upb_MessageDef* m, _upb_MessageDef_Insert(m, shortname, shortnamelen, field_v, ctx->arena); if (!ok) _upb_DefBuilder_OomErr(ctx); - if (strcmp(shortname, json_name) != 0) { - if (upb_strtable_lookup(&m->ntof, json_name, &v)) { - _upb_DefBuilder_Errf(ctx, "duplicate json_name (%s)", json_name); - } + // TODO: Once editions is supported this should turn into a + // check on LEGACY_BEST_EFFORT + if (strcmp(shortname, json_name) != 0 && + upb_FileDef_Syntax(m->file) == kUpb_Syntax_Proto3 && + upb_strtable_lookup(&m->ntof, json_name, &v)) { + _upb_DefBuilder_Errf( + ctx, "duplicate json_name for (%s) with original field name (%s)", + shortname, json_name); + } - const size_t json_size = strlen(json_name); - const upb_value json_v = _upb_DefType_Pack(f, UPB_DEFTYPE_FIELD_JSONNAME); - ok = _upb_MessageDef_Insert(m, json_name, json_size, json_v, ctx->arena); - if (!ok) _upb_DefBuilder_OomErr(ctx); + if (upb_strtable_lookup(&m->jtof, json_name, &v)) { + _upb_DefBuilder_Errf(ctx, "duplicate json_name (%s)", json_name); } + const size_t json_size = strlen(json_name); + ok = upb_strtable_insert(&m->jtof, json_name, json_size, + upb_value_constptr(f), ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + if (upb_inttable_lookup(&m->itof, field_number, NULL)) { _upb_DefBuilder_Errf(ctx, "duplicate field number (%u)", field_number); } @@ -11371,6 +11379,9 @@ static void create_msgdef(upb_DefBuilder* ctx, const char* prefix, ok = upb_strtable_init(&m->ntof, n_oneof + n_field, ctx->arena); if (!ok) _upb_DefBuilder_OomErr(ctx); + ok = upb_strtable_init(&m->jtof, n_field, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + UPB_DEF_SET_OPTIONS(m->opts, DescriptorProto, MessageOptions, msg_proto); m->oneof_count = n_oneof; diff --git a/php/ext/google/protobuf/php-upb.h b/php/ext/google/protobuf/php-upb.h index 392287be18..17700cb3ba 100644 --- a/php/ext/google/protobuf/php-upb.h +++ b/php/ext/google/protobuf/php-upb.h @@ -10115,7 +10115,6 @@ typedef enum { // Only inside message table. UPB_DEFTYPE_FIELD = 0, UPB_DEFTYPE_ONEOF = 1, - UPB_DEFTYPE_FIELD_JSONNAME = 2, } upb_deftype_t; #ifdef __cplusplus diff --git a/ruby/ext/google/protobuf_c/ruby-upb.c b/ruby/ext/google/protobuf_c/ruby-upb.c index b47c57f37e..13a44754ae 100644 --- a/ruby/ext/google/protobuf_c/ruby-upb.c +++ b/ruby/ext/google/protobuf_c/ruby-upb.c @@ -10263,6 +10263,9 @@ struct upb_MessageDef { upb_inttable itof; upb_strtable ntof; + // Looking up fields by json name. + upb_strtable jtof; + /* All nested defs. * MEM: We could save some space here by putting nested defs in a contiguous * region and calculating counts from offsets or vice-versa. */ @@ -10288,9 +10291,6 @@ struct upb_MessageDef { bool in_message_set; bool is_sorted; upb_WellKnown well_known_type; -#if UINTPTR_MAX == 0xffffffff - uint32_t padding; // Increase size to a multiple of 8. -#endif }; static void assign_msg_wellknowntype(upb_MessageDef* m) { @@ -10433,16 +10433,16 @@ bool upb_MessageDef_FindByNameWithSize(const upb_MessageDef* m, const upb_FieldDef* upb_MessageDef_FindByJsonNameWithSize( const upb_MessageDef* m, const char* name, size_t size) { upb_value val; - const upb_FieldDef* f; + + if (upb_strtable_lookup2(&m->jtof, name, size, &val)) { + return upb_value_getconstptr(val); + } if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) { return NULL; } - f = _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD); - if (!f) f = _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD_JSONNAME); - - return f; + return _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD); } int upb_MessageDef_ExtensionRangeCount(const upb_MessageDef* m) { @@ -10622,17 +10622,25 @@ void _upb_MessageDef_InsertField(upb_DefBuilder* ctx, upb_MessageDef* m, _upb_MessageDef_Insert(m, shortname, shortnamelen, field_v, ctx->arena); if (!ok) _upb_DefBuilder_OomErr(ctx); - if (strcmp(shortname, json_name) != 0) { - if (upb_strtable_lookup(&m->ntof, json_name, &v)) { - _upb_DefBuilder_Errf(ctx, "duplicate json_name (%s)", json_name); - } + // TODO: Once editions is supported this should turn into a + // check on LEGACY_BEST_EFFORT + if (strcmp(shortname, json_name) != 0 && + upb_FileDef_Syntax(m->file) == kUpb_Syntax_Proto3 && + upb_strtable_lookup(&m->ntof, json_name, &v)) { + _upb_DefBuilder_Errf( + ctx, "duplicate json_name for (%s) with original field name (%s)", + shortname, json_name); + } - const size_t json_size = strlen(json_name); - const upb_value json_v = _upb_DefType_Pack(f, UPB_DEFTYPE_FIELD_JSONNAME); - ok = _upb_MessageDef_Insert(m, json_name, json_size, json_v, ctx->arena); - if (!ok) _upb_DefBuilder_OomErr(ctx); + if (upb_strtable_lookup(&m->jtof, json_name, &v)) { + _upb_DefBuilder_Errf(ctx, "duplicate json_name (%s)", json_name); } + const size_t json_size = strlen(json_name); + ok = upb_strtable_insert(&m->jtof, json_name, json_size, + upb_value_constptr(f), ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + if (upb_inttable_lookup(&m->itof, field_number, NULL)) { _upb_DefBuilder_Errf(ctx, "duplicate field number (%u)", field_number); } @@ -10885,6 +10893,9 @@ static void create_msgdef(upb_DefBuilder* ctx, const char* prefix, ok = upb_strtable_init(&m->ntof, n_oneof + n_field, ctx->arena); if (!ok) _upb_DefBuilder_OomErr(ctx); + ok = upb_strtable_init(&m->jtof, n_field, ctx->arena); + if (!ok) _upb_DefBuilder_OomErr(ctx); + UPB_DEF_SET_OPTIONS(m->opts, DescriptorProto, MessageOptions, msg_proto); m->oneof_count = n_oneof; diff --git a/ruby/ext/google/protobuf_c/ruby-upb.h b/ruby/ext/google/protobuf_c/ruby-upb.h index 0c4dbb81f7..6d98c1c6b9 100755 --- a/ruby/ext/google/protobuf_c/ruby-upb.h +++ b/ruby/ext/google/protobuf_c/ruby-upb.h @@ -10619,7 +10619,6 @@ typedef enum { // Only inside message table. UPB_DEFTYPE_FIELD = 0, UPB_DEFTYPE_ONEOF = 1, - UPB_DEFTYPE_FIELD_JSONNAME = 2, } upb_deftype_t; #ifdef __cplusplus