Commit 26591fc4 authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

added CopyMessage to replicate MergeFrom behaviour on different pools

parent 97435c72
...@@ -83,12 +83,10 @@ MessageTreeNode Marshaller::Marshal(const Message & message) ...@@ -83,12 +83,10 @@ MessageTreeNode Marshaller::Marshal(const Message & message)
// with the parameter message as root // with the parameter message as root
*messageTree.add_children() = nestedMessageTree; *messageTree.add_children() = nestedMessageTree;
// TODO Needs our own unsafe Copy, as Normalized profanedb.protobuf.Key is different from Schema profanedb.protobuf.Key
// The field in the normalized message is assigned the root key from the contained message tree // The field in the normalized message is assigned the root key from the contained message tree
normalizedMessage->GetReflection() this->CopyMessage(nestedMessageTree.message().key(),
->MutableMessage(normalizedMessage, normalizedField) normalizedMessage->GetReflection()->MutableMessage(
->MergeFrom(nestedMessageTree.message().key()); normalizedMessage, normalizedField));
} }
else { else {
// Other fields are simply copied over, // Other fields are simply copied over,
...@@ -145,9 +143,9 @@ const Message & Marshaller::Unmarshal(const StorableMessage & storable) ...@@ -145,9 +143,9 @@ const Message & Marshaller::Unmarshal(const StorableMessage & storable)
const FieldDescriptor * originalField = originalMessage->GetDescriptor()->field(normalizedField->index()); const FieldDescriptor * originalField = originalMessage->GetDescriptor()->field(normalizedField->index());
// Copy the nested unmarshalled message to the original one // Copy the nested unmarshalled message to the original one
originalMessage->GetReflection() this->CopyMessage(nestedMessage,
->MutableMessage(originalMessage, originalField) originalMessage->GetReflection()->MutableMessage(
->MergeFrom(nestedMessage); originalMessage, originalField));
} }
else { // if field is not a reference else { // if field is not a reference
...@@ -156,11 +154,19 @@ const Message & Marshaller::Unmarshal(const StorableMessage & storable) ...@@ -156,11 +154,19 @@ const Message & Marshaller::Unmarshal(const StorableMessage & storable)
this->CopyField(normalizedField, *normalizedMessage, originalMessage); this->CopyField(normalizedField, *normalizedMessage, originalMessage);
} }
} }
return *originalMessage; return *originalMessage;
} }
void Marshaller::CopyMessage(const Message & from, Message * to)
{
std::vector< const FieldDescriptor * > setFields;
from.GetReflection()->ListFields(from, &setFields);
for (auto fromField : setFields)
this->CopyField(fromField, from, to);
}
void Marshaller::CopyField( void Marshaller::CopyField(
const FieldDescriptor * fromField, const FieldDescriptor * fromField,
const Message & from, const Message & from,
...@@ -216,13 +222,9 @@ void Marshaller::CopyField( ...@@ -216,13 +222,9 @@ void Marshaller::CopyField(
#undef HANDLE_TYPE #undef HANDLE_TYPE
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
const Message & nestedFrom = fromReflection->GetMessage(from, fromField); this->CopyMessage(
Message * nestedTo = toReflection->MutableMessage(to, toField); fromReflection->GetMessage(from, fromField),
toReflection->MutableMessage(to, toField));
std::vector< const FieldDescriptor * > setFields;
nestedFrom.GetReflection()->ListFields(from, &setFields);
for (auto field: setFields)
this->CopyField(field, nestedFrom, nestedTo);
break; break;
} }
} }
......
...@@ -57,6 +57,11 @@ private: ...@@ -57,6 +57,11 @@ private:
// Storage is used to recursively retrieve them. // Storage is used to recursively retrieve them.
const std::shared_ptr<profanedb::vault::Storage> storage; const std::shared_ptr<profanedb::vault::Storage> storage;
// Copy a Message, can't use MergeFrom because it checks Descriptors
void CopyMessage(
const google::protobuf::Message & from,
google::protobuf::Message * to);
// Copy a field from a message to another. // Copy a field from a message to another.
// Differs from MergeFrom because it doesn't check whether Descriptors match. // Differs from MergeFrom because it doesn't check whether Descriptors match.
void CopyField( void CopyField(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment