proto_file_parser.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. *
  3. * Copyright 2016 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_TEST_CPP_UTIL_PROTO_FILE_PARSER_H
  19. #define GRPC_TEST_CPP_UTIL_PROTO_FILE_PARSER_H
  20. #include <memory>
  21. #include <grpcpp/channel.h>
  22. #include "test/cpp/util/config_grpc_cli.h"
  23. #include "test/cpp/util/proto_reflection_descriptor_database.h"
  24. #if defined(_WIN32) && !defined(__CYGWIN__)
  25. #define GRPC_CLI_PATH_SEPARATOR ";"
  26. #else
  27. #define GRPC_CLI_PATH_SEPARATOR ":"
  28. #endif
  29. namespace grpc {
  30. namespace testing {
  31. class ErrorPrinter;
  32. // Find method and associated request/response types.
  33. class ProtoFileParser {
  34. public:
  35. // The parser will search proto files using the server reflection service
  36. // provided on the given channel. The given protofiles in a source tree rooted
  37. // from proto_path will also be searched.
  38. ProtoFileParser(const std::shared_ptr<grpc::Channel>& channel,
  39. const std::string& proto_path, const std::string& protofiles);
  40. ~ProtoFileParser();
  41. // The input method name in the following four functions could be a partial
  42. // string such as Service.Method or even just Method. It will log an error if
  43. // there is ambiguity.
  44. // Full method name is in the form of Service.Method, it's good to be used in
  45. // descriptor database queries.
  46. std::string GetFullMethodName(const std::string& method);
  47. // Formatted method name is in the form of /Service/Method, it's good to be
  48. // used as the argument of Stub::Call()
  49. std::string GetFormattedMethodName(const std::string& method);
  50. /// Converts a text or json string to its binary proto representation for the
  51. /// given method's input or return type.
  52. /// \param method the name of the method (does not need to be fully qualified
  53. /// name)
  54. /// \param formatted_proto the text- or json-formatted proto string
  55. /// \param is_request if \c true the resolved type is that of the input
  56. /// parameter of the method, otherwise it is the output type
  57. /// \param is_json_format if \c true the \c formatted_proto is treated as a
  58. /// json-formatted proto, otherwise it is treated as a text-formatted
  59. /// proto
  60. /// \return the serialised binary proto representation of \c formatted_proto
  61. std::string GetSerializedProtoFromMethod(const std::string& method,
  62. const std::string& formatted_proto,
  63. bool is_request,
  64. bool is_json_format);
  65. /// Converts a text or json string to its proto representation for the given
  66. /// message type.
  67. /// \param formatted_proto the text- or json-formatted proto string
  68. /// \return the serialised binary proto representation of \c formatted_proto
  69. std::string GetSerializedProtoFromMessageType(
  70. const std::string& message_type_name, const std::string& formatted_proto,
  71. bool is_json_format);
  72. /// Converts a binary proto string to its text or json string representation
  73. /// for the given method's input or return type.
  74. /// \param method the name of the method (does not need to be a fully
  75. /// qualified name)
  76. /// \param the serialised binary proto representation of type
  77. /// \c message_type_name
  78. /// \return the text- or json-formatted proto string of \c serialized_proto
  79. std::string GetFormattedStringFromMethod(const std::string& method,
  80. const std::string& serialized_proto,
  81. bool is_request,
  82. bool is_json_format);
  83. /// Converts a binary proto string to its text or json string representation
  84. /// for the given message type.
  85. /// \param the serialised binary proto representation of type
  86. /// \c message_type_name
  87. /// \return the text- or json-formatted proto string of \c serialized_proto
  88. std::string GetFormattedStringFromMessageType(
  89. const std::string& message_type_name, const std::string& serialized_proto,
  90. bool is_json_format);
  91. bool IsStreaming(const std::string& method, bool is_request);
  92. bool HasError() const { return has_error_; }
  93. void LogError(const std::string& error_msg);
  94. private:
  95. std::string GetMessageTypeFromMethod(const std::string& method,
  96. bool is_request);
  97. bool has_error_;
  98. std::string request_text_;
  99. protobuf::compiler::DiskSourceTree source_tree_;
  100. std::unique_ptr<ErrorPrinter> error_printer_;
  101. std::unique_ptr<protobuf::compiler::Importer> importer_;
  102. std::unique_ptr<grpc::ProtoReflectionDescriptorDatabase> reflection_db_;
  103. std::unique_ptr<protobuf::DescriptorPoolDatabase> file_db_;
  104. std::unique_ptr<protobuf::DescriptorDatabase> desc_db_;
  105. std::unique_ptr<protobuf::DescriptorPool> desc_pool_;
  106. std::unique_ptr<protobuf::DynamicMessageFactory> dynamic_factory_;
  107. std::unique_ptr<grpc::protobuf::Message> request_prototype_;
  108. std::unique_ptr<grpc::protobuf::Message> response_prototype_;
  109. std::unordered_map<std::string, std::string> known_methods_;
  110. std::vector<const protobuf::ServiceDescriptor*> service_desc_list_;
  111. };
  112. } // namespace testing
  113. } // namespace grpc
  114. #endif // GRPC_TEST_CPP_UTIL_PROTO_FILE_PARSER_H