Skip to content

Commit

Permalink
Update mini table API comment
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 463868386
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jul 28, 2022
1 parent 33313a2 commit e09d6fc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
1 change: 1 addition & 0 deletions upb/mini_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ UPB_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable(
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,
int32_t val) {
uint32_t uval = (uint32_t)val;
Expand Down
25 changes: 5 additions & 20 deletions upb/mini_table_accessors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_Message* msg, const upb_MiniTable_Extension* ext_table,
int decode_options, upb_Arena* arena,
Expand All @@ -199,7 +184,7 @@ upb_GetExtension_Status upb_MiniTable_GetOrPromoteExtension(

// Check unknown fields, if available promote.
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) {
UPB_ASSERT(result.status != kUpb_GetExtension_ParseError);
return kUpb_GetExtension_NotPresent;
Expand Down Expand Up @@ -258,7 +243,7 @@ upb_GetExtensionAsBytes_Status upb_MiniTable_GetExtensionAsBytes(
return kUpb_GetExtensionAsBytes_Ok;
}
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) {
UPB_ASSERT(result.status != kUpb_GetExtension_ParseError);
return kUpb_GetExtensionAsBytes_NotPresent;
Expand Down Expand Up @@ -329,10 +314,10 @@ enum {
kUpb_MessageSet_MessageTag = (3 << 3) | kUpb_WireType_Delimited,
};

static find_unknown_ret UnknownFieldSet_FindField(const upb_Message* msg,
int field_number) {
upb_FindUnknownRet upb_MiniTable_FindUnknown(const upb_Message* msg,
uint32_t field_number) {
size_t size;
find_unknown_ret ret;
upb_FindUnknownRet ret;

const char* ptr = upb_Message_GetUnknown(msg, &size);
if (size == 0) {
Expand Down
17 changes: 17 additions & 0 deletions upb/mini_table_accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ upb_GetExtensionAsBytes_Status upb_MiniTable_GetExtensionAsBytes(
int encode_options, upb_Arena* arena, const char** extension_data,
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
} /* extern "C" */
#endif
Expand Down
32 changes: 32 additions & 0 deletions upb/mini_table_accessors_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,38 @@ TEST(GeneratedCode, GetMutableMessage) {
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) {
upb_Arena* arena = upb_Arena_New();
upb_test_ModelWithExtensions* msg = upb_test_ModelWithExtensions_new(arena);
Expand Down

0 comments on commit e09d6fc

Please sign in to comment.