• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 grpc::string & prefix)32 inline grpc::string GetCommentsHelper(const DescriptorType* desc, bool leading,
33                                       const grpc::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   grpc::string name() const { return method_->name(); }
43 
input_type_name()44   grpc::string input_type_name() const {
45     return grpc_cpp_generator::ClassName(method_->input_type(), true);
46   }
output_type_name()47   grpc::string output_type_name() const {
48     return grpc_cpp_generator::ClassName(method_->output_type(), true);
49   }
50 
get_input_type_name()51   grpc::string get_input_type_name() const {
52     return method_->input_type()->file()->name();
53   }
get_output_type_name()54   grpc::string get_output_type_name() const {
55     return method_->output_type()->file()->name();
56   }
57 
get_module_and_message_path_input(grpc::string * str,grpc::string generator_file_name,bool generate_in_pb2_grpc,grpc::string import_prefix)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,
64         import_prefix);
65   }
66 
get_module_and_message_path_output(grpc::string * str,grpc::string generator_file_name,bool generate_in_pb2_grpc,grpc::string import_prefix)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,
73         import_prefix);
74   }
75 
NoStreaming()76   bool NoStreaming() const {
77     return !method_->client_streaming() && !method_->server_streaming();
78   }
79 
ClientStreaming()80   bool ClientStreaming() const { return method_->client_streaming(); }
81 
ServerStreaming()82   bool ServerStreaming() const { return method_->server_streaming(); }
83 
BidiStreaming()84   bool BidiStreaming() const {
85     return method_->client_streaming() && method_->server_streaming();
86   }
87 
GetLeadingComments(const grpc::string prefix)88   grpc::string GetLeadingComments(const grpc::string prefix) const {
89     return GetCommentsHelper(method_, true, prefix);
90   }
91 
GetTrailingComments(const grpc::string prefix)92   grpc::string GetTrailingComments(const grpc::string prefix) const {
93     return GetCommentsHelper(method_, false, prefix);
94   }
95 
GetAllComments()96   vector<grpc::string> GetAllComments() const {
97     return grpc_python_generator::get_all_comments(method_);
98   }
99 
100  private:
101   const grpc::protobuf::MethodDescriptor* method_;
102 };
103 
104 class ProtoBufService : public grpc_generator::Service {
105  public:
ProtoBufService(const grpc::protobuf::ServiceDescriptor * service)106   ProtoBufService(const grpc::protobuf::ServiceDescriptor* service)
107       : service_(service) {}
108 
name()109   grpc::string name() const { return service_->name(); }
110 
method_count()111   int method_count() const { return service_->method_count(); };
method(int i)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)));
115   };
116 
GetLeadingComments(const grpc::string prefix)117   grpc::string GetLeadingComments(const grpc::string prefix) const {
118     return GetCommentsHelper(service_, true, prefix);
119   }
120 
GetTrailingComments(const grpc::string prefix)121   grpc::string GetTrailingComments(const grpc::string prefix) const {
122     return GetCommentsHelper(service_, false, prefix);
123   }
124 
GetAllComments()125   vector<grpc::string> GetAllComments() const {
126     return grpc_python_generator::get_all_comments(service_);
127   }
128 
129  private:
130   const grpc::protobuf::ServiceDescriptor* service_;
131 };
132 
133 class ProtoBufPrinter : public grpc_generator::Printer {
134  public:
ProtoBufPrinter(grpc::string * str)135   ProtoBufPrinter(grpc::string* str)
136       : output_stream_(str), printer_(&output_stream_, '$') {}
137 
Print(const std::map<grpc::string,grpc::string> & vars,const char * string_template)138   void Print(const std::map<grpc::string, grpc::string>& vars,
139              const char* string_template) {
140     printer_.Print(vars, string_template);
141   }
142 
Print(const char * string)143   void Print(const char* string) { printer_.Print(string); }
PrintRaw(const char * string)144   void PrintRaw(const char* string) { printer_.PrintRaw(string); }
Indent()145   void Indent() { printer_.Indent(); }
Outdent()146   void Outdent() { printer_.Outdent(); }
147 
148  private:
149   grpc::protobuf::io::StringOutputStream output_stream_;
150   grpc::protobuf::io::Printer printer_;
151 };
152 
153 class ProtoBufFile : public grpc_generator::File {
154  public:
ProtoBufFile(const grpc::protobuf::FileDescriptor * file)155   ProtoBufFile(const grpc::protobuf::FileDescriptor* file) : file_(file) {}
156 
filename()157   grpc::string filename() const { return file_->name(); }
filename_without_ext()158   grpc::string filename_without_ext() const {
159     return grpc_generator::StripProto(filename());
160   }
161 
package()162   grpc::string package() const { return file_->package(); }
package_parts()163   std::vector<grpc::string> package_parts() const {
164     return grpc_generator::tokenize(package(), ".");
165   }
166 
additional_headers()167   grpc::string additional_headers() const { return ""; }
168 
service_count()169   int service_count() const { return file_->service_count(); };
service(int i)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)));
173   }
174 
CreatePrinter(grpc::string * str)175   std::unique_ptr<grpc_generator::Printer> CreatePrinter(
176       grpc::string* str) const {
177     return std::unique_ptr<grpc_generator::Printer>(new ProtoBufPrinter(str));
178   }
179 
GetLeadingComments(const grpc::string prefix)180   grpc::string GetLeadingComments(const grpc::string prefix) const {
181     return GetCommentsHelper(file_, true, prefix);
182   }
183 
GetTrailingComments(const grpc::string prefix)184   grpc::string GetTrailingComments(const grpc::string prefix) const {
185     return GetCommentsHelper(file_, false, prefix);
186   }
187 
GetAllComments()188   vector<grpc::string> GetAllComments() const {
189     return grpc_python_generator::get_all_comments(file_);
190   }
191 
192  private:
193   const grpc::protobuf::FileDescriptor* file_;
194 };
195 
196 #endif  // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
197