Update mini table API comment

PiperOrigin-RevId: 463868386
pull/13171/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 33313a2891
commit e09d6fcb6d
  1. 1
      upb/mini_table.h
  2. 25
      upb/mini_table_accessors.c
  3. 17
      upb/mini_table_accessors.h
  4. 32
      upb/mini_table_accessors_test.cc

@ -45,6 +45,7 @@ UPB_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable(
return mini_table->subs[field->submsg_index].submsg; return mini_table->subs[field->submsg_index].submsg;
} }
// Validates enum value against range defined by enum mini table.
UPB_INLINE bool upb_MiniTable_Enum_CheckValue(const upb_MiniTable_Enum* e, UPB_INLINE bool upb_MiniTable_Enum_CheckValue(const upb_MiniTable_Enum* e,
int32_t val) { int32_t val) {
uint32_t uval = (uint32_t)val; uint32_t uval = (uint32_t)val;

@ -172,21 +172,6 @@ static const char* decode_tag(const char* ptr, uint32_t* val) {
} }
} }
typedef enum {
kUpb_FindUnknown_Ok,
kUpb_FindUnknown_NotPresent,
kUpb_FindUnknown_ParseError,
} upb_FindUnknown_Status;
typedef struct {
upb_FindUnknown_Status status;
const char* ptr;
size_t len;
} find_unknown_ret;
static find_unknown_ret UnknownFieldSet_FindField(const upb_Message* msg,
int field_number);
upb_GetExtension_Status upb_MiniTable_GetOrPromoteExtension( upb_GetExtension_Status upb_MiniTable_GetOrPromoteExtension(
upb_Message* msg, const upb_MiniTable_Extension* ext_table, upb_Message* msg, const upb_MiniTable_Extension* ext_table,
int decode_options, upb_Arena* arena, int decode_options, upb_Arena* arena,
@ -199,7 +184,7 @@ upb_GetExtension_Status upb_MiniTable_GetOrPromoteExtension(
// Check unknown fields, if available promote. // Check unknown fields, if available promote.
int field_number = ext_table->field.number; int field_number = ext_table->field.number;
find_unknown_ret result = UnknownFieldSet_FindField(msg, field_number); upb_FindUnknownRet result = upb_MiniTable_FindUnknown(msg, field_number);
if (result.status != kUpb_FindUnknown_Ok) { if (result.status != kUpb_FindUnknown_Ok) {
UPB_ASSERT(result.status != kUpb_GetExtension_ParseError); UPB_ASSERT(result.status != kUpb_GetExtension_ParseError);
return kUpb_GetExtension_NotPresent; return kUpb_GetExtension_NotPresent;
@ -258,7 +243,7 @@ upb_GetExtensionAsBytes_Status upb_MiniTable_GetExtensionAsBytes(
return kUpb_GetExtensionAsBytes_Ok; return kUpb_GetExtensionAsBytes_Ok;
} }
int field_number = ext_table->field.number; int field_number = ext_table->field.number;
find_unknown_ret result = UnknownFieldSet_FindField(msg, field_number); upb_FindUnknownRet result = upb_MiniTable_FindUnknown(msg, field_number);
if (result.status != kUpb_FindUnknown_Ok) { if (result.status != kUpb_FindUnknown_Ok) {
UPB_ASSERT(result.status != kUpb_GetExtension_ParseError); UPB_ASSERT(result.status != kUpb_GetExtension_ParseError);
return kUpb_GetExtensionAsBytes_NotPresent; return kUpb_GetExtensionAsBytes_NotPresent;
@ -329,10 +314,10 @@ enum {
kUpb_MessageSet_MessageTag = (3 << 3) | kUpb_WireType_Delimited, kUpb_MessageSet_MessageTag = (3 << 3) | kUpb_WireType_Delimited,
}; };
static find_unknown_ret UnknownFieldSet_FindField(const upb_Message* msg, upb_FindUnknownRet upb_MiniTable_FindUnknown(const upb_Message* msg,
int field_number) { uint32_t field_number) {
size_t size; size_t size;
find_unknown_ret ret; upb_FindUnknownRet ret;
const char* ptr = upb_Message_GetUnknown(msg, &size); const char* ptr = upb_Message_GetUnknown(msg, &size);
if (size == 0) { if (size == 0) {

@ -262,6 +262,23 @@ upb_GetExtensionAsBytes_Status upb_MiniTable_GetExtensionAsBytes(
int encode_options, upb_Arena* arena, const char** extension_data, int encode_options, upb_Arena* arena, const char** extension_data,
size_t* len); size_t* len);
typedef enum {
kUpb_FindUnknown_Ok,
kUpb_FindUnknown_NotPresent,
kUpb_FindUnknown_ParseError,
} upb_FindUnknown_Status;
typedef struct {
upb_FindUnknown_Status status;
// Start of unknown field data in message arena.
const char* ptr;
// Size of unknown field data.
size_t len;
} upb_FindUnknownRet;
upb_FindUnknownRet upb_MiniTable_FindUnknown(const upb_Message* msg,
uint32_t field_number);
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
#endif #endif

@ -376,6 +376,38 @@ TEST(GeneratedCode, GetMutableMessage) {
upb_Arena_Free(arena); upb_Arena_Free(arena);
} }
TEST(GeneratedCode, FindUnknown) {
upb_Arena* arena = upb_Arena_New();
upb_test_ModelWithExtensions* msg = upb_test_ModelWithExtensions_new(arena);
upb_test_ModelWithExtensions_set_random_int32(msg, 10);
upb_test_ModelWithExtensions_set_random_name(
msg, upb_StringView_FromString("Hello"));
upb_test_ModelExtension1* extension1 = upb_test_ModelExtension1_new(arena);
upb_test_ModelExtension1_set_str(extension1,
upb_StringView_FromString("World"));
upb_test_ModelExtension1_set_model_ext(msg, extension1, arena);
size_t serialized_size;
char* serialized =
upb_test_ModelWithExtensions_serialize(msg, arena, &serialized_size);
upb_test_EmptyMessageWithExtensions* base_msg =
upb_test_EmptyMessageWithExtensions_parse(serialized, serialized_size,
arena);
upb_FindUnknownRet result = upb_MiniTable_FindUnknown(
base_msg, upb_test_ModelExtension1_model_ext_ext.field.number);
EXPECT_EQ(kUpb_FindUnknown_Ok, result.status);
result = upb_MiniTable_FindUnknown(
base_msg, upb_test_ModelExtension2_model_ext_ext.field.number);
EXPECT_EQ(kUpb_FindUnknown_NotPresent, result.status);
upb_Arena_Free(arena);
}
TEST(GeneratedCode, Extensions) { TEST(GeneratedCode, Extensions) {
upb_Arena* arena = upb_Arena_New(); upb_Arena* arena = upb_Arena_New();
upb_test_ModelWithExtensions* msg = upb_test_ModelWithExtensions_new(arena); upb_test_ModelWithExtensions* msg = upb_test_ModelWithExtensions_new(arena);

Loading…
Cancel
Save