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