python_generator_helpers.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #ifndef GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H
  34. #define GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H
  35. #include <cstring>
  36. #include <fstream>
  37. #include <iostream>
  38. #include <vector>
  39. #include "src/compiler/config.h"
  40. #include "src/compiler/generator_helpers.h"
  41. #include "src/compiler/python_generator.h"
  42. #include "src/compiler/python_private_generator.h"
  43. using std::vector;
  44. using grpc_generator::StringReplace;
  45. using grpc_generator::StripProto;
  46. using grpc::protobuf::Descriptor;
  47. using grpc::protobuf::FileDescriptor;
  48. using grpc::protobuf::MethodDescriptor;
  49. using grpc::protobuf::ServiceDescriptor;
  50. using grpc::protobuf::compiler::GeneratorContext;
  51. using grpc::protobuf::io::CodedOutputStream;
  52. using grpc::protobuf::io::Printer;
  53. using grpc::protobuf::io::StringOutputStream;
  54. using grpc::protobuf::io::ZeroCopyOutputStream;
  55. namespace grpc_python_generator {
  56. namespace {
  57. typedef vector<const Descriptor*> DescriptorVector;
  58. typedef vector<grpc::string> StringVector;
  59. // TODO(https://github.com/google/protobuf/issues/888):
  60. // Export `ModuleName` from protobuf's
  61. // `src/google/protobuf/compiler/python/python_generator.cc` file.
  62. grpc::string ModuleName(const grpc::string& filename,
  63. const grpc::string& import_prefix) {
  64. grpc::string basename = StripProto(filename);
  65. basename = StringReplace(basename, "-", "_");
  66. basename = StringReplace(basename, "/", ".");
  67. return import_prefix + basename + "_pb2";
  68. }
  69. // TODO(https://github.com/google/protobuf/issues/888):
  70. // Export `ModuleAlias` from protobuf's
  71. // `src/google/protobuf/compiler/python/python_generator.cc` file.
  72. grpc::string ModuleAlias(const grpc::string& filename,
  73. const grpc::string& import_prefix) {
  74. grpc::string module_name = ModuleName(filename, import_prefix);
  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(const Descriptor* type, grpc::string* out,
  83. grpc::string generator_file_name,
  84. bool generate_in_pb2_grpc,
  85. grpc::string& import_prefix) {
  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) + ".";
  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. } // namespace
  126. } // namespace grpc_python_generator
  127. #endif // GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H