blob: bb77063796667b47c59138b034532fe01f91b556 [file] [log] [blame]
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +05301/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053033
34#ifndef GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
35#define GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H
36
37#include "src/compiler/config.h"
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053038#include "src/compiler/cpp_generator_helpers.h"
Harsh Vardhan93077ae2016-12-26 18:23:45 +053039#include "src/compiler/python_generator_helpers.h"
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053040#include "src/compiler/python_private_generator.h"
41#include "src/compiler/schema_interface.h"
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053042
43#include <vector>
44
45// Get leading or trailing comments in a string.
46template <typename DescriptorType>
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053047inline grpc::string GetCommentsHelper(const DescriptorType *desc, bool leading,
48 const grpc::string &prefix) {
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053049 return grpc_generator::GetPrefixedComments(desc, leading, prefix);
50}
51
52class ProtoBufMethod : public grpc_generator::Method {
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053053 public:
54 ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method)
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053055 : method_(method) {}
56
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053057 grpc::string name() const { return method_->name(); }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053058
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053059 grpc::string input_type_name() const {
60 return grpc_cpp_generator::ClassName(method_->input_type(), true);
61 }
62 grpc::string output_type_name() const {
63 return grpc_cpp_generator::ClassName(method_->output_type(), true);
64 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053065
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053066 grpc::string get_input_type_name() const {
67 return method_->input_type()->file()->name();
68 }
69 grpc::string get_output_type_name() const {
70 return method_->output_type()->file()->name();
71 }
Harsh Vardhan6a9950d2017-02-07 12:01:44 +053072
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053073 bool get_module_and_message_path_input(grpc::string *str,
74 grpc::string generator_file_name,
75 bool generate_in_pb2_grpc) const {
76 return grpc_python_generator::GetModuleAndMessagePath(
77 method_->input_type(), str, generator_file_name, generate_in_pb2_grpc);
78 }
Harsh Vardhan93077ae2016-12-26 18:23:45 +053079
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053080 bool get_module_and_message_path_output(grpc::string *str,
81 grpc::string generator_file_name,
82 bool generate_in_pb2_grpc) const {
83 return grpc_python_generator::GetModuleAndMessagePath(
84 method_->output_type(), str, generator_file_name, generate_in_pb2_grpc);
85 }
Harsh Vardhan93077ae2016-12-26 18:23:45 +053086
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053087 bool NoStreaming() const {
88 return !method_->client_streaming() && !method_->server_streaming();
89 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053090
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053091 bool ClientStreaming() const {
92 return method_->client_streaming() && !method_->server_streaming();
93 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +053094
Harsh Vardhancacd5e82017-02-26 21:18:36 +053095 bool python_ClientStreaming() const { return method_->client_streaming(); }
96
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +053097 bool ServerStreaming() const {
98 return !method_->client_streaming() && method_->server_streaming();
99 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530100
Harsh Vardhancacd5e82017-02-26 21:18:36 +0530101 bool python_ServerStreaming() const { return method_->server_streaming(); }
102
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530103 bool BidiStreaming() const {
104 return method_->client_streaming() && method_->server_streaming();
105 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530106
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530107 grpc::string GetLeadingComments(const grpc::string prefix) const {
108 return GetCommentsHelper(method_, true, prefix);
109 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530110
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530111 grpc::string GetTrailingComments(const grpc::string prefix) const {
112 return GetCommentsHelper(method_, false, prefix);
113 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530114
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530115 vector<grpc::string> GetAllComments() const {
116 return grpc_python_generator::get_all_comments(method_);
117 }
Harsh Vardhan93077ae2016-12-26 18:23:45 +0530118
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530119 private:
120 const grpc::protobuf::MethodDescriptor *method_;
121};
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530122
123class ProtoBufService : public grpc_generator::Service {
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530124 public:
125 ProtoBufService(const grpc::protobuf::ServiceDescriptor *service)
126 : service_(service) {}
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530127
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530128 grpc::string name() const { return service_->name(); }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530129
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530130 int method_count() const { return service_->method_count(); };
131 std::unique_ptr<const grpc_generator::Method> method(int i) const {
132 return std::unique_ptr<const grpc_generator::Method>(
133 new ProtoBufMethod(service_->method(i)));
134 };
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530135
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530136 grpc::string GetLeadingComments(const grpc::string prefix) const {
137 return GetCommentsHelper(service_, true, prefix);
138 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530139
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530140 grpc::string GetTrailingComments(const grpc::string prefix) const {
141 return GetCommentsHelper(service_, false, prefix);
142 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530143
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530144 vector<grpc::string> GetAllComments() const {
145 return grpc_python_generator::get_all_comments(service_);
146 }
Harsh Vardhan93077ae2016-12-26 18:23:45 +0530147
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530148 private:
149 const grpc::protobuf::ServiceDescriptor *service_;
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530150};
151
152class ProtoBufPrinter : public grpc_generator::Printer {
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530153 public:
154 ProtoBufPrinter(grpc::string *str)
155 : output_stream_(str), printer_(&output_stream_, '$') {}
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530156
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530157 void Print(const std::map<grpc::string, grpc::string> &vars,
158 const char *string_template) {
159 printer_.Print(vars, string_template);
160 }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530161
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530162 void Print(const char *string) { printer_.Print(string); }
163 void Indent() { printer_.Indent(); }
164 void Outdent() { printer_.Outdent(); }
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530165
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530166 private:
167 grpc::protobuf::io::StringOutputStream output_stream_;
168 grpc::protobuf::io::Printer printer_;
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530169};
170
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530171class ProtoBufFile : public grpc_generator::File {
172 public:
173 ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {}
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530174
Harsh Vardhan4b0ba1a2017-02-25 20:54:52 +0530175 grpc::string filename() const { return file_->name(); }
176 grpc::string filename_without_ext() const {
177 return grpc_generator::StripProto(filename());
178 }
179
180 grpc::string package() const { return file_->package(); }
181 std::vector<grpc::string> package_parts() const {
182 return grpc_generator::tokenize(package(), ".");
183 }
184
185 grpc::string additional_headers() const { return ""; }
186
187 int service_count() const { return file_->service_count(); };
188 std::unique_ptr<const grpc_generator::Service> service(int i) const {
189 return std::unique_ptr<const grpc_generator::Service>(
190 new ProtoBufService(file_->service(i)));
191 }
192
193 std::unique_ptr<grpc_generator::Printer> CreatePrinter(
194 grpc::string *str) const {
195 return std::unique_ptr<grpc_generator::Printer>(new ProtoBufPrinter(str));
196 }
197
198 grpc::string GetLeadingComments(const grpc::string prefix) const {
199 return GetCommentsHelper(file_, true, prefix);
200 }
201
202 grpc::string GetTrailingComments(const grpc::string prefix) const {
203 return GetCommentsHelper(file_, false, prefix);
204 }
205
206 vector<grpc::string> GetAllComments() const {
207 return grpc_python_generator::get_all_comments(file_);
208 }
209
210 private:
211 const grpc::protobuf::FileDescriptor *file_;
212};
Harsh Vardhan5b8fa872016-09-12 20:37:42 +0530213
Harsh Vardhancacd5e82017-02-26 21:18:36 +0530214#endif // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H