Commit d2756657 authored by Giorgio Azzinnaro's avatar Giorgio Azzinnaro

Testing dynamic protobuf compile

Load a test proto, find Prodis defined primary key
parents
# Created by https://www.gitignore.io/api/cmake,kdevelop4
### CMake ###
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
build/
### KDevelop4 ###
*.kdev4
.kdev4/
# End of https://www.gitignore.io/api/cmake,kdevelop4
cmake_minimum_required(VERSION 2.6)
project(ProdisDB)
set(ProdisDB_VERSION_MAJOR 0)
set(ProdisDB_VERSION_MINOR 1)
include_directories("${PROJECT_SOURCE_DIR}/src")
add_executable(ProdisDB
src/prodisdb/server/main.cpp
src/prodisdb/server/prodis_error_collector.cpp
src/prodisdb/protobuf/options.pb.cc)
target_link_libraries(ProdisDB protobuf)
This diff is collapsed.
This diff is collapsed.
syntax = "proto3";
package prodisdb.protobuf;
option go_package = "gitlab.com/prodisdb/protobuf/db";
option csharp_namespace = "ProdisDB.Protobuf";
option java_package = "com.prodisdb.protobuf";
option objc_class_prefix = "PDB";
message Empty {}
message Serializable {
string descriptor = 1;
bytes blob = 2;
}
message PrimaryKey {
string descriptor = 1;
bytes uid = 2;
}
service Prodis {
rpc Get (PrimaryKey) returns (Serializable);
rpc Create (Serializable) returns (Empty);
rpc Delete (PrimaryKey) returns (Empty);
rpc Update (Serializable) returns (Empty);
}
This diff is collapsed.
This diff is collapsed.
syntax = "proto2";
package prodisdb.protobuf;
option go_package = "gitlab.com/prodisdb/protobuf/options";
option csharp_namespace = "ProdisDB.Protobuf";
option java_package = "com.prodisdb.protobuf";
option objc_class_prefix = "PDB";
import "google/protobuf/descriptor.proto";
// These options should be used during schema definition,
// applying them to some of the fields in protobuf
message FieldOptions {
optional bool pk = 1;
}
extend google.protobuf.FieldOptions {
optional FieldOptions options = 99999;
}
#include <iostream>
#include <string>
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <prodisdb/protobuf/options.pb.h>
#include <prodisdb/server/prodis_error_collector.h>
const std::string VIRTUAL_PROTO_MAP = "schema";
const std::string PROTO_DIR = "/home/giorgio/Documents/ProdisDB/test"; // TODO Should be config
int main(int argc, char *argv[]) {
google::protobuf::compiler::DiskSourceTree *sourceTree;
google::protobuf::io::ZeroCopyInputStream *inputStream;
google::protobuf::compiler::MultiFileErrorCollector *errCollector = new ProdisErrorCollector();
google::protobuf::compiler::Importer *importer;
google::protobuf::compiler::SourceTreeDescriptorDatabase *descriptorDb;
google::protobuf::DescriptorPool *pool;
sourceTree = new google::protobuf::compiler::DiskSourceTree();
sourceTree->MapPath(VIRTUAL_PROTO_MAP, PROTO_DIR);
sourceTree->MapPath("", "/home/giorgio/Documents/ProdisDB/src");
sourceTree->MapPath("", "/usr/include");
inputStream = sourceTree->Open(VIRTUAL_PROTO_MAP);
if (inputStream == NULL) {
std::cerr << "Couldn't open .proto source tree: " << sourceTree->GetLastErrorMessage() << "\n";
}
descriptorDb = new google::protobuf::compiler::SourceTreeDescriptorDatabase(sourceTree);
descriptorDb->RecordErrorsTo(errCollector);
pool = new google::protobuf::DescriptorPool(descriptorDb);
pool->FindFileByName("prodisdb/protobuf/options.proto");
pool->FindFileByName(VIRTUAL_PROTO_MAP + "/test.proto");
const google::protobuf::Descriptor *message = pool->FindMessageTypeByName("test.Test");
const google::protobuf::FieldDescriptor *fd;
for (int idx = 0; idx < message->field_count(); idx++) {
fd = message->field(idx);
std::cout
<< fd->full_name()
<< " is primary key: "
<< fd->options().GetExtension(prodisdb::protobuf::options).pk()
<< "\n";
if (fd->options().GetExtension(prodisdb::protobuf::options).pk()) {
break;
}
}
}
#include "prodis_error_collector.h"
ProdisErrorCollector::ProdisErrorCollector()
{
}
void ProdisErrorCollector::AddError(const std::__cxx11::string& filename, int line, int column, const std::__cxx11::string& message)
{
if (line == -1) { // Entire file error
std::cerr << filename << " error: " << message << "\n";
} else {
std::cerr << filename << " " << line+1 << ":" << column+1 << " error: " << message << "\n";
}
}
void ProdisErrorCollector::AddWarning(const std::__cxx11::string& filename, int line, int column, const std::__cxx11::string& message)
{
std::cerr << filename << " " << line+1 << ":" << column+1 << " warning: " << message << "\n";
}
#ifndef PRODISERRORCOLLECTOR_H
#define PRODISERRORCOLLECTOR_H
#include <iostream>
#include <google/protobuf/compiler/importer.h>
class ProdisErrorCollector : public google::protobuf::compiler::MultiFileErrorCollector
{
public:
ProdisErrorCollector();
virtual void AddError(const std::__cxx11::string& filename, int line, int column, const std::__cxx11::string& message);
virtual void AddWarning(const std::__cxx11::string& filename, int line, int column, const std::__cxx11::string& message);
};
#endif // PRODISERRORCOLLECTOR_H
syntax = "proto3";
package test;
import "prodisdb/protobuf/options.proto";
message Test {
int32 field_one_int = 1 [ (prodisdb.protobuf.options).pk = true ];
string field_two_str = 2;
bool field_three_bool = 3;
bytes field_four_bytes = 4;
}
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