blob: 1551908156af2480116bf0b251a7961173c58ba9 [file] [log] [blame]
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +05301/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +05304 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +05308 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053016 *
17 */
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053018
19#ifndef GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
20#define GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
21
22#include "src/compiler/config.h"
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053023#include "src/compiler/cpp_generator_helpers.h"
Harsh Vardhan93077ae2016-12-26 18:23:45 +053024#include "src/compiler/python_generator_helpers.h"
Vijay Pai4fdb08a2017-11-14 16:48:45 -080025#include "src/compiler/python_private_generator.h"
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053026#include "src/compiler/schema_interface.h"
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053027
28#include <vector>
29
30// Get leading or trailing comments in a string.
31template <typename DescriptorType>
Craig Tillerbaa14a92017-11-03 09:09:36 -070032inline grpc::string GetCommentsHelper(const DescriptorType* desc, bool leading,
33 const grpc::string& prefix) {
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053034 return grpc_generator::GetPrefixedComments(desc, leading, prefix);
35}
36
37class ProtoBufMethod : public grpc_generator::Method {
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053038 public:
Craig Tillerbaa14a92017-11-03 09:09:36 -070039 ProtoBufMethod(const grpc::protobuf::MethodDescriptor* method)
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053040 : method_(method) {}
41
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053042 grpc::string name() const { return method_->name(); }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053043
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053044 grpc::string input_type_name() const {
45 return grpc_cpp_generator::ClassName(method_->input_type(), true);
46 }
47 grpc::string output_type_name() const {
48 return grpc_cpp_generator::ClassName(method_->output_type(), true);
49 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053050
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053051 grpc::string get_input_type_name() const {
52 return method_->input_type()->file()->name();
53 }
54 grpc::string get_output_type_name() const {
55 return method_->output_type()->file()->name();
56 }
Harsh Vardhan6a9950d2017-02-07 12:01:44 +053057
Craig Tillerbaa14a92017-11-03 09:09:36 -070058 bool get_module_and_message_path_input(grpc::string* str,
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053059 grpc::string generator_file_name,
Harsh Vardhanabf9ce22017-03-14 22:56:31 +053060 bool generate_in_pb2_grpc,
Harsh Vardhand48ee252017-03-14 23:10:03 +053061 grpc::string import_prefix) const {
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053062 return grpc_python_generator::GetModuleAndMessagePath(
Harsh Vardhanabf9ce22017-03-14 22:56:31 +053063 method_->input_type(), str, generator_file_name, generate_in_pb2_grpc,
64 import_prefix);
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053065 }
Harsh Vardhan93077ae2016-12-26 18:23:45 +053066
Craig Tillerbaa14a92017-11-03 09:09:36 -070067 bool get_module_and_message_path_output(grpc::string* str,
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053068 grpc::string generator_file_name,
Harsh Vardhanabf9ce22017-03-14 22:56:31 +053069 bool generate_in_pb2_grpc,
Harsh Vardhand48ee252017-03-14 23:10:03 +053070 grpc::string import_prefix) const {
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053071 return grpc_python_generator::GetModuleAndMessagePath(
Harsh Vardhanabf9ce22017-03-14 22:56:31 +053072 method_->output_type(), str, generator_file_name, generate_in_pb2_grpc,
73 import_prefix);
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053074 }
Harsh Vardhan93077ae2016-12-26 18:23:45 +053075
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053076 bool NoStreaming() const {
77 return !method_->client_streaming() && !method_->server_streaming();
78 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053079
Harsh Vardhan974f3d72017-03-03 14:19:32 +053080 bool ClientStreaming() const { return method_->client_streaming(); }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053081
Harsh Vardhan974f3d72017-03-03 14:19:32 +053082 bool ServerStreaming() const { return method_->server_streaming(); }
Harsh Vardhancacd5e82017-02-26 21:18:36 +053083
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053084 bool BidiStreaming() const {
85 return method_->client_streaming() && method_->server_streaming();
86 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053087
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053088 grpc::string GetLeadingComments(const grpc::string prefix) const {
89 return GetCommentsHelper(method_, true, prefix);
90 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053091
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053092 grpc::string GetTrailingComments(const grpc::string prefix) const {
93 return GetCommentsHelper(method_, false, prefix);
94 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053095
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053096 vector<grpc::string> GetAllComments() const {
97 return grpc_python_generator::get_all_comments(method_);
98 }
Harsh Vardhan93077ae2016-12-26 18:23:45 +053099
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530100 private:
Craig Tillerbaa14a92017-11-03 09:09:36 -0700101 const grpc::protobuf::MethodDescriptor* method_;
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530102};
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530103
104class ProtoBufService : public grpc_generator::Service {
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530105 public:
Craig Tillerbaa14a92017-11-03 09:09:36 -0700106 ProtoBufService(const grpc::protobuf::ServiceDescriptor* service)
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530107 : service_(service) {}
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530108
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530109 grpc::string name() const { return service_->name(); }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530110
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530111 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)));
115 };
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530116
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530117 grpc::string GetLeadingComments(const grpc::string prefix) const {
118 return GetCommentsHelper(service_, true, prefix);
119 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530120
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530121 grpc::string GetTrailingComments(const grpc::string prefix) const {
122 return GetCommentsHelper(service_, false, prefix);
123 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530124
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530125 vector<grpc::string> GetAllComments() const {
126 return grpc_python_generator::get_all_comments(service_);
127 }
Harsh Vardhan93077ae2016-12-26 18:23:45 +0530128
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530129 private:
Craig Tillerbaa14a92017-11-03 09:09:36 -0700130 const grpc::protobuf::ServiceDescriptor* service_;
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530131};
132
133class ProtoBufPrinter : public grpc_generator::Printer {
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530134 public:
Craig Tillerbaa14a92017-11-03 09:09:36 -0700135 ProtoBufPrinter(grpc::string* str)
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530136 : output_stream_(str), printer_(&output_stream_, '$') {}
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530137
Craig Tillerbaa14a92017-11-03 09:09:36 -0700138 void Print(const std::map<grpc::string, grpc::string>& vars,
139 const char* string_template) {
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530140 printer_.Print(vars, string_template);
141 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530142
Craig Tillerbaa14a92017-11-03 09:09:36 -0700143 void Print(const char* string) { printer_.Print(string); }
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530144 void Indent() { printer_.Indent(); }
145 void Outdent() { printer_.Outdent(); }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530146
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530147 private:
148 grpc::protobuf::io::StringOutputStream output_stream_;
149 grpc::protobuf::io::Printer printer_;
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530150};
151
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530152class ProtoBufFile : public grpc_generator::File {
153 public:
Craig Tillerbaa14a92017-11-03 09:09:36 -0700154 ProtoBufFile(const grpc::protobuf::FileDescriptor* file) : file_(file) {}
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530155
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530156 grpc::string filename() const { return file_->name(); }
157 grpc::string filename_without_ext() const {
158 return grpc_generator::StripProto(filename());
159 }
160
161 grpc::string package() const { return file_->package(); }
162 std::vector<grpc::string> package_parts() const {
163 return grpc_generator::tokenize(package(), ".");
164 }
165
166 grpc::string additional_headers() const { return ""; }
167
168 int service_count() const { return file_->service_count(); };
169 std::unique_ptr<const grpc_generator::Service> service(int i) const {
170 return std::unique_ptr<const grpc_generator::Service>(
171 new ProtoBufService(file_->service(i)));
172 }
173
174 std::unique_ptr<grpc_generator::Printer> CreatePrinter(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700175 grpc::string* str) const {
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530176 return std::unique_ptr<grpc_generator::Printer>(new ProtoBufPrinter(str));
177 }
178
179 grpc::string GetLeadingComments(const grpc::string prefix) const {
180 return GetCommentsHelper(file_, true, prefix);
181 }
182
183 grpc::string GetTrailingComments(const grpc::string prefix) const {
184 return GetCommentsHelper(file_, false, prefix);
185 }
186
187 vector<grpc::string> GetAllComments() const {
188 return grpc_python_generator::get_all_comments(file_);
189 }
190
191 private:
Craig Tillerbaa14a92017-11-03 09:09:36 -0700192 const grpc::protobuf::FileDescriptor* file_;
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530193};
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530194
Harsh Vardhancacd5e82017-02-26 21:18:36 +0530195#endif // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H