• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 grpc::string& proto_path,
41                   const grpc::string& protofiles);
42 
43   ~ProtoFileParser();
44 
45   // The input method name in the following four functions could be a partial
46   // string such as Service.Method or even just Method. It will log an error if
47   // there is ambiguity.
48   // Full method name is in the form of Service.Method, it's good to be used in
49   // descriptor database queries.
50   grpc::string GetFullMethodName(const grpc::string& method);
51 
52   // Formatted method name is in the form of /Service/Method, it's good to be
53   // used as the argument of Stub::Call()
54   grpc::string GetFormattedMethodName(const grpc::string& method);
55 
56   grpc::string GetSerializedProtoFromMethod(
57       const grpc::string& method, const grpc::string& text_format_proto,
58       bool is_request);
59 
60   grpc::string GetTextFormatFromMethod(const grpc::string& method,
61                                        const grpc::string& serialized_proto,
62                                        bool is_request);
63 
64   grpc::string GetSerializedProtoFromMessageType(
65       const grpc::string& message_type_name,
66       const grpc::string& text_format_proto);
67 
68   grpc::string GetTextFormatFromMessageType(
69       const grpc::string& message_type_name,
70       const grpc::string& serialized_proto);
71 
72   bool IsStreaming(const grpc::string& method, bool is_request);
73 
HasError()74   bool HasError() const { return has_error_; }
75 
76   void LogError(const grpc::string& error_msg);
77 
78  private:
79   grpc::string GetMessageTypeFromMethod(const grpc::string& method,
80                                         bool is_request);
81 
82   bool has_error_;
83   grpc::string request_text_;
84   protobuf::compiler::DiskSourceTree source_tree_;
85   std::unique_ptr<ErrorPrinter> error_printer_;
86   std::unique_ptr<protobuf::compiler::Importer> importer_;
87   std::unique_ptr<grpc::ProtoReflectionDescriptorDatabase> reflection_db_;
88   std::unique_ptr<protobuf::DescriptorPoolDatabase> file_db_;
89   std::unique_ptr<protobuf::DescriptorDatabase> desc_db_;
90   std::unique_ptr<protobuf::DescriptorPool> desc_pool_;
91   std::unique_ptr<protobuf::DynamicMessageFactory> dynamic_factory_;
92   std::unique_ptr<grpc::protobuf::Message> request_prototype_;
93   std::unique_ptr<grpc::protobuf::Message> response_prototype_;
94   std::unordered_map<grpc::string, grpc::string> known_methods_;
95   std::vector<const protobuf::ServiceDescriptor*> service_desc_list_;
96 };
97 
98 }  // namespace testing
99 }  // namespace grpc
100 
101 #endif  // GRPC_TEST_CPP_UTIL_PROTO_FILE_PARSER_H
102