add defaults to the mini table scalar getter accessor functions

also inline upb_MiniTable_HasField()

PiperOrigin-RevId: 487588457
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 721c284959
commit 0e76047f5f
  1. 15
      upb/mini_table/accessors.c
  2. 51
      upb/mini_table/accessors.h
  3. 29
      upb/mini_table/accessors_test.cc

@ -86,19 +86,6 @@ static int _upb_MiniTableField_CTypeLg2Size(const upb_MiniTableField* f) {
return sizes[f->descriptortype];
}
bool upb_MiniTable_HasField(const upb_Message* msg,
const upb_MiniTableField* field) {
if (_upb_MiniTableField_InOneOf(field)) {
return _upb_getoneofcase_field(msg, field) == field->number;
} else if (field->presence > 0) {
return _upb_hasbit_field(msg, field);
} else {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Message ||
field->descriptortype == kUpb_FieldType_Group);
return upb_MiniTable_GetMessage(msg, field) != NULL;
}
}
void upb_MiniTable_ClearField(upb_Message* msg,
const upb_MiniTableField* field) {
char* mem = UPB_PTR_AT(msg, field->offset, char);
@ -413,7 +400,7 @@ upb_UnknownToMessageRet upb_MiniTable_PromoteUnknownToMessage(
upb_Message* message = NULL;
// Callers should check that message is not set first before calling
// PromotoUnknownToMessage.
UPB_ASSERT(upb_MiniTable_GetMessage(msg, field) == NULL);
UPB_ASSERT(upb_MiniTable_GetMessage(msg, field, NULL) == NULL);
upb_UnknownToMessageRet ret;
ret.status = kUpb_UnknownToMessage_Ok;
do {

@ -53,15 +53,24 @@ UPB_INLINE void _upb_MiniTable_SetPresence(upb_Message* msg,
// EVERYTHING ABOVE THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
bool upb_MiniTable_HasField(const upb_Message* msg,
const upb_MiniTableField* field);
void upb_MiniTable_ClearField(upb_Message* msg,
const upb_MiniTableField* field);
UPB_INLINE bool upb_MiniTable_HasField(const upb_Message* msg,
const upb_MiniTableField* field) {
if (_upb_MiniTableField_InOneOf(field)) {
return _upb_getoneofcase_field(msg, field) == field->number;
}
UPB_ASSERT(field->presence > 0);
return _upb_hasbit_field(msg, field);
}
UPB_INLINE bool upb_MiniTable_GetBool(const upb_Message* msg,
const upb_MiniTableField* field) {
const upb_MiniTableField* field,
bool default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Bool);
if (default_val && !upb_MiniTable_HasField(msg, field)) return default_val;
return *UPB_PTR_AT(msg, field->offset, bool);
}
@ -74,11 +83,13 @@ UPB_INLINE void upb_MiniTable_SetBool(upb_Message* msg,
}
UPB_INLINE int32_t upb_MiniTable_GetInt32(const upb_Message* msg,
const upb_MiniTableField* field) {
const upb_MiniTableField* field,
int32_t default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int32 ||
field->descriptortype == kUpb_FieldType_SInt32 ||
field->descriptortype == kUpb_FieldType_SFixed32 ||
field->descriptortype == kUpb_FieldType_Enum);
if (default_val && !upb_MiniTable_HasField(msg, field)) return default_val;
return *UPB_PTR_AT(msg, field->offset, int32_t);
}
@ -93,9 +104,11 @@ UPB_INLINE void upb_MiniTable_SetInt32(upb_Message* msg,
}
UPB_INLINE uint32_t upb_MiniTable_GetUInt32(const upb_Message* msg,
const upb_MiniTableField* field) {
const upb_MiniTableField* field,
uint32_t default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_UInt32 ||
field->descriptortype == kUpb_FieldType_Fixed32);
if (default_val && !upb_MiniTable_HasField(msg, field)) return default_val;
return *UPB_PTR_AT(msg, field->offset, uint32_t);
}
@ -120,10 +133,12 @@ UPB_INLINE void upb_MiniTable_SetEnumProto2(upb_Message* msg,
}
UPB_INLINE int64_t upb_MiniTable_GetInt64(const upb_Message* msg,
const upb_MiniTableField* field) {
const upb_MiniTableField* field,
uint64_t default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int64 ||
field->descriptortype == kUpb_FieldType_SInt64 ||
field->descriptortype == kUpb_FieldType_SFixed64);
if (default_val && !upb_MiniTable_HasField(msg, field)) return default_val;
return *UPB_PTR_AT(msg, field->offset, int64_t);
}
@ -138,9 +153,11 @@ UPB_INLINE void upb_MiniTable_SetInt64(upb_Message* msg,
}
UPB_INLINE uint64_t upb_MiniTable_GetUInt64(const upb_Message* msg,
const upb_MiniTableField* field) {
const upb_MiniTableField* field,
uint64_t default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_UInt64 ||
field->descriptortype == kUpb_FieldType_Fixed64);
if (default_val && !upb_MiniTable_HasField(msg, field)) return default_val;
return *UPB_PTR_AT(msg, field->offset, uint64_t);
}
@ -154,8 +171,10 @@ UPB_INLINE void upb_MiniTable_SetUInt64(upb_Message* msg,
}
UPB_INLINE float upb_MiniTable_GetFloat(const upb_Message* msg,
const upb_MiniTableField* field) {
const upb_MiniTableField* field,
float default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Float);
if (default_val && !upb_MiniTable_HasField(msg, field)) return default_val;
return *UPB_PTR_AT(msg, field->offset, float);
}
@ -168,8 +187,10 @@ UPB_INLINE void upb_MiniTable_SetFloat(upb_Message* msg,
}
UPB_INLINE double upb_MiniTable_GetDouble(const upb_Message* msg,
const upb_MiniTableField* field) {
const upb_MiniTableField* field,
double default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Double);
if (default_val && !upb_MiniTable_HasField(msg, field)) return default_val;
return *UPB_PTR_AT(msg, field->offset, double);
}
@ -181,10 +202,12 @@ UPB_INLINE void upb_MiniTable_SetDouble(upb_Message* msg,
*UPB_PTR_AT(msg, field->offset, double) = value;
}
UPB_INLINE upb_StringView upb_MiniTable_GetString(
const upb_Message* msg, const upb_MiniTableField* field) {
UPB_INLINE upb_StringView
upb_MiniTable_GetString(const upb_Message* msg, const upb_MiniTableField* field,
upb_StringView def_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Bytes ||
field->descriptortype == kUpb_FieldType_String);
if (def_val.size && !upb_MiniTable_HasField(msg, field)) return def_val;
return *UPB_PTR_AT(msg, field->offset, upb_StringView);
}
@ -198,9 +221,11 @@ UPB_INLINE void upb_MiniTable_SetString(upb_Message* msg,
}
UPB_INLINE const upb_Message* upb_MiniTable_GetMessage(
const upb_Message* msg, const upb_MiniTableField* field) {
const upb_Message* msg, const upb_MiniTableField* field,
upb_Message* default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Message ||
field->descriptortype == kUpb_FieldType_Group);
if (default_val && !upb_MiniTable_HasField(msg, field)) return default_val;
return *UPB_PTR_AT(msg, field->offset, const upb_Message*);
}

@ -159,10 +159,10 @@ TEST(GeneratedCode, ScalarsProto2) {
EXPECT_EQ(
0, protobuf_test_messages_proto2_TestAllTypesProto2_optional_int32(msg));
EXPECT_EQ(0, upb_MiniTable_GetInt32(msg, optional_int32_field));
EXPECT_EQ(0, upb_MiniTable_GetInt32(msg, optional_int32_field, 0));
upb_MiniTable_SetInt32(msg, optional_int32_field, kTestInt32);
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_int32_field));
EXPECT_EQ(kTestInt32, upb_MiniTable_GetInt32(msg, optional_int32_field));
EXPECT_EQ(kTestInt32, upb_MiniTable_GetInt32(msg, optional_int32_field, 0));
EXPECT_EQ(
kTestInt32,
protobuf_test_messages_proto2_TestAllTypesProto2_optional_int32(msg));
@ -172,9 +172,10 @@ TEST(GeneratedCode, ScalarsProto2) {
EXPECT_EQ(
0, protobuf_test_messages_proto2_TestAllTypesProto2_optional_uint32(msg));
EXPECT_EQ(0, upb_MiniTable_GetUInt32(msg, optional_uint32_field));
EXPECT_EQ(0, upb_MiniTable_GetUInt32(msg, optional_uint32_field, 0));
upb_MiniTable_SetUInt32(msg, optional_uint32_field, kTestUInt32);
EXPECT_EQ(kTestUInt32, upb_MiniTable_GetUInt32(msg, optional_uint32_field));
EXPECT_EQ(kTestUInt32,
upb_MiniTable_GetUInt32(msg, optional_uint32_field, 0));
EXPECT_EQ(
kTestUInt32,
protobuf_test_messages_proto2_TestAllTypesProto2_optional_uint32(msg));
@ -197,7 +198,7 @@ TEST(GeneratedCode, ScalarProto3) {
upb_MiniTable_SetInt64(msg, optional_int64_field, -1);
EXPECT_EQ(
-1, protobuf_test_messages_proto3_TestAllTypesProto3_optional_int64(msg));
EXPECT_EQ(-1, upb_MiniTable_GetInt64(msg, optional_int64_field));
EXPECT_EQ(-1, upb_MiniTable_GetInt64(msg, optional_int64_field, 0));
EXPECT_EQ(
0, protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint64(msg));
@ -205,7 +206,8 @@ TEST(GeneratedCode, ScalarProto3) {
EXPECT_EQ(
kTestUInt64,
protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint64(msg));
EXPECT_EQ(kTestUInt64, upb_MiniTable_GetUInt64(msg, optional_uint64_field));
EXPECT_EQ(kTestUInt64,
upb_MiniTable_GetUInt64(msg, optional_uint64_field, 0));
upb_Arena_Free(arena);
}
@ -224,7 +226,8 @@ TEST(GeneratedCode, Strings) {
protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_string(
msg, upb_StringView_FromString(kTestStr1));
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_string_field));
upb_StringView value = upb_MiniTable_GetString(msg, optional_string_field);
upb_StringView value = upb_MiniTable_GetString(msg, optional_string_field,
upb_StringView{NULL, 0});
std::string read_value = std::string(value.data, value.size);
EXPECT_EQ(kTestStr1, read_value);
// Clear.
@ -257,7 +260,7 @@ TEST(GeneratedCode, SubMessage) {
find_proto2_field(kFieldOptionalNestedMessage);
const upb_Message* test_message =
upb_MiniTable_GetMessage(msg, optional_message_field);
upb_MiniTable_GetMessage(msg, optional_message_field, NULL);
EXPECT_EQ(NULL, test_message);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_message_field));
@ -273,14 +276,14 @@ TEST(GeneratedCode, SubMessage) {
// Read back using mini table API.
const upb_Message* sub_message =
upb_MiniTable_GetMessage(msg, optional_message_field);
upb_MiniTable_GetMessage(msg, optional_message_field, NULL);
EXPECT_EQ(true, sub_message != NULL);
const upb_MiniTableField* nested_message_a_field =
upb_MiniTable_FindFieldByNumber(
&protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage_msg_init,
kFieldOptionalNestedMessageA);
EXPECT_EQ(5, upb_MiniTable_GetInt32(sub_message, nested_message_a_field));
EXPECT_EQ(5, upb_MiniTable_GetInt32(sub_message, nested_message_a_field, 0));
upb_MiniTable_ClearField(msg, optional_message_field);
EXPECT_EQ(
@ -305,7 +308,7 @@ TEST(GeneratedCode, SubMessage) {
msg) != NULL);
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_message_field));
EXPECT_EQ(123,
upb_MiniTable_GetInt32(mutable_message, nested_message_a_field));
upb_MiniTable_GetInt32(mutable_message, nested_message_a_field, 0));
upb_Arena_Free(arena);
}
@ -522,7 +525,7 @@ TEST(GeneratedCode, PromoteUnknownMessage) {
mini_table, nullptr, 0, arena);
EXPECT_EQ(decode_status, kUpb_DecodeStatus_Ok);
int32_t val = upb_MiniTable_GetInt32(
msg, upb_MiniTable_FindFieldByNumber(mini_table, 4));
msg, upb_MiniTable_FindFieldByNumber(mini_table, 4), 0);
EXPECT_EQ(val, 11);
upb_FindUnknownRet unknown = upb_MiniTable_FindUnknown(msg, 5);
EXPECT_EQ(unknown.status, kUpb_FindUnknown_Ok);
@ -538,7 +541,7 @@ TEST(GeneratedCode, PromoteUnknownMessage) {
&upb_test_ModelWithExtensions_msg_init, decode_options, arena);
EXPECT_EQ(promote_result.status, kUpb_UnknownToMessage_Ok);
const upb_Message* promoted_message =
upb_MiniTable_GetMessage(msg, &mini_table->fields[1]);
upb_MiniTable_GetMessage(msg, &mini_table->fields[1], NULL);
EXPECT_EQ(upb_test_ModelWithExtensions_random_int32(
(upb_test_ModelWithExtensions*)promoted_message),
12);

Loading…
Cancel
Save