3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
20 #define GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
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"
30 // Get leading or trailing comments in a string.
31 template <typename DescriptorType>
32 inline grpc::string GetCommentsHelper(const DescriptorType* desc, bool leading,
33 const grpc::string& prefix) {
34 return grpc_generator::GetPrefixedComments(desc, leading, prefix);
37 class ProtoBufMethod : public grpc_generator::Method {
39 ProtoBufMethod(const grpc::protobuf::MethodDescriptor* method)
42 grpc::string name() const { return method_->name(); }
44 grpc::string input_type_name() const {
45 return grpc_cpp_generator::ClassName(method_->input_type(), true);
47 grpc::string output_type_name() const {
48 return grpc_cpp_generator::ClassName(method_->output_type(), true);
51 grpc::string get_input_type_name() const {
52 return method_->input_type()->file()->name();
54 grpc::string get_output_type_name() const {
55 return method_->output_type()->file()->name();
58 bool get_module_and_message_path_input(grpc::string* str,
59 grpc::string generator_file_name,
60 bool generate_in_pb2_grpc,
61 grpc::string import_prefix) const {
62 return grpc_python_generator::GetModuleAndMessagePath(
63 method_->input_type(), str, generator_file_name, generate_in_pb2_grpc,
67 bool get_module_and_message_path_output(grpc::string* str,
68 grpc::string generator_file_name,
69 bool generate_in_pb2_grpc,
70 grpc::string import_prefix) const {
71 return grpc_python_generator::GetModuleAndMessagePath(
72 method_->output_type(), str, generator_file_name, generate_in_pb2_grpc,
76 bool NoStreaming() const {
77 return !method_->client_streaming() && !method_->server_streaming();
80 bool ClientStreaming() const { return method_->client_streaming(); }
82 bool ServerStreaming() const { return method_->server_streaming(); }
84 bool BidiStreaming() const {
85 return method_->client_streaming() && method_->server_streaming();
88 grpc::string GetLeadingComments(const grpc::string prefix) const {
89 return GetCommentsHelper(method_, true, prefix);
92 grpc::string GetTrailingComments(const grpc::string prefix) const {
93 return GetCommentsHelper(method_, false, prefix);
96 vector<grpc::string> GetAllComments() const {
97 return grpc_python_generator::get_all_comments(method_);
101 const grpc::protobuf::MethodDescriptor* method_;
104 class ProtoBufService : public grpc_generator::Service {
106 ProtoBufService(const grpc::protobuf::ServiceDescriptor* service)
107 : service_(service) {}
109 grpc::string name() const { return service_->name(); }
111 int method_count() const { return service_->method_count(); };
112 std::unique_ptr<const grpc_generator::Method> method(int i) const {
113 return std::unique_ptr<const grpc_generator::Method>(
114 new ProtoBufMethod(service_->method(i)));
117 grpc::string GetLeadingComments(const grpc::string prefix) const {
118 return GetCommentsHelper(service_, true, prefix);
121 grpc::string GetTrailingComments(const grpc::string prefix) const {
122 return GetCommentsHelper(service_, false, prefix);
125 vector<grpc::string> GetAllComments() const {
126 return grpc_python_generator::get_all_comments(service_);
130 const grpc::protobuf::ServiceDescriptor* service_;
133 class ProtoBufPrinter : public grpc_generator::Printer {
135 ProtoBufPrinter(grpc::string* str)
136 : output_stream_(str), printer_(&output_stream_, '$') {}
138 void Print(const std::map<grpc::string, grpc::string>& vars,
139 const char* string_template) {
140 printer_.Print(vars, string_template);
143 void Print(const char* string) { printer_.Print(string); }
144 void PrintRaw(const char* string) { printer_.PrintRaw(string); }
145 void Indent() { printer_.Indent(); }
146 void Outdent() { printer_.Outdent(); }
149 grpc::protobuf::io::StringOutputStream output_stream_;
150 grpc::protobuf::io::Printer printer_;
153 class ProtoBufFile : public grpc_generator::File {
155 ProtoBufFile(const grpc::protobuf::FileDescriptor* file) : file_(file) {}
157 grpc::string filename() const { return file_->name(); }
158 grpc::string filename_without_ext() const {
159 return grpc_generator::StripProto(filename());
162 grpc::string package() const { return file_->package(); }
163 std::vector<grpc::string> package_parts() const {
164 return grpc_generator::tokenize(package(), ".");
167 grpc::string additional_headers() const { return ""; }
169 int service_count() const { return file_->service_count(); };
170 std::unique_ptr<const grpc_generator::Service> service(int i) const {
171 return std::unique_ptr<const grpc_generator::Service>(
172 new ProtoBufService(file_->service(i)));
175 std::unique_ptr<grpc_generator::Printer> CreatePrinter(
176 grpc::string* str) const {
177 return std::unique_ptr<grpc_generator::Printer>(new ProtoBufPrinter(str));
180 grpc::string GetLeadingComments(const grpc::string prefix) const {
181 return GetCommentsHelper(file_, true, prefix);
184 grpc::string GetTrailingComments(const grpc::string prefix) const {
185 return GetCommentsHelper(file_, false, prefix);
188 vector<grpc::string> GetAllComments() const {
189 return grpc_python_generator::get_all_comments(file_);
193 const grpc::protobuf::FileDescriptor* file_;
196 #endif // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H