consolidate the field type stuff

PiperOrigin-RevId: 518553642
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 8562ccc1f0
commit 4e25ba4575
  1. 24
      upb/base/descriptor_constants.h
  2. 11
      upb/mini_table/common_internal.h
  3. 2
      upb/mini_table/decode.c
  4. 2
      upb/mini_table/encode.c
  5. 1
      upb/mini_table/encode_internal.h
  6. 5
      upb/mini_table/encode_test.cc

@ -28,6 +28,9 @@
#ifndef UPB_BASE_DESCRIPTOR_CONSTANTS_H_
#define UPB_BASE_DESCRIPTOR_CONSTANTS_H_
// Must be last.
#include "upb/port/def.inc"
// The types a field can have. Note that this list is not identical to the
// types defined in descriptor.proto, which gives INT32 and SINT32 separate
// types (we distinguish the two with the "integer encoding" enum below).
@ -77,4 +80,25 @@ typedef enum {
#define kUpb_FieldType_SizeOf 19
#ifdef __cplusplus
extern "C" {
#endif
UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType type) {
// clang-format off
const unsigned kUnpackableTypes =
(1 << kUpb_FieldType_String) |
(1 << kUpb_FieldType_Bytes) |
(1 << kUpb_FieldType_Message) |
(1 << kUpb_FieldType_Group);
// clang-format on
return (1 << type) & ~kUnpackableTypes;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_BASE_DESCRIPTOR_CONSTANTS_H_ */

@ -102,17 +102,6 @@ UPB_INLINE char _upb_FromBase92(uint8_t ch) {
return _kUpb_FromBase92[ch - ' '];
}
UPB_INLINE bool _upb_FieldType_IsPackable(upb_FieldType type) {
// clang-format off
const unsigned kUnpackableTypes =
(1 << kUpb_FieldType_String) |
(1 << kUpb_FieldType_Bytes) |
(1 << kUpb_FieldType_Message) |
(1 << kUpb_FieldType_Group);
// clang-format on
return (1 << type) & ~kUnpackableTypes;
}
#ifdef __cplusplus
} /* extern "C" */
#endif

@ -156,7 +156,7 @@ static bool upb_MiniTable_HasSub(upb_MiniTableField* field,
static bool upb_MtDecoder_FieldIsPackable(upb_MiniTableField* field) {
return (field->mode & kUpb_FieldMode_Array) &&
_upb_FieldType_IsPackable(field->descriptortype);
upb_FieldType_IsPackable(field->descriptortype);
}
static void upb_MiniTable_SetTypeAndSub(upb_MiniTableField* field,

@ -203,7 +203,7 @@ char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr,
// are bit flags).
encoded_type += kUpb_EncodedType_RepeatedBase;
if (_upb_FieldType_IsPackable(type)) {
if (upb_FieldType_IsPackable(type)) {
bool field_is_packed = field_mod & kUpb_FieldModifier_IsPacked;
bool default_is_packed = in->state.msg_state.msg_modifiers &
kUpb_MessageModifier_DefaultIsPacked;

@ -28,6 +28,7 @@
#ifndef UPB_MINI_TABLE_ENCODE_INTERNAL_H_
#define UPB_MINI_TABLE_ENCODE_INTERNAL_H_
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/common.h"
// Must be last.

@ -25,6 +25,9 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string_view>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/flat_hash_set.h"
@ -208,7 +211,7 @@ TEST(MiniTablePlatformIndependentTest, Base92Roundtrip) {
TEST(MiniTablePlatformIndependentTest, IsTypePackable) {
for (int i = 1; i <= protobuf::FieldDescriptor::MAX_TYPE; i++) {
EXPECT_EQ(_upb_FieldType_IsPackable(static_cast<upb_FieldType>(i)),
EXPECT_EQ(upb_FieldType_IsPackable(static_cast<upb_FieldType>(i)),
protobuf::FieldDescriptor::IsTypePackable(
static_cast<protobuf::FieldDescriptor::Type>(i)));
}

Loading…
Cancel
Save