mirror of
https://github.com/protocolbuffers/protobuf
synced 2026-08-26 02:23:14 -04:00
Faster Python cpp protos assignment to bytes/string fields.
PiperOrigin-RevId: 928459028
This commit is contained in:
parent
0a10408a63
commit
593ab176d6
1 changed files with 20 additions and 14 deletions
|
|
@ -100,6 +100,18 @@ class MessageReflectionFriend {
|
|||
const MapKey& map_key) {
|
||||
return reflection->ContainsMapKey(message, field, map_key);
|
||||
}
|
||||
|
||||
static void SetString(absl::string_view value, Message* message,
|
||||
const FieldDescriptor* descriptor,
|
||||
const Reflection* reflection, bool append, int index) {
|
||||
if (append) {
|
||||
reflection->AddStringView(message, descriptor, value);
|
||||
} else if (index < 0) {
|
||||
reflection->SetStringView(message, descriptor, value);
|
||||
} else {
|
||||
reflection->SetRepeatedStringView(message, descriptor, index, value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static PyObject* kDESCRIPTOR;
|
||||
|
|
@ -641,22 +653,16 @@ std::optional<absl::string_view> CheckString(
|
|||
}
|
||||
|
||||
bool CheckAndSetString(PyObject* arg, Message* message,
|
||||
const FieldDescriptor* descriptor,
|
||||
const FieldDescriptor* field_descriptor,
|
||||
const Reflection* reflection, bool append, int index) {
|
||||
std::optional<absl::string_view> value = CheckString(arg, descriptor);
|
||||
if (!value.has_value()) {
|
||||
return false;
|
||||
if (std::optional<absl::string_view> value =
|
||||
CheckString(arg, field_descriptor);
|
||||
value.has_value()) {
|
||||
MessageReflectionFriend::SetString(*value, message, field_descriptor,
|
||||
reflection, append, index);
|
||||
return true;
|
||||
}
|
||||
std::string value_string(*value);
|
||||
if (append) {
|
||||
reflection->AddString(message, descriptor, std::move(value_string));
|
||||
} else if (index < 0) {
|
||||
reflection->SetString(message, descriptor, std::move(value_string));
|
||||
} else {
|
||||
reflection->SetRepeatedString(message, descriptor, index,
|
||||
std::move(value_string));
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
PyObject* ToStringObject(const FieldDescriptor* descriptor,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue