bool ProtoBuf::ImportFile(const string& pbfile, const string& pbinc, DiskSourceTree& dst, Importer*& ipt, const FileDescriptor*& fd) { // 分离proto_file的目录和文件名 char* dd; dd = strdup(pbfile.c_str()); string dir = dirname(dd); free(dd); dd = strdup(pbfile.c_str()); string file = basename(dd); free(dd); // 增加proto_inc EList<string> arrPath; if (! pbinc.empty()) { Str::SplitTrimNoNull(pbinc, arrPath, ":"); } // 构造proto,遍历所有的service dst.MapPath("", dir); GLOG(IM_DEBUG, "add %s to map path", dir.c_str()); for(string* i = arrPath.begin(); ! arrPath.end(); i = arrPath.next()) { GLOG(IM_DEBUG, "add %s to map path", i->c_str()); dst.MapPath("", *i); } dst.MapPath("", SETUP_ROOT_DIR "" PROTO_DIR "/"); dst.MapPath("", SETUP_ROOT_DIR "/dev/proto/"); ipt = new Importer(&dst, new IM_ErrorCollector()); fd = ipt->Import(file); return fd != NULL; }
void MapPathToDiskSourceTree(DiskSourceTree& src_tree, vector<string>& include_path_vec) { for (uint32_t i = 0; i < include_path_vec.size(); i++) { src_tree.MapPath("", include_path_vec[i]); } return; }
int main() { DiskSourceTree sourceTree; sourceTree.MapPath("", "./"); Importer importer(&sourceTree, NULL); importer.Import("Foo.proto"); const Descriptor *descriptor = importer.pool()->FindMessageTypeByName("Pair"); cout << descriptor->DebugString(); DynamicMessageFactory factory; const Message *message = factory.GetPrototype(descriptor); Message *pair = message->New(); const Reflection *reflection = pair->GetReflection(); const FieldDescriptor *field = NULL; field = descriptor->FindFieldByName("key"); reflection->SetString(pair, field, "my key"); field = descriptor->FindFieldByName("value"); reflection->SetUInt32(pair, field, 1111); cout << pair->DebugString(); delete pair; return 0; }
void register_protobuf_file(string filePath, string fileName, int proto_hadoop) { if ( g_listPBFile.end() != find(g_listPBFile.begin(), g_listPBFile.end(), filePath + fileName ) ) { return; } g_listPBFile.push_back( filePath + fileName ); DiskSourceTree sourceTree; sourceTree.MapPath("", filePath); // keep the Descriptor data in memory Importer* importer = new Importer(&sourceTree, NULL); const FileDescriptor* file = NULL; file = importer->Import(fileName); if (NULL == file) { return; } // recursive parse import file for (int iDependencyIndex = 0; iDependencyIndex < file->dependency_count(); iDependencyIndex++) { const FileDescriptor* dependencyFile = NULL; dependencyFile = file->dependency(iDependencyIndex); if (NULL == dependencyFile) { return; } register_protobuf_file(filePath, dependencyFile->name(), proto_hadoop); } // parse message for (int iMsgIndex = 0; iMsgIndex < file->message_type_count(); iMsgIndex++) { const Descriptor* message = file->message_type(iMsgIndex); register_protobuf_message(message, proto_hadoop); } // parse service for (int iServiceIndex = 0; iServiceIndex < file->service_count(); iServiceIndex++) { const ServiceDescriptor* service = file->service(iServiceIndex); for (int iMethodIndex = 0; iMethodIndex < service->method_count(); iMethodIndex++) { const MethodDescriptor* method = service->method(iMethodIndex); MethodInfo methodInfo; methodInfo.methodParamType = method->input_type()->full_name(); methodInfo.methodReturnType = method->output_type()->full_name(); g_mapMethod.insert(pair<string, MethodInfo>(method->name(), methodInfo)); } } // end parse service }
int DynamicParser::init(const char* protoname, const char* protopath) { DiskSourceTree sourceTree; // find proto file in current directory sourceTree.MapPath("", protopath); // import proto file Importer importer(&sourceTree, NULL); importer.Import(protoname); return 0; }