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