Skip to content

Commit

Permalink
Merge GetEnum into GetInt32. Rename SetEnum to SetEnumProto2 to be cl…
Browse files Browse the repository at this point in the history
…ear that upb only treats Proto2 enum as enum. Proto3 enums should use SetInt32.

PiperOrigin-RevId: 467000685
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Aug 11, 2022
1 parent 63a4a7d commit 0c65313
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
5 changes: 5 additions & 0 deletions upb/mini_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ UPB_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable(
return mini_table->subs[field->submsg_index].submsg;
}

UPB_INLINE const upb_MiniTable_Enum* upb_MiniTable_GetSubEnumTable(
const upb_MiniTable* mini_table, const upb_MiniTable_Field* field) {
return mini_table->subs[field->submsg_index].subenum;
}

// 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) {
Expand Down
19 changes: 9 additions & 10 deletions upb/mini_table_accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "upb/array.h"
#include "upb/internal/mini_table_accessors.h"
#include "upb/mini_table.h"
#include "upb/msg_internal.h"

// Must be last.
Expand Down Expand Up @@ -63,7 +64,8 @@ UPB_INLINE int32_t upb_MiniTable_GetInt32(const upb_Message* msg,
const upb_MiniTable_Field* field) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int32 ||
field->descriptortype == kUpb_FieldType_SInt32 ||
field->descriptortype == kUpb_FieldType_SFixed32);
field->descriptortype == kUpb_FieldType_SFixed32 ||
field->descriptortype == kUpb_FieldType_Enum);
return *UPB_PTR_AT(msg, field->offset, int32_t);
}

Expand Down Expand Up @@ -93,16 +95,13 @@ UPB_INLINE void upb_MiniTable_SetUInt32(upb_Message* msg,
*UPB_PTR_AT(msg, field->offset, uint32_t) = value;
}

UPB_INLINE int32_t upb_MiniTable_GetEnum(const upb_Message* msg,
const upb_MiniTable_Field* field) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Enum);
return *UPB_PTR_AT(msg, field->offset, int32_t);
}

UPB_INLINE void upb_MiniTable_SetEnum(upb_Message* msg,
const upb_MiniTable_Field* field,
int32_t value) {
UPB_INLINE void upb_MiniTable_SetEnumProto2(upb_Message* msg,
const upb_MiniTable* msg_mini_table,
const upb_MiniTable_Field* field,
int32_t value) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Enum);
UPB_ASSERT(upb_MiniTable_Enum_CheckValue(
upb_MiniTable_GetSubEnumTable(msg_mini_table, field), value));
_upb_MiniTable_SetPresence(msg, field);
*UPB_PTR_AT(msg, field->offset, int32_t) = value;
}
Expand Down

0 comments on commit 0c65313

Please sign in to comment.