Commit a905d39e authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

test nested message

Parser unnest message with primary key
parent dd099ac0
#include "server.h"
int main(int argc, char* argv[]) {
profanedb::server::Server server;
server.Run();
return 0;
}
......@@ -17,9 +17,11 @@ profanedb::protobuf::GetResp profanedb::storage::Db::Get(const profanedb::protob
profanedb::protobuf::PutResp profanedb::storage::Db::Put(const profanedb::protobuf::PutReq & request)
{
auto map = parser.ParseMessage(request.serializable());
std::cout << parser.ParseMessage(request) << std::endl;
// std::cout << parser.ParseMessage(request.serializable()) << std::endl;
for (auto const & obj: map) {
std::cout << obj.first << ":" << std::endl << obj.second.DebugString() << std::endl;
}
}
profanedb::protobuf::DelResp profanedb::storage::Db::Delete(const profanedb::protobuf::DelReq & request)
......
......@@ -24,7 +24,7 @@ profanedb::storage::Parser::~Parser()
{
}
std::string profanedb::storage::Parser::ParseMessage(const Any & serializable)
map<std::string, const google::protobuf::Message &> profanedb::storage::Parser::ParseMessage(const google::protobuf::Any& serializable)
{
std::string type = serializable.type_url();
......@@ -32,39 +32,35 @@ std::string profanedb::storage::Parser::ParseMessage(const Any & serializable)
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()) {
return fd->full_name() + "=" + FieldToString(container, fd);
}
}
return NULL;
return ParseMessage(*container);
}
std::string profanedb::storage::Parser::ParseMessage(const google::protobuf::Message & message)
map<std::string, const google::protobuf::Message &> profanedb::storage::Parser::ParseMessage(const google::protobuf::Message& message)
{
auto dependencies = new map<std::string, const google::protobuf::Message &>();
auto fields = new std::vector< const FieldDescriptor * >();
message.GetReflection()->ListFields(message, fields);
std::string key;
for (auto const & fd: *fields) {
if (fd->message_type() != NULL) {
ParseMessage(message.GetReflection()->GetMessage(message, fd));
auto nested = ParseMessage(message.GetReflection()->GetMessage(message, fd, &messageFactory));
dependencies->insert(nested.begin(), nested.end());
} else {
auto options = fd->options().GetExtension(profanedb::protobuf::options);
if (options.key()) {
return fd->full_name() + "=" + FieldToString(&message, fd);
key = fd->full_name() + "$" + FieldToString(&message, fd);
}
}
}
return NULL;
dependencies->insert( std::pair< std::string, const google::protobuf::Message & >(key, message) );
return *dependencies;
}
std::string profanedb::storage::Parser::FieldToString(const google::protobuf::Message * container, const google::protobuf::FieldDescriptor * fd)
......
......@@ -2,6 +2,7 @@
#define PARSER_H
#include <iostream>
#include <map>
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/io/zero_copy_stream.h>
......@@ -23,9 +24,12 @@ public:
Parser();
~Parser();
// Get key out of
std::string ParseMessage(const Any & serializable);
std::string ParseMessage(const Message & message);
// Generate a Dynamic Message out of an Any object, and return a set of dependent objects
map<std::string, const Message &> ParseMessage(const Any & serializable);
// Parse and object and return a set of dependent messages
map<std::string, const Message &> ParseMessage(const Message & message);
private:
io::ZeroCopyInputStream * inputStream;
compiler::DiskSourceTree sourceTree;
......
......@@ -9,5 +9,12 @@ message Test {
string field_two_str = 2;
bool field_three_bool = 3;
bytes field_four_bytes = 4;
Nested field_five_nested = 5;
}
message Nested {
string nested_field_one_str = 1 [ (profanedb.protobuf.options).key = true ];
int64 nested_field_two_int = 2;
double nested_field_three_double = 3;
}
......@@ -13,14 +13,20 @@ def run():
to_serialize = test_pb2.Test(
field_one_int = 123,
field_two_str = "my_string",
field_three_bool = True
field_three_bool = True,
field_four_bytes = b'bytes',
field_five_nested =
test_pb2.Nested(
nested_field_one_str = "nested string",
nested_field_two_int = 1902923490,
nested_field_three_double = 1728.543344
)
)
serializable = any_pb2.Any()
serializable.Pack(to_serialize)
print (serializable.TypeName())
stub.Put(db_pb2.PutReq(
serializable = serializable
))
......
......@@ -20,7 +20,7 @@ DESCRIPTOR = _descriptor.FileDescriptor(
name='test.proto',
package='test',
syntax='proto3',
serialized_pb=_b('\n\ntest.proto\x12\x04test\x1a profanedb/protobuf/options.proto\"p\n\x04Test\x12\x1d\n\rfield_one_int\x18\x01 \x01(\x05\x42\x06\xfa\xe9\x30\x02\x08\x01\x12\x15\n\rfield_two_str\x18\x02 \x01(\t\x12\x18\n\x10\x66ield_three_bool\x18\x03 \x01(\x08\x12\x18\n\x10\x66ield_four_bytes\x18\x04 \x01(\x0c\x62\x06proto3')
serialized_pb=_b('\n\ntest.proto\x12\x04test\x1a profanedb/protobuf/options.proto\"\x99\x01\n\x04Test\x12\x1d\n\rfield_one_int\x18\x01 \x01(\x05\x42\x06\xfa\xe9\x30\x02\x08\x01\x12\x15\n\rfield_two_str\x18\x02 \x01(\t\x12\x18\n\x10\x66ield_three_bool\x18\x03 \x01(\x08\x12\x18\n\x10\x66ield_four_bytes\x18\x04 \x01(\x0c\x12\'\n\x11\x66ield_five_nested\x18\x05 \x01(\x0b\x32\x0c.test.Nested\"o\n\x06Nested\x12$\n\x14nested_field_one_str\x18\x01 \x01(\tB\x06\xfa\xe9\x30\x02\x08\x01\x12\x1c\n\x14nested_field_two_int\x18\x02 \x01(\x03\x12!\n\x19nested_field_three_double\x18\x03 \x01(\x01\x62\x06proto3')
,
dependencies=[profanedb_dot_protobuf_dot_options__pb2.DESCRIPTOR,])
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
......@@ -63,6 +63,58 @@ _TEST = _descriptor.Descriptor(
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='field_five_nested', full_name='test.Test.field_five_nested', index=4,
number=5, type=11, cpp_type=10, label=1,
has_default_value=False, default_value=None,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
syntax='proto3',
extension_ranges=[],
oneofs=[
],
serialized_start=55,
serialized_end=208,
)
_NESTED = _descriptor.Descriptor(
name='Nested',
full_name='test.Nested',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='nested_field_one_str', full_name='test.Nested.nested_field_one_str', index=0,
number=1, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=_descriptor._ParseOptions(descriptor_pb2.FieldOptions(), _b('\372\3510\002\010\001'))),
_descriptor.FieldDescriptor(
name='nested_field_two_int', full_name='test.Nested.nested_field_two_int', index=1,
number=2, type=3, cpp_type=2, label=1,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='nested_field_three_double', full_name='test.Nested.nested_field_three_double', index=2,
number=3, type=1, cpp_type=5, label=1,
has_default_value=False, default_value=float(0),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
......@@ -75,11 +127,13 @@ _TEST = _descriptor.Descriptor(
extension_ranges=[],
oneofs=[
],
serialized_start=54,
serialized_end=166,
serialized_start=210,
serialized_end=321,
)
_TEST.fields_by_name['field_five_nested'].message_type = _NESTED
DESCRIPTOR.message_types_by_name['Test'] = _TEST
DESCRIPTOR.message_types_by_name['Nested'] = _NESTED
Test = _reflection.GeneratedProtocolMessageType('Test', (_message.Message,), dict(
DESCRIPTOR = _TEST,
......@@ -88,9 +142,18 @@ Test = _reflection.GeneratedProtocolMessageType('Test', (_message.Message,), dic
))
_sym_db.RegisterMessage(Test)
Nested = _reflection.GeneratedProtocolMessageType('Nested', (_message.Message,), dict(
DESCRIPTOR = _NESTED,
__module__ = 'test_pb2'
# @@protoc_insertion_point(class_scope:test.Nested)
))
_sym_db.RegisterMessage(Nested)
_TEST.fields_by_name['field_one_int'].has_options = True
_TEST.fields_by_name['field_one_int']._options = _descriptor._ParseOptions(descriptor_pb2.FieldOptions(), _b('\372\3510\002\010\001'))
_NESTED.fields_by_name['nested_field_one_str'].has_options = True
_NESTED.fields_by_name['nested_field_one_str']._options = _descriptor._ParseOptions(descriptor_pb2.FieldOptions(), _b('\372\3510\002\010\001'))
try:
# THESE ELEMENTS WILL BE DEPRECATED.
# Please use the generated *_pb2_grpc.py files instead.
......
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