python_generator_helpers.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H
  19. #define GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H
  20. #include <cstring>
  21. #include <fstream>
  22. #include <iostream>
  23. #include <vector>
  24. #include "src/compiler/config.h"
  25. #include "src/compiler/generator_helpers.h"
  26. #include "src/compiler/python_generator.h"
  27. #include "src/compiler/python_private_generator.h"
  28. using grpc::protobuf::Descriptor;
  29. using grpc::protobuf::FileDescriptor;
  30. using grpc::protobuf::MethodDescriptor;
  31. using grpc::protobuf::ServiceDescriptor;
  32. using grpc::protobuf::compiler::GeneratorContext;
  33. using grpc::protobuf::io::CodedOutputStream;
  34. using grpc::protobuf::io::Printer;
  35. using grpc::protobuf::io::StringOutputStream;
  36. using grpc::protobuf::io::ZeroCopyOutputStream;
  37. using grpc_generator::StringReplace;
  38. using grpc_generator::StripProto;
  39. using std::vector;
  40. namespace grpc_python_generator {
  41. namespace {
  42. typedef vector<const Descriptor*> DescriptorVector;
  43. typedef vector<grpc::string> StringVector;
  44. static grpc::string StripModulePrefixes(
  45. const grpc::string& raw_module_name,
  46. const std::vector<grpc::string>& prefixes_to_filter) {
  47. for (const auto& prefix : prefixes_to_filter) {
  48. if (raw_module_name.rfind(prefix, 0) == 0) {
  49. return raw_module_name.substr(prefix.size(),
  50. raw_module_name.size() - prefix.size());
  51. }
  52. }
  53. return raw_module_name;
  54. }
  55. // TODO(https://github.com/google/protobuf/issues/888):
  56. // Export `ModuleName` from protobuf's
  57. // `src/google/protobuf/compiler/python/python_generator.cc` file.
  58. grpc::string ModuleName(const grpc::string& filename,
  59. const grpc::string& import_prefix,
  60. const std::vector<grpc::string>& prefixes_to_filter) {
  61. grpc::string basename = StripProto(filename);
  62. basename = StringReplace(basename, "-", "_");
  63. basename = StringReplace(basename, "/", ".");
  64. return StripModulePrefixes(import_prefix + basename + "_pb2",
  65. prefixes_to_filter);
  66. }
  67. // TODO(https://github.com/google/protobuf/issues/888):
  68. // Export `ModuleAlias` from protobuf's
  69. // `src/google/protobuf/compiler/python/python_generator.cc` file.
  70. grpc::string ModuleAlias(const grpc::string& filename,
  71. const grpc::string& import_prefix,
  72. const std::vector<grpc::string>& prefixes_to_filter) {
  73. grpc::string module_name =
  74. ModuleName(filename, import_prefix, prefixes_to_filter);
  75. // We can't have dots in the module name, so we replace each with _dot_.
  76. // But that could lead to a collision between a.b and a_dot_b, so we also
  77. // duplicate each underscore.
  78. module_name = StringReplace(module_name, "_", "__");
  79. module_name = StringReplace(module_name, ".", "_dot_");
  80. return module_name;
  81. }
  82. bool GetModuleAndMessagePath(
  83. const Descriptor* type, grpc::string* out, grpc::string generator_file_name,
  84. bool generate_in_pb2_grpc, grpc::string& import_prefix,
  85. const std::vector<grpc::string>& prefixes_to_filter) {
  86. const Descriptor* path_elem_type = type;
  87. DescriptorVector message_path;
  88. do {
  89. message_path.push_back(path_elem_type);
  90. path_elem_type = path_elem_type->containing_type();
  91. } while (path_elem_type); // implicit nullptr comparison; don't be explicit
  92. grpc::string file_name = type->file()->name();
  93. static const int proto_suffix_length = strlen(".proto");
  94. if (!(file_name.size() > static_cast<size_t>(proto_suffix_length) &&
  95. file_name.find_last_of(".proto") == file_name.size() - 1)) {
  96. return false;
  97. }
  98. grpc::string module;
  99. if (generator_file_name != file_name || generate_in_pb2_grpc) {
  100. module = ModuleAlias(file_name, import_prefix, prefixes_to_filter) + ".";
  101. } else {
  102. module = "";
  103. }
  104. grpc::string message_type;
  105. for (DescriptorVector::reverse_iterator path_iter = message_path.rbegin();
  106. path_iter != message_path.rend(); ++path_iter) {
  107. message_type += (*path_iter)->name() + ".";
  108. }
  109. // no pop_back prior to C++11
  110. message_type.resize(message_type.size() - 1);
  111. *out = module + message_type;
  112. return true;
  113. }
  114. template <typename DescriptorType>
  115. StringVector get_all_comments(const DescriptorType* descriptor) {
  116. StringVector comments;
  117. grpc_generator::GetComment(
  118. descriptor, grpc_generator::COMMENTTYPE_LEADING_DETACHED, &comments);
  119. grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_LEADING,
  120. &comments);
  121. grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_TRAILING,
  122. &comments);
  123. return comments;
  124. }
  125. inline void Split(const grpc::string& s, char delim,
  126. std::vector<grpc::string>* append_to) {
  127. auto current = s.begin();
  128. while (current <= s.end()) {
  129. auto next = std::find(current, s.end(), delim);
  130. append_to->emplace_back(current, next);
  131. current = next + 1;
  132. }
  133. }
  134. } // namespace
  135. } // namespace grpc_python_generator
  136. #endif // GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H