Skip to content

Commit 82e83dd

Browse files
honglookercopybara-github
authored andcommitted
wire_format.cc: Refactor Message and Group handling into a helper func
PiperOrigin-RevId: 632191495
1 parent 510c143 commit 82e83dd

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

src/google/protobuf/wire_format.cc

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,44 @@ struct WireFormat::MessageSetParser {
766766
const Reflection* reflection;
767767
};
768768

769+
static const char* HandleMessage(Message* msg, const char* ptr,
770+
internal::ParseContext* ctx, uint64_t tag,
771+
const Reflection* reflection,
772+
const FieldDescriptor* field) {
773+
Message* sub_message;
774+
if (field->is_repeated()) {
775+
sub_message = reflection->AddMessage(msg, field, ctx->data().factory);
776+
} else {
777+
sub_message = reflection->MutableMessage(msg, field, ctx->data().factory);
778+
}
779+
780+
if (WireFormatLite::GetTagWireType(tag) ==
781+
WireFormatLite::WIRETYPE_START_GROUP) {
782+
return ctx->ParseGroup(sub_message, ptr, tag);
783+
} else {
784+
ABSL_DCHECK(WireFormatLite::GetTagWireType(tag) ==
785+
WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
786+
}
787+
788+
ptr = ctx->ParseMessage(sub_message, ptr);
789+
790+
// For map entries, if the value is an unknown enum we have to push it
791+
// into the unknown field set and remove it from the list.
792+
if (ptr != nullptr && field->is_map()) {
793+
auto* value_field = field->message_type()->map_value();
794+
auto* enum_type = value_field->enum_type();
795+
if (enum_type != nullptr &&
796+
!internal::cpp::HasPreservingUnknownEnumSemantics(value_field) &&
797+
enum_type->FindValueByNumber(sub_message->GetReflection()->GetEnumValue(
798+
*sub_message, value_field)) == nullptr) {
799+
reflection->MutableUnknownFields(msg)->AddLengthDelimited(
800+
field->number(), sub_message->SerializeAsString());
801+
reflection->RemoveLast(msg, field);
802+
}
803+
}
804+
return ptr;
805+
}
806+
769807
const char* WireFormat::_InternalParse(Message* msg, const char* ptr,
770808
internal::ParseContext* ctx) {
771809
const Descriptor* descriptor = msg->GetDescriptor();
@@ -995,46 +1033,9 @@ const char* WireFormat::_InternalParseAndMergeField(
9951033
return ptr;
9961034
}
9971035

998-
case FieldDescriptor::TYPE_GROUP: {
999-
Message* sub_message;
1000-
if (field->is_repeated()) {
1001-
sub_message = reflection->AddMessage(msg, field, ctx->data().factory);
1002-
} else {
1003-
sub_message =
1004-
reflection->MutableMessage(msg, field, ctx->data().factory);
1005-
}
1006-
1007-
return ctx->ParseGroup(sub_message, ptr, tag);
1008-
}
1009-
1010-
case FieldDescriptor::TYPE_MESSAGE: {
1011-
Message* sub_message;
1012-
if (field->is_repeated()) {
1013-
sub_message = reflection->AddMessage(msg, field, ctx->data().factory);
1014-
} else {
1015-
sub_message =
1016-
reflection->MutableMessage(msg, field, ctx->data().factory);
1017-
}
1018-
ptr = ctx->ParseMessage(sub_message, ptr);
1019-
1020-
// For map entries, if the value is an unknown enum we have to push it
1021-
// into the unknown field set and remove it from the list.
1022-
if (ptr != nullptr && field->is_map()) {
1023-
auto* value_field = field->message_type()->map_value();
1024-
auto* enum_type = value_field->enum_type();
1025-
if (enum_type != nullptr &&
1026-
!internal::cpp::HasPreservingUnknownEnumSemantics(value_field) &&
1027-
enum_type->FindValueByNumber(
1028-
sub_message->GetReflection()->GetEnumValue(
1029-
*sub_message, value_field)) == nullptr) {
1030-
reflection->MutableUnknownFields(msg)->AddLengthDelimited(
1031-
field->number(), sub_message->SerializeAsString());
1032-
reflection->RemoveLast(msg, field);
1033-
}
1034-
}
1035-
1036-
return ptr;
1037-
}
1036+
case FieldDescriptor::TYPE_MESSAGE:
1037+
case FieldDescriptor::TYPE_GROUP:
1038+
return HandleMessage(msg, ptr, ctx, tag, reflection, field);
10381039
}
10391040

10401041
// GCC 8 complains about control reaching end of non-void function here.

0 commit comments

Comments
 (0)