Commit dd099ac0 authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

testing message reflection

parent 2cd8da31
add_library(profanedb_storage parser.cpp db.cpp)
add_library(profanedb_storage db.cpp parser.cpp)
target_link_libraries(profanedb_storage profanedb_protobuf ${ROCKSDB_LIBRARIES})
......@@ -17,7 +17,9 @@ profanedb::protobuf::GetResp profanedb::storage::Db::Get(const profanedb::protob
profanedb::protobuf::PutResp profanedb::storage::Db::Put(const profanedb::protobuf::PutReq & request)
{
std::cout << parser.ParseMessage(request.serializable()) << std::endl;
std::cout << parser.ParseMessage(request) << std::endl;
// std::cout << parser.ParseMessage(request.serializable()) << std::endl;
}
profanedb::protobuf::DelResp profanedb::storage::Db::Delete(const profanedb::protobuf::DelReq & request)
......
......@@ -29,22 +29,42 @@ std::string profanedb::storage::Parser::ParseMessage(const Any & serializable)
std::string type = serializable.type_url();
const Descriptor * definition = pool->FindMessageTypeByName(type.substr(type.rfind('/')+1, string::npos));
const FieldDescriptor * fd;
// TODO Should check for multiple keys (might be supported later), for now throw error
for (int idx = 0; idx < definition->field_count(); idx++) {
fd = definition->field(idx);
Message * container = messageFactory.GetPrototype(definition)->New();
serializable.UnpackTo(container);
auto fields = new std::vector< const FieldDescriptor * >();
container->GetReflection()->ListFields(*container, fields);
for (auto const & fd: *fields) {
if (fd->options().GetExtension(profanedb::protobuf::options).key()) {
break;
return fd->full_name() + "=" + FieldToString(container, fd);
}
}
Message * container = messageFactory.GetPrototype(definition)->New();
return NULL;
}
std::string profanedb::storage::Parser::ParseMessage(const google::protobuf::Message & message)
{
auto fields = new std::vector< const FieldDescriptor * >();
serializable.UnpackTo(container);
message.GetReflection()->ListFields(message, fields);
for (auto const & fd: *fields) {
if (fd->message_type() != NULL) {
ParseMessage(message.GetReflection()->GetMessage(message, fd));
} else {
auto options = fd->options().GetExtension(profanedb::protobuf::options);
if (options.key()) {
return fd->full_name() + "=" + FieldToString(&message, fd);
}
}
}
return definition->full_name() + "$" + FieldToString(container, fd);
return NULL;
}
std::string profanedb::storage::Parser::FieldToString(const google::protobuf::Message * container, const google::protobuf::FieldDescriptor * fd)
......
......@@ -23,7 +23,9 @@ public:
Parser();
~Parser();
// Get key out of
std::string ParseMessage(const Any & serializable);
std::string ParseMessage(const Message & message);
private:
io::ZeroCopyInputStream * inputStream;
compiler::DiskSourceTree sourceTree;
......
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