1 /*
2 *
3 * Copyright 2015 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_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
20 #define GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
21
22 #include "src/compiler/config.h"
23 #include "src/compiler/cpp_generator_helpers.h"
24 #include "src/compiler/python_generator_helpers.h"
25 #include "src/compiler/python_private_generator.h"
26 #include "src/compiler/schema_interface.h"
27
28 #include <vector>
29
30 // Get leading or trailing comments in a string.
31 template <typename DescriptorType>
GetCommentsHelper(const DescriptorType * desc,bool leading,const std::string & prefix)32 inline std::string GetCommentsHelper(const DescriptorType* desc, bool leading,
33 const std::string& prefix) {
34 return grpc_generator::GetPrefixedComments(desc, leading, prefix);
35 }
36
37 class ProtoBufMethod : public grpc_generator::Method {
38 public:
ProtoBufMethod(const grpc::protobuf::MethodDescriptor * method)39 ProtoBufMethod(const grpc::protobuf::MethodDescriptor* method)
40 : method_(method) {}
41
name()42 std::string name() const { return method_->name(); }
43
input_type_name()44 std::string input_type_name() const {
45 return grpc_cpp_generator::ClassName(method_->input_type(), true);
46 }
output_type_name()47 std::string output_type_name() const {
48 return grpc_cpp_generator::ClassName(method_->output_type(), true);
49 }
50
get_input_type_name()51 std::string get_input_type_name() const {
52 return method_->input_type()->file()->name();
53 }
get_output_type_name()54 std::string get_output_type_name() const {
55 return method_->output_type()->file()->name();
56 }
57
58 // TODO(https://github.com/grpc/grpc/issues/18800): Clean this up.
get_module_and_message_path_input(std::string * str,std::string generator_file_name,bool generate_in_pb2_grpc,std::string import_prefix,const std::vector<std::string> & prefixes_to_filter)59 bool get_module_and_message_path_input(
60 std::string* str, std::string generator_file_name,
61 bool generate_in_pb2_grpc, std::string import_prefix,
62 const std::vector<std::string>& prefixes_to_filter) const final {
63 return grpc_python_generator::GetModuleAndMessagePath(
64 method_->input_type(), str, generator_file_name, generate_in_pb2_grpc,
65 import_prefix, prefixes_to_filter);
66 }
67
get_module_and_message_path_output(std::string * str,std::string generator_file_name,bool generate_in_pb2_grpc,std::string import_prefix,const std::vector<std::string> & prefixes_to_filter)68 bool get_module_and_message_path_output(
69 std::string* str, std::string generator_file_name,
70 bool generate_in_pb2_grpc, std::string import_prefix,
71 const std::vector<std::string>& prefixes_to_filter) const final {
72 return grpc_python_generator::GetModuleAndMessagePath(
73 method_->output_type(), str, generator_file_name, generate_in_pb2_grpc,
74 import_prefix, prefixes_to_filter);
75 }
76
NoStreaming()77 bool NoStreaming() const {
78 return !method_->client_streaming() && !method_->server_streaming();
79 }
80
ClientStreaming()81 bool ClientStreaming() const { return method_->client_streaming(); }
82
ServerStreaming()83 bool ServerStreaming() const { return method_->server_streaming(); }
84
BidiStreaming()85 bool BidiStreaming() const {
86 return method_->client_streaming() && method_->server_streaming();
87 }
88
GetLeadingComments(const std::string prefix)89 std::string GetLeadingComments(const std::string prefix) const {
90 return GetCommentsHelper(method_, true, prefix);
91 }
92
GetTrailingComments(const std::string prefix)93 std::string GetTrailingComments(const std::string prefix) const {
94 return GetCommentsHelper(method_, false, prefix);
95 }
96
GetAllComments()97 vector<std::string> GetAllComments() const {
98 return grpc_python_generator::get_all_comments(method_);
99 }
100
101 private:
102 const grpc::protobuf::MethodDescriptor* method_;
103 };
104
105 class ProtoBufService : public grpc_generator::Service {
106 public:
ProtoBufService(const grpc::protobuf::ServiceDescriptor * service)107 ProtoBufService(const grpc::protobuf::ServiceDescriptor* service)
108 : service_(service) {}
109
name()110 std::string name() const { return service_->name(); }
111
method_count()112 int method_count() const { return service_->method_count(); }
method(int i)113 std::unique_ptr<const grpc_generator::Method> method(int i) const {
114 return std::unique_ptr<const grpc_generator::Method>(
115 new ProtoBufMethod(service_->method(i)));
116 }
117
GetLeadingComments(const std::string prefix)118 std::string GetLeadingComments(const std::string prefix) const {
119 return GetCommentsHelper(service_, true, prefix);
120 }
121
GetTrailingComments(const std::string prefix)122 std::string GetTrailingComments(const std::string prefix) const {
123 return GetCommentsHelper(service_, false, prefix);
124 }
125
GetAllComments()126 vector<std::string> GetAllComments() const {
127 return grpc_python_generator::get_all_comments(service_);
128 }
129
130 private:
131 const grpc::protobuf::ServiceDescriptor* service_;
132 };
133
134 class ProtoBufPrinter : public grpc_generator::Printer {
135 public:
ProtoBufPrinter(std::string * str)136 ProtoBufPrinter(std::string* str)
137 : output_stream_(str), printer_(&output_stream_, '$') {}
138
Print(const std::map<std::string,std::string> & vars,const char * string_template)139 void Print(const std::map<std::string, std::string>& vars,
140 const char* string_template) {
141 printer_.Print(vars, string_template);
142 }
143
Print(const char * string)144 void Print(const char* string) { printer_.Print(string); }
PrintRaw(const char * string)145 void PrintRaw(const char* string) { printer_.PrintRaw(string); }
Indent()146 void Indent() { printer_.Indent(); }
Outdent()147 void Outdent() { printer_.Outdent(); }
148
149 private:
150 grpc::protobuf::io::StringOutputStream output_stream_;
151 grpc::protobuf::io::Printer printer_;
152 };
153
154 class ProtoBufFile : public grpc_generator::File {
155 public:
ProtoBufFile(const grpc::protobuf::FileDescriptor * file)156 ProtoBufFile(const grpc::protobuf::FileDescriptor* file) : file_(file) {}
157
filename()158 std::string filename() const { return file_->name(); }
filename_without_ext()159 std::string filename_without_ext() const {
160 return grpc_generator::StripProto(filename());
161 }
162
package()163 std::string package() const { return file_->package(); }
package_parts()164 std::vector<std::string> package_parts() const {
165 return grpc_generator::tokenize(package(), ".");
166 }
167
additional_headers()168 std::string additional_headers() const { return ""; }
169
service_count()170 int service_count() const { return file_->service_count(); }
service(int i)171 std::unique_ptr<const grpc_generator::Service> service(int i) const {
172 return std::unique_ptr<const grpc_generator::Service>(
173 new ProtoBufService(file_->service(i)));
174 }
175
CreatePrinter(std::string * str)176 std::unique_ptr<grpc_generator::Printer> CreatePrinter(
177 std::string* str) const {
178 return std::unique_ptr<grpc_generator::Printer>(new ProtoBufPrinter(str));
179 }
180
GetLeadingComments(const std::string prefix)181 std::string GetLeadingComments(const std::string prefix) const {
182 return GetCommentsHelper(file_, true, prefix);
183 }
184
GetTrailingComments(const std::string prefix)185 std::string GetTrailingComments(const std::string prefix) const {
186 return GetCommentsHelper(file_, false, prefix);
187 }
188
GetAllComments()189 vector<std::string> GetAllComments() const {
190 return grpc_python_generator::get_all_comments(file_);
191 }
192
GetImportNames()193 vector<std::string> GetImportNames() const {
194 vector<std::string> proto_names;
195 for (int i = 0; i < file_->dependency_count(); ++i) {
196 const auto& dep = *file_->dependency(i);
197 proto_names.push_back(dep.name());
198 }
199 return proto_names;
200 }
201
202 private:
203 const grpc::protobuf::FileDescriptor* file_;
204 };
205
206 #endif // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
207