Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 1 | /* |
| 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 Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 33 | |
| 34 | #ifndef GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H |
| 35 | #define GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H |
| 36 | |
| 37 | #include "src/compiler/config.h" |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 38 | #include "src/compiler/cpp_generator_helpers.h" |
Harsh Vardhan | 93077ae | 2016-12-26 18:23:45 +0530 | [diff] [blame] | 39 | #include "src/compiler/python_generator_helpers.h" |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 40 | #include "src/compiler/python_private_generator.h" |
| 41 | #include "src/compiler/schema_interface.h" |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 42 | |
| 43 | #include <vector> |
| 44 | |
| 45 | // Get leading or trailing comments in a string. |
| 46 | template <typename DescriptorType> |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 47 | inline grpc::string GetCommentsHelper(const DescriptorType *desc, bool leading, |
| 48 | const grpc::string &prefix) { |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 49 | return grpc_generator::GetPrefixedComments(desc, leading, prefix); |
| 50 | } |
| 51 | |
| 52 | class ProtoBufMethod : public grpc_generator::Method { |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 53 | public: |
| 54 | ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method) |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 55 | : method_(method) {} |
| 56 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 57 | grpc::string name() const { return method_->name(); } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 58 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 59 | 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 Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 65 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 66 | 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 Vardhan | 6a9950d | 2017-02-07 12:01:44 +0530 | [diff] [blame] | 72 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 73 | 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 Vardhan | 93077ae | 2016-12-26 18:23:45 +0530 | [diff] [blame] | 79 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 80 | 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 Vardhan | 93077ae | 2016-12-26 18:23:45 +0530 | [diff] [blame] | 86 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 87 | bool NoStreaming() const { |
| 88 | return !method_->client_streaming() && !method_->server_streaming(); |
| 89 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 90 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 91 | bool ClientStreaming() const { |
| 92 | return method_->client_streaming() && !method_->server_streaming(); |
| 93 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 94 | |
Harsh Vardhan | cacd5e8 | 2017-02-26 21:18:36 +0530 | [diff] [blame^] | 95 | bool python_ClientStreaming() const { return method_->client_streaming(); } |
| 96 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 97 | bool ServerStreaming() const { |
| 98 | return !method_->client_streaming() && method_->server_streaming(); |
| 99 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 100 | |
Harsh Vardhan | cacd5e8 | 2017-02-26 21:18:36 +0530 | [diff] [blame^] | 101 | bool python_ServerStreaming() const { return method_->server_streaming(); } |
| 102 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 103 | bool BidiStreaming() const { |
| 104 | return method_->client_streaming() && method_->server_streaming(); |
| 105 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 106 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 107 | grpc::string GetLeadingComments(const grpc::string prefix) const { |
| 108 | return GetCommentsHelper(method_, true, prefix); |
| 109 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 110 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 111 | grpc::string GetTrailingComments(const grpc::string prefix) const { |
| 112 | return GetCommentsHelper(method_, false, prefix); |
| 113 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 114 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 115 | vector<grpc::string> GetAllComments() const { |
| 116 | return grpc_python_generator::get_all_comments(method_); |
| 117 | } |
Harsh Vardhan | 93077ae | 2016-12-26 18:23:45 +0530 | [diff] [blame] | 118 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 119 | private: |
| 120 | const grpc::protobuf::MethodDescriptor *method_; |
| 121 | }; |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 122 | |
| 123 | class ProtoBufService : public grpc_generator::Service { |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 124 | public: |
| 125 | ProtoBufService(const grpc::protobuf::ServiceDescriptor *service) |
| 126 | : service_(service) {} |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 127 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 128 | grpc::string name() const { return service_->name(); } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 129 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 130 | 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 Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 135 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 136 | grpc::string GetLeadingComments(const grpc::string prefix) const { |
| 137 | return GetCommentsHelper(service_, true, prefix); |
| 138 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 139 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 140 | grpc::string GetTrailingComments(const grpc::string prefix) const { |
| 141 | return GetCommentsHelper(service_, false, prefix); |
| 142 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 143 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 144 | vector<grpc::string> GetAllComments() const { |
| 145 | return grpc_python_generator::get_all_comments(service_); |
| 146 | } |
Harsh Vardhan | 93077ae | 2016-12-26 18:23:45 +0530 | [diff] [blame] | 147 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 148 | private: |
| 149 | const grpc::protobuf::ServiceDescriptor *service_; |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | class ProtoBufPrinter : public grpc_generator::Printer { |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 153 | public: |
| 154 | ProtoBufPrinter(grpc::string *str) |
| 155 | : output_stream_(str), printer_(&output_stream_, '$') {} |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 156 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 157 | void Print(const std::map<grpc::string, grpc::string> &vars, |
| 158 | const char *string_template) { |
| 159 | printer_.Print(vars, string_template); |
| 160 | } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 161 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 162 | void Print(const char *string) { printer_.Print(string); } |
| 163 | void Indent() { printer_.Indent(); } |
| 164 | void Outdent() { printer_.Outdent(); } |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 165 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 166 | private: |
| 167 | grpc::protobuf::io::StringOutputStream output_stream_; |
| 168 | grpc::protobuf::io::Printer printer_; |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 169 | }; |
| 170 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 171 | class ProtoBufFile : public grpc_generator::File { |
| 172 | public: |
| 173 | ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {} |
Harsh Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 174 | |
Harsh Vardhan | 4b0ba1a | 2017-02-25 20:54:52 +0530 | [diff] [blame] | 175 | 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 Vardhan | 5b8fa87 | 2016-09-12 20:37:42 +0530 | [diff] [blame] | 213 | |
Harsh Vardhan | cacd5e8 | 2017-02-26 21:18:36 +0530 | [diff] [blame^] | 214 | #endif // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H |